N2N CPP MCQ Level2
N2N CPP MCQ Level2
int main()
{
cout << funStruct<10>::val << endl;
return 0;
}
Ans - 1024
int main()
{
fun<int> (1);
cout << endl;
fun<int>(1);
cout << endl;
fun<double>(1.1);
cout << endl;
return 0;
}
Ans –
x = 1 count = 0
x = 1 count = 1
x = 1.1 count = 0
Q1. State whether the following statements about the constructor are
True or False.
i) Constructors should always be declared in the private section.
ii) Constructors are invoked automatically when the objects are created.
Ans - False, True
Q2. When an object is created and initialized at the same time, a ……………….
gets called.
Ans - Copy constructor
Q3. …………….. constructor will not do anything and defined just to satisfy
the compiler.
Ans - Implicit
Q5. The constructors that can take arguments are called ……………...
Ans - Parameterized constructor
Q10. C++ provides a special ………………… called the constructor, which enables
an object to initialize itself when it is created.
Ans - Member function
Q12. In C++, ……………………. creates objects even through it was not defined in
the class.
Ans - Implicit constructor
Q14. The ………………… constructor can be called with either one argument or
no arguments.
Ans - Default argument
Q2. Which of the following statements are not true about destructor?
1. It is invoked when object goes out of the scope
2. Like constructor, it can also have parameters
3. It can be virtual
4. It should always be declared in private section
5. It bears same name as that of the class preceded by Lambda sign.
Ans - Only 2, 4, 5
Q3. Consider the below C++ program.
#include<iostream>
using namespace std;
class A
{
public:
A(){ cout <<"1";}
A(const A &obj){ cout <<"2";}
};
class B: virtual A
{
public:
B(){cout <<"3";}
B(const B & obj){cout<<"4";}
};
class C: virtual A
{
public:
C(){cout<<"5";}
C(const C & obj){cout <<"6";}
};
class D:B,C
{
public:
D(){cout<<"7";}
D(const D & obj){cout <<"8";}
};
int main()
{
D d1;
D d(d1);
}
Which of the below is not printed?
Q7. In case of inheritance where both base and derived class are having
constructors, when an object of derived class is created then
___________ .
Ans - Constructor of base class will be executed first followed by derived
class
Q11. If default constructor is not defined, then how the objects of the
class will be created?
Ans - Compiler provides its default constructor to build the object
Q14. A destructor is used to destroy the objects that have been created by
a ………………..
Ans - Constructor
Q2. In C++, which one of the following statements is correct when a class
grants friend status to another class?
Ans - All member functions of the class granted friendship have
unrestricted access to the members of the class granting the friendship.
Q7. In C++, which other keywords are used to declare composite data
types other than 'class' keyword?
Ans - both struct & union
Q17. The access specifier of the classes in C++ programs by default are :
Ans – private
Q25. In C++, when a data member of the new class is an object of another
class, it is called as
Ans - New class is a composite of other objects
Q27. In C++, a virtual function that has no definition within the base class
is called ____________.
Ans - Pure virtual function
Q28. Which data type values are placed in the base class?
Ans - Default type values
Q8. What is the output of this C++ program(if size of int is 4 bytes)?
#include<iostream>
using namespace std;
class Test
{
static int x;
int *ptr;
int y;
};
int main()
{
Test t;
cout << sizeof(t) << " ";
cout << sizeof(Test *);
}
Ans – 8 4
Q9.
What is the output of this C++ program?
#include <iostream>
using namespace std;
class Rect
{
int x, y;
public:
void set_values (int,int);
int area ()
{
return (x * y);
}
};
void Rect::set_values (int a, int b)
{
x = a;
y = b;
}
int main ()
{
Rect recta, rectb;
recta.set_values (5, 6);
rectb.set_values (7, 6);
cout << "recta area: " << recta.area();
cout << "rectb area: " << rectb.area();
return 0;
}
Ans - recta area: 30rectb area: 42
Q14. When the inheritance is private, the private methods in base class
are __________ in the derived class (in C++).
Ans – inaccessible
Q16. What is the output of this C++ program(If int size is 4 bytes)?
#include<iostream>
using namespace std;
class base {
int arr[10];
};
class b1: public base { };
class b2: public base { };
class derived: public b1, public b2 {};
int main(void)
{
cout << sizeof(derived);
return 0;
}
Ans – 80
Q17. What is the output of this C++ program?
#include <iostream>
using namespace std;
class rect
{
int x, y;
public:
void val (int, int);
int area ()
{
return (x * y);
}
};
void rect::val (int a, int b)
{
x = a;
y = b;
}
int main ()
{
rect rect;
rect.val (3, 4);
cout << "rect area: " << rect.area();
return 0;
}
Ans - rect area: 12
Q11. How many number of pointer (*) does C have against a pointer
variable declaration?
Ans – No limits