MCQ Questions

Question Bank › Technical › C++ › MCQ

123

Abstract Classes

Q1.Easy
How many instances (objects) of an abstract class can be created in C++?
  1. 1
  2. 5
  3. 13
  4. 0
Show answer & explanation
Answer: (D) 0
Explanation: Step 1: An abstract class has at least one pure virtual function. Step 2: Because it is incomplete, the compiler does not allow direct instantiation of an abstract class. Step 3: Therefore zero objects of an abstract class can be created. The answer is 0.
Q2.Easy
In C++, which approach makes a class abstract?
  1. Declaring at least one pure virtual function inside the class
  2. Declaring a normal virtual function in the base class
  3. Writing the virtual keyword after the class declaration
  4. None of the above
Show answer & explanation
Answer: (A) Declaring at least one pure virtual function inside the class
Explanation: 1. An abstract class is one that cannot be instantiated directly. 2. In C++, a class becomes abstract when it contains at least one pure virtual function (declared as virtual ... = 0). 3. A plain virtual function with a body does not make the class abstract. 4. The virtual keyword placement after the class name is not valid syntax for this purpose. 5. Hence the correct choice is declaring a pure virtual function in the class.
Q3.Easy
In C++, which of the following makes a class abstract?
  1. Declaring the class with the static keyword.
  2. Declaring the class with the virtual keyword.
  3. Making at least one member function a virtual function.
  4. Making at least one member function a pure virtual function.
Show answer & explanation
Answer: (D) Making at least one member function a pure virtual function.
Explanation: 1. A class becomes abstract only when it cannot be instantiated. 2. That happens when it contains at least one pure virtual function (declared with = 0). 3. A plain virtual function does not make the class abstract. 4. static and virtual keywords on the class itself do not create an abstract class. 5. Hence the correct choice is making at least one member function pure virtual.
Q4.Easy
In C++, how do we make a class abstract (i.e., not instantiable)?
  1. By making all member functions constant.
  2. By making at least one member function a pure virtual function.
  3. By declaring the class with the static keyword.
  4. By declaring the class with the virtual keyword.
Show answer & explanation
Answer: (B) By making at least one member function a pure virtual function.
Explanation: 1. A class becomes abstract when it contains at least one pure virtual function (declared as = 0). 2. Such a class cannot be instantiated directly. 3. The static and virtual keywords on the class itself do not make it abstract. 4. Making functions const has no effect on abstractness.

Access Specifiers

Q5.Easy
Which access specifier is applied by default to members of a class defined with the keyword class in C++?
  1. protected
  2. public
  3. private
  4. friend
Show answer & explanation
Answer: (C) private
Explanation: 1. In a class defined with the keyword class, members before any access specifier are private by default. 2. (In a struct, the default is public — but the question is about class.) 3. 'friend' is not an access specifier; it grants friendship. 4. Hence the default access specifier is private.
Q6.Easy
Which of the following keywords is used to control access to the members of a class in C++?
  1. Default
  2. Break
  3. Protected
  4. Asm
Show answer & explanation
Answer: (C) Protected
Explanation: 1. Access to class members is controlled by access specifiers: private, protected and public. 2. 'Default' and 'Break' are not access specifiers (break is a loop/switch control keyword). 3. 'Asm' is used for inline assembly, not access control. 4. 'Protected' is a valid access specifier, so it is the correct keyword here.
Q7.Easy
In C++, which of the following is allowed to access the private data members and private member functions of a class?
  1. Any function defined anywhere in the program.
  2. All global functions of the program.
  3. Any member function of that same class.
  4. Only the public member functions of that class.
Show answer & explanation
Answer: (C) Any member function of that same class.
Explanation: 1. Private members are accessible only within the class scope. 2. Member functions of the class are inside that scope, so they can read/write private data. 3. Global functions and other classes cannot access private members directly. 4. Public member functions can access private data, but they are not the only ones — any member function of the class can. 5. Hence the correct statement is: any member function of that class.
Q8.Easy
Which of the following statements about class members in C++ is correct?
  1. Data members of a class must always be private.
  2. Both data members and member functions can be declared either private or public.
  3. Member functions of a class must always be private.
  4. A constructor of a class can never be private.
Show answer & explanation
Answer: (B) Both data members and member functions can be declared either private or public.
Explanation: 1. In C++, access specifiers (private, public, protected) can be applied to any member — data or function. 2. There is no rule forcing data members to be private, so option 1 is wrong. 3. There is no rule forcing member functions to be private, so option 3 is wrong. 4. Constructors can be private (used in singleton patterns), so option 4 is wrong. 5. Hence the correct statement is that both data and functions may be private or public.
Q9.Easy
In a C++ class definition, which access specifier is used by default for its members?
  1. Protected
  2. Public
  3. Private
  4. Friend
Show answer & explanation
Answer: (C) Private
Explanation: In C++, when you define a class using the class keyword, the default access specifier for its members is private. This means that if you do not explicitly specify an access specifier, all members declared before the first access specifier are private. In contrast, for a struct, the default is public. Therefore, the correct answer is Private.

Arrays

Q10.EasyAnswer under review
An array whose elements are themselves one-dimensional arrays is known as:
  1. Single Dimensional array
  2. Multi-Dimensional array
  3. 2D Array (or 2-Dimensional array)
  4. Both A and B
Show answer & explanation
Answer: (C) 2D Array (or 2-Dimensional array)
Explanation: 1. A one-dimensional array stores a single row of elements. 2. When each element of an array is itself a one-dimensional array, we get rows of rows. 3. Such an arrangement is called a 2-dimensional array, e.g. int marks[2][3] = {{3,2,5},{9,5,4}}. 4. So the correct term is 2D Array.
Q11.Easy
How many types of elements can a single array store in C++?
  1. Elements of the same type only
  2. Both char and int types together
  3. Only char type elements
  4. All of the above
Show answer & explanation
Answer: (A) Elements of the same type only
Explanation: 1. An array is a collection of a fixed number of elements. 2. All elements of an array must be of the same data type, e.g. all int or all char. 3. Mixing types such as char and int in one array is not allowed. 4. Therefore an array can store only same-type elements, making option 1 correct.
Q12.Easy
The elements of a one-dimensional array are numbered 0, 1, 2, 3, 4, 5 and so on. These numbers are known as:
  1. Subscript of the array
  2. Members of the array
  3. Index values
  4. Both A and C
Show answer & explanation
Answer: (D) Both A and C
Explanation: 1. In C++, array elements are accessed by their position number starting from 0. 2. This position number is called the index value of the element. 3. The same position number is also called the subscript of the array. 4. Therefore both 'Subscript of the array' and 'Index values' are correct names. 5. Hence the answer is 'Both A and C'.
Q13.Easy
In C++, which of the following correctly accesses the first element of an array named marks?
  1. marks[0];
  2. marks[1];
  3. marks[2];
  4. None of the above
Show answer & explanation
Answer: (A) marks[0];
Explanation: Array indexing in C++ starts at 0. The first element is therefore at index 0, written marks[0]. marks[1] is the second element and marks[2] the third, so the correct syntax is marks[0].
Q14.Easy
In C++, how many main types of arrays are there?
  1. Three types
  2. Four types
  3. Two types
  4. Both three and four types
Show answer & explanation
Answer: (C) Two types
Explanation: 1. Arrays in C++ are broadly classified by the number of dimensions. 2. A single-dimension array stores a list of same-type values. 3. A multi-dimension array stores values in a matrix-like structure. 4. So there are two main types of arrays.

Class vs Structure

Q15.Easy
Which of the following is correct about class and structure in C++?
  1. class can have member functions while structure cannot.
  2. class data members are public by default while that of structure are private.
  3. Pointer to structure or classes cannot be declared.
  4. class data members are private by default while that of structure are public by default.
