Download as pdf or txt
Download as pdf or txt
You are on page 1of 112

Kindly read the instructions carefully

1. All these questions are important for examination point of view,


so practice them well.
2. If you have any doubt or facing any problem regarding these
questions you can mail us at coderslodgeofficial@gmail.com or
drop a message in our WhatsApp or telegram group.
3. If you want to support us, give your valuable feedback so that
next time we can improve while interacting with you.
4. Reminder-Practice all questions well it will build your concept
clear, and you can easily score good in your exams.

Connect with us
• If you want to join our WhatsApp community then mail us at:-
coderslodgeofficial@gmail.com with your preferred branch(with year) and
college name.
• Join our Telegram group:- https://t.me/coderslodgeofficial
• Like our Facebook page: - https://www.facebook.com/coderslodge
• Follow us on Instagram: - https://www.instagram.com/coderslodge/
• Follow us on Twitter: - https://twitter.com/CodersLodge
• Follow us on LinkedIn: - https://www.linkedin.com/company/coderslodge

Kindly share your valuable feedback on

coderslodgeofficial@gmail.com
1. Which of the following explains Polymorphism?

a. int func(int, int);


float func1(float,
float);

b. int func(int);
int func(int);

c. int func(float);
float func(int, int,
char);

d. int func(); int


new_func();

Answer: C

2. Which of the following feature of OOPs is not used in the


following C++ code?
class A

Kindly share your valuable feedback on

coderslodgeofficial@gmail.com
{ int i;
public:
void print(){cout<<"hello"<<i;}
}

class B: public A
{ int j;
public:
void assign(int a){j = a;}
}
a) Abstraction
b) Encapsulation
c) Inheritance
d) Polymorphism
Answer: d
3. Predict the output of below C++ programs.
#include<iostream> using namespace std;
class Base1 { public:
Base1()
{ cout << " Base1's constructor called" << endl; }
};

Kindly share your valuable feedback on

coderslodgeofficial@gmail.com
class Base2 {
public:
Base2()
{ cout << "Base2's constructor called" << endl; }
};

class Derived: public Base1, public Base2 {


public:
Derived()
{ cout << "Derived's constructor called" << endl; }
};

int main()
{
Derived d;
return 0;
}

Output: Base1’s constructor called


Base2’s constructor called
Derived’s constructor called

Kindly share your valuable feedback on

coderslodgeofficial@gmail.com
4. Predict the output of below C++ programs.

#include<iostream>
using namespace std;

class A { public:
A(int ii = 0) : i(ii) {}
void show() { cout << "i = " << i << endl;}
private: int i;
};

class B { public: B(int xx) : x(xx)


{} operator A() const { return A(x); }
private: int x;
};

void g(A a)
{ a.show();
}

int main()
{ B b(10);
g(b); g(20);
getchar();
return 0;
}

Kindly share your valuable feedback on

coderslodgeofficial@gmail.com
Output: i = 10
i = 20

5. What will be the output of the following C++ code?


#include <iostream> using namespace std;
class A{ public:
A(){
cout<<"Constructor called\n";
}
~A(){
cout<<"Destructor called\n";
}
};
int main(int argc, char const *argv[])
{
A *a = new A[5];
delete a;
return 0;
}
a) “Constructor called” five times and then “Destructor
called” five times

Kindly share your valuable feedback on

coderslodgeofficial@gmail.com
b) “Constructor called” five times and then “Destructor
called” once
c) Error
d) Segmentation fault
Answer: b
6. Predict the output of below C++ programs.
#include<iostream> using namespace
std; class P { public: void print()
{ cout <<" Inside P::"; }
};

class Q : public P {
public: void
print()
{ cout <<" Inside Q"; }
};

class R: public Q {
};

int main(void)

Kindly share your valuable feedback on

coderslodgeofficial@gmail.com
{R
r;
r.print(); return
0;
}
Output: Inside Q
7. What will be the output of the following C++ code?
#include <iostream> using namespace std;

class A
{ int
a;
A() { a = 5;}
};

int main()
{
A *obj = new A;
cout << obj->a;
}

Kindly share your valuable feedback on

coderslodgeofficial@gmail.com
a) 5
b) Garbage value
c) Compile-time error
d) Run-time error
Answer: c
8. Predict the output of below C++ program.
#include<iostream>
#include<stdio.h> using
namespace std; class
Base
{
public: Base()
{
fun(); //note: fun() is virtual
}
virtual void fun()
{
cout<<"\nBase Function";
}
};

class Derived: public Base

Kindly share your valuable feedback on

coderslodgeofficial@gmail.com
{
public:
Derived(){} virtual
void fun()
{
cout<<"\nDerived Function";
} };
int main()
{
Base* pBase = new Derived();
delete pBase; return 0;
}
Output: Base Function
9. Predict the output of below C++ program.
#include<iostream> using namespace
std;

int x = 10; void


fun()
{
int x = 2;

Kindly share your valuable feedback on

coderslodgeofficial@gmail.com
{
int x = 1;
cout << ::x << endl;
}
}

int main()
{
fun();
return 0;
}
Output: 10
10. Predict the output of below C++ program.
#include<iostream> using namespace std; int &fun()
{ static int a = 10;
return a;
}

int main() { int


&y = fun(); y =
y +30;
cout<<fun();
return 0;
}

Kindly share your valuable feedback on

coderslodgeofficial@gmail.com
Output: 40

11. What will be the output of the following C++ code?


#include<iostream> using namespace std; class A
{
~A(){
cout<<"Destructor called\n";
}
};
int main()
{
A a;
return 0;
}
a) Destructor called
b) Nothing will be printed
c) Error
d) Segmentation fault Answer: c
12. What will be the output of the following C++ code?
#include <iostream> using namespace std;
class Test

Kindly share your valuable feedback on

coderslodgeofficial@gmail.com
{ static int x; public:
Test() { x++; } static int
getX() {return x;}
};
int Test::x = 0; int
main()
{
cout << Test::getX() << " ";
Test t[5]; cout <<
Test::getX();
}
a) 0 0
b) 5 0
c) 0 5
d) 5 5
Answer: c
13. Predict the output of below C++ program.
#include<iostream> using namespace std;

class Test {
int value; public:

Kindly share your valuable feedback on

coderslodgeofficial@gmail.com
Test (int v = 0) {value = v;}
int getValue() { return value; }
};
int main() {
const Test t;
cout << t.getValue();
return 0;
}
Output: Compiler Error
14. What will be the output of the following C++ code?
#include <iostream> using namespace std; int
main()
{
int x = -1;
unsigned int y = 2;

if(x > y)
{
cout << "x is greater";
}
else

Kindly share your valuable feedback on

coderslodgeofficial@gmail.com
{
cout << "y is greater";
}
}
a) x is greater
b) y is greater
c) implementation defined
d) arbitrary Answer: a
15. Predict the output of following C++ program.
#include<iostream> using namespace std;
class Test1
{
int x; public:
void show() { }
};
class Test2
{
int x;
public:
virtual void show() { }
};

