C++ MCQ ( 61-90)

61. What is the default value of a static variable in C++?
a) Zero
b) Undefined
c) Null
d) Random

Answer: a) Zero


62. Which of the following is the correct way to declare a reference to a pointer in C++?
a) int*& ptr;
b) int* ref;
c) int&* ptr;
d) &int* ptr;

Answer: a) int*& ptr;


63. What is the correct syntax for including a header file in C++?
a) #include <filename>
b) #include “filename”
c) include <filename>
d) include “filename”

Answer: b) #include “filename”


64. Which of the following is true about the ‘static’ keyword when used for a variable in a function?
a) The variable gets initialized every time the function is called.
b) The variable retains its value across function calls.
c) The variable is private to the function.
d) The variable is global to the entire program.

Answer: b) The variable retains its value across function calls.


65. Which of the following data types cannot be used as a base class in C++?
a) int
b) char
c) float
d) void

Answer: d) void


66. What is the output of the following code?

#include<iostream>  
using namespace std;  
int main() {  
    int x = 10;  
    int y = 20;  
    cout << (x < y ? x : y) << endl;  
    return 0;  
}  

a) 10
b) 20
c) x
d) y

Answer: a) 10


67. Which of the following is the correct syntax for declaring a virtual function in C++?
a) virtual return_type function_name();
b) return_type virtual function_name();
c) virtual function_name();
d) function_name() virtual;

Answer: a) virtual return_type function_name();


68. Which of the following functions in C++ is used to get the size of an object in memory?
a) sizeof()
b) memory()
c) length()
d) get_size()

Answer: a) sizeof()


69. Which of the following is the correct way to create an array of pointers in C++?
a) int* arr[];
b) int arr*[];
c) int arr[10];
d) int
arr[10];

Answer: d) int* arr[10];


70. What is the purpose of the ‘explicit’ keyword in C++?
a) It prevents implicit type conversions for constructors and conversion operators.
b) It marks a function as being purely virtual.
c) It specifies that a function will be executed at runtime.
d) It allows type casting in C++.

Answer: a) It prevents implicit type conversions for constructors and conversion operators.


71. What is the result of the following expression in C++?

int a = 5, b = 10;  
cout << (a < b && b > 5) << endl;

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

Answer: b) 1


72. Which of the following is true about the ‘new’ keyword in C++?
a) It can be used to allocate memory on the stack.
b) It is used to allocate memory on the heap.
c) It automatically frees the memory.
d) It allocates memory but doesn’t initialize it.

Answer: b) It is used to allocate memory on the heap.


73. What is the main benefit of using templates in C++?
a) Code reuse for different data types.
b) Provides better performance for built-in data types.
c) Makes the code easier to debug.
d) Increases the execution speed of functions.

Answer: a) Code reuse for different data types.


74. Which of the following statements is used to prevent further code execution in a function in C++?
a) return;
b) break;
c) exit();
d) stop();

Answer: a) return;


75. Which of the following is true about the ‘friend’ class in C++?
a) A friend class can access private members of other classes.
b) A friend class can only access public members.
c) A friend class cannot be inherited.
d) A friend class is a type of derived class.

Answer: a) A friend class can access private members of other classes.


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

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

a) 10
b) 20
c) 30
d) arr[2]

Answer: c) 30


77. Which of the following is the correct way to declare a multi-dimensional array in C++?
a) int arr[5, 5];
b) int arr[5][5];
c) int arr[5,5][5];
d) int arr(5,5);

Answer: b) int arr[5][5];


78. What is the correct way to declare a constant reference in C++?
a) const int& ref;
b) reference const int& ref;
c) int& const ref;
d) const reference int& ref;

Answer: a) const int& ref;


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

#include<iostream>  
using namespace std;  
int main() {  
    int x = 10, y = 20;  
    cout << (x == 10 || y == 10) << endl;  
    return 0;  
}  

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

Answer: b) 1


80. Which of the following is true about constructors in C++?
a) Constructors are called when an object is created.
b) Constructors are used to free memory.
c) Constructors are invoked manually.
d) Constructors can only be declared inside a class.

Answer: a) Constructors are called when an object is created.


81. Which of the following is the correct syntax for defining a function template in C++?
a) template function_name<T>(args)
b) template <typename T> function_name(args)
c) function template<T> name(args)
d) template <T> function_name(args)

Answer: b) template <typename T> function_name(args)


82. Which of the following is true about function overloading in C++?
a) Function overloading is based on the return type.
b) Functions can be overloaded if they have the same parameter types.
c) Function overloading is resolved at compile-time.
d) Function overloading is not allowed in C++.

Answer: c) Function overloading is resolved at compile-time.


83. Which of the following statements is correct about destructors in C++?
a) A destructor cannot be inherited.
b) A destructor can have parameters.
c) A destructor cannot be virtual.
d) A destructor is called explicitly by the programmer.

Answer: a) A destructor cannot be inherited.


84. Which of the following statements is used to declare an array in C++?
a) array<int> arr;
b) int arr[10];
c) int[] arr;
d) arr array<int>[10];

Answer: b) int arr[10];


85. What is the result of the following code?

int x = 10, y = 20; 
x = x + y; 
cout << x;

a) 10
b) 20
c) 30
d) 100

Answer: c) 30


86. Which of the following is a valid loop structure in C++?
a) for loop
b) while loop
c) do-while loop
d) All of the above

Answer: d) All of the above


87. What is the purpose of the ‘continue’ statement in C++?
a) It skips the current iteration and moves to the next iteration in a loop.
b) It terminates the loop and continues to the next statement outside the loop.
c) It exits the program.
d) It causes the loop to run indefinitely.

Answer: a) It skips the current iteration and moves to the next iteration in a loop.


88. Which of the following is true about the ‘delete’ operator in C++?
a) The ‘delete’ operator is used to deallocate memory allocated with ‘malloc’.
b) The ‘delete’ operator is used to deallocate memory allocated with ‘new’.
c) The ‘delete’ operator is used to free stack memory.
d) The ‘delete’ operator is used to delete files.

Answer: b) The ‘delete’ operator is used to deallocate memory allocated with ‘new’.


89. What is the purpose of the ‘default’ keyword in a switch statement in C++?
a) It defines a default function.
b) It is used to provide a default value when no case matches.
c) It forces the switch statement to always execute.
d) It exits the switch statement.

Answer: b) It is used to provide a default value when no case matches.


90. Which of the following is true about C++ strings?
a) C++ strings are immutable.
b) C++ strings are dynamic arrays of characters.
c) C++ strings can only store one character.
d) C++ strings are only used with the standard template library.

Answer: b) C++ strings are dynamic arrays of characters.