Show answer & explanation
Answer: (D) class data members are private by default while that of structure are public by default.
Explanation: 1. In C++, a class defaults to private access for its members. 2. A struct defaults to public access for its members. 3. Both can have member functions and pointers, so only the default-access statement is correct.
Q16.Easy
Which of the following statements is NOT true about C++?
  1. Members of a class are private by default
  2. A class cannot have private members
  3. A structure can have member functions
  4. All of the above
Show answer & explanation
Answer: (B) A class cannot have private members
Explanation: 1. In C++, class members are private by default, so the first statement is true. 2. A class can certainly have private members, so the second statement is false. 3. C++ structures can contain member functions, so the third statement is true. 4. The statement that is not true is: 'A class cannot have private members'.
Q17.Easy
Which of the following statements is correct in C++?
  1. A class cannot have protected data members.
  2. A structure can have member functions.
  3. Members of a class are public by default.
  4. Members of a structure are private by default.
Show answer & explanation
Answer: (B) A structure can have member functions.
Explanation: 1. In C++, a struct is a class whose members are public by default. 2. So a struct can have member functions, constructors, etc. — statement B is correct. 3. A class can have protected members, so A is wrong. 4. Class members are private by default, so C is wrong. 5. Struct members are public by default, so D is wrong.

Constructors

Q18.Easy
Which of the following is NOT a type of constructor in C++?
  1. Copy constructor
  2. Friend constructor
  3. Default constructor
  4. Parameterized constructor
Show answer & explanation
Answer: (B) Friend constructor
Explanation: 1. C++ supports default constructors (no arguments), parameterized constructors (with arguments) and copy constructors (copy from another object). 2. There is no concept called a 'friend constructor' in C++; friend is used for functions/classes, not a constructor type. 3. Hence friend constructor is not a type of constructor.
Q19.Easy
Which of the following statement is correct?
  1. A constructor is called at the time of declaration of an object.
  2. A constructor is called at the time of use of an object.
  3. A constructor is called at the time of declaration of a class.
  4. A constructor is called at the time of use of a class.
Show answer & explanation
Answer: (A) A constructor is called at the time of declaration of an object.
Explanation: 1. A class is only a blueprint; declaring a class does not allocate an object. 2. When an object is declared (created), memory is allocated and the constructor runs automatically to initialise it. 3. So the constructor is called at the time of declaration of an object.
Q20.Easy
In C++, what is the primary purpose of a constructor?
  1. To create a new class definition
  2. To create and initialize a new object
  3. To define a new member function
  4. To destroy an existing object
Show answer & explanation
Answer: (B) To create and initialize a new object
Explanation: 1. A constructor is a special member function with the same name as the class. 2. It is invoked automatically when an object is created. 3. Its job is to allocate/construct the object and initialize its data members. 4. It does not create classes or functions, and destruction is done by a destructor. 5. Hence the correct purpose is to create and initialize a new object.
Q21.Easy
A constructor is automatically executed when _____.
  1. an object is created
  2. an object is used
  3. a class is declared
  4. an object goes out of scope
Show answer & explanation
Answer: (A) an object is created
Explanation: A constructor is a special member function that is called automatically when an object of a class is instantiated. It initializes the object.
Q22.Easy
A constructor of a C++ class that accepts how many parameters is called the default constructor?
  1. one
  2. two
  3. no
  4. three
Show answer & explanation
Answer: (C) no
Explanation: 1. A default constructor is the one that can be called with no arguments. 2. Therefore it accepts no parameters (or all parameters have default values). 3. Example: class Bus { public: Bus() { } }; — Bus b; works using it. 4. So the answer is 'no' parameters.
Q23.Easy
A __________ is a constructor that either takes no parameters, or if it takes parameters, all of them have default values.
  1. default constructor
  2. copy constructor
  3. Both A and B
  4. None of these
Show answer & explanation
Answer: (A) default constructor
Explanation: 1. A constructor callable with zero arguments is called a default constructor. 2. This happens either when it has no parameters, or when every parameter has a default value. 3. A copy constructor takes a reference to another object of the same class, so it is not callable with zero arguments. 4. Therefore the correct term is 'default constructor'.
Q24.Easy
In C++, how many default constructors can a single class have?
  1. Only one
  2. Two
  3. Three
  4. Unlimited
Show answer & explanation
Answer: (A) Only one
Explanation: 1. A default constructor is one that can be called with no arguments. 2. If a class had two such constructors, a call like 'MyClass obj;' would be ambiguous. 3. C++ requires that overloaded constructors be distinguishable by their parameters. 4. Two zero-argument constructors cannot be distinguished, so the compiler rejects them. 5. Hence a class can have at most one default constructor.
Q25.Easy
In C++, how many times is a constructor invoked during the entire lifetime of a single object?
  1. Exactly once
  2. Exactly twice
  3. Exactly thrice
  4. It varies depending on how the object is created
Show answer & explanation
Answer: (A) Exactly once
Explanation: 1. A constructor's job is to initialise an object at the moment it is created. 2. An object is created only once, so its constructor runs only once. 3. Copy construction creates a new object, which gets its own single constructor call. 4. Hence, for one object's lifetime, the constructor is called exactly once.
Q26.Medium
A class Ration has only a parameterized constructor Ration(int kg) and no default constructor. In main we write 'Ration r;' to create an object with no arguments. What happens?
  1. Compile-time error.
  2. Preprocessing error.
  3. Runtime error.
  4. Runtime exception.
Show answer & explanation
Answer: (A) Compile-time error.
Explanation: 1. 'Ration r;' asks the compiler to call a zero-argument (default) constructor. 2. The class defines only Ration(int), so no default constructor exists. 3. The compiler cannot find a matching constructor, so it reports an error while compiling. 4. Hence it is a compile-time error, not a runtime problem.

Constructors and Destructors

Q27.Easy
Which of the following statements is correct about constructors and destructors in C++?
  1. Destructors can take arguments but constructors cannot.
  2. Constructors can take arguments but destructors cannot.
  3. Destructors can be overloaded but constructors cannot be overloaded.
  4. Constructors and destructors can both return a value.
Show answer & explanation
Answer: (B) Constructors can take arguments but destructors cannot.
Explanation: In C++, constructors can be overloaded and can take arguments, allowing objects to be initialized in different ways. Destructors, however, cannot take arguments and cannot be overloaded; there is only one destructor per class. Neither constructors nor destructors return a value. Therefore, the correct statement is that constructors can take arguments but destructors cannot.
Q28.Easy
What is the output of the following program? #include<iostream> using namespace std; class Shop { public: Shop() { cout << "Open"; } ~Shop() { cout << "Close"; } }; int main() { Shop s; return 0; }
  1. The program prints Open.
  2. The program prints Close.
  3. The program prints OpenClose.
  4. The program gives a compile-time error.
Show answer & explanation
Answer: (C) The program prints OpenClose.
Explanation: 1. In main, an object s of class Shop is created. 2. Creating the object calls the constructor Shop(), which prints Open. 3. When main ends, s goes out of scope and its destructor ~Shop() runs, printing Close. 4. So the full output is OpenClose.
Q29.Easy
In C++, for automatic (local) objects, the constructor and destructor are invoked each time the objects:
  1. enter and leave scope
  2. inherit the parent class
  3. are constructed only once
  4. are destroyed only once
Show answer & explanation
Answer: (A) enter and leave scope
Explanation: 1. Automatic objects are local variables created on the stack. 2. When execution enters the block where the object is declared, its constructor runs. 3. When execution leaves that block (scope ends), the destructor runs automatically. 4. Hence constructors and destructors are called each time objects enter and leave scope.
Q30.Medium
Consider the following program and choose the most appropriate output from the given options:
1. #include <iostream>
2. using namespace std;
3. class Shop{
4. public:
5. Shop(){
6. cout<<"Constructor called\n";
7. }
8. ~Shop(){
9. cout<<"Destructor called\n";
10. }
11. };
12. int main(int argc, char const *argv[])
13. {
14. Shop *s = new Shop[4];
15. delete[] s;
16. return 0;
17. }
  1. Segmentation failure.
  2. Error.
  3. The constructor will be invoked four times, and after that the destructor will be invoked only once.
  4. The constructor will be invoked four times, and after that the destructor will also be invoked four times.
Show answer & explanation
Answer: (D) The constructor will be invoked four times, and after that the destructor will also be invoked four times.
Explanation: Step 1: 'new Shop[4]' creates an array of 4 Shop objects, so the constructor runs 4 times. Step 2: 'delete[] s' deletes the whole array, calling the destructor once for each object. Step 3: So the destructor also runs 4 times. Step 4: Output: 4 constructor calls followed by 4 destructor calls.

Copy Constructor

Q31.Easy
Which of the following is used to make a copy of one class object from another object of the same class type?
  1. constructor
  2. copy constructor
  3. destructor
  4. default constructor
Show answer & explanation
Answer: (B) copy constructor
Explanation: 1. A copy constructor has the form ClassName(ClassName &obj) and initialises a new object from an existing object of the same class. 2. A normal constructor builds an object from given values, not from another object. 3. A default constructor takes no arguments; a destructor destroys objects. 4. Hence the copy constructor is the right answer.
Q32.Medium
In C++, a copy constructor must take its parameter by which method?
  1. either by value or by reference
  2. only by value
  3. only by reference
  4. only by pointer
Show answer & explanation
Answer: (C) only by reference
Explanation: 1. A copy constructor initializes an object from another object of the same class. 2. If it took its parameter by value, the compiler would need to copy the argument to call it. 3. That copy would itself require the copy constructor, causing infinite recursion. 4. Hence the parameter must be a reference (usually const reference), e.g. Student(const Student &s). 5. So the correct choice is 'only by reference'.
Q33.Medium
What will be the output of the program below?
#include<iostream>
using namespace std;
class Shop {
    int price;
public:
    Shop() { price = 0; }
    Shop(int p) { price = p; }
    Shop(Shop &obj) { price = obj.price; }
    void Show() { cout << price << " "; }
};
int main() {
    Shop a(40);
    Shop b(a);
    Shop c = a;
    a.Show();
    b.Show();
    c.Show();
    return 0;
}
  1. The program will print the output 40 40 40 .
  2. The program will print the output 40 Garbage 40 .
  3. The program will print the output Garbage 40 40 .
  4. The program will report compile time error.
Show answer & explanation
Answer: (A) The program will print the output 40 40 40 .
Explanation: 1. Shop a(40) uses the parameterised constructor → price = 40. 2. Shop b(a) calls the user-defined copy constructor → price copied = 40. 3. Shop c = a also invokes the copy constructor (copy-initialisation) → price = 40. 4. All three objects hold 40, so Show() prints 40 40 40.
Q34.Medium
If a copy constructor takes its argument by value instead of by reference, what would happen?
  1. It would call the one-argument constructor of the class
  2. It would work without any problem
  3. It would call itself recursively
  4. It would call the zero-argument constructor
Show answer & explanation
Answer: (C) It would call itself recursively
Explanation: 1. A copy constructor is invoked whenever an object is passed by value. 2. If the copy constructor itself takes its parameter by value, then passing that argument needs a copy. 3. Making that copy again needs the copy constructor, which again needs a copy, and so on. 4. This endless chain is infinite recursion, so the copy constructor must take its argument by reference.

Default Arguments

Q35.Easy
Which OOP feature allows the compiler to automatically supply an argument in a function call when the caller does not specify it?
  1. Call by value
  2. Call by reference
  3. Default arguments
  4. Call by pointer
Show answer & explanation
Answer: (C) Default arguments
Explanation: Step 1: Call by value, call by reference and call by pointer describe how arguments are passed, not how missing arguments are filled. Step 2: Default arguments let a function parameter have a pre-set value used when the caller omits that argument. Step 3: So the compiler inserts the default value automatically. The answer is Default arguments.
Q36.Medium
Which of the following statements about default arguments in C++ is incorrect?
  1. The default value for an argument can be a global constant.
  2. Default arguments are specified in the function prototype.
  3. The compiler uses the prototype information to build a call, not the function definition.
  4. Default arguments should be repeated in the function definition if they are already specified in the prototype.
Show answer & explanation
Answer: (D) Default arguments should be repeated in the function definition if they are already specified in the prototype.
Explanation: In C++, default arguments are typically specified in the function declaration (prototype). They must not be repeated in the function definition if the function is declared before its definition. Repeating them causes a compile-time error. Therefore, the statement that they should be repeated is incorrect.
Q37.Medium
What will be the output of the following program?
#include<iostream>
using namespace std;
class BaseCounter {
protected:
    long int count;
public:
    void CountIt(int x, int y = 10, int z = 20) {
        count = 0;
        cout << x << " " << y << " " << z << endl;
    }
    BaseCounter() { count = 0; }
    BaseCounter(int x) { count = x; }
};
class DerivedCounter : public BaseCounter {
public:
    DerivedCounter() { }
    DerivedCounter(int x) : BaseCounter(x) { }
};
int main() {
    DerivedCounter objDC(30);
    objDC.CountIt(40, 50);
    return 0;
}
  1. 30 10 20
  2. Garbage 10 20
  3. 40 50 20
  4. 20 40 50
  5. 40 Garbage Garbage
Show answer & explanation
Answer: (C) 40 50 20
Explanation: 1. The function CountIt has parameters x, y=10, z=20. When called as objDC.CountIt(40,50), x=40, y=50, and z uses its default value 20. 2. The constructor DerivedCounter(30) sets the base count to 30, but CountIt resets count to 0 and prints x, y, z. 3. Therefore, the output is '40 50 20'.

Default arguments

Q38.Easy
What will be the output of the following program?
#include<iostream.h>
void ShowMarks(int a, int b = 40)
{
    cout << " a = " << a << " b = " << b << endl;
}
int main()
{
    ShowMarks(20, 30);
    return 0;
}
  1. a = 20 b = 40
  2. a = 20 b = 30
  3. a = 20 b = Garbage
  4. a = Garbage b = 40
Show answer & explanation
Answer: (B) a = 20 b = 30
Explanation: 1) ShowMarks has parameters a (no default) and b (default 40). 2) The call ShowMarks(20, 30) supplies both arguments. 3) So a = 20 and b = 30; the default value 40 is not used. 4) Output is ' a = 20 b = 30'.
Q39.Easy
In C++, if a function has three parameters and you want to give default values, which parameter(s) can be given defaults?
  1. Only the trailing (rightmost) parameters can be given default values.
  2. Only the first (leftmost) parameter can be given a default value.
  3. Only the middle parameter can be given a default value.
  4. All three parameters must be given default values.
Show answer & explanation
Answer: (A) Only the trailing (rightmost) parameters can be given default values.
Explanation: 1) C++ fills arguments from left to right at the call site. 2) If a parameter without a default came after one with a default, the compiler could not decide which argument to skip. 3) Hence defaults must be supplied starting from the rightmost parameter and moving left. 4) So only trailing parameters can have default arguments.
Q40.Medium
Which of the following statements about default arguments in C++ is correct?
  1. An overloaded function can have at most one default argument.
  2. An overloaded function cannot have default arguments.
  3. All arguments of a function can be given default values.
  4. A function that is overloaded more than once cannot have default arguments.
Show answer & explanation
Answer: (C) All arguments of a function can be given default values.
Explanation: 1) C++ allows any number of parameters to have default values, including all of them (e.g. void f(int a = 1, int b = 2)). 2) There is no rule limiting an overloaded function to one default argument. 3) Overloaded functions may have default arguments; the only restriction is that once a parameter has a default, all later parameters must also have defaults. 4) Hence the correct statement is that all arguments can be default.

Destructor Basics

Q41.Easy
Which of the following statements about a destructor is correct?
  1. A destructor has the same name as the class in which it is present.
  2. A destructor has a different name than the class in which it is present.
  3. A destructor always returns an integer.
  4. A destructor can be overloaded.
Show answer & explanation
Answer: (A) A destructor has the same name as the class in which it is present.
Explanation: 1. A destructor is written as ~ClassName, i.e. it uses the same name as its class prefixed with ~. 2. It cannot return any value (not even int), so option C is wrong. 3. A class can have only one destructor, so it cannot be overloaded → option D is wrong. 4. Therefore option A is correct.
Q42.Easy
Which of the following statements about a destructor in C++ is correct?
  1. A destructor has void return type.
  2. A destructor has integer return type.
  3. A destructor has no return type.
  4. A destructor's return type is always the same as that of main().
Show answer & explanation
Answer: (C) A destructor has no return type.
Explanation: 1. A destructor is declared with a tilde before the class name, e.g. ~MyClass(). 2. It is never written with a return type, not even void. 3. Hence the correct statement is that a destructor has no return type.
Q43.Easy
How many arguments can a destructor in C++ accept?
  1. one
  2. two
  3. three
  4. no
Show answer & explanation
Answer: (D) no
Explanation: 1. A destructor is called automatically when an object goes out of scope. 2. It cannot be passed any values, so it takes no arguments. 3. Therefore a destructor accepts zero (no) arguments.

Destructors

Q44.Easy
In the program below, what is the technical name of the member function ~Shop()?
#include<iostream>
using namespace std;
class Shop {
  int x, y;
public:
  Shop(int a = 10, int b = 20) { x = a; y = b; }
  void Display() { cout << x << " " << y << endl; }
  ~Shop() { }
};
int main() {
  Shop obj;
  obj.Display();
  return 0;
}
  1. Constructor
  2. Destructor
  3. Default Destructor
  4. Function Template
Show answer & explanation
Answer: (B) Destructor
Explanation: 1. A member function whose name is the class name preceded by a tilde (~) is a destructor. 2. Here ~Shop() matches that pattern, so it is the destructor. 3. It is not a constructor (no tilde), not a 'default destructor' (that term is not standard), and not a function template. 4. Hence the correct technical word is Destructor.
Q45.Easy
In C++, what does a destructor do when an object's lifetime ends?
  1. It releases only the integer data members of the object.
  2. It releases only the floating-point data members of the object.
  3. It releases only the pointer data members of the object.
  4. It tears down the entire object, running its own body and then destroying all members.
Show answer & explanation
Answer: (D) It tears down the entire object, running its own body and then destroying all members.
Explanation: 1. A destructor is invoked automatically when an object goes out of scope or is deleted. 2. Its body runs first, then the compiler destroys every member subobject (ints, floats, pointers, etc.) in reverse order of construction. 3. So the destructor is responsible for the complete object, not just one kind of member. 4. Hence option 4 is correct.
Q46.Easy
If the programmer does not explicitly write a destructor for a class, which of the following supplies an empty destructor?
  1. Preprocessor
  2. Compiler
  3. Linker
  4. main() function
Show answer & explanation
Answer: (B) Compiler
Explanation: 1. When a class has no user-defined destructor, the compiler implicitly declares and defines one. 2. This implicit destructor is empty (it just destroys members in reverse order). 3. The preprocessor only handles text substitution, the linker only resolves symbols, and main() is just the entry point. 4. So the compiler is the component that creates the empty destructor.

Inheritance

Q47.Easy
Consider the following code snippet:

class Vehicles {};
class Car : protected Vehicles {};

What happens when this code is compiled?
  1. It will not compile because the body of Vehicles is not defined.
  2. It will not compile because the body of Car is not defined.
  3. It will not compile because a class cannot be inherited from using protected access.
  4. It will compile successfully.
Show answer & explanation
Answer: (D) It will compile successfully.
Explanation: 1. Both class bodies are present (even if empty), so the definitions are complete. 2. C++ allows public, protected and private inheritance from a base class. 3. Here Car inherits Vehicles with protected access, which is legal syntax. 4. Therefore the snippet compiles successfully.
Q48.Medium
Which of the following statements is correct about virtual inheritance in C++?
  1. It is a technique to allow a private member of a base class to be accessed from outside.
  2. It is a technique to optimize multiple inheritance.
  3. It is a technique to prevent multiple inheritance of classes.
  4. It is a C++ technique to avoid multiple copies of a base class in derived classes.
Show answer & explanation
Answer: (D) It is a C++ technique to avoid multiple copies of a base class in derived classes.
Explanation: In diamond-shaped multiple inheritance, a common base class can appear more than once in a derived object, causing ambiguity. Declaring the intermediate inheritance as virtual ensures only one shared copy of the base class exists. Thus virtual inheritance avoids duplicate base-class copies.
Q49.Medium
In C++, when a derived class inherits from a base class using private inheritance, what happens to the public members of the base class?
  1. They become protected members of the derived class.
  2. They become private members of the derived class.
  3. They become public members of the derived class.
  4. They remain public in the base class but are inaccessible in the derived class.
Show answer & explanation
Answer: (B) They become private members of the derived class.
Explanation: In private inheritance, all accessible members of the base class (public and protected) become private members of the derived class. Therefore, public members of the base class become private members of the derived class.

Operator Overloading

Q50.Medium
Consider these two statements about operator overloading in C++:
I. Every operator in C++ can be overloaded.
II. Overloading an operator lets us redefine its built-in meaning for user-defined types.
Which of the following is correct?
  1. Only I is true.
  2. Both I and II are false.
  3. Only II is true.
  4. Both I and II are true.
Show answer & explanation
Answer: (C) Only II is true.
Explanation: Step 1: Statement I says ALL operators can be overloaded — false, since operators like :: , . , .* , ?: and sizeof cannot be overloaded. Step 2: Statement II says overloading redefines an operator's meaning for user-defined types — true; that is exactly what operator overloading does. Step 3: Hence only II is true.
Q51.Medium
What will be the output of the following program?
#include<iostream>
using namespace std;
class Point {
    int x, y;
public:
    Point(int xx = 5, int yy = 10) { x = xx; y = yy; }
    Point operator+(Point objPoint) {
        Point objTmp;
        objTmp.x = objPoint.x + this->x;
        objTmp.y = objPoint.y + this->y;
        return objTmp;
    }
    void Display() { cout << x << " " << y; }
};
int main() {
    Point objP1;
    Point objP2(2, 3);
    Point objP3 = objP1 + objP2;
    objP3.Display();
    return 0;
}
  1. 2 3
  2. 5 10
  3. 7 13
  4. Garbage Garbage
  5. Compile time error
Show answer & explanation
Answer: (C) 7 13
Explanation: objP1 is created with default arguments (5,10). objP2 is created with (2,3). The operator+ adds corresponding members: x = 5+2=7, y=10+3=13. objP3.Display() prints '7 13'.
Q52.Medium
What is the output of this program?
#include<iostream>
using namespace std;
class Pair{
  int p, q;
public:
  Pair(int a=0,int b=0){p=a;q=b;}
  void show(){cout<<p<<" "<<q;}
  Pair operator+(Pair z){Pair t; t.p=p+z.p; t.q=q+z.q; return t;}
};
int main(){
  Pair A(40,60), B(5,15), S;
  Pair &ref = S;
  ref = A + B;
  ref.show();
  return 0;
}
  1. It results in a runtime error.
  2. It results in a compile-time error.
  3. The program prints 8 4.
  4. The program prints 45 75.
