1. Which of the following is the correct syntax to include a header file in C++?
- a) #include <header.h>
- b) #include "header.h"
- c) Both a and b
- d) import <header.h>
Answer: C - C++ supports both angle brackets for system headers and quotes for user-defined headers.
2. What is the size of 'int' data type in C++ typically?
- a) 2 bytes
- b) 4 bytes
- c) 8 bytes
- d) Depends on the compiler and system architecture
Answer: D - The size of 'int' is implementation-defined and varies across different systems and compilers.
3. Which operator is used for dynamic memory allocation in C++?
- a) malloc
- b) calloc
- c) new
- d) alloc
Answer: C - 'new' is the operator used for dynamic memory allocation in C++.
4. What is the output of the following code?
int x = 5;
cout << x++ << ++x;
- a) 5 6
- b) 5 7
- c) 6 7
- d) 6 6
Answer: B - x++ is post-increment (prints 5 then increments), ++x is pre-increment (increments then prints 7).
5. Which of the following is not a valid C++ identifier?
- a) _variable
- b) 2ndVariable
- c) variable_name
- d) variableName
Answer: B - Identifiers cannot start with a digit in C++.
6. What is the default access specifier for members of a class in C++?
- a) public
- b) private
- c) protected
- d) friend
Answer: B - Class members are private by default in C++.
7. Which of the following is not a C++ storage class specifier?
- a) auto
- b) register
- c) extern
- d) internal
Answer: D - 'internal' is not a valid storage class specifier in C++.
8. What is the correct way to declare a constant in C++?
- a) constant int x = 5;
- b) #define x 5
- c) const int x = 5;
- d) Both b and c
Answer: D - Both #define and const can be used to declare constants, but const is preferred in C++.
9. Which header file is required for using std::cout?
- a) <iostream>
- b) <ostream>
- c) <console>
- d) <print>
Answer: A - The <iostream> header is required for standard input/output operations.
10. What is the purpose of the 'using namespace std;' statement?
- a) It includes all standard library headers
- b) It makes all std namespace members available without qualification
- c) It optimizes the code for better performance
- d) It initializes the standard input/output streams
Answer: B - It allows using standard library components without the std:: prefix.
11. Which of the following is not a fundamental data type in C++?
- a) int
- b) float
- c) string
- d) char
Answer: C - 'string' is a class in the standard library, not a fundamental type.
12. What is the correct syntax for a function template in C++?
- a) template <class T> T func(T a) {}
- b) template <typename T> T func(T a) {}
- c) Both a and b
- d) template <T> T func(T a) {}
Answer: C - Both 'class' and 'typename' keywords can be used in template declarations.
13. Which of the following statements about references in C++ is false?
- a) References must be initialized when declared
- b) References cannot be NULL
- c) References can be reassigned to refer to different objects
- d) A reference is an alias for an existing variable
Answer: C - References cannot be reassigned after initialization.
14. What is the output of: cout << sizeof('a'); in C++?
- a) 1
- b) 2
- c) 4
- d) Implementation defined
Answer: A - In C++, character literals are of type char, which is typically 1 byte.
15. Which of these is not a valid way to pass arguments to a function?
- a) Pass by value
- b) Pass by reference
- c) Pass by pointer
- d) Pass by variable
Answer: D - "Pass by variable" is not a valid parameter passing mechanism in C++.
16. What is the correct way to declare a pure virtual function in C++?
- a) virtual void func() = 0;
- b) void virtual func() = 0;
- c) void func() = 0;
- d) Both a and b
Answer: D - The 'virtual' keyword can appear before or after the return type.
17. Which of the following is true about function overloading in C++?
- a) Functions must differ in return type
- b) Functions must differ in number or type of parameters
- c) Functions must have different names
- d) Functions must be in different namespaces
Answer: B - Overloaded functions must differ in their parameter lists.
18. What is the purpose of the 'explicit' keyword in C++?
- a) To prevent implicit conversions for constructors
- b) To make a function execute faster
- c) To export a function to other translation units
- d) To declare a function as virtual
Answer: A - 'explicit' prevents implicit conversions for single-argument constructors.
19. Which operator cannot be overloaded in C++?
- a) +
- b) . (dot operator)
- c) <<
- d) []
Answer: B - The dot operator (.) cannot be overloaded in C++.
20. What is the correct way to handle exceptions in C++?
- a) try { } catch { }
- b) try { } catch(...) { }
- c) try { } except { }
- d) try { } handle { }
Answer: B - The correct syntax is try block followed by catch block(s).