- Which of the following best describes polymorphism in C++?
a) Multiple functions with the same name but different parameters
b) The ability of an object to take many forms
c) Inheritance from multiple classes
d) None of the above
Answer: b) The ability of an object to take many forms
- Which keyword is used to prevent a function from being overridden in derived classes?
a) private
b) static
c) final
d) virtual
Answer: c) final
- Which operator is used to allocate memory dynamically in C++?
a) malloc
b) allocate
c) new
d) mem_alloc
Answer: c) new
- Which of the following correctly declares a pointer in C++?
a) int ptr;
b) int *ptr;
c) pointer ptr;
d) ptr int;
Answer: b) int *ptr;
- What will happen if
delete
is used on a NULL pointer?
a) Crash
b) Compilation error
c) No effect
d) Undefined behavior
Answer: c) No effect
- Which of the following is true about constructors in C++?
a) They must have the same name as the class
b) They can return values
c) They are called explicitly
d) None of the above
Answer: a) They must have the same name as the class
- Which access specifier allows members to be inherited but not accessed outside the class hierarchy?
a) public
b) private
c) protected
d) final
Answer: c) protected
- What is the default access specifier for members of a struct in C++?
a) private
b) public
c) protected
d) internal
Answer: b) public
- Which of the following is NOT a valid C++ data type?
a) bool
b) float
c) real
d) double
Answer: c) real
- What is the purpose of the
friend
keyword in C++?
a) To allow one class to access private members of another class
b) To create a pointer to an object
c) To prevent inheritance
d) To create a friend list
Answer: a) To allow one class to access private members of another class
- Which keyword is used to define a class template in C++?
a) class
b) template
c) typename
d) both b and c
Answer: d) both b and c
- Which of the following can be overloaded in C++?
a) Operators
b) Constructors
c) Functions
d) All of the above
Answer: d) All of the above
- What is the output of
5 % 2
in C++?
a) 0
b) 1
c) 2
d) 5
Answer: b) 1
- Which operator is used to access members of a structure using a pointer?
a) . (dot operator)
b) -> (arrow operator)
c) :: (scope resolution)
d) # (hash)
Answer: b) -> (arrow operator)
- Which of the following is NOT a valid loop control statement in C++?
a) break
b) continue
c) exit
d) skip
Answer: d) skip
- What will happen if a C++ program tries to divide by zero in integer division?
a) Runtime error
b) Compilation error
c) Undefined behavior
d) 0
Answer: c) Undefined behavior
- Which header file is required to use the
string
class in C++?
a) iostream
b) string.h
c) string
d) stdio.h
Answer: c) string
- Which of the following statements about destructors is true?
a) They can be overloaded
b) They are called when an object is destroyed
c) They must return an int
d) They take parameters
Answer: b) They are called when an object is destroyed
- What does
cin.ignore()
do in C++?
a) Ignores a specified number of characters from the input buffer
b) Ignores the entire input
c) Reads a line of input
d) Clears the console
Answer: a) Ignores a specified number of characters from the input buffer
- Which STL container stores elements in a key-value pair format?
a) vector
b) list
c) map
d) deque
Answer: c) map
- Which function is used to find the length of a string in C++?
a) strlen()
b) length()
c) size()
d) Both b and c
Answer: d) Both b and c
- What is the purpose of
virtual
functions in C++?
a) To make a function accessible outside the class
b) To allow function overriding in derived classes
c) To optimize memory usage
d) To prevent inheritance
Answer: b) To allow function overriding in derived classes
- What is the default access level for members of a class in C++?
a) private
b) public
c) protected
d) None of the above
Answer: a) private
- Which C++ feature allows multiple functions to have the same name but different parameters?
a) Function overloading
b) Function overriding
c) Encapsulation
d) Abstraction
Answer: a) Function overloading
- Which of the following statements about
static
members in C++ is true?
a) They are shared among all objects of a class
b) They must be accessed using an object
c) They are destroyed when an object goes out of scope
d) None of the above
Answer: a) They are shared among all objects of a class