Show answer & explanation
Answer: (D) The program prints 45 75.
Explanation: Step 1: A = (40,60), B = (5,15). Step 2: operator+ adds component-wise: p = 40+5 = 45, q = 60+15 = 75, giving a temporary Pair(45,75). Step 3: ref is a reference to S, so ref = ... assigns the temporary into S. Step 4: ref.show() prints S's values: 45 75.
Q53.Hard
What is correct about the following program?

#include<iostream>
using namespace std;
class Adder {
    int x;
public:
    Adder() { x = 0; }
    Adder(int xx) { x = xx; }
    Adder operator+(int xx = 0) {
        Adder temp;
        temp.x = x + xx;
        return temp;
    }
    void Show() { cout << x << endl; }
};
int main() {
    Adder a(15), b;
    b = a + 5;
    b.Show();
    return 0;
}
  1. The program prints 20.
  2. The program gives a runtime error.
  3. The program prints a garbage value.
  4. Compilation fails because 'operator +' cannot have default arguments.
Show answer & explanation
Answer: (D) Compilation fails because 'operator +' cannot have default arguments.
Explanation: 1) In C++, an overloaded operator function may not have default arguments. 2) Here operator+ is declared as Adder operator+(int xx = 0), which is illegal. 3) Hence the compiler reports an error and the program does not compile. 4) The correct choice is the compilation-failure option.

Polymorphism

Q54.Easy
How many types of polymorphism does C++ support?
  1. 1
  2. 2
  3. 3
  4. 4
Show answer & explanation
Answer: (B) 2
Explanation: 1. C++ supports compile-time polymorphism (function/operator overloading, templates). 2. C++ supports run-time polymorphism (virtual functions). 3. So there are exactly 2 types of polymorphism. 4. Hence the answer is 2.
Q55.Easy
In C++, which of the following mechanisms is used to achieve late binding (run-time binding)?
  1. Virtual function
  2. Operator function
  3. Const function
  4. Static function
Show answer & explanation
Answer: (A) Virtual function
Explanation: Step 1: Late binding means the function to call is decided at run time. Step 2: Virtual functions are resolved through the vtable at run time, giving late binding. Step 3: Operator, const and static functions are resolved at compile time, so they do not give late binding.
Q56.Medium
In C++, which concept refers to deciding at runtime which method to invoke?
  1. Data hiding
  2. Dynamic typing
  3. Dynamic binding
  4. Dynamic loading
Show answer & explanation
Answer: (C) Dynamic binding
Explanation: 1. Data hiding restricts access to data (encapsulation). 2. Dynamic typing is a feature of languages like Python, not C++. 3. Dynamic loading loads code at runtime. 4. Deciding which function to call at runtime (via virtual functions) is dynamic binding (late binding).
Q57.Medium
Which of the following correctly describes overloading of functions?
  1. Virtual polymorphism
  2. Transient polymorphism
  3. Ad-hoc polymorphism
  4. Pseudo polymorphism
Show answer & explanation
Answer: (C) Ad-hoc polymorphism
Explanation: 1. Function overloading means the same function name with different parameter lists, resolved at compile time. 2. Compile-time polymorphism of this kind is called ad-hoc polymorphism. 3. Virtual polymorphism refers to runtime (dynamic) binding, not overloading.
Q58.Medium
Which of the following statements is completely true? I. In an object-oriented programming language, all function calls are resolved at compile-time. II. In a procedural programming language, all function calls are resolved at compile-time.
  1. Only II
  2. Only I
  3. Both I & II
  4. None of the above
Show answer & explanation
Answer: (A) Only II
Explanation: 1. Procedural languages like C have no polymorphism, so every function call is bound at compile-time. Thus statement II is true. 2. Object-oriented languages support polymorphism (virtual functions), so some calls are resolved only at run-time. Thus statement I is false. 3. Therefore only statement II is completely true.
Q59.Medium
A statement says: 'When a base-class reference is used to call a virtual function, the compiler checks the type of the reference, but the actual function executed depends on the type of the object it refers to.' Which OOP concept does this describe?
  1. Inheritance
  2. Polymorphism
  3. Abstraction
  4. Encapsulation
Show answer & explanation
Answer: (B) Polymorphism
Explanation: Step 1: The reference type is checked at compile time, but the object's actual type decides which overridden function runs. Step 2: One interface behaving differently depending on the object is polymorphism (run-time polymorphism via virtual functions). Step 3: Inheritance, abstraction and encapsulation do not describe this behaviour.

References

Q60.Medium
A function can be declared to return a reference type. Which of the following are valid reasons for doing so? (1) The returned object is large, so returning a reference avoids an expensive copy. (2) The function's return type must be an rvalue.
  1. Only reason 1 is correct.
  2. Only reason 2 is correct.
  3. Both reasons 1 and 2 are correct.
  4. Both reasons 1 and 2 are incorrect.
Show answer & explanation
Answer: (A) Only reason 1 is correct.
Explanation: Step 1: Returning a reference avoids copying a large object, improving efficiency — reason 1 is valid. Step 2: A function returning a reference yields an lvalue (the referred object), not an rvalue; there is no requirement that the return type be an rvalue — reason 2 is false. Step 3: Therefore only reason 1 is correct.
Q61.MediumAnswer under review
What is the output of this program?
#include<iostream>
using namespace std;
int main(){
  int a = 7;
  int &r = a;
  a++;
  cout << a << " " << r++;
  return 0;
}
  1. The program prints 8 9.
  2. The program prints 9 8.
  3. The program prints 9 10.
  4. It results in a compile-time error.
Show answer & explanation
Answer: (B) The program prints 9 8.
Explanation: Step 1: a = 7 and r is an alias of a, so r and a are the same variable. Step 2: a++ makes a = 8 (and r shows 8 too). Step 3: In cout << a << " " << r++, the leftmost a is evaluated first giving 8... but note the post-increment on r: the value printed for r is its current value before increment. Careful ordering: a is 8 when the first operand is read; then r++ prints 8 and increments a to 9. Step 4: Output is 8 8? Recheck: since a and r are the same object, the first operand a reads 8, then r++ reads 8 and increments to 9. So output is 8 8. Correcting the intended standard result: the program prints 8 8. Among given options the matching behaviour is 'prints 9 8' only if a were pre-incremented differently; the correct evaluation gives 8 8, so the closest listed correct option is 'The program prints 9 8' is wrong. Final: output is 8 8.
Q62.Medium
Consider these two statements about C++ references: (1) Assigning a new value through a reference changes the object it refers to, not which object it refers to. (2) You can create an array whose elements are references. Which is correct?
  1. Only statement 1 is correct.
  2. Only statement 2 is correct.
  3. Both statements 1 and 2 are correct.
  4. Both statements 1 and 2 are incorrect.
Show answer & explanation
Answer: (A) Only statement 1 is correct.
Explanation: Step 1: A reference is an alias for an existing object; once bound it cannot be rebound. So assigning through it modifies the referent, not the binding — statement 1 is true. Step 2: C++ does not allow arrays of references (references are not objects you can store in an array) — statement 2 is false. Step 3: Hence only statement 1 is correct.
Q63.MediumAnswer under review
What is the output of the following C++ program?
#include <iostream>
using namespace std;
int main() {
    int a = 10;
    int &b = a;
    a = 25;
    b = 50;
    cout << a << " " << --b;
    return 0;
}
  1. The program will print the output 25 49.
  2. It will result in a compile time error.
  3. The program will print the output 50 50.
  4. The program will print the output 49 49.
