C++ MCQ ( 31-60)
31. Which of the following is the correct syntax for defining a member function outside the class definition?
a) return_type class_name::function_name() { }
b) class_name::function_name() { return_type; }
c) function_name() { class_name; return_type; }
d) return_type::class_name function_name() { }
Answer: a) return_type class_name::function_name() { }
32. Which of the following is the correct way to initialize a constant variable in C++?
a) const int a;
b) const int a = 10;
c) int const a;
d) constant int a = 10;
Answer: b) const int a = 10;
33. What is the purpose of the ‘virtual’ keyword in C++?
a) It allows dynamic memory allocation.
b) It allows dynamic polymorphism.
c) It defines a pointer to an object.
d) It helps in error handling.
Answer: b) It allows dynamic polymorphism.
34. Which of the following is true about destructors in C++?
a) Destructors cannot be overloaded.
b) A destructor is called explicitly.
c) Destructors are called at the start of the program.
d) A destructor can have a return type.
Answer: a) Destructors cannot be overloaded.
35. In C++, which function is used to allocate memory dynamically for an array?
a) malloc()
b) calloc()
c) new[]
d) new
Answer: c) new[]
36. Which of the following is the correct syntax for declaring a function pointer in C++?
a) int *ptr();
b) int (*ptr)();
c) int ptr();
d) ptr() int;
Answer: b) int (*ptr)();
37. What is the output of the following code?
#include<iostream>
using namespace std;
int main() {
int a = 5, b = 6;
cout << a + b << endl;
return 0;
}
a) 11
b) 56
c) 5
d) 6
Answer: a) 11
38. Which of the following is correct about a pure virtual function in C++?
a) It must have a body.
b) It cannot be called directly.
c) It is a function that has no return type.
d) It can be defined inside a class.
Answer: b) It cannot be called directly.
39. Which of the following statements is correct about function overloading in C++?
a) Overloaded functions must have different function names.
b) Function overloading is resolved at runtime.
c) Function overloading allows functions to have the same name with different parameters.
d) Function overloading is not supported in C++.
Answer: c) Function overloading allows functions to have the same name with different parameters.
40. Which of the following is a feature of C++ that enables the creation of new data types by combining existing ones?
a) Inheritance
b) Polymorphism
c) Encapsulation
d) Type abstraction
Answer: d) Type abstraction
41. Which of the following is the correct syntax for declaring a class object in C++?
a) class object_name;
b) object_name class;
c) class object_name();
d) object_name class_name;
Answer: d) object_name class_name;
42. In C++, what does the ‘friend’ keyword mean?
a) It defines a special function in the class.
b) It allows non-member functions or classes to access private members of a class.
c) It is used to declare a class as a friend.
d) It defines a relationship between two classes.
Answer: b) It allows non-member functions or classes to access private members of a class.
43. What is the purpose of the ‘static’ keyword in C++?
a) It defines a function that is local to the current function.
b) It makes a variable or function persist for the lifetime of the program.
c) It is used to define the global variables.
d) It allows the variable to be shared between objects of a class.
Answer: b) It makes a variable or function persist for the lifetime of the program.
44. What is the output of the following code?
#include<iostream>
using namespace std;
int main() {
int a = 3, b = 5;
cout << a++ + ++b << endl;
return 0;
}
a) 8
b) 9
c) 7
d) 6
Answer: b) 9
45. Which of the following is true about constructors in C++?
a) Constructors can be explicitly called.
b) A class can have multiple constructors with different signatures.
c) Constructors can have a return type.
d) Constructors are inherited by derived classes.
Answer: b) A class can have multiple constructors with different signatures.
46. Which of the following is the correct syntax for defining a namespace in C++?
a) namespace myNamespace {};
b) namespace myNamespace;
c) namespace {myNamespace};
d) namespace = myNamespace {};
Answer: a) namespace myNamespace {};
47. What is the output of the following code?
#include<iostream>
using namespace std;
int main() {
int x = 10;
cout << ++x * 5 << endl;
return 0;
}
a) 50
b) 55
c) 10
d) 15
Answer: b) 55
48. Which of the following statements is used to exit a program in C++?
a) exit()
b) terminate()
c) stop()
d) break()
Answer: a) exit()
49. Which of the following is true about inline functions in C++?
a) Inline functions cannot be defined inside a class.
b) Inline functions are defined outside the class.
c) Inline functions are typically used for small functions to reduce the overhead of function calls.
d) Inline functions can only be used for constructors.
Answer: c) Inline functions are typically used for small functions to reduce the overhead of function calls.
50. In C++, which of the following operators is used to access the members of a class through a pointer?
a) . (dot)
b) -> (arrow)
c) :: (scope resolution)
d) & (address-of)
Answer: b) -> (arrow)
51. What is the output of the following code?
#include<iostream>
using namespace std;
int main() {
int a = 3, b = 4;
cout << a * b << endl;
return 0;
}
a) 12
b) 7
c) 34
d) 4
Answer: a) 12
52. Which of the following is the correct way to initialize an array in C++?
a) int arr[5] = {1, 2, 3, 4, 5};
b) int arr[5] = (1, 2, 3, 4, 5);
c) int arr[5] = 1, 2, 3, 4, 5;
d) int arr[] = {1, 2, 3, 4, 5};
Answer: a) int arr[5] = {1, 2, 3, 4, 5};
53. What is the output of the following code?
#include<iostream>
using namespace std;
int main() {
for(int i = 0; i < 3; i++) {
if(i == 1) break;
cout << i << " ";
}
return 0;
}
a) 0 1
b) 0 2
c) 1 2
d) 0
Answer: a) 0 1
54. Which of the following statements is used to define a constant pointer in C++?
a) const int* ptr;
b) int* const ptr;
c) int const* ptr;
d) ptr const int*;
Answer: b) int* const ptr;
55. Which of the following is true about the ‘new’ and ‘delete’ operators in C++?
a) ‘new’ is used to allocate memory for arrays, and ‘delete’ deallocates single objects.
b) ‘new’ is used for dynamically allocating memory, and ‘delete’ is used for deallocating it.
c) ‘new’ is used for deallocating memory, and ‘delete’ allocates memory.
d) ‘new’ and ‘delete’ are not supported in C++.
Answer: b) ‘new’ is used for dynamically allocating memory, and ‘delete’ is used for deallocating it.
56. What is the output of the following code?
#include<iostream>
using namespace std;
int main() {
int arr[3] = {1, 2, 3};
cout << arr[2] << endl;
return 0;
}
a) 1
b) 2
c) 3
d) arr[2]
Answer: c) 3
57. Which of the following is used to handle memory allocation failures in C++?
a) try-catch block
b) new exception
c) malloc()
d) nothrow
Answer: a) try-catch block
58. Which of the following best describes the scope of a variable in C++?
a) The scope is the part of the program where the variable is accessible.
b) The scope is limited to the block in which the variable is declared.
c) The scope is global by default.
d) A variable’s scope is always local to the function.
Answer: a) The scope is the part of the program where the variable is accessible.
59. Which of the following is true about function pointers in C++?
a) Function pointers can be used to call functions indirectly.
b) Function pointers cannot point to member functions.
c) Function pointers are used to define class constructors.
d) Function pointers must be declared in the global scope.
Answer: a) Function pointers can be used to call functions indirectly.
60. Which of the following will correctly declare a reference to an integer in C++?
a) int& ref;
b) ref& int;
c) reference int& ref;
d) int ref&;
Answer: a) int& ref;