C++ MCQs with answers Page - 100

Here, you will find a collection of MCQ questions on C++. Go through these questions to enhance your preparation for upcoming examinations and interviews.

To check the correct answer, simply click the View Answer button provided for each question.

Have your own questions to contribute? Click the button below to share your MCQs with others!

+ Add Question

A

Admin • 833.24K Points
Coach

Q. 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

Admin • 833.24K Points
Coach

Q. 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

Admin • 833.24K Points
Coach

Q. Which class constructor is called in the following C++ code?

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

A

Admin • 833.24K Points
Coach

Q. Suppose the size of an integer is 4 bytes, what is the output of the following code?

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

A

Admin • 833.24K Points
Coach

Q. What is the output of the following C++ code?

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

A

Admin • 833.24K Points
Coach

Q. 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

A

Admin • 833.24K Points
Coach

Q. 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

A

Admin • 833.24K Points
Coach

Q. What is the output of the following C++ code?

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 these

A

Admin • 833.24K Points
Coach

Q. 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.

A

Admin • 833.24K Points
Coach

Q. What is the output of the following C++ code?

Code:
#include <iostream> 
using namespace std; 
class A 
{ 
  int s; 
   public: 
  #define ABC(A::s)
};
 
int main(int argc, char const *argv[])
{
  cout << "Hello world";
  return 0;
}
  • (A) Hello world
  • (B) Compilation error
  • (C) Segmentation Fault
  • (D) No output