MCQ

C++ OOPs MCQ Questions and Answers – Part 5

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 syntax to override the + operator for class A?

A int +(arguments){}

B int [+](arguments){}

C A operator+(arguments){}

D A operator[+](arguments){}

C
 

2. What is the output of the following C++ code?
#include<iostream> 
using namespace std; 
  
class MyClass
{ 
  public: 
    MyClass(); 
}; 
  
MyClass::MyClass()  { 
    cout << "Constructor is called\n"; 
} 
  
int main() 
{ 
    cout << " Before \n"; 
    MyClass myclass(); 
    cout << " After \n"; 
    return 0; 
}

A Before
Constructor is called
After

B Before
After

C Constructor is called
Before
After

D None of the above.
 

 
B
Note that the line “MyClass maclass();” is not a constructor call. The compiler considers this line as a function declaration that receives no parameters and returns an object of type MyClass.
 

 

 

3. What is the output of the following C++ code?
#include<iostream> 
  
using namespace std; 
  
class MyClass { 
    int val; 
public: 
    MyClass (int v = 0) {val = v;} 
    int getValue() { return val; } 
}; 
  
int main() { 
    const MyClass obj;   
    cout << obj.getValue(); 
    return 0; 
}

A 0

B Random value

C Compilation error

D None of the above

C
const object cannot call a non-const function. The above code can be fixed either by making getValue() const or by making obj non-const.
 

 
We can correct the code by adding “const” to getValue() as follows:

int getValue() const { return val; }

Then the code displays: 0
 

 

 

4. Which of the following operators cannot be overloaded?

A ? :

B +

C

D %

A
?:, :: and . cannot be overwritten +, -, % can be overwritten.

 

 
 

5. Which of the following operators can be overloaded?

A ? :

B ::

C .

D ==

D
?:, :: and . cannot be overloaded whereas == can be overloaded.

 

 

6. What is the output of the following C++ code?
#include<iostream>
  
using namespace std; 
  
class MyClass { 
    int &val; 
public: 
    MyClass (int &v) { val = v; } 
    int getValue() { return val; } 
}; 
  
int main() 
{ 
    int v = 10; 
    MyClass obj(v); 
    cout << obj.getValue() << " "; 
    v = 20; 
    cout << obj.getValue() << endl; 
    return 0; 
}

A 10

B 20

C 10 20

D Compilation error

D
Since val is a reference in MyClass, it must be initialized with an initialization list, as shown in the code below.

#include<iostream>
  
using namespace std; 
  
class MyClass { 
    int &val; 
public: 
    MyClass (int &v):val(v) {  } 
    int getValue() { return val; } 
}; 
  
int main() 
{ 
    int v = 10; 
    MyClass obj(v); 
    cout << obj.getValue() << " "; 
    v = 20; 
    cout << obj.getValue() << endl; 
    return 0; 
}

Output:

 

 

7. An exception is generated using the keyword __________

A throws

B throw

C threw

D Thrown
 

 
B
Exceptions can be thrown anywhere in a block of code using the throw statement. The operand of the throw statement determines the type for the exception. It can be any expression. The expression’s result type determines the type of the exception. Example:

double division(int x, int y) {
   if( y == 0 ) {
      throw "Division by zero!";
   }
   return (x/y);
}

 

 

8. If the default constructor is not defined, how will the objects of the class be created?

A An error will occur at runtime.

B The compiler will generate an error

C The compiler provides its default constructor to build the object.

D None of the above

C
If we define our own constructor, the compiler does not create the default constructor.

 

 

9. What is the output of the following code?
#include <iostream> 
using namespace std; 
  
class MyClass { 
private: 
  static const int val = 10; 
public: 
  static int getValue() { return val; } 
}; 
  
int main() { 
  cout << MyClass::getValue() << endl; 
  return 0; 
}

A 10

B 0

C Compilation error

D None of the above

A
As a general rule, initialization of data members in the class declaration in C++ is not allowed, but members with “static const” are handled differently and can be initialized in the declaration.
 

 

 

10. Is it mandatory to call a constructor to create an object?

A Yes

B No

A
Yes, you need a constructor every time you create an object in C++. If you do not specify any value for the object, then a default constructor is called and automatically assigns a default value to class variables.

 

 
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 *