- What is the default value of an uninitialized pointer in C++?
a) NULL
b) 0
c) Undefined
d) 1
Answer: a) NULL
- Which of the following will cause a segmentation fault in C++?
a) Accessing an array element out of bounds
b) Dereferencing a NULL pointer
c) Using uninitialized variables
d) All of the above
Answer: d) All of the above
- Which of the following correctly defines a constant in C++?
a) const int x = 5;
b) static int x = 5;
c) final int x = 5;
d) immutable int x = 5;
Answer: a) const int x = 5;
- What does the
sizeof
operator do in C++?
a) Returns the size of a variable in bytes
b) Returns the size of a variable in kilobytes
c) Returns the length of a string
d) Returns the memory address of a variable
Answer: a) Returns the size of a variable in bytes
- Which of the following is true about dynamic memory allocation in C++?
a) Memory allocated dynamically must always be freed manually
b) Dynamic memory allocation happens automatically during runtime
c) Memory allocation is always handled by the operating system
d) Dynamic memory allocation is not possible in C++
Answer: a) Memory allocated dynamically must always be freed manually
- Which of the following is NOT true about a constructor in C++?
a) A constructor must have the same name as the class
b) A constructor can have parameters
c) A constructor cannot return a value
d) A constructor is invoked explicitly by the user
Answer: d) A constructor is invoked explicitly by the user
- What is the purpose of the
new
keyword in C++?
a) To create a new class
b) To allocate memory dynamically
c) To redefine a function
d) To destroy an object
Answer: b) To allocate memory dynamically
- Which operator is used to access members of a class through a pointer to an object?
a) .
b) ->
c) &
d) ::
Answer: b) ->
- Which function is used to open a file in binary mode in C++?
a) open()
b) ifstream()
c) fopen() with "b"
d) read()
Answer: c) fopen() with "b"
- Which of the following is the correct syntax to declare an array of 10 integers in C++?
a) int arr[10];
b) int arr = {10};
c) int[] arr(10);
d) int arr(10);
Answer: a) int arr[10];
- What is the default value of a static variable in C++?
a) Undefined
b) 0
c) NULL
d) Random value
Answer: b) 0
- What is the output of the following code?
int a = 10;
int b = 20;
cout << a++ + ++b;
a) 30
b) 31
c) 29
d) 10
Answer: b) 31
- Which of the following is a characteristic of a virtual function in C++?
a) A virtual function can only be defined inside a class
b) A virtual function can be overridden in derived classes
c) A virtual function cannot be called directly
d) None of the above
Answer: b) A virtual function can be overridden in derived classes
- Which of the following is true about C++ exception handling?
a) It uses the try-catch-finally
block
b) It only allows catching exceptions of type int
c) It does not support multiple exceptions
d) Exceptions are caught using the catch
keyword
Answer: d) Exceptions are caught using the catch
keyword
- Which of the following is the correct way to declare a function pointer in C++?
a) int funcPointer();
b) int (*funcPointer)(int, int);
c) int *funcPointer(int, int);
d) void funcPointer(int, int);
Answer: b) int (*funcPointer)(int, int);
- What is the difference between
++a
and a++
in C++?
a) There is no difference
b) ++a
increments the value before it is used, and a++
increments it after
c) ++a
works only with integers, while a++
works with floats
d) ++a
is invalid syntax
Answer: b) ++a
increments the value before it is used, and a++
increments it after
- Which of the following is used to prevent multiple definitions of the same object in C++ header files?
a) #include
b) #ifndef
c) #pragma once
d) Both b and c
Answer: d) Both b and c
- Which of the following operators can be overloaded in C++?
a) Assignment operator
b) Conditional operator
c) Scope resolution operator
d) Member selection operator
Answer: a) Assignment operator
- Which of the following is true about the
static
keyword in C++?
a) It can be used to declare local variables that retain their value across function calls
b) It prevents inheritance in derived classes
c) It can only be used with classes
d) It makes variables private
Answer: a) It can be used to declare local variables that retain their value across function calls
- Which of the following will cause a runtime error in C++?
a) Accessing an out-of-bounds element in an array
b) Using an uninitialized pointer
c) Dividing by zero
d) All of the above
Answer: d) All of the above
- Which of the following correctly defines a class with a constructor in C++?
a) class MyClass { MyClass() {}; };
b) class MyClass { MyClass(int x) {}; };
c) class MyClass { public: MyClass(); };
d) class MyClass { private: MyClass() {}; };
Answer: b) class MyClass { MyClass(int x) {}; };
- What does the
mutable
keyword do in C++?
a) It allows a member to be modified even if the object is constant
b) It makes a function constant
c) It allows inheritance from multiple classes
d) It makes the object non-copyable
Answer: a) It allows a member to be modified even if the object is constant
- Which of the following is true about the
friend
function in C++?
a) A friend function is a member of a class
b) A friend function can access private and protected members of a class
c) A friend function can be overridden in derived classes
d) A friend function must be declared inside the class
Answer: b) A friend function can access private and protected members of a class
- What does the
std::vector
container do in C++?
a) Stores elements in a linked list
b) Stores elements in a key-value pair format
c) Provides dynamic array functionality
d) Provides constant-time access to elements
Answer: c) Provides dynamic array functionality
- Which of the following is a valid way to return multiple values from a function in C++?
a) Using an array
b) Using a pointer
c) Using a tuple or struct
d) All of the above
Answer: d) All of the above