MCQ

C++ OOPs MCQ Questions and Answers – Part 3

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 output of the following C++ code?
#include <iostream>
using namespace std;
 
class A
{
	static int val;
 
   public:
	void write(int i){
		val = i;
	}
	void read(){
		cout << val;
	}
};
 
int main(int argc, char const *argv[])
{
	A a = A();
	a.write(10);
	a.read();
	return 0;
}

A 10

B Segmentation fault

C Compilation error

D None of the above
 

 
C
Each static member of a class is initialized before it is used. As ‘val’ is a static member of class A and is not initialized, the program will display an error.
 

 

 

2. What is the output of the following C++ code?
#include <iostream>
using namespace std;
 
class A
{
	static int val;
 
   public:
	void write(int i){
		val = i;
	}
	void read(){
		cout << val;
	}
};

int A::val = 55;

int main(int argc, char const *argv[])
{
	A a = A();
	a.write(10);
	a.read();
	return 0;
}

A 55

B 10

C Compilation error

D None of the above

B
As ‘val’ is a static member of class A, it is therefore a global variable of class A, i.e. any modification made by an object is reflected on all other objects.
 

 

 

3. What is the output of the following C++ code?
#include <iostream>
using namespace std;
 
class A
{
   int val = 55;
 
   public:
	void write(int i){
		val = i;
	}
	static void read(){
		cout << val;
	}
};

int main(int argc, char const *argv[])
{
	A a = A();
	a.write(10);
	a.read();
	return 0;
}

A 55

B 10

C Compilation error

D None of the above
 

 
C
Since the read() function is a static function and static members can only access static members, the program will display an error.
 

 

 

4. When allocating memory dynamically in C++, the new operator returns the value _________ if the memory allocation fails.

A False

B NULL

C Zero

D None of the above

B
If the memory allocation fails, the “new” operator returns NULL.

MyClass* obj = new MyClass();
if (obj == NULL) {   //necessary only if your compiler is old
  std::cerr << "Unable to allocate memory" << std::endl;
  abort();
}

 

 

5. We can create objects of type ‘abstract class’?

A True

B False

B
You cannot create an instance of an abstract class because its implementation is not complete.

 

 

6. The default value of a static variable is _____.

A 1

B 0

C Depends on the compiler

D None of the above

B
In C++ programming, static variables always have the default value Zero.

 

 
 

7. What is the output of the following C++ code?
#include<iostream> 
using namespace std; 
  
int a = 55; 
void fun() 
{ 
    int a = 20; 
    { 
        int a = 10; 
        cout << ::a << endl;  
    } 
} 
  
int main() 
{ 
    fun(); 
    return 0; 
}

A 10

B 20

C 55

D None of the above

C

 
If the scope resolution operator :: is placed before a variable name, the global variable is referenced. So, if we delete int a = 55;, the program compilation will fail.
 

8. Which class functions are called inline functions?

A All functions declared inside a class

B All functions defined outside the class of

C All functions defined inside or with the keyword inline

D All functions accessing static members of the class.

C
All functions defined in the class or functions with the inline keyword in front of them are inline functions of a class.

 

 

9. The object is created in _________

A A class

B Constructor

C Destructor

D Attributes

A
When you create an object, you create an “instance” of a class.

 

 
 

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

using namespace std;

class Point {
private:
    int x;
    int y;
public:
    Point(int a, int b);
};

Point::Point(int a = 0, int b = 0)  {
    x = a;
    y = b;
    cout << "Constructor called";
}
  
int main()
{
   Point p1, *p2;
   return 0;
}

A Displays “Constructor called” only once.

B Displays “Constructor called” twice.

C Does not display anything.

D Compilation error

A
If we look at the statement Point p1, *p2;, we can see that only an object is created here. p2 is just a pointer variable, not an object.
 

 

 
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 *