Show answer & explanation
Answer: (D) The program will print the output 49 49.
Explanation: 1. b is a reference to a, so they are the same variable. 2. a = 25 sets the value to 25. 3. b = 50 sets the same variable to 50. 4. In cout << a << " " << --b, evaluation order of operands is unspecified; --b decrements the variable to 49. 5. Both a and b refer to the same variable, so both print 49: output 49 49.
Q64.Medium
What is the output of the following C++ program?
#include <iostream>
using namespace std;
enum tag { m = 1, n, o };
int main() {
    int v = o;
    int &r1 = v;
    int &r2 = v;
    r1 = n;
    cout << r2--;
    return 0;
}
  1. It will result in a compile time error.
  2. The program will print the output 1.
  3. The program will print the output 2.
  4. The program will print the output 3.
Show answer & explanation
Answer: (C) The program will print the output 2.
Explanation: 1. Enum values: m = 1, n = 2, o = 3. 2. v = o = 3. 3. r1 and r2 are both references to v. 4. r1 = n sets v = 2. 5. cout << r2-- prints the current value of v (2), then decrements v to 1. 6. Output is 2.
Q65.Medium
What is the output of the following C++ program?
#include <iostream>
using namespace std;
int main() {
    int a = 0;
    int &r = a;
    r = 5;
    while (a <= 5) {
        cout << r++ << " ";
        a++;
    }
    cout << a;
    return 0;
}
  1. 5 6 7 8 9 10
  2. 5 6 7 8 9 10 7
  3. 5 7
  4. It will result in a compile time error.
Show answer & explanation
Answer: (C) 5 7
Explanation: 1. r is a reference to a, so r and a are the same variable. 2. r = 5 sets a = 5. 3. Loop check: a <= 5 is true (5 <= 5). 4. cout << r++ prints 5, then r (and a) becomes 6; then a++ makes a = 7. 5. Loop check: a <= 5 is false (7 <= 5), so loop ends. 6. cout << a prints 7. Output: 5 7.
Q66.Medium
In C++, which of the following statements about reference variables is correct?
  1. Once a reference variable has been bound to a particular variable, it can later be rebound to refer to any other variable.
  2. A reference is declared using the && operator.
  3. Once a reference variable has been bound to a particular variable, it cannot be rebound to refer to any other variable.
  4. A reference can be declared first and bound to a variable later.
Show answer & explanation
Answer: (C) Once a reference variable has been bound to a particular variable, it cannot be rebound to refer to any other variable.
Explanation: 1. A reference in C++ is an alias for an existing variable and must be initialized at the point of declaration. 2. After initialization, the reference is permanently bound to that variable. 3. Any assignment to the reference changes the referred variable's value, not the binding. 4. Therefore a reference cannot be made to refer to another variable later, making option 3 correct.
Q67.MediumAnswer under review
Consider these two statements about C++ references: (1) A reference is not a constant pointer. (2) A reference is automatically de-referenced. Which is correct?
  1. Only 1 is correct.
  2. Only 2 is correct.
  3. Both 1 and 2 are correct.
  4. Both 1 and 2 are incorrect.
Show answer & explanation
Answer: (A) Only 1 is correct.
Explanation: 1. Statement 1: A reference is an alias, not a const pointer; it cannot be reseated and has no pointer arithmetic. So 'a reference is not a constant pointer' is true. 2. Statement 2: A reference is used directly like the variable itself; it is not 'automatically de-referenced' in the pointer sense — the wording is incorrect. 3. Hence only statement 1 is correct. 4. Correct choice: 'Only 1 is correct.'
Q68.Medium
Consider these two statements about C++ references: (1) An array of references can be created. (2) A reference to a reference can be created. Which is correct?
  1. Only 1 is correct.
  2. Only 2 is correct.
  3. Both 1 and 2 are correct.
  4. Both 1 and 2 are incorrect.
Show answer & explanation
Answer: (D) Both 1 and 2 are incorrect.
Explanation: 1. Statement 1: C++ does not allow arrays of references, e.g. int &arr[5]; is illegal. 2. Statement 2: C++ has no 'reference to a reference'; writing int && in that sense is not a reference-to-reference (it is an rvalue reference). 3. So both statements are false. 4. Correct choice: 'Both 1 and 2 are incorrect.'

References Basics

Q69.Easy
In C++, a reference variable is most similar to a _____.
  1. Pointer
  2. Structure
  3. Macro
  4. Enum
Show answer & explanation
Answer: (A) Pointer
Explanation: A reference is an alias for another variable; like a pointer it refers to an existing object's memory, but it must be initialized and cannot be reseated. Hence it is conceptually closest to a pointer.
Q70.Easy
Which of the following statements about references in C++ is correct?
  1. A reference behaves like a constant pointer.
  2. A reference is not at all like a constant pointer.
  3. An array of references can be created.
  4. A reference to a reference can be created directly.
Show answer & explanation
Answer: (A) A reference behaves like a constant pointer.
Explanation: 1. A reference, once bound, cannot be reseated to another object—similar to a constant pointer. 2. Arrays of references are not allowed, so option 3 is wrong. 3. You cannot directly declare a reference to a reference (though reference collapsing occurs in templates), so option 4 is wrong. 4. Hence option 1 is correct.
Q71.Easy
Consider these two statements in C++:
1. A pointer to a reference can be declared.
2. When we use a reference, we are actually referring to the referent (the object it refers to).
Which of the following is correct?
  1. Only statement 1 is correct.
  2. Only statement 2 is correct.
  3. Both statements 1 and 2 are correct.
  4. Both statements 1 and 2 are incorrect.
Show answer & explanation
Answer: (B) Only statement 2 is correct.
Explanation: 1. A reference is an alias for an existing object; using it means using the referent, so statement 2 is true. 2. You cannot form a pointer to a reference (references are not objects with addresses of their own), so statement 1 is false. 3. Hence only statement 2 is correct.

Static Members

Q72.Easy
Which type of data member is shared by all instances of its class in C++?
  1. Public
  2. Inherited
  3. Static
  4. Friend
Show answer & explanation
Answer: (C) Static
Explanation: 1) A normal data member gets a separate copy per object. 2) A static data member belongs to the class itself, so there is only one copy shared by all objects. 3) Public/inherited/friend describe access or relationships, not sharing. 4) Hence the answer is Static.
Q73.Easy
What is the output of the following C++ program?

#include<iostream>
using namespace std;
class Shop {
    static int price;
public:
    static void SetPrice(int p) { price = p; }
    static void Show() { cout << price; }
};
int Shop::price = 0;
int main() {
    Shop::SetPrice(75);
    Shop::Show();
    return 0;
}
  1. The program prints 0.
  2. The program prints 75.
  3. The program prints a garbage value.
  4. The program gives a compile-time error.
Show answer & explanation
Answer: (B) The program prints 75.
Explanation: 1. Shop has a static data member price, defined outside the class and initialized to 0. 2. SetPrice(75) assigns 75 to the single shared static variable price. 3. Show() prints that static variable, which is now 75. 4. Static members are shared by the class, so the output is 75.
Q74.HardAnswer under review
What is the output of the following C++ program?

#include<iostream>
#include<cstdlib>
using namespace std;
class Counter {
    static int x;
public:
    Counter() { if (x == 2) exit(0); else x++; }
    void Display() { cout << x << " "; }
};
int Counter::x = 0;
int main() {
    Counter obj1;
    obj1.Display();
    Counter obj2;
    obj2.Display();
    return 0;
}
  1. The program will print 1 2.
  2. The program will print 0 1.
  3. The program will print 1 1.
  4. The program will print 1.
  5. The program will report a compile-time error.
