MCQ

C++ OOPs MCQ Questions and Answers – Part 2

Multiple choice questions and answers (MCQs) on C++ to prepare for exams, tests, and certifications. These questions are taken from a real written exam and some parts are taken from an interview. So you will find questions on basic techniques such as Variables, Operators, Conditional Statement, Functions, and more. This quiz will easily prepare anyone to pass their online test.
 

1. What is the use of constructors in C++?

A Initialize objects

B Build data members

C Both initialize objects and build data members

D Delete objects

A
Once the object is declared, the constructor is also declared by default.

 

 

2. When “struct” is used instead of the “class” keyword, will anything change in the program?

A Access is public by default

B Access is private by default

C Access is protected by default

D Access is denied

A
For structures, by default, all members (data and functions) are public.

 

 
 

3. Which class constructor is called in the following C++ code?
#include<iostream> 
  
using namespace std; 

class A { 
 public: 
     A() 
     { cout << " The constructor of class A is called." << endl;  } 
}; 
  
class B { 
 public: 
     B() 
     { cout << " The constructor of class B is called." << endl;  } 
}; 
  
class C: public A, public B { 
   public: 
     C() 
     {  cout << " The constructor of class C is called." << endl;  } 
}; 
  
int main() 
{ 
   C c; 
   return 0; 
}

A Classe C

B Class A and B

C Class A, B and C

D Compilation error

C
In case of multiple inheritance, the constructors of parent classes are always called in the order of inheritance from left to right and the destructors are called in reverse order.
 

 

 

4. Suppose the size of an integer is 4 bytes, what is the output of the following code?
#include<iostream> 

using namespace std; 
  
class A { 
    int arr[10];      
}; 
  
class B: public A { }; 
  
class C: public A { }; 
  
class D: public B, public C {}; 
  
int main(void) 
{  
  cout << sizeof(D); 
  return 0; 
}

A 4

B 40

C 80

D 160
 

 
C
Since B and C inherit from the parent class A, two copies of the parent class are present in class D, so 2*(4*10)=80 bytes
 

 

 

5. What is the output of the following C++ code?
#include<iostream> 
using namespace std; 
 
class A {
 private: 
    int val; 	
 public: 
    A(int v = 0) : val(v) {} 
    void display() { cout << "val = " << val << endl;} 
}; 
 
class B { 
 private: 
    int val; 
 public: 
    B(int v) : val(v) {} 
    operator A() const { return A(val); } 
}; 
 
void f(A a) 
{  a.display(); } 
 
int main() { 
  B b(5); 
  f(b); 
  f(55); 
  return 0; 
}

A val = 5 and val = 5

B val = 5 and val = 55

C val = 5

D val = 55

B
Since there is a conversion constructor in class A, an integer value can be assigned to objects of class A and the function call f(55) works correctly. In addition, there is an overloaded conversion operator in class B, so we can call f() with objects of class B.
 

 

 

6. How are objects self-referenced in a class member function?

A Use * with this object’s name

B Using the keyword “object”

C Use the pointer “this”

D By passing “self” as a parameter in the member function

C
In classes, objects are self-referenced using “this” pointer inside member functions. e.g. this->val to access the data member of the current object.

 

 

7. What does a mutable member of a class mean?

A A member that can never be changed

B A member that can only be modified if it is not a member of a constant object

C A member that can be modified even if it is a member of a constant object

D A member that is global in the entire class

C
Mutable members are those that can be modified even if they are members of a constant object. You can change their value even from a constant member function of this class.

 

 
 

8. What is the output of the following C++ code?
#include<iostream>

using namespace std;

class A {
public:
   void show()
   { cout <<" show() method of class A"; }
};

class B : public A {
public:
   void show()
   { cout <<" show() method of class B"; }
};

class C: public B {
}; 
  
int main(void)
{ 
  C c; 
  c.show(); 
  return 0; 
}

A “show() method of class A”

B “show() method of class B”

C Both A and B are true.

D None of the above

B
The show() method is not defined in the C class. It is therefore searched in the inheritance hierarchy. show() is present in both classes A and B, which one should be called? The idea is the following: if there is a multi-level inheritance, the function is then searched linearly in the inheritance hierarchy until a matching function is found.
 

 

 

9. Choose the correct statement:

A Macros and inline functions are the same thing.

B Macros look like function calls but are not.

C Inline functions look like functions but are not.

D The inline functions are always huge.

B
Macros in C++ look like function calls but are not actually function calls.

 

 
 

10. What is the output of the following C++ code?
#include <iostream> 
using namespace std; 
class A 
{ 
	int s; 
   public: 
	#define ABC(A::s)
};
 
int main(int argc, char const *argv[])
{
	cout << "Welcome to StackHowTo!";
	return 0;
}

A Welcome to StackHowTo!

B Compilation error

C Segmentation Fault

D No output.

B
Macros cannot access the private member of a class so #define ABC (A::s) will display an error.
 

 

 
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *