C++ MCQ (121-150)
121. Which of the following is a valid operator to compare two pointers in C++?
a) ==
b) !=
c) >
d) All of the above
Answer: d) All of the above
122. What is the default access modifier for members of a class in C++?
a) private
b) public
c) protected
d) None of the above
Answer: a) private
123. Which of the following is a correct way to initialize a constant integer in C++?
a) const int x = 10;
b) int const x = 10;
c) const x = 10;
d) a and b
Answer: d) a and b
124. Which of the following will correctly declare a function that accepts a pointer to a constant integer in C++?
a) void func(const int* ptr);
b) void func(int* const ptr);
c) void func(int* ptr);
d) void func(const* int ptr);
Answer: a) void func(const int* ptr);
125. Which of the following is true about function templates in C++?
a) A template function allows functions to operate with generic types.
b) A template function is specifically designed for integers.
c) Template functions can only be used in header files.
d) Template functions only work for built-in data types.
Answer: a) A template function allows functions to operate with generic types.
126. What is the output of the following code?
#include<iostream>
using namespace std;
int main() {
int a = 5, b = 10;
cout << (a < b) ? a : b << endl;
return 0;
}
a) 5
b) 10
c) a
d) b
Answer: b) 10
127. What does the following code do in C++?
int* ptr = new int(10);
a) Allocates memory for an integer and initializes it with 10.
b) Allocates memory for a pointer to an integer.
c) Allocates memory for an integer but doesn’t initialize it.
d) Initializes a pointer to 10.
Answer: a) Allocates memory for an integer and initializes it with 10.
128. Which of the following C++ features allows multiple functions with the same name but different parameter types?
a) Function overriding
b) Function overloading
c) Operator overloading
d) Virtual functions
Answer: b) Function overloading
129. Which of the following keywords is used to define a constant expression in C++?
a) const
b) constant
c) constexpr
d) final
Answer: c) constexpr
130. Which of the following is the correct way to declare a pointer to a constant integer in C++?
a) int const* ptr;
b) const int* ptr;
c) const* int ptr;
d) a and b
Answer: d) a and b
131. What is the output of the following C++ code?
a) 1
b) 2
c) 3
d) Error
Answer: a) 1
132. What is the result of the following expression in C++?
int a = 5, b = 10;
cout << (a == b) << endl;
a) 0
b) 1
c) true
d) false
Answer: a) 0
133. Which of the following is the correct syntax for a constructor in C++?
a) MyClass() { }
b) void MyClass() { }
c) MyClass() : { }
d) MyClass { }
Answer: a) MyClass() { }
134. What is the purpose of the static
keyword when used for a variable inside a function in C++?
a) The variable is initialized every time the function is called.
b) The variable is accessible only within the function.
c) The variable retains its value between function calls.
d) The variable is treated as a global variable.
Answer: c) The variable retains its value between function calls.
135. Which of the following is used to define a virtual function in C++?
a) virtual function_name();
b) function_name virtual();
c) virtual return_type function_name();
d) return_type virtual function_name();
Answer: c) virtual return_type function_name();
136. What is the output of the following code?
#include<iostream>
using namespace std;
int main() {
int x = 10;
cout << x++ << endl;
return 0;
}
a) 10
b) 11
c) x
d) Error
Answer: a) 10
137. What is the use of the new
keyword in C++?
a) It creates a new variable in memory.
b) It allocates memory for an object on the heap.
c) It frees memory that was allocated dynamically.
d) It initializes a variable.
Answer: b) It allocates memory for an object on the heap.
138. Which of the following is the correct way to declare a destructor in C++?
a) MyClass() {}
b) void MyClass() {}
c) MyClass() {}
d) MyClass::() {}
Answer: a) ~MyClass() {}
139. Which of the following is true about the delete
keyword in C++?
a) It is used to free memory allocated using malloc()
.
b) It is used to delete an object created by new
.
c) It frees memory allocated on the stack.
d) It frees memory allocated for global variables.
Answer: b) It is used to delete an object created by new
.
140. Which of the following is the correct way to declare a reference to an integer in C++?
a) int &ref;
b) ref int&;
c) int ref&;
d) &int ref;
Answer: a) int &ref;
141. What does the catch
block in C++ do?
a) It defines the exception.
b) It handles exceptions thrown by a throw
statement.
c) It prevents exceptions from being thrown.
d) It rethrows exceptions.
Answer: b) It handles exceptions thrown by a throw
statement.
142. Which of the following is used for dynamic memory allocation in C++?
a) malloc()
b) calloc()
c) new
d) new[]
Answer: c) new
143. What is the output of the following C++ code?
#include<iostream>
using namespace std;
int main() {
int x = 5;
cout << ++x * 2 << endl;
return 0;
}
a) 10
b) 12
c) 15
d) 7
Answer: b) 12
144. Which of the following C++ features allows multiple objects to share a function definition?
a) Function overloading
b) Function overriding
c) Inheritance
d) Virtual functions
Answer: c) Inheritance
145. What is the default value of an uninitialized static variable in C++?
a) 0
b) Undefined
c) Null
d) Garbage value
Answer: a) 0
146. Which of the following is true about operator overloading in C++?
a) It is used to change the behavior of operators.
b) It is only available for built-in data types.
c) It allows operators to be used only for primitive types.
d) It is not allowed in C++.
Answer: a) It is used to change the behavior of operators.
147. What is the purpose of the try
block in C++?
a) It is used to handle exceptions.
b) It is used to define a block of code to be tested for errors.
c) It is used to declare a pointer to an exception.
d) It is used to prevent exceptions from being thrown.
Answer: b) It is used to define a block of code to be tested for errors.
148. What will be the output of the following code?
#include<iostream>
using namespace std;
int main() {
int arr[] = {1, 2, 3};
cout << *(arr + 2) << endl;
return 0;
}
a) 1
b) 2
c) 3
d) arr[2]
Answer: c) 3
149. What does the sizeof
operator do in C++?
a) It returns the size of the variable in bytes.
b) It allocates memory for the variable.
c) It checks if a variable is null.
d) It frees the memory occupied by the variable.
Answer: a) It returns the size of the variable in bytes.
150. What is the output of the following C++ code?
#include<iostream>
using namespace std;
int main() {
int a = 3, b = 5;
cout << a + b << endl;
return 0;
}
a) 3
b) 5
c) 8
d) 15
Answer: c) 8