C++ MCQ (151-180)

151. What is the default value of a static local variable in C++ if it is not initialized?
a) 0
b) Undefined
c) NULL
d) Garbage value

Answer: a) 0


152. Which of the following will NOT compile in C++?
a) const int a = 5;
b) const int* p = &a;
c) p = 10;
d) int
const p = &a;

Answer: c) *p = 10;


153. Which of the following is the correct way to call a function from a derived class in C++?
a) Call function in base class.
b) Call function using scope resolution.
c) Call function using the object of the derived class.
d) You cannot call a function from a derived class.

Answer: c) Call function using the object of the derived class.


154. What is the output of the following C++ code?

#include<iostream>  
using namespace std;  
int main() {  
    int x = 10;  
    cout << x-- << " " << --x << endl;  
    return 0;  
}  

a) 10 8
b) 10 9
c) 9 8
d) 9 9

Answer: a) 10 8


155. Which of the following is the correct syntax to declare an array of 5 integers in C++?
a) int arr[5];
b) int arr = {5};
c) int[5] arr;
d) array int arr[5];

Answer: a) int arr[5];


156. Which of the following is used to declare a member function of a class as virtual in C++?
a) virtual function_name()
b) function_name virtual()
c) function_name() virtual
d) virtual return_type function_name()

Answer: d) virtual return_type function_name()


157. Which operator is used to access the members of a class through a pointer in C++?
a) .
b) ->
c) *
d) &

Answer: b) ->


158. Which of the following statements is true about C++ destructors?
a) Destructors can take parameters.
b) Destructors are called explicitly.
c) A class can have multiple destructors.
d) Destructors are called automatically when an object goes out of scope.

Answer: d) Destructors are called automatically when an object goes out of scope.


159. What is the output of the following C++ code?

#include<iostream>  
using namespace std;  
int main() {  
    int a = 10, b = 5;  
    cout << (a >= b) << endl;  
    return 0;  
}  

a) 0
b) 1
c) true
d) false

Answer: b) 1


160. Which of the following C++ keywords is used to declare a constant value?
a) const
b) constant
c) static
d) final

Answer: a) const


161. What is the purpose of the ‘public’ access modifier in C++?
a) The members are accessible only within the class.
b) The members are accessible from outside the class.
c) The members are only accessible to derived classes.
d) The members cannot be accessed from outside the class.

Answer: b) The members are accessible from outside the class.


162. What will be the output of the following C++ code?

#include<iostream>  
using namespace std;  
int main() {  
    char c = 'A';  
    cout << ++c << endl;  
    return 0;  
}  

a) A
b) B
c) a
d) Error

Answer: b) B


163. Which of the following C++ operators is used to allocate memory dynamically?
a) malloc
b) calloc
c) new
d) free

Answer: c) new


164. Which of the following statements is false about function overloading in C++?
a) Functions with the same name but different signatures can be overloaded.
b) Overloading is based on the return type of the function.
c) Overloaded functions must have different parameter types or numbers.
d) Function overloading occurs at compile time.

Answer: b) Overloading is based on the return type of the function.


165. Which of the following is used to declare a constant pointer in C++?
a) int* const ptr;
b) const int* ptr;
c) const ptr int*;
d) int const ptr*;

Answer: a) int* const ptr;


166. What is the output of the following C++ code?

#include<iostream>  
using namespace std;  
int main() {  
    int arr[] = {1, 2, 3};  
    cout << arr[1] << endl;  
    return 0;  
}  

a) 1
b) 2
c) 3
d) Error

Answer: b) 2


167. In C++, which of the following is correct for dynamic memory allocation?
a) new allocates memory on the heap, and delete deallocates it.
b) malloc allocates memory on the heap, and free deallocates it.
c) new allocates memory on the stack, and delete deallocates it.
d) malloc allocates memory on the stack, and free deallocates it.

Answer: a) new allocates memory on the heap, and delete deallocates it.


168. Which of the following is the correct syntax for a pointer to a constant integer?
a) int* const ptr;
b) const int* ptr;
c) int const* ptr;
d) b and c

Answer: d) b and c


169. Which of the following is used to allocate memory dynamically for an array of integers in C++?
a) new int[10];
b) new int;
c) malloc(sizeof(int) * 10);
d) a and c

Answer: a) new int[10];


170. Which of the following is used to deallocate memory allocated by the ‘new’ operator in C++?
a) delete
b) delete[]
c) free
d) a and b

Answer: d) a and b


171. Which of the following C++ operators is used to access members of a structure using a pointer?
a) .
b) ->
c) &
d) *

Answer: b) ->


172. What will be the output of the following C++ code?

#include<iostream>  
using namespace std;  
int main() {  
    int x = 4;  
    cout << (x != 5) << endl;  
    return 0;  
}  

a) 0
b) 1
c) true
d) false

Answer: b) 1


173. Which of the following C++ data types can hold a decimal value?
a) int
b) char
c) double
d) bool

Answer: c) double


174. Which of the following is the correct way to declare a constructor in C++?
a) constructor() {}
b) MyClass() {}
c) void MyClass() {}
d) MyClass constructor() {}

Answer: b) MyClass() {}


175. What will be the output of the following code?

#include<iostream>  
using namespace std;  
int main() {  
    int a = 2, b = 3;  
    cout << (a == 2 && b == 3) << endl;  
    return 0;  
}  

a) 0
b) 1
c) true
d) false

Answer: b) 1


176. Which of the following is the correct way to use a reference in C++?
a) int& ref = 10;
b) int ref = &10;
c) int& ref = 10;
d) int* ref = &10;

Answer: a) int& ref = 10;


177. Which of the following is the correct definition for a destructor in C++?
a) ~ClassName() {}
b) ClassName() {}
c) void ClassName() {}
d) void ~ClassName() {}

Answer: a) ~ClassName() {}


178. What does the nullptr keyword represent in C++?
a) A null pointer.
b) A memory address that contains null data.
c) A constant that stores null.
d) A valid pointer value.

Answer: a) A null pointer.


179. What will be the output of the following C++ code?

#include<iostream>  
using namespace std;  
int main() {  
    int arr[3] = {4, 5, 6};  
    cout << *arr + 1 << endl;  
    return 0;  
}  

a) 4
b) 5
c) 6
d) 7

Answer: b) 5


180. Which of the following C++ constructs is used for exception handling?
a) try-catch
b) try-throw
c) catch-throw
d) All of the above

Answer: d) All of the above

Leave a Reply