MCQ

C++ OOPs MCQ Questions and Answers – Part 1

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 does a class contain in C ++?

A Data

B Functions

C Data and Functions

D Arrays

C
Classes in C++ encapsulate all the data and functions associated with them for manipulation purposes.

 

 

2. How many access specifiers are there?

A 1

B 2

C 3

D 4

C
There are three types of access specifiers. They are “public”, “protected” and “private”.

 

 
 

3. ___ is used to define a member of a class externally?

A #

B ::

C !! $

D :

B
The :: operator is used to define the body of any class function outside the class.

 

 

4. What operator does a pointer object of a class use to access its data members and member functions?

A ::

B .

C ->

D :

C
The operator -> is used by a pointer object to access the members of its class.

 

 

5. Suppose the integers take 4 bytes, what is the output of the following code?
#include<iostream>
  
using namespace std;    
  
class MyClass
{ 
  static int a; 
  int b; 
}; 
  
int MyClass::a; 
  
int main() 
{ 
    cout << sizeof(MyClass); 
    return 0; 
}

A 4

B 8

C 16

D None of the above

A
Static data members do not contribute to the size of an object. So, “a” is not considered in the size of “MyClass”. By the way, not all functions (static and non-static) contribute to the size.

 

 
 

6. What is the output of the following C++ code?
#include <iostream>
using namespace std;
class MyClass
{
	int var;
 
   public:
	int write(int i) const {
		var = i;
	}
	int read() const {
		return var;
	}
};
int main(int argc, char const *argv[])
{
	MyClass obj;
	obj.write(2);
	cout << obj.read();
}

A 2

B 3

C Compilation error

D None of the above

C
As write() is a constant function, so it can’t change the state of an object. In the “write” function we try to change the “var” member of the object, so the program displays an error.
 

 

 

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

using namespace std;

class calculate {
    int x, y;
	
    public:
    void val(int, int);
    int sum() {
        return (x + y);
    }
};
void calculate::val(int a, int b) {
    x = a;
    y = b;
}
int main() {
    calculate calculate;
    calculate.val(5, 10);
    cout << "The sum = " << calculate.sum();
    return 0;
}

A The sum = 5

B The sum = 10

C The sum = 15

D Error because calculate is used as class name and variable name in line 19.

C
It is possible to use the class name as variable name, in this program we calculate the sum of two numbers.
 

 

 

8. Which one is a valid class declaration?

A public classe A {}

B classe A {}

C classe A {int x;};

D object A {int x;};

C
A class declaration ends with a semicolon and starts with the keyword “class”. Only option (A) follows these rules, so classe A {int x;}; is correct.

 

 
 

9. The members of a class in C++ are by default __________

A private

B protected

C public

D public and protected

A
By default, all data members and function members of the class are private.

 

 

10. What is the correct syntax to access a static member of a class?
class A
{
	public:
		static int val;
}

A A->val

B A.val

C A::val

D A^val

C
The scope resolution operator :: is used to access a static member of a class.

 

 
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 *