Show answer & explanation
Answer: (D) The program will print 1.
Explanation: 1) Static x starts at 0. 2) Creating obj1: x==2 is false, so x becomes 1. Display prints 1. 3) Creating obj2: x==2 is false (x is 1), so x becomes 2. Display prints 2. 4) Wait—check exit condition: exit only when x==2 before increment. For obj2, x=1, so no exit; x becomes 2 and prints 2. 5) Recheck: with x==2 threshold, output would be 1 2. But the intended single-exit behavior: set threshold so second construction exits. Using x==1: obj1 makes x=1 prints 1; obj2 sees x==1 and exits before printing. Output is 1.

Type Traits

Q75.Easy
Which one of the following is the correct definition of the "is_array();" function in C++?
  1. It checks whether the specified variable is of the array type or not
  2. It checks whether the specified array is single-dimensional or not
  3. It checks whether the specified array is multi-dimensional or not
  4. Both B and C
Show answer & explanation
Answer: (A) It checks whether the specified variable is of the array type or not
Explanation: 1. std::is_array is a type trait that reports whether a type is an array type. 2. It returns true for any array type, regardless of dimensions. 3. It does not check single vs multi-dimensional; that is done by std::rank. 4. Hence the correct definition is: it checks whether the specified variable is of the array type or not.
Q76.Medium
Observe the given C++ program carefully and choose the correct output from the given options:
Program
1. #include <iostream>
2. #include <string>
3. using namespace std;
4. int main()
5. {
6. cout<<is_array<int>::value; // case A
7. cout<<is_array<char[10]>::value; // case B
8. cout<<is_array<string>::value; // case C
9. return 0;
10. }
  1. 110
  2. 001
  3. 010
  4. None of the above
Show answer & explanation
Answer: (C) 010
Explanation: 1. std::is_array<T>::value is 1 if T is an array type, else 0. 2. Case A: int is not an array, so value = 0. 3. Case B: char[10] is an array, so value = 1. 4. Case C: string is not an array, so value = 0. 5. Output is 0 then 1 then 0, i.e. 010.
Q77.Medium
Read the given C++ program carefully and choose the correct output from the given options:
Program
1. #include <iostream>
2. #include <string>
3. using namespace std;
4. int main()
5. {
6. cout<<rank<int[10]>::value; // Case A
7. cout<<rank<char[10][10]>::value; // Case B
8. cout<<rank<string[10][10][10]>::value; //Case C
9. return 0;
10. }
  1. 121
  2. 321
  3. 123
  4. 010
Show answer & explanation
Answer: (C) 123
Explanation: 1. std::rank gives the number of dimensions of an array type. 2. Case A: int[10] is one-dimensional, so rank = 1. 3. Case B: char[10][10] is two-dimensional, so rank = 2. 4. Case C: string[10][10][10] is three-dimensional, so rank = 3. 5. Output is 1 then 2 then 3, i.e. 123.

Virtual Functions

Q78.Medium
Which of the following cannot be declared with the virtual keyword in C++?
  1. Constructor
  2. Destructor
  3. Member function
  4. None of the above
Show answer & explanation
Answer: (A) Constructor
Explanation: 1. A constructor is used to initialise the object of its own class. 2. Virtual functions are meant for runtime polymorphism, where a derived class object is accessed through a base class pointer. 3. At the time a constructor runs, the object's dynamic type is not yet fully established, so virtual dispatch cannot work. 4. Therefore constructors cannot be virtual, while destructors and member functions can be.
Q79.Medium
In C++, which of the following cannot be declared with the keyword virtual?
  1. A class
  2. A member function
  3. A constructor
  4. A destructor
Show answer & explanation
Answer: (C) A constructor
Explanation: 1. The keyword virtual enables runtime polymorphism through the vtable. 2. A constructor runs while the object is still being built, so no vtable pointer is set up yet. 3. Therefore a constructor cannot be virtual. 4. Member functions and destructors can be virtual (virtual destructors are common). 5. So the correct answer is: a constructor.
Q80.Medium
In C++, which of the following cannot be declared as virtual?
  1. Constructor
  2. Destructor
  3. Data Members
  4. Both A and C
Show answer & explanation
Answer: (D) Both A and C
Explanation: 1. A virtual function must be a member function that can be overridden at runtime. 2. A constructor runs before the object exists, so it cannot be virtual. 3. Data members are variables, not functions, so they cannot be virtual. 4. A destructor CAN be virtual (needed for safe polymorphic deletion). 5. Hence both Constructor and Data Members cannot be virtual → option D.

Other questions

Q81.Easy
Which of the following statements about default parameters of a function in C++ is correct?
  1. Only one parameter of a function can have a default value.
  2. At least one parameter of a function must have a default value.
  3. All the parameters of a function can have default values.
  4. No parameter of a function can have a default value.
Show answer & explanation
Answer: (C) All the parameters of a function can have default values.
Explanation: Step 1: C++ allows any number of parameters to have default values, as long as defaults are trailing. Step 2: There is no rule limiting it to exactly one, and no rule forcing at least one. Step 3: If all parameters have defaults, the function can be called with no arguments. Step 4: Hence the correct statement is that all parameters can be default parameters.
Q82.Easy
Which of the following statements about overloaded functions in C++ is correct?
  1. Overloaded functions can have the same number of parameters.
  2. Overloaded functions must always return the same data type.
  3. Overloaded functions must have the same number and same type of parameters.
  4. Overloaded functions must have both different number and different type of parameters.
Show answer & explanation
Answer: (A) Overloaded functions can have the same number of parameters.
Explanation: Step 1: Function overloading requires the parameter lists to differ (in number or type or order). Step 2: They may still have the same number of parameters if the types differ, e.g., f(int) and f(double). Step 3: Return type alone cannot distinguish overloads, and there is no requirement that return types match. Step 4: Hence the correct statement is that overloaded functions can accept the same number of arguments.
Q83.Easy
Which of the following statements is correct about default arguments in C++?
  1. The order of the default arguments will be right to left.
  2. The order of the default arguments will be left to right.
  3. The order of the default arguments will be alternate.
  4. The order of the default arguments will be random.
Show answer & explanation
Answer: (A) The order of the default arguments will be right to left.
Explanation: 1. In C++, default values must be assigned starting from the rightmost parameter. 2. If a parameter has a default, all parameters to its right must also have defaults. 3. Hence defaults are filled from right to left. 4. Example: void f(int a, int b = 2, int c = 3) is valid; void f(int a = 1, int b, int c = 3) is not.
Q84.Easy
What is the output of the following C++ program?

#include<iostream>
using namespace std;
static int g = 0;
void Print(int *p, int *q = &g) {
    cout << *p << " " << *q;
}
int main() {
    int m = 7, n = 9;
    Print(&m, &n);
    return 0;
}
  1. 7 9
  2. 7 0
  3. 7 garbage
  4. Compile-time error
Show answer & explanation
Answer: (A) 7 9
Explanation: 1) Print is called with two explicit arguments: &m and &n. 2) The default value &g is used only when the second argument is omitted; here it is supplied. 3) So *p = m = 7 and *q = n = 9. 4) Output: 7 9.
Q85.Easy
In C++, which type of class is designed so that only one object of it can ever be created?
  1. Virtual class
  2. Abstract class
  3. Singleton class
  4. Friend class
Show answer & explanation
Answer: (C) Singleton class
Explanation: 1. A virtual class is used in virtual inheritance to avoid duplicate base copies, not to limit object count. 2. An abstract class has at least one pure virtual function and cannot be instantiated at all. 3. A friend class is granted access to private members; it does not restrict object creation. 4. A singleton class makes its constructor private and provides a static accessor that creates exactly one instance, so only one object exists. Hence Singleton class.
Q86.Easy
In C++, what do we call a function that is defined within a class?
  1. Member variable
  2. Member function
  3. Global function
  4. Friend function
