21. Which of the following is true about constructors in C++?
- a) They can return values
- b) They can be virtual
- c) They have the same name as the class
- d) They can be inherited
Answer: C - Constructors have the same name as their class and cannot return values or be virtual.
22. What is the output of: cout << (5 >> 1);
Answer: A - The >> operator performs a right shift (binary 101 becomes 10 which is 2 in decimal).
23. Which of these is not a type of inheritance in C++?
- a) Single
- b) Multiple
- c) Multilevel
- d) Circular
Answer: D - Circular inheritance is not a valid type in C++.
24. What is the correct syntax to declare a destructor?
- a) ~ClassName()
- b) ClassName~()
- c) void ~ClassName()
- d) destructor ClassName()
Answer: A - Destructors are declared with a tilde (~) followed by the class name.
25. Which of the following is not a valid C++ type casting operator?
- a) static_cast
- b) dynamic_cast
- c) const_cast
- d) safe_cast
Answer: D - safe_cast is not a standard C++ casting operator.
26. What is the purpose of the 'mutable' keyword in C++?
- a) To make a variable constant
- b) To allow modification of a class member in a const function
- c) To make a variable thread-safe
- d) To prevent inheritance
Answer: B - mutable allows modification of a member variable even in const member functions.
27. Which STL container provides fastest access to elements by index?
- a) list
- b) vector
- c) map
- d) set
Answer: B - vector provides O(1) random access to elements by index.
28. What is the output of: cout << sizeof(void*);
- a) 1
- b) 2
- c) 4
- d) Depends on the system architecture
Answer: D - The size of a pointer depends on the system (4 bytes on 32-bit, 8 bytes on 64-bit).
29. Which of these is not a valid way to initialize an array in C++?
- a) int arr[] = {1, 2, 3};
- b) int arr[3] = {1, 2, 3};
- c) int arr[3]; arr = {1, 2, 3};
- d) int arr[3] = {};
Answer: C - Arrays cannot be assigned with initializer lists after declaration.
30. What is the purpose of the 'volatile' keyword in C++?
- a) To make a variable constant
- b) To indicate a variable may change unexpectedly
- c) To optimize variable access
- d) To make a variable thread-local
Answer: B - volatile tells the compiler that a variable's value may change outside program control.
31. Which of these is not a valid C++ loop structure?
- a) for
- b) while
- c) do-while
- d) until
Answer: D - 'until' is not a valid loop structure in C++.
32. What is the correct way to declare a function that doesn't throw any exceptions?
- a) void func() noexcept;
- b) void func() throw();
- c) void func() noex;
- d) Both a and b
Answer: D - Both noexcept (C++11) and throw() (deprecated) indicate no exceptions.
33. Which of the following is not a valid C++ access specifier?
- a) public
- b) private
- c) protected
- d) internal
Answer: D - 'internal' is not a C++ access specifier (it's from C#).
34. What is the output of: cout << (true ? "Hello" : "World");
- a) Hello
- b) World
- c) Compilation error
- d) Runtime error
Answer: A - The ternary operator evaluates to the first expression since the condition is true.
35. Which STL algorithm is used to sort elements in a container?
- a) std::find
- b) std::sort
- c) std::order
- d) std::arrange
Answer: B - std::sort is used to sort elements in a range.
36. What is the purpose of the 'friend' keyword in C++?
- a) To create a new class
- b) To allow access to private members
- c) To prevent inheritance
- d) To make a function virtual
Answer: B - friend allows external functions or classes to access private members.
37. Which of these is not a valid C++11 feature?
- a) Lambda expressions
- b) Range-based for loops
- c) Smart pointers
- d) Modules
Answer: D - Modules were introduced in C++20, not C++11.
38. What is the correct way to declare a move constructor?
- a) ClassName(ClassName& other)
- b) ClassName(const ClassName& other)
- c) ClassName(ClassName&& other)
- d) ClassName(ClassName other)
Answer: C - Move constructors take rvalue references (&&).
39. Which of these is not a valid smart pointer in C++?
- a) unique_ptr
- b) shared_ptr
- c) weak_ptr
- d) raw_ptr
Answer: D - raw_ptr is not a standard smart pointer type.
40. What is the output of: cout << typeid(5.0f).name();
- a) int
- b) float
- c) double
- d) Implementation defined string
Answer: D - typeid().name() returns an implementation-defined string representation.