1. What is the primary purpose of the iostream header file in C++?
Step-by-Step Explanation
iostream provides objects like cin and cout for standard input and output.
Answer: a) Input/output stream operations. ✅
2. Which keyword is used to declare a class in C++?
Step-by-Step Explanation
The class keyword is used to define a user-defined data type that can contain data members and member functions.
Answer: c) class ✅
3. What is the purpose of the :: operator in C++?
Step-by-Step Explanation
The :: operator is used to access members of a class or namespace when they are hidden by local variables or other scopes.
Answer: b) Scope resolution operator. ✅
4. Which keyword is used to define a constant variable in C++?
Step-by-Step Explanation
The const keyword makes a variable read-only.
Answer: d) const ✅
5. What is the purpose of a constructor in a C++ class?
Step-by-Step Explanation
A constructor is a special member function that is automatically called when an object is created.
Answer: a) To initialize the object's data members. ✅
6. Which keyword is used to inherit a class in C++?
Step-by-Step Explanation
The : symbol followed by the access specifier and base class name is used for inheritance.
Answer: d) : ✅
7. What does the new operator do in C++?
Step-by-Step Explanation
The new operator dynamically allocates memory for an object and returns a pointer to it.
Answer: d) Allocates memory for an object. ✅
8. Which access specifier makes a class member accessible only within the class and its friend functions?
Step-by-Step Explanation
private members are accessible only within the class and its friend functions.
Answer: c) private ✅
9. What is the purpose of a destructor in a C++ class?
Step-by-Step Explanation
A destructor is a special member function that is automatically called when an object is destroyed.
Answer: d) To deallocate memory for the object. ✅
10. Which operator is used to access members of a class through a pointer?
Step-by-Step Explanation
The -> operator is used to access members of a class through a pointer to an object.
Answer: d) -> ✅
11. What is the purpose of the virtual keyword in C++?
Step-by-Step Explanation
The virtual keyword enables dynamic binding or runtime polymorphism by allowing derived classes to override base class functions.
Answer: b) To enable runtime polymorphism. ✅
12. What is the purpose of function overloading in C++?
Step-by-Step Explanation
Function overloading allows multiple functions with the same name to be defined, as long as they have different parameter lists.
Answer: b) To define multiple functions with the same name but different parameters. ✅
13. What is the purpose of a friend function in C++?
Step-by-Step Explanation
A friend function is a non-member function that can access the private and protected members of a class.
Answer: a) To access private members of a class. ✅
14. Which exception handling keywords are used in C++?
Step-by-Step Explanation
try encloses code that might throw an exception, catch handles the exception, and throw throws an exception.
Answer: d) try, catch, throw ✅
15. What is the purpose of templates in C++?
Step-by-Step Explanation
Templates allow you to write generic code that can work with different data types.
Answer: b) To create generic functions and classes. ✅
16. What is the purpose of the static keyword when used with a class member?
Step-by-Step Explanation
A static member belongs to the class itself, not to any particular object of the class.
Answer: a) To make the member belong to the class rather than the object. ✅
17. What is the purpose of namespaces in C++?
Step-by-Step Explanation
Namespaces provide a way to group related entities (classes, functions, variables) to avoid naming conflicts.
Answer: c) To avoid name collisions. ✅
18. Which operator is used to dereference a pointer in C++?
Step-by-Step Explanation
The * operator is used to dereference a pointer, accessing the value it points to.
Answer: b) * ✅
19. What is the purpose of the inline keyword in C++?
Step-by-Step Explanation
The inline keyword suggests to the compiler that it should replace function calls with the actual code of the function, potentially improving performance.
Answer: c) To suggest the compiler to replace function calls with function bodies. ✅
20. What is the purpose of the auto keyword in C++11 and later?
Step-by-Step Explanation
The auto keyword allows the compiler to deduce the data type of a variable from its initializer.
Answer: b) To deduce the type of a variable from its initializer. ✅