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
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
[st_adsense]
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
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
[st_adsense]
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
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
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
[st_adsense]
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
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.
[st_adsense]
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.
[st_adsense]