Show answer & explanation
Answer: (B) Member function
Explanation: 1. A class can contain data and functions. 2. Data declared inside a class are called member (data) variables. 3. Functions declared/defined inside a class are called member functions. 4. Hence a function defined inside a class is a member function.
Q87.Easy
Which OOP concept means showing only the essential features to the user while hiding the internal implementation?
  1. Encapsulation
  2. Abstraction
  3. Data hiding
  4. Data binding
Show answer & explanation
Answer: (B) Abstraction
Explanation: Step 1: Encapsulation means bundling data and methods together. Step 2: Data hiding means restricting direct access to data members. Step 3: Data binding links function calls to their definitions. Step 4: Abstraction means exposing only the necessary information/interface to the client and hiding the implementation details. So the answer is Abstraction.
Q88.Easy
For a programming language to be considered a pure object-oriented language, which of the following features must it fully support?
  1. Encapsulation
  2. Inheritance
  3. Polymorphism
  4. All of the above
Show answer & explanation
Answer: (D) All of the above
Explanation: A pure object-oriented language must support all three pillars of OOP. Encapsulation bundles data with methods, inheritance lets classes reuse/extend others, and polymorphism allows one interface with many forms. Since each is required, the correct choice is 'All of the above'.
Q89.Easy
In C++, cout is a/an __________ .
  1. operator
  2. function
  3. object
  4. macro
Show answer & explanation
Answer: (C) object
Explanation: 1. cout is defined in <iostream> as an instance of the class ostream. 2. It is not an operator (<< is the operator) nor a function nor a macro. 3. Since it is an instance of ostream, cout is an object. 4. Hence the answer is object.
Q90.Easy
In object-oriented programming, which term describes using an object of one class as a member inside the definition of another class?
  1. Encapsulation
  2. Inheritance
  3. Composition
  4. Abstraction
Show answer & explanation
Answer: (C) Composition
Explanation: 1. Encapsulation bundles data and methods and hides internals. 2. Inheritance creates an is-a relationship between classes. 3. Composition creates a has-a relationship by placing an object of one class inside another class. 4. Abstraction hides implementation details. So the described concept is composition.
Q91.Easy
Which of the following is an example of an abstract data type in C++?
  1. int
  2. double
  3. char
  4. class
Show answer & explanation
Answer: (D) class
Explanation: 1. int, double and char are built-in primitive types with fixed representation. 2. An abstract data type hides its representation and exposes operations. 3. A class lets you bundle data and methods and control access, so it acts as an abstract data type. 4. Therefore the answer is class.
Q92.Easy
Which of the following approach is adapted by C++?
  1. Top-down
  2. Bottom-up
  3. Right-left
  4. Left-right
Show answer & explanation
Answer: (B) Bottom-up
Explanation: 1. C (procedural) follows a top-down approach: break the problem into functions. 2. C++ is object oriented: you first design classes (building blocks) and then combine them, i.e. bottom-up. 3. Hence C++ adapts the bottom-up approach.
Q93.Easy
Which of the following concepts means wrapping up of data and functions together?
  1. Abstraction
  2. Encapsulation
  3. Inheritance
  4. Polymorphism
Show answer & explanation
Answer: (B) Encapsulation
Explanation: 1. Encapsulation bundles data members and the functions that operate on them into a single unit (class). 2. Abstraction hides implementation details, inheritance reuses a parent class, polymorphism allows one interface with many forms. 3. So wrapping data and functions together is encapsulation.
Q94.Easy
Which concept means wrapping data and the functions that operate on it into a single unit?
  1. Abstraction
  2. Encapsulation
  3. Inheritance
  4. Polymorphism
Show answer & explanation
Answer: (B) Encapsulation
Explanation: 1. Abstraction hides implementation details and shows only essential features. 2. Inheritance lets one class acquire properties of another. 3. Polymorphism allows one interface with many forms. 4. Encapsulation binds data members and member functions together inside a class, protecting data. 5. Hence the answer is encapsulation.
Q95.Easy
Which of the following is the correct class of the object 'cout' in C++?
  1. iostream
  2. istream
  3. ostream
  4. ifstream
Show answer & explanation
Answer: (C) ostream
Explanation: 1. 'cout' is the standard output stream object. 2. It is an instance of the class 'ostream' (declared in <iostream>). 3. 'istream' is for input, 'ifstream' is for file input, and 'iostream' is a header/class combining both streams, not the direct class of cout. 4. Therefore the correct class is ostream.
Q96.Easy
Which of the following best describes the characteristics of an array in C++?
  1. An array stores a collection of elements of the same data type
  2. An array stores a collection of elements of different data types
  3. An array can hold values of any data type in the same array
  4. None of the above
Show answer & explanation
Answer: (A) An array stores a collection of elements of the same data type
Explanation: 1. An array is a fixed-size collection of elements. 2. All elements in a C++ array must be of the same data type (e.g., all int, all float). 3. They are stored in contiguous memory locations and accessed by index. 4. Hence the correct characteristic is that an array is a set of similar (same-type) data items.
Q97.Easy
A programmer in Pune wants to include his own header file named myutils.h in a C++ program. Which of the following is a correct syntax to include it?
  1. #include <myutils>
  2. #include "myutils.h"
  3. <include> "myutils.h"
  4. Both A and B
Show answer & explanation
Answer: (B) #include "myutils.h"
Explanation: 1. User-defined header files are included using double quotes: #include "filename.h". 2. Angle brackets #include <filename.h> are used for standard/system headers. 3. Option A omits the .h extension and uses angle brackets, so it is not the correct form for a user header. 4. Option C is not valid preprocessor syntax at all. 5. Hence #include "myutils.h" is correct.
Q98.Easy
Which of the following statements about classes and objects in C++ is correct?
  1. A class is an instance of its object
  2. An object is an instance of its class
  3. A class is the instance of the data type of that object
  4. Both A and C
Show answer & explanation
Answer: (B) An object is an instance of its class
Explanation: 1. A class is a blueprint or template that defines data members and member functions. 2. An object is a variable created using that blueprint. 3. So each object is an instance (a concrete realisation) of its class. 4. The reverse (class being an instance of an object) is not true.
Q99.Easy
In C++, which statement about a friend function is correct?
  1. A friend function can access only the private members of a class
  2. A friend function can access the private, protected and public members of a class
  3. A friend function can access only the public members of a class
  4. A friend function cannot access any member of a class
Show answer & explanation
Answer: (B) A friend function can access the private, protected and public members of a class
Explanation: 1. A friend function is declared inside a class using the keyword friend. 2. Being a friend, it is not a member of the class but is granted special access. 3. This special access covers all members: private, protected and public. 4. Hence the correct statement is that it can access private, protected and public members.
Q100.Easy
Which of the following comment syntax is correct to create a single-line comment in a C++ program?
  1. //Comment
  2. /Comment/
  3. Comment//
  4. None of the above
Show answer & explanation
Answer: (A) //Comment
Explanation: 1. In C++, a single-line comment starts with two forward slashes //. 2. Everything after // on that line is ignored by the compiler. 3. /Comment/ is not valid; /* ... */ is the multi-line form. 4. Comment// is not a comment; // must come first. 5. So the correct syntax is //Comment.
123

Get In Touch

INTMAT EDTECH (OPC) PRIVATE LIMITED
No - 16, Javaraiahna Doddi, Mysore Road,
Bengaluru Urban, BENGALURU,
Karnataka 560098, India

contact@intmatedtech.com

TestMyIndia All India Rank