Kindly share your valuable feedback on

coderslodgeofficial@gmail.com
int main(void)
{
cout<<sizeof(Test1)<<endl;
cout<<sizeof(Test2)<<endl;
return 0;
}
Output: 4
8
16. Predict the output of following C++ program.
#include<iostream> using namespace std;

class Test
{
private: static int
count; public:
static Test& fun();
};

int Test::count = 0;

Test& Test::fun()

Kindly share your valuable feedback on

coderslodgeofficial@gmail.com
{
Test::count++;
cout<<Test::count<<" ";
return *this;
}

int main()
{
Test t;
t.fun().fun().fun().fun();
return 0;
}
Output: Compiler Error: 'this' is unavailable for static
member functions
17. Predict the output of following C++ program.
#include<iostream>
#include<string.h> using
namespace std; class
String

Kindly share your valuable feedback on

coderslodgeofficial@gmail.com
char *p;
int len;
public:
String(const char *a);
};

String::String(const char *a)


{
int length = strlen(a); p
= new char[length +1];
strcpy(p, a);
cout << "Constructor Called " << endl;
}

int main()
{
String s1("Coders"); const char
*name = "codersLodge"; s1 = name;
return 0;
}
Output: Constructor Called

Kindly share your valuable feedback on

coderslodgeofficial@gmail.com
Constructor Called
18. What will be the output of the following C++
code? #include <iostream> using namespace
std; int main()
{
int i;
const char *arr[] = {"C", "C++", "Java", "VBA"};
const char *(*ptr)[4] = &arr; cout <<
++(*ptr)[2]; return 0;
}
a) ava
b) java
c) c++
d) compile time error Answer: a
19. Predict the output of following C++ program.
#include<iostream> using namespace std; class
Test
{
private :
int marks = 85;
public :
Test(int marks)

Kindly share your valuable feedback on

coderslodgeofficial@gmail.com
{
cout<< this->marks;
cout<<endl;
}
};
int main()
{
Test t(95); return 0;
}
Output: 85
20. Predict the output of following C++ program.
#include<iostream> using namespace std;

class A
{ public :
A()
{
func();
}
~A()
{

Kindly share your valuable feedback on

coderslodgeofficial@gmail.com
func();
}
void func()
{
cout<< 3;
cout<<endl;
}
void fun()
{
func();
}
};
class B : public A
{
void func()
{
cout<< 2;
cout<<endl;
}
};
int main()

Kindly share your valuable feedback on

coderslodgeofficial@gmail.com
{
B b;
b.fun();
return 0;
}
Output: 3
3
3

Kindly share your valuable feedback on

coderslodgeofficial@gmail.com
OBJECT ORIENTED PROGRAMMING USING C++

Kindly read the instructions carefully

1. All these questions are important for examination point of view,


so practice them well.
2. If you have any doubt or facing any problem regarding these
questions you can mail us at coderslodgeofficial@gmail.com or
drop a message in our WhatsApp or telegram group.
3. If you want to support us, give your valuable feedback so that
next time we can improve while interacting with you.
4. Reminder-Practice all questions well it will build your concept
clear, and you can easily score good in your exams.

Connect with us
• If you want to join our WhatsApp community then mail us at:-
coderslodgeofficial@gmail.com with your preferred branch(with year) and
college name.
• Join our Telegram group:- https://t.me/coderslodgeofficial
• Like our Facebook page :- https://www.facebook.com/coderslodge
• Follow us on Instagram:-https://www.instagram.com/coderslodge/
• Follow us on Twitter:- https://twitter.com/CodersLodge
• Follow us on Linkedin:- https://www.linkedin.com/company/coderslodge

Kindly share your valuable feedback on


coderslodgeofficial@gmail.com
1. Predict the output of following C++ program.
#include<iostream>
using namespace std;
class A
{
public:
virtual void fun() {cout << "A" << endl ;}
};
class B: public A
{
public:
virtual void fun() {cout << "B" << endl;}
};
class C: public B
{
public:
virtual void fun() {cout << "C" << endl;}
};

int main()
{
A *a = new C;
A *b = new B;
Kindly share your valuable feedback on
coderslodgeofficial@gmail.com
a->fun();
b->fun();
return 0;
}
Output: C
B
2. Predict the output of following C++ program.
#include<iostream>
using namespace std;
class A
{
public :
int x=20;
};
class B
{
public :
int x=10;
};
int main()
{
A obj1;
B obj2;
Kindly share your valuable feedback on
coderslodgeofficial@gmail.com
obj1 = obj2;
cout<< obj1.x;
cout<<endl;
return 0;
}
Output: The program will not generate output due to
compilation error.

3. Predict the output of following C++ program.


#include<iostream>
using namespace std;

int main()
{
int *ptr = new int(5);
cout << *ptr;
return 0;
}
Output: 5

4. What will be the output of the following C++ code?


#include <iostream>
using namespace std;
int main()
{
int a = 5, b = 6, c;

Kindly share your valuable feedback on


coderslodgeofficial@gmail.com
c = (a > b) ? a : b;
cout << c;
return 0;
}
a) 6
b) 5
c) 4
d) 7
Answer: a
5. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
int n = 15;
for ( ; ;)
cout << n;
return 0;
}
a) error
b) 15
c) infinite times of printing n
d) none of the mentioned
Answer: c

Kindly share your valuable feedback on


coderslodgeofficial@gmail.com
6. Predict the output of following C++ program.
#include<iostream>
using namespace std;
class Test
{
private:
int x;
public:
void setX (int x) { Test::x = x; }
void print() { cout << "x = " << x << endl; }
};

int main()
{
Test obj;
int x = 40;
obj.setX(x);
obj.print();
return 0;
}
Output: x=40

Kindly share your valuable feedback on


coderslodgeofficial@gmail.com
7. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int fun(int=0, int = 0);
int main()
{
cout << fun(5);
return 0;
}
int fun(int x, int y) { return (x+y); }
a) -5
b) 0
c) 10
d) 5
Answer: d
8. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int operate (int a, int b)
{
return (a * b);
}
float operate (float a, float b)

Kindly share your valuable feedback on


coderslodgeofficial@gmail.com
{
return (a / b);
}
int main()
{
int x = 5, y = 2;
float n = 5.0, m = 2.0;
cout << operate(x, y) <<"\t";
cout << operate (n, m);
return 0;
}
a) 10.0 5.0
b) 5.0 2.5
c) 10.0 5
d) 10 2.5
Answer: d
9. Predict the output of following C++ program
#include <iostream>
using namespace std;
class A
{
int id;
static int count;

Kindly share your valuable feedback on


coderslodgeofficial@gmail.com
public:
A()
{
count++;
id = count;
cout << "constructor called " << id << endl;
}
~A()
{
cout << "destructor called " << id << endl;
}
};
int A::count = 0;
int main()
{
A a[2];
return 0;
}
Output: constructor called 1
constructor called 2
destructor called 2
destructor called 1

