- What is the correct syntax to include a header file in C++?
- A)
include <iostream>
- B)
#include <iostream>
- C)
#include iostream
- D)
import <iostream>
- Which of the following is a valid comment in C++?
- A)
/* This is a comment */
- B)
// This is a comment
- C)
# This is a comment
- D) Both A and B
- Which keyword is used to declare a class in C++?
- A)
struct
- B)
class
- C)
object
- D)
type
- What will be the output of the following code?
std::cout << 10 + 20 + " C++";
- A)
30 C++
- B)
C++
- C)
error
- D)
10 C++
- How do you declare a constant variable in C++?
- A)
const int x = 10;
- B)
constant int x = 10;
- C)
int const x = 10;
- D) Both A and C
- What does
std::endl do in C++?
- A) Inserts a space
- B) Ends a line and flushes the output buffer
- C) Comments the line
- D) Terminates the program
- In C++, what is the default access specifier for class members?
- A)
public
- B)
private
- C)
protected
- D)
friend
- Which operator is used for pointer dereferencing in C++?
- Which of the following is true about constructors in C++?
- A) They return values.
- B) They are called automatically when an object is created.
- C) They can be explicitly called.
- D) They can have return types.
- How do you define a function in C++?
- A)
function int add(int a, int b) {}
- B)
def int add(int a, int b) {}
- C)
int add(int a, int b) {}
- D)
int add(int a, b): {}
- Which of the following methods can be used for memory allocation in C++?
- A)
malloc
- B)
calloc
- C)
new
- D) All of the above
- How is inheritance implemented in C++?
- A) By using the
extends keyword
- B) Through interface implementation
- C) By using the
: symbol
- D) None of the above
- What is the purpose of the
virtual keyword in C++?
- A) To create virtual memory
- B) To enable dynamic binding
- C) To declare static members
- D) To prevent overriding
- Which of the following correctly creates a derived class in C++?
- A)
class Derived : public Base {}
- B)
class Derived extends Base {}
- C)
class Derived inherits Base {}
- D)
class Derived(Base) {}
- What will be the result of the following code snippet?
int a = 5;
int b = 10;
int c = a < b ? a : b;
- A)
5
- B)
10
- C)
error
- D)
false
- Which of the following is a standard library of C++?
- A)
iostream
- B)
cstdio
- C)
vector
- D) All of the above
- How would you declare a function that takes a reference as an argument?
- A)
void func(int &x);
- B)
void func(int x&);
- C)
void func(int *x);
- D)
void func(int x);
- What is an example of a C++ keyword used for exception handling?
- A)
catch
- B)
throw
- C)
try
- D) All of the above
- What is the output of
std::cout << (5 + 3) * 2;?
- Which of the following constructs are present in C++ for loop control?
- A)
for
- B)
while
- C)
do-while
- D) All of the above
- What’s the difference between
== and = in C++?
- A)
== is for assignment, = is for comparison.
- B)
== is for comparison, = is for assignment.
- C) They are the same.
- D)
== is a logical operator, = is a bitwise operator.
- How do you declare a multi-dimensional array in C++?
- A)
int arr[3][4];
- B)
int[3][4] arr;
- C)
int arr[3, 4];
- D)
int arr(3, 4);
- What does the
new operator do in C++?
- A) It creates a new function.
- B) It allocates memory dynamically.
- C) It creates a new class.
- D) It initializes static variables.
- Which of the following is NOT a valid identifier in C++?
- A)
var_1
- B)
1var
- C)
_var
- D)
var
- What does the following C++ code snippet do?
std::string str = "Hello";
str += " World!";
- A) It throws an error.
- B) It concatenates “Hello” and ” World!”.
- C) It initializes an integer.
- D) It declares a constant string.
- Which keyword is used to exit a function immediately in C++?
- A)
stop
- B)
exit
- C)
return
- D)
break
- Which header file is required to use
std::vector?
- A)
<vector.h>
- B)
<stdio.h>
- C)
<vector>
- D) None of the above
- What is the purpose of the
#define directive in C++?
- A) To include header files
- B) To define constants or macros
- C) To declare functions
- D) To create classes
- What is the result of the expression
sizeof(int)?
- A) The size of an integer variable in bytes.
- B) The total number of integers.
- C) An error.
- D) Undefined behavior.
- What does polymorphism refer to in C++?
- A) Overloading operators
- B) Multiple forms of a function
- C) Inheriting from multiple classes
- D) None of the above
- Which one among the following can lead to memory leaks in C++?
- A) Not using
delete on dynamically allocated memory.
- B) Using
const for pointers.
- C) Incorrect initializations.
- D) Using stack variables.
- What is the typical return type of the
main function in a C++ program?
- A)
int
- B)
void
- C)
float
- D)
char
- Which C++ feature allows one function to be used with different types?
- A) Function overloading
- B) Operator overloading
- C) Inheritance
- D) Encapsulation
- What does STL stand for in C++?
- A) Standard Template Library
- B) Standard Type Library
- C) System Template Library
- D) Simple Template Library
- Which of the following correctly initializes a string in C++?
- A)
string str = "Hello";
- B)
std::string str("Hello");
- C) Both A and B
- D)
char str[] = "Hello";
- What is
nullptr in C++?
- A) A NULL pointer constant
- B) Represents no value
- C) A keyword for creating objects
- D) A function of the pointer type
- Which operator is used for typecasting in C++?
- A)
#
- B)
::
- C)
as
- D)
dynamic_cast
- What is the output of the following code?
int x = 10;
if (x == 10) {
std::cout << "Ten";
}
- A)
10
- B)
Ten
- C)
error
- D) Nothing
- What does the
static keyword signify in C++?
- A) A temporary variable
- B) A global variable
- C) A variable that maintains its value across function calls
- D) A constant variable
- How do we pass an array to a function in C++?
- A) By passing its first element
- B) By using
& operator
- C) By sending it always by value
- D) Directly, as a parameter
- Which of these is a valid destructor in C++?
- A)
~ClassName()
- B)
ClassName::~
- C)
destructor ClassName {}
- D)
ClassName()~
- What will be the result of:
int a = 2;
std::cout << a++;
- A)
2
- B)
3
- C)
error
- D) The program crashes
- What is the purpose of the
friend keyword?
- A) To declare a friendship between classes
- B) To protect class members
- C) To create constant variables
- D) None of the above
- Which of the following operators can be overloaded in C++?
- A)
+
- B)
-
- C)
*
- D) All of the above
- Which of these types of constructor is available in C++?
- A) Default constructor
- B) Copy constructor
- C) Parameterized constructor
- D) All of the above
- Which exception type is thrown if there is division by zero in C++?
- A)
std::invalid_argument
- B)
std::runtime_error
- C)
std::out_of_range
- D) None of the above
- What does the
sizeof() function return?
- A) Memory space allocated to all variables
- B) Total memory in use
- C) The size of the data type or object in bytes
- D) Total size of the program
- Which keyword is used for creating an instance of a class?
- A)
new
- B)
class
- C)
make
- D)
instance
- What is RAII in C++?
- A) Resource Acquisition Is Initialization
- B) Random Access Is Important
- C) Reference Assignment Is Initiated
- D) None of the above
- How can you declare an abstract class in C++?
- A) By using
abstract
- B) By having at least one pure virtual function
- C) By using
virtual without defining any method
- D) By inheriting from another abstract class.