Kindly share your valuable feedback on


coderslodgeofficial@gmail.com
10. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
struct a
{
int count;
};
struct b
{
int* value;
};
struct c : public a, public b
{
};
int main()
{
c* p = new c;
p->value = 0;
cout << "Inherited";
return 0;
}
a) Inherited
b) Error
Kindly share your valuable feedback on
coderslodgeofficial@gmail.com
c) Runtime error
d) inherited
Answer: a
11. Predict the output of following C++ program.
#include <iostream>
using namespace std;
class A
{
public:
void print() { cout << "A::print()"; }
};

class B : private A
{
public:
void print() { cout << "B::print()"; }
};

class C : public B
{
public:
void print() { A::print(); }
};
Kindly share your valuable feedback on
coderslodgeofficial@gmail.com
int main()
{
C b;
b.print();
}
Output: Compiler Error: ‘A’ is not an accessible base of
‘C’
12. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class A
{
int a, b;
float d;
public:
void change(int i){
a = i;
}
void value_of_a(){
cout<<a;
}
Kindly share your valuable feedback on
coderslodgeofficial@gmail.com
};

class B: private A
{

};
int main(int argc, char const *argv[])
{
B b;
cout<<sizeof(B);
return 0;
}
a) 8
b) 12
c) Error
d) Segmentation fault
Answer: b
13. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class A{
float d;

Kindly share your valuable feedback on


coderslodgeofficial@gmail.com
public:
virtual void func(){
cout<<"Hello this is class A\n";
}
};
class B: public A{
int a = 15;
public:
void func(){
cout<<"Hello this is class B\n";
}
};
int main(int argc, char const *argv[])
{
B b;
b.func();
return 0;
}
a) Hello this is class B
b) Hello this is class A
c) Error
d) Segmentation fault
Answer: a

Kindly share your valuable feedback on


coderslodgeofficial@gmail.com
14. Predict the output of following C++ program.
#include<iostream>
using namespace std;
class Base {
protected:
int x;
public:
Base (int i){ x = i;}
};

class Derived : public Base {


public:
Derived (int i):Base(i) { }
void print() { cout << x; }
};

int main()
{
Derived d(10);
d.print();
}
Output: 10
Kindly share your valuable feedback on
coderslodgeofficial@gmail.com
15. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class Mammal
{
public:
virtual void Define(){
cout<<"I'm a Mammal\n";
}
};

class Human: public Mammal


{
private:
void Define(){
cout<<"I'm a Human\n";
}
};

int main(int argc, char const *argv[])


{
Kindly share your valuable feedback on
coderslodgeofficial@gmail.com
Mammal *M = new Mammal();
Human H;
M = &H;
M->Define();
return 0;
}
a) Error
b) Segmnetation fault
c) I’m a Human
d) Garbage Value
Answer: c
16. Predict the output of following C++ program.
#include <iostream>
using std::cout;
class Test
{
public:
Test();
~Test();
};
Test::Test()
{
cout << "Constructor is executed\n";

Kindly share your valuable feedback on


coderslodgeofficial@gmail.com
}
Test::~Test()
{
cout << "Destructor is executed\n";
}
int main()
{
delete new Test();
return 0;
}
Output: Constructor is executed
Destructor is executed
17. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class A
{
int a;
public:
A(){
a = 0;
}
Kindly share your valuable feedback on
coderslodgeofficial@gmail.com
void show(){
a++;
cout<<"a: "<<a<<endl;
}
};

class B: public A
{
public:
};

int main(int argc, char const *argv[])


{
B b;
b.show();
return 0;
}
a) a: 1
b) a: 0
c) Error
d) Segmentation fault
Answer: a

Kindly share your valuable feedback on


coderslodgeofficial@gmail.com
18. What will be the output of the following C++ code?
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

void func(int a, int b)


{

if(b == 0){
throw "This value of b will make the product zero. "
"So please provide positive values.\n";
}
else{
cout<<"Product of "<<a<<" and "<<b<<" is:
"<<a*b<<endl;
}
}

int main()
{
try{

Kindly share your valuable feedback on


coderslodgeofficial@gmail.com
func(5,0);
}
catch(const char* e){
cout<<e;
}
}
a) 0
b) 5
c) This value of b will make the product zero. So please provide
positive values.
d) Product of 5 and 0 is: 0
Answer: c
19. What will be the output of this program?

#include <iostream>
using namespace std;
int a = 90;

int fun(int x, int *y = &a)


{
*y = x + *y;
return x + *y;
}

Kindly share your valuable feedback on


coderslodgeofficial@gmail.com
int main()
{
int a = 5, b = 10;

a = fun(a);
cout << a << " " << b << endl;

b = fun(::a,&a);
cout << a << " " << b << endl;

return 0;
}

Output: 100 10
195 290
20. What will be the output of the following C++ code?
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class A
{
int a;
Kindly share your valuable feedback on
coderslodgeofficial@gmail.com
public:
A(){}
};

class B: public A
{
int b;
public:
B(){}
};

void func()
{
B b;
throw b;
}

int main()
{
try{
func();
}
catch(A a){
Kindly share your valuable feedback on
coderslodgeofficial@gmail.com
cout<<"Caught A Class\n";
}
catch(B b){
cout<<"Caught B Class\n";
}
}
a) Caught B Class
b) Caught A Class
c) Compile-time error
d) Run-time error
Answer: b

Kindly share your valuable feedback on


coderslodgeofficial@gmail.com
OUTPUT BASED QUESTIONS -1

#include <iostream> using


1. 52
namespace std;
class course { int x, y; public:
void course1(int xx, int yy) { x
= ++xx; y = ++yy;
} void Display()
{ cout<<x+y<<"
";
}
}; int main() {
course obj;
obj.course1(20,30);
obj.Display();
return 0;
}

2. 10
#include <iostream> using
namespace std;
namespace first
{ int x = 5;
int y = 10;
} namespace second {
double x = 3.1416;
double y = 2.7183;
}
int main() { using
first::x; using
second::y; bool a,b; a
= x > y; b = first::y <
second::x; cout<<a<<b;
return
0;
}

#include <iostream> using


3. namespace std;
52

int f(int p, int q)


{ if(p > q)
return p; else
return q;
} int main() {
int a=5, b=10;
int k; bool
x=true; bool
y=f(a,b); k =
((a*b)+(x+y));
cout<<k;
}

4. 1010
#include <iostream>
using namespace std;
int main() { int i,j;
j = 10; i = (j++, j
+100, 999+j); cout<<i;
return 0;
}

#include <iostream>
5. 17.1 157.32
using namespace std;
class Room { public: double
length; double breadth;
double height; double
calculateArea() {
return length * breadth;
}
double calculateVolume() {
return length * breadth * height;
}

};
int main() { Room
room1; room1.length
= 4.5;
room1.breadth = 3.8;

room1.height = 9.2;
cout<<room1.calculateArea()<<room1.calculateVolume();
return 0;
}

#include <iostream> using 12.566


6.
namespace std;
4
#define PI 3.14159
int main() { float
r = 2; float
circle; circle = 2
* PI * r;
cout<<circle;
return 0;
}

#include <iostream> using


7. erro
namespace std;
int main() { int a r
= 10; if(a<10) {
for(i=0;i<10;i++)
cout<<i;
}
return 0;
}

#include <iostream> using


8.
namespace std;
int x=1; 1
void fun() {
int x = 2;
{ int x =
3;
cout<<::x<<endl;
}
}
int main() {
fun();
return 0;
}

9. 543
#include <iostream> using
namespace std;
int main() { int
n; for(n=5; n>0; n-
-) { cout<<n;
if(n==3)
break;
}
return 0;
}

#include <iostream> using infinitel


10.
namespace std;
int main() { int y
a=10; if(a<15) {
print 10
time: cout<<a;
goto time;
}
return 0;
}

#include <iostream>
11. This loop will run
int main() {
int i; for( forever infinite
; ; ) {
std::cout<<("This loop will run time
forever.\n"); } return 0; s
}

#include <iostream> using 203565506


12.
namespace std;
5
int g =
100;

int main() {
int a; {
int b;
b=20;

a=35;
g=65;
cout<<b<<a<<g;
}
a=50;
cout<<a<<g;
return
0;
}

#include <iostream> using


13.
namespace std;
int main() { 3
int i;
if(cout<<"0")
i=3; else
i=5;

cout<<i;
return 0;
}

#include <iostream>
14. 1234
4
int main() { int
i=0,x=0;
for(i=1;i<10;i*=2) {
x++;
std::cout<<x;
}
std::cout<<x;
return 0;
}

#include <iostream> using


15. -1 1
namespace std;

int main() {
int i=3;
int l=i/-2;
int k=i%-2;
cout<<l<<k;
return
0;
}

#include <iostream> using


16. 6
namespace std;
int main() {
int a=5, b=6, c;
c=(a>b) ? a : b;
cout<<c;
return
0;
}

17. 3
#include <iostream> using
namespace std;
void fnn() { int
x=2; {
int x=3;
cout<<x<<endl;
}
} int
main() {
fnn();
return 0;
}

#include <iostream> using


18. findcours
namespace std;
class ABC e
{ public:
ABC() {
cout<<"find";
} ~ABC() {
cout<<"course";
}
};
int main() {
ABC obj;
return 0;
}

#include <iostream> using


19. Compiler error
namespace std;
class A {
public:
void f(){
cout<<"A::f()"<<endl;
}
}; class
B:public A{
public:
void fb(){
cout<<"A::fb()"<<endl;
}
}; class
C:public A{
public:
void fc(){
cout<<"A::fc()"<<endl;
}
}; class D: public
B,public C{ public:
void fd(){
cout<<"A::fd()"<<endl;
}
};
int main() {
D obj;
obj.f();

return 0;
}

20 error
#include <iostream>
using namespace std;
class B
{ int
b;
public:
B(int i)
{
b=i;
} };
class C
{
B b;public:C(int i)
{
b=B(i);

}
friend void show();
};
void show()
{
C c(10);
cout<<"value of b
is:"<<c.b.b<<endl; int main();
return 0;
}

21. #include <iostream> cannot convert


using namespace std;
error:
'std::basic_ostream<char>'
int main()
{
to 'int' in assignment
int a=5,b=6,c; c=(a>b)?(cout<<"a is
c=(a>b)?(cout<<"a is greater"):(cout<<"b is greater"):(cout<<"b
greater"); cout<<c; return 0;
} greater"); is

#include <iostream>
22. using namespace std;
543
int main()
{
int n;
for(n=5;n>0;n--)
{
cout<<n;
if(n==3)
break;
}
return 0;
}
#include <iostream>
23. using namespace std;
Print natural
int main() numbers 1 to 100
{
int n=1;
cout<<"the numbers are:";
do {

cout<<n<<endl;
n++; }
while(n<=100);
cout<<endl;
return 0;
}

#include <iostream>
24. using namespace std;
3
int main()
{

int i;
if (cout<<"0:")i=3;
else i=5;
printf("%d",i); return
0;
}

#include <iostream> using


25. namespace std; class Box
erro
r
{ private:
int length;
public:
Box()
{
length(0)
{}
friend int printlength(Box);
};
int printlength(Box b)
{
b.length += 10;
return b.length;
}
int main()

Box b;
cout << printlength(b) << endl;

return 0;
}

#include <iostream>
26. using namespace std;
int main() 6
{ int a = 5, b
= 6, c; c = (a > b)
? a : b; cout << c;
return 0;
}

#include <iostream> using


27. namespace std; class B
erro
{ int r
b;
public:
B(int i)
{
b=i;
}
};
class C
{
B b;public:C(int i)
{
b=B(i);
}
friend void show();
};
void show()
{
C c(10);
cout<<"value of b is:"<<c.b.b<<endl;
int main();
{
show();
}
return 0;
}

#include <iostream> using


28. namespace std; namespace
ns1 4
{
int a=4;
}
namespace ns2
8
{
int a=8;
} 12
int main()
{ int a=12;
ns1::a,ns2::a;
cout<<ns1::a<<endl;
cout<<ns2::a<<endl;
cout<<a; return 0;
}

#include <iostream> using


29. namespace std; class Test
fun()….fun()cons
t
{
protected:
int x;
public:
Test(int i):x(i)
{}
void fun()
const {
cout<<"fun()const"<<endl;
}
void fun()
{

cout<<"fun()"<<endl;
} };
int main()
{
Test t1(10);
const Test t2(20);
t1.fun();
t2.fun();

return 0;
}

30. error
#include <iostream>
using namespace std;
class A { int a;
public: int
assign(int i)
const {
a=i; }
int
return_value()
const {
return a;
} };
int main()
{
A obj;
obj.assign(5);
cout<<obj.return_value();

return 0;
}

#include <iostream>
31. using namespace std;
Print natural
numbers 1 to 100
int main()
{ int
n=1;
cout<<"the num
are"; do {

cout<<n<<endl;
n++; }
while(n<=100);
cout<<endl;

return 0;
}

32. Compile time error


#include <iostream> using
namespace std; #define PI
int main()
{
float r=2;
float circle;
circle=2*PI*r;
cout<<circle;
return 0;
}

#include <iostream> using


33. namespace std;
Garbage value

int main()
{ int i,a=10;
if(a<10){
for(i=0;i<10;i++)
cout<<i;
}
else
{
cout<<i;
}
return 0;
}

#include <iostream> class


34. Test
Compiles and run
{ public: fine
int i;
void get();
};
void Test::get()
{
std::cout << "enter the value of i:";
std::cin >> i;
} Test t;
int
main()
{
Test t;
t.get();
std::cout << "value of i in local t:" << 'n';
::t.get();
std::cout << "value of i in global t:" << ::t.i << 'n';
return 0;
}
#include <iostream> using
35. namespace std; inline
666
void
displayNum(int num)
{
cout<<num<<endl;
}
int main()
{
displayNum(666);

return 0;
}

#include <iostream> using


36. namespace std; namespace
10
first
{
int x=5;
int y=10;
}
namespace second
{
double x=3.1416;
double y=2.7183;
}
int main()
{
using first::x;
using second::y;
bool a,b;
a=x>y;

b=first::y<second::x;
cout<<a<<b; return
0;
}

37. Compile time error


#include <iostream>
using namespace std;
class TEMP
{ int
x; public:
TEMP();
~TEMP();
void Show()
const
};
TEMP::TEMP()
{
x=50 }
void TEMP
Show() const{
cout<<x;
}

int main()
{
TEMP obj;
obj.Show(); return
0;
}

#include <iostream>
38. using namespace std;
findcours
class ABC e
{
public:
ABC()
{
cout<<"find";
}
~ABC()
{
cout<<"course";
}
};
int main()
{
ABC obj;
return 0;
}

#include <iostream>
39. using namespace std;
int main() 6
{ int a = 5, b
= 6, c; c = (a > b)
? a : b; cout << c;
return 0;
}

#include <iostream> using


40. Compile time error
namespace std;
int grades(int a = 0, int b= 0, int c)
{ return (a+b+c);
}
int main()
{ cout <<
grades(10); return
0;
}

#include <iostream>
41. using namespace std;
erro
class A r
{ int
x;
public:
void setX(int i) { x = i;}
void print() { cout << x;}
}; class
B:public A
{
public:
B() {setX(10);}
}; class
C:public A
{
public:
C() {setX(20);}
}; class D:public B,
public C{

};
int
main() {
D d;
d.print();
return 0;

42. Option d(Class fail)


#include <iostream> using
namespace std;
abstract class
student
{ public: int
grades;
calc_marks();
} class student1:public
student
{
public : calc_marks()
{
return 20;
} };
class student2:public student
{

public : calc_marks()
{
return 30;
} }; class
fail { int
grades;
};

#include <iostream>
43. 40
using namespace std;
int main()
{ int arr[] =
{10,20,30,40,50}; int*p =
arr; p+= 3; cout<<*p;
return 0;
}

#include <iostream>
44. student1 Age=20
using namespace std;
student2 Age=25
class student
{ private:
int age;
public:
student() {
age = 20;
}
student(int a) {
age = a;
} int
getAge() {
return age;
} };
int main()
{
student stu1, stu2(25);
cout<< "student1 Age = " << stu1.getAge()<< endl;
cout<< "student2 Age = " << stu2.getAge()<< endl;

return 0;
}

#include <iostream>
45. 4
using namespace
std; class Base{
int ABC;
}; class Derived1 :
Base{
}; class Derived2 :
Derived1{
};
int main()
{
Derived2 D;
cout << sizeof(D);
return 0;
}

#include <iostream> using


46. count:1
namespace std;
2
class ABC{
private:
int x;
public:
ABC() : x(10) {}
void operator ++(){
x = x+2;
} void Print() {
cout << "count:" << x;
} }; int
main() {
ABC obj;
++obj;
obj.Print();
return 0;
}

#include <iostream> using


47. Boo
namespace std;
class Box{ l
int capacity;
public:
operator==(box b)
Box() {} {
Box(double capacity) {
this->capacity = capacity; return this-
}
};
>capacity <
b.capacity ? true :
false;
}
int main()
{
Box b1(10);
Box b2 = Box(14);
if(b1 == b2) {
cout<<"Equal";
} else{
cout<<"Not Equal";
}
return 0;
}

#include <iostream> using


48. fun()
namespace std;
class Test{ fun() const
protected:
int x;
public:
Test(int i):x(i) {} void fun() const {cout
<< "fun() const" << endl;
} void fun() {cout << "fun()"
<< endl;
} };
int main() {
Test t1 (10); const Test t2
(20); t1.fun(); t2.fun();
return 0;
}

#include <iostream> using


49.
namespace std;
8
namespace
first{ int
var=5;
}
namespace second{
double var=3.1416;
}
int main() { int a;
a=first::var+second::var;
cout<<a;

return 0;
}

#include <iostream> using


50. Compile time error
namespace std;
class A { private: int
x,y; public:void A(int a,
int b) { x=a; y=b;
}
};
int main() {
A s;
return 0;
}

51. 10
#include <iostream> using
namespace std;
class Box {
private: int
length; public:
Box():length(0) {}
friend int printLength(Box);
}; int printLength(Box
b) {
b.length +=10;
return b.length;
}
int main() { Box b;
cout<<printLength(b)<<endl;
return 0;
}

#include <iostream>
52. value of capacity is
using namespace std; :10
class Box { int
capacity; public:
Box(int cap) {
capacity = cap;

} friend void
show();

};
void show() { Box b(10); cout<<"Value of
capacity is:"<< b.capacity<<endl;
} int main()
{
show();
return 0;
}

#include <iostream> using


53.
namespace std;
15
class base {
int val1 , val2;
public:
void get() {
cout<<"Enter two values:";
cin>>val1>>val2;
} friend float
mean(base ob);
}; float mean(base ob) { return
float(ob.val1 + ob.val2) /2;
}; int main () { base obj;
obj.get(); cout<<"\n Mean value is:" <<
mean(obj); return 0;
}

#include <iostream> using


54. Constructor called
namespace std;
class Point
{
public:
Point() {
cout << " Constructor called";
} }; int
main() {
Point t1;
return 0;
}

#include <iostream>
55. Value of capacity is:
using namespace
std; 10
class Box { int
capacity; public:
Box(int cap) {
capacity = cap;
} friend void
show();

};
void show() { Box b(10); cout<<"Value of
capacity is:"<< b.capacity<<endl;
} int main()
{
show();
return 0;
}

56.

#include <iostream>
57. 50
using namespace
std; class TEMP {
int x; public:
TEMP();
~TEMP();
void Show() const;
};
TEMP::TEMP() {
x = 50;
} void TEMP::Show()
const{ cout<< x;
}
int main() {
TEMP obj; obj.Show(); return 0;
}

#include <iostream>
58. 10
using namespace std;

int main()
{ int i;
for(i=0; i<10;i++); {
cout<<i;
}
return 0;
}

#include<iostream> using
59. 10 20
namespace std;
class
A { int
x;
public:
void setX(int i){x=i;} void
print() {cout<<x;}
}; class B :
public A { public:
B() {setX(10);}
}; class C :
public A { public:
C() {setX(20);
}; class D : public B,
public C {

};
int main() {
D d;
d.print();
return 0;
}

#include<iostream> using
60.
namespace std;
31
int fun (int x,int *py, int
**ppz)
{ int
y,z;

**ppz += 2;
z = **ppz;
*py += 12; y
= *py; x +=
3; return
x+y+z;
}
int main()
{ int c,*b,**a;
c=4; b=&c;
a=&b;
cout<<fun(c,b,a);
return 0;
}

#include <iostream> using


61. Calculated value is:
namespace std;
class calculate 6
{ private: int
val; public:
calculate ():val(5){} void
operator ++() {
++val; } void display() { cout
<<"Calculated values is: "<<val<<endl;
} }; int main
() { calculate
cal1;
++cal1;
cal1.display(); return
0;
}

63. Error : ambiguous


#include <iostream> using
call to void disp()
namespace std;

class Test
{
int a;
}; class
Test1 {

int x; Test
t2; public:
operator Test() {return t2;} operator
int() {return x;}
}; void disp(int x){cout<<"disp(int)
called";} void disp (Test
t){cout<<"disp(Test)called"; } int main () {
Test1 t; disp(t); return 0;
}

#include <iostream>
64. limit of simcard2 is
#include <string> using
less
namespace std; class
simcard
{ int
limit;

public:
simcard() {}
simcard(double limit)
{ this->limit =
limit;
} bool
operator<(simcard b)
{
return b.limit < this->limit ? true : false;
}
};
int main()
{
simcard s1(10);
simcard s2 = simcard(14);
if (s1 < s2)
{ cout << "limit of simcard1 is
less";
} else { cout << "limit
of simcard2 is less";
}
return 0;
}

#include<iostream> using
65.
namespace std; int main() {
int arr[]={10,20,30,40,50}; 40
int*p=arr; p+=3;
cout<<*p; return 0;
}

66. 50 49
#include<iostream> using
namespace std;

int main()
{ int
arr[]={10,20,30,40,50};
int*p=arr; p+=4;
cout<<*p<<" ";
--*p;
cout<<*p;
return 0;
}

#include<iostream> using
67. 8500.26
namespace std; void
disp(int x) 3
{ cout
<<x;
} void disp(double
y)
{
cout<<y;
} int
main(void)
{ disp(8);
disp(500.263);
return 0;
}

#include<iostream> using
68.
namespace std;
class Box 10
{ private
:
int length; public:
Box():length(0) {}
friend int printLength(Box);
}; int printLength (Box
b)
{
b.length+=10;
return b.length; }
int main ()
{ Box b;
cout<<printLength(b)<<endl;
return 0;
}

#include<iostream> using
69. Value of capacity is
namespace std;
1
class Box
{ int
0
capacity;
public:
Box(int cap)
{
capacity=cap;
} friend void
show();
};
void show()
{
Box b(10);
cout<<"value of capacity is "<< b.capacity<<endl;
}
int main ()
{
show();
return 0;
}

#include<iostream> using
70.
namespace std;
0
int main() { int i, a =
10; if (a < 10) {
for (i = 0; i < 10; i++)
cout << i;
}

else {
cout << i;
}
return 0;
}

#include<iostream> using
71.
namespace std;
namespace Box1 16
{ int a =4;
} namespace
Box2 { int
a =13;
} int main
() { int
a=16;
Box1::a;
Box2::a;
cout<< a;
return 0;
}

#include<iostream> using
72.
namespace std;
namespace ns1 4
{ int a =
4;
} namespace 8
ns2 { int
a = 8;
} 12
int main () { int a =
12; ns1::a;
ns2::a; cout <<
ns1::a << endl; cout
<< ns2::a << endl;
cout << a; return 0;
}

#include<iostream> using
73.
namespace std;
class Box { 10
private: int
length;
public: Box():

length(0) { } friend
int printLength(Box);
}; int printLength(Box
b) {
b.length += 10;
return b.length;
}
int main() { Box b; cout
<< printLength(b) << endl;
return 0;
}

#include<iostream> using
74. Value of the
namespace std;
capacity is: 10
class Box { int
capacity; public:
Box(int cap){
capacity = cap;
} friend void
show();
};
void show() { Box b(10); cout<<"Value of
capacity is: "<< b.capacity<< endl; } int main() {
show(); return 0;
}

#include <iostream> using


75.
namespace std;
class B { int Error
b; public:
B(int i){ b
= i;
}
};
class C{
B b;
public: C(int i) {
b= B(i);
} friend void
show();

};
void show() { C c(10);
cout<<"value of b is: "<< c.b.b << endl;
} int
main(){
show();
return 0;
}

#include<iostream>
76. This loop will run
int main() { int i; for (; ;) {
forever. infinitely
std::cout<<("This loop will run forever. \n");
}
return 0;
}

#include<iostream> using
77. Compiler error
namespace std; int fun(int x=0,
int y=0, int z) { return
(x+y+z);
} int main() {
cout << fun(10);
return 0;
}

#include <iostream> using


78. Compiler error
namespace std;

class Point {
Point() {
cout<<"Constructro called";
} }; int
main() {
Point t1;
return 0;
}

#include <iostream> using


79. 20 19
namespace std;
class course { int
x,y; public:
course(int xx){
x= ++xx;
} void
Display(){
cout<<--x<<" ";
}
}; int main() {
course obj(20);
obj.Display();
obj.Display();
return 0;
}

#include <iostream> using


80.
namespace std;
class TMP { int p; 80
public:TMP(int xx, char ch){
p = xx + int(ch);
cout<<p;
}
};
int main() {
TMP obj(15,'A');
return 0;
}

#include <iostream> using


81. The program will
namespace std;
class A { static int x; report compile time
public: static void Set(int xx){
x=xx; erro
r
} void
Display(){
cout<<x;
}
}; int
A::x=0;

int main() {
A::Set(33);
A::Display();
return 0;

#include <iostream> using


82. No output
namespace std;
class
constt{
public:
int a, b;
// Default Constructor constt() {a=10; b=20;}
}; int
main() {
// Default constructor called
automatically // when the object is
created constt c; constt c; constt
c1(10,20);
cout<<"a:"<<c.a<<endl<<"b:"<<c.b;
return
1;
}

83. Compile time error


#include <iostream> using
namespace std;
class A{ private:
int x, y; public:
void A(int a, int b){
x=a; y=b;
}
};
int main() {
A s;
return 0;
}

#include <iostream> using


84.
namespace std;
int i; 1
class A{
public:
~A(){
i=10;

}
}; int
foo(){
i=3; A
ob;
return 1;
} int main() {
cout<<foo()<<endl;
return 0;
}

85. 20 19
#include <iostream> using
namespace std;
class
course{
int x, y;
public:
course(int xx){
x=++xx;
} void
Display(){
cout<<--x<<" ";
}
}; int main() {
course obj(20);
obj.Display();
obj.Display();
return 0;
}
OUTPUT BASED IMPORTANT QUESTIONS-2
1.What will be the output of the following C++ code?

#include<iostream>
using namespace std;
int main()
{
int i=3;
int l=i/-2;
int k= i% -2;
cout<<l<<k;
return 0;
}

• Compile time Error


• -1 1
• 1 -1
• Implementation defined

2.What will be the output of the following C++ code?

#include<iostream>
using namespace std;
namespace first
{ int var= 5; }
namespace second
{
double var =3.1416 ;
}
int main()
{
int a;
a= first :: var + second:: var;
cout<<a;
return 0;
}

• 8.31416
• 8
• 9
• Compile time Error
3. What will be the output of the following C++ code?
#include<iostream>
using namespace std;

int g=100;
int main()
{
int a;
{
int b;
b=20;
a=35;
g=65;
cout<<b<<a<<g;
}
a=50;
cout<<a<<g;
return 0;
}

• 2035655065
• 2035655035
• 2035635065
• 2035645065

4. What will be the output of the following C++ code?

#include<iostream>
using namespace std;
class Test {
protected:
int x;
public:
Test (int i):x(i) { }
void fun() const
{
cout<<"func() const" <<endl;
}
void fun()
{
cout<<"fun()"<<endl;
}
};

int main()
{
Test t1 (10);
const Test t2(20);
t1.fun();
t2.fun();
return 0;

• fun() ---- fun() const


• fun() const ---- fun()
• fun() --- c) fun()
• fun() const ---- fun() const

5. What will be the output of the following C++ code?

#include<iostream>
using namespace std;

class A
{
int a;
public:
int assign(int i)
const {a=i;}
int return_value()
const { return a;}
};
int main()
{
A obj;
obj.assign(5);
cout<<obj.return_value();
}

• 5
• -5
• 10
• Error

6. . What will be the output of the following C++ code?

#include<iostream>
using namespace std;

int main()
{int n=1;
cout<<"The numbers are";
do
{
cout<<n<<endl;
n++;
}
while (n<=100);
cout<<endl;
}

• Print natural numbers 0 to 99


• Print natural numbers 1 to 99
• Print natural numbers 0 to 100
• Print natural numbers 1 to 100

7.What will be the output of the following C++ code?

#include<iostream>
using namespace std;

inline void displayNum (int num)


{
cout<<num<<endl;
}
int main()
{
displayNum(666);
return 0;
}

• 6
• 66
• 666
• Compile time Error
8.What will be the output of the following C++ code?

#include<iostream>
using namespace std;
class Test {
public:
void Testfunc()
{
cout<<"Hello from Test()";
}
}a;
int main()
{
a.Testfunc();
cout<<"Main Started";
return 0;
}

• Main Started
• Main Started Hello from Test()
• Hello from Test() Main Standard
• Compile Error :Global objects are not allowed

9. What will be the output of the following C++ code?

#include<iostream>
using namespace std;
class Empty {};
int main()
{
cout<<sizeof(Empty);
return 0;
}

• A non-zero value
• 0
• Compile Error
• Runtime Error

10. What will be the output of the following C++ code?

#include<iostream>
using namespace std;
class Mycpp();
int main()
{
Mycpp obj;
return 0;
}

• Compilation Error - Constructor Missing


• Nothing would be printed
• Undefined
• In constructor

11.What will be the output of the following C++ code?

#include<iostream>
using namespace std;
void addprint()
{
static int s=1; s++;
cout<<s;
}
int main()
{
addprint();
addprint();
addprint();
return 0;
}

• 234
• 111
• 123
• 235

12. What will be the output of the following C++ code?

#include<iostream>
using namespace std;
class course
{
int x,y;
public:
course(int xx)
{
x= ++xx;
}
void Display()
{
cout<<--x << " ";
}
};
int main()
{
course obj(20);
obj.Display();
obj.Display();
return 0;
}
• 20 19
• 21 4
• 20 5
• 21 19

13.What will be the output of the following C++ code?

#include<iostream>
using namespace std;

namespace Box1 {
int a=4;
}
namespace Box2
{
int a=13;
}
int main()
{
int a=16;
Box1::a;
Box2::a;
cout<<a;
return 0;
}

• 4
• 13
• 16
• Compile Time Error

14.What will be the output of the following C++ code?

#include<iostream>
using namespace std;
class Room
{public:
double length; double breadth; double height;
double calculateArea()
{return length*breadth;
}
double calculateVolume()
{return length*breadth*height;
}
};
int main()
{
Room room1;
room1.length=4.5;
room1.breadth=3.8;
room1.height=9.2;
cout<<room1.calculateArea()<< room1.calculateVolume();
return 0;
}

• 17.1 157.32
• 17 157
• 16.9 156.3
• Error

15. What will be the output of the following C++ code?

#include<iostream>
using namespace std;
class Temp
{
static int x;
public:
static void Set(int xx){x=xx;}
void Display()
{cout<<x;
}
};

int Temp::x=0;
int main()
{
Temp::Set(33);
Temp::Display();
return 0;
}

• The program will print the output 0.


• The program will print the output 33.
• The program will print the output Garbage.
• The program will report compile time error.

16. What will be the output of the following C++ code?

#include<iostream>
using namespace std;
class ABC{
public:
ABC()
{
cout<<"find";
}
~ABC()
{
cout<<"course";
}
};
int main()
{
ABC obj;
return 0;
}
• Find
• Course
• findcourse
• Compile time Error

17.What will be the output of the following C++ code?

#include<iostream>
using namespace std;
class CLS{
int x;
public:
CLS(int xx, float yy)
{
cout<<char(yy);
} };
int main()
{
CLS obj(35,99.50);
return 0;
}

• 99
• ASCII value of 99
• Garbage value
• 99.5

18. What will be the output of the following C++ code?

#include <iostream>
#include <string>
using namespace std;
class Box
{
int capacity;
public:
Box(int cap){
capacity = cap;
}

friend void show();


};

void show()
{
Box b(10);
cout<<"Value of capacity is: "<<b.capacity<<endl;
}

int main()
{
show();
return 0;
}

• Value of capacity is: 10


• Value of capacity is: 100
• Error
• Segmentation fault

19. What will be the output of the following code if two input values taken are 10
& 20.

#include <iostream>
using namespace std;
class base{
int val1, val2;
public:
void get(){
cout<<"Enter two values :" ;
cin>>val1>>val2;
}
friend float mean(base ob);
};
float mean(base ob)
{
return float(ob.val1 + ob.val2) / 2;
}
int main()
{
base obj;
obj.get();
cout <<"Mean value is "<< mean(obj);
return 0;
}

• 10
• 20
• 15
• 25

20. What will be the output of the following C++ code?

#include <iostream>
using namespace std;
class Box
{
private:
int length;
public:
Box(): length(0) { }
friend int printLength(Box);
};
int printLength(Box b)
{
b.length += 10;
return b.length;
}
int main()
{
Box b;
cout<< printLength(b)<<endl;
return 0;
}

• 9
• 10
• 20
• 30

21. What will be the output of the following C++ code?

#include<iostream>
using namespace std;
void fnn()
{
int x=2;
{
int x=3; cout<<x<<endl;
}
}
int main()
{
fnn();
return 0;
}

• 1
• 2
• 3
• 0
22. What will be the output of the following C++ code?

#include<iostream>
using namespace std;
class A{
int id; static int count;
public:
A()
{
count++; id= count;
cout<<"Constructer for id"<<id<<endl;
}
~A()
{
cout<<"Destructor for id"<<id<<endl; }
};
int A::count=0;
int main()
{
A a[3];
return 0;
}

• constructor for id 1 constructor for id 2 constructor for id 3 destructor


for id 3 destructor for id 2 destructor for id 1
• constructor for id 1 constructor for id 2 constructor for id 3 destructor
for id 1 destructor for id 2 destructor for id 3
• Compiler dependent
• Constructor for id 1 destructor for id 1

23. What will be the output of the following C++ code?

#include<iostream>
using namespace std;
class A{
public:
~A()
{ int i=10; } };
int foo() {
int i=3; A obj;
return i;
}
int main()
{
cout<<foo()<<endl;
return 0;
}

• 0
• 10
• 3
• 5
24. What will be the output of the following C++ code?

#include<iostream>
using namespace std;
int fun(int x=0, int y=0, int z)
{
return (x+y+z);
}
int main()
{
cout<<fun(10);
return 0;
}

• 10
• 0
• 20
• Compile Error
25. What will be the output of the following C++ code?

#include<iostream>
using namespace std;
int main()
{
int i=1;
i=i-1; while(i){
cout<<"Its a while loop";
i++;
}
return 0;
}

• 1
• 2
• 0
• Infinite times

26. What will be the output of the following C++ code?

#include<iostream>
using namespace std;
int main(){
int n;
for(n=5;n>0;n--)
{
cout<<n;
if (n==3)
break;
}
return 0;
}

• 543
• 53
• 5432
• 54

28. What will be the output of the following C++ code?

#include<iostream>
using namespace std;
namespace ns1
{
int a=4;
}
namespace ns2
{
int a=8;
}
int main()
{
int a=12;
ns1::a;
ns2::a;
cout<<ns1::a<<endl;
cout<<ns2::a <<endl;
cout<< a;
return 0;

• 4 8 12
• 8 12 4
• 12 4 8
• Compile Time Error

29. What will be the output of the following C++ code?

#include<iostream>
using namespace std;
int main(){
int i=0,x=0;
for(i=1;i<10;i*=2)
{
x++; std::cout<<x;
}
std::cout<<x;
return 0;
}

• 1234567899
• 12345678910
• 123455
• 12344
30.What will be the output of the following C++ code?

#include<iostream>
using namespace std;
int main()
{
int i,j; j=10;
i=(j++,j+100,999+j);
cout<<i;
return 0;
}

• 1000
• 11
• 1010
• 1001

31. What will be the output of the following C++ code?

#include<iostream>
using namespace std;
int main()
{
int i; for(i=0; i<10; i++);
{cout<<i;
}
return 0;
}

• 123456789
• 10
• 12345678910
• Compile time Error

32. Which of the following is true about the following program:

#include<iostream>
using namespace std;
class Test
{
public:
int i;
void get();
};

void Test::get()
{
std::cout<<"Enter the value of i:";
std::cin>> i;
}

Test t;// Global object


int main()
{
Test t;// local object
t.get();
std::cout <<"value of i in local t:"<<t.i<<'n';
::t.get();
std::cout <<"value of i in global t:"<< ::t.i <<'n';
return 0;
}

• Compile Error: Cannot have two objects with same class name
• Compiler Error in Line “::t.get();”
• Compile Error in line “std::cout”
• Compiles and Runs Fine

33. What will be the output of the following C++ code?

#include<iostream>
using namespace std;
int main()
{
int a=10;
if(a<15){
time:cout<<a;
goto time;
}
return 0;
}

• 1010
• 10
• Infinity print 10
• Compile time error
34. What will be the output of the following C++ code?

#include<iostream>
using namespace std;
class B{
private: int b;
public:
void showA(){
std::cout<<"Hello";
} };
int main()
{
B b;
b.showA();
}

• Hello
• Hello Hello
• No output
• Compile Time Error

35. What will be the output of the following C++ code?

#include<iostream>
using namespace std;
int main()
{
int a=5,b=6,c;
c=(a>b)?a:b;
cout<<c;
return 0;
}

• 4
• 5
• 6
• 7

36. What will be the output of the following C++ code?


#include<iostream>
using namespace std;
class TMP {int p;
public:
TMP(int xx, char ch)
{
p=xx+int (ch);
cout<<p;
} };
int main(){
TMP obj(15,'A');
return 0;
}
• 80
• 112
• Compilation error
• Garbage Value
37. What will be the output of the following C++ code?

#include<iostream>
using namespace std;
class ABC{
public:
void ABC1()
{
cout<<"find";
}
};
int main(){
ABC obj; ABC a;
return 0;
}

• F
• Find
• No output
• Compile Time error

38. What will be the output of the following C++ code?

#include<iostream>
using namespace std;
int main()
{
int a=5,b=6,c;
c=(a>b)?(cout<< " a is greater"):(cout<< " b is greater");
cout<<c;
return 0;
}

• 4
• 5
• 6
• Error

39. What will be the output of the following C++ code?

#include <iostream>
using namespace std;
int f(int p,int q)
{
if(p>q)
return p;
else
return q;
}
int main()
{
int a=5,b=10; int k;
bool x=true;
bool y=f(a,b);
k=((a*b)+(x+y));
cout<<k;
}

• 55
• 52
• 62
• 75

40. What will be the output of the following C++ code?

#include <iostream>
using namespace std;
namespace first
{
int x=5;
int y=10;
}
namespace second
{
double x=3.1416; double y=2.7183;
}
int main()
{ using first::x;
using second::y;
bool a,b; a=x>y;b=first::y<second::x;
cout<<a<<b;
return 0;
}

• 11
• 1
• 0
• 10

41. What will be the output of the following C++ code?

#include<iostream>
using namespace std;

#define PI 3.14159
int main()
{
float r=2;
float circle;
circle= 2* PI *r;
cout<<circle;
return 0;
}

• 12.5664
• 13.5664
• 10
• 15

42. What will be the output of the following C++ code?


#include<iostream>
using namespace std;
int main()
{
int a=10;
if(a<10)
{
for(i=0;i<10;i++)
cout<<i;
}
else
{
cout<<i;
}
return 0;
}

• 123456789
• 123456789
• 0
• Error

43. What will be the output of the following C++ code?

#include<iostream>
using namespace std;
class course{
int x,y;
public:
void course1(int xx, int yy){
x=++xx; y=++yy;
}
void Display()
{
cout<<x+y<<" ";
} };
int main(){
course obj;
obj.course1(20,30);
obj.Display();
return 0;
}

• 50
• 52
• 20
• 30

You might also like