C++ is a versatile and widely used programming language. Whether you’re a beginner just starting with C++ or looking for a refresher, this quiz will test your basic understanding of the language.
Quiz Outline:
- Basics & Syntax
- Data Types & Variables
- Control Structures
- Functions
- Arrays & Strings
- Pointers & References
- OOP Concepts (Classes, Inheritance, Polymorphism)
- STL (Standard Template Library)
- Memory Management
- More
1. Which symbol marks the beginning and end of a C++ statement?
Answer:
Explanation:
In C++, statements are terminated using a semicolon.
2. Which of the following is a valid variable name in C++?
Answer:
Explanation:
Variables in C++ can start with an underscore or a letter, but not with a number or special character.
3. Which of the following is the correct way to add comments in C++?
Answer:
Explanation:
// is used for single-line comments, while /* */ is used for multi-line comments.
4. Which header file is required for input and output operations?
Answer:
Explanation:
The
5. Which of the following is not a C++ data type?
Answer:
Explanation:
The correct data type for boolean values in C++ is bool, not boolean.
6. What is the size (in bytes) of an int data type on most 32-bit systems?
Answer:
Explanation:
On most 32-bit systems, an int is 4 bytes.
7. What data type would you use to store a single character?
Answer:
Explanation:
The char data type is used to store a single character.
8. Which keyword is used to declare a constant?
Answer:
Explanation:
In C++, the const keyword is used to declare a constant value.
9. What will be the output of cout << 5/2;?
Answer:
Explanation:
Both 5 and 2 are integers, so the division will be integer division, which discards the remainder.
10. Which of the following can be a valid value for a bool data type?
Answer:
Explanation:
In C++, bool can take values true or false, which are equivalent to 1 and 0, respectively.
11. Which of the following control structures allows the execution of a block of statements multiple times based on a condition?
Answer:
Explanation:
Loops, like for, while, and do-while, allow statements to be executed multiple times based on a condition.
12. Which keyword is used to exit a loop prematurely?
Answer:
Explanation:
The break statement is used to exit a loop before its normal completion.
13. Which of the following is NOT a loop in C++?
Answer:
Explanation:
C++ is used for for traditional looping, and while and do-while for conditional looping. There isn’t a foreach loop in standard C++, but there’s a range-based for loop.
14. Which keyword is used to define a function in C++?
Answer:
Explanation:
In C++, functions are defined by specifying the return type followed by the function name and parameters. No specific keyword like def or fun is required.
15. If a function doesn’t return any value, what should be its return type?
Answer:
Explanation:
Functions that don’t return a value have a return type of void.
16. Which of the following is NOT a valid function declaration?
Answer:
Explanation:
The return type is missing in option d, making it an invalid function declaration.
17. What is the first index of an array in C++?
Answer:
Explanation:
In C++, arrays are zero-indexed, meaning the first element has an index of 0.
18. How do you find the length of a string named str?
Answer:
Explanation:
The length() function is a member function of the string class in C++.
19. Which of the following is the correct way to declare an array of 5 integers?
Answer:
Explanation:
Arrays in C++ are declared with the type, followed by the name and size in square brackets.
20. If string str = “Hello”;, what is str[1]?
Answer:
Explanation:
Strings in C++ are zero-indexed, so str[1] would return the second character, which is ‘e’.
21. What is encapsulation in C++?
Answer:
Explanation:
Encapsulation is a fundamental concept in object-oriented programming that binds together the data and functions that manipulate the data.
22. Which keyword is used to create an object of a class in C++?
Answer:
Explanation:
The new keyword is used to dynamically create an instance (object) of a class.
23. Which of the following is a member function?
Answer:
Explanation:
A member function is a function that is declared inside a class and acts upon the data members of that class.
24. What is the main role of a constructor in a class?
Answer:
Explanation:
Constructors are special class functions that are executed whenever an object of the class is created. They help in initializing the object’s data.
25. Which of the following is true about destructors?
Answer:
Explanation:
A destructor is a special member function of a class that is executed whenever an object of its class goes out of scope or is explicitly deleted. A destructor will have the exact same name as the class prefixed with a tilde (~) and it can neither return a value nor can it take any parameters.
26. Which of the following is used for dynamic memory allocation?
Answer:
Explanation:
In C++, a new operator is used for dynamic memory allocation which initializes the memory and returns its address.
27. Which of the following is used to free the memory allocated by the ‘new’ operator?
Answer:
Explanation:
The delete operator is used to free the memory that was previously allocated by the new operator.
28. What is the process by which a function can be made to behave differently based on its arguments?
Answer:
Explanation:
Polymorphism is a characteristic of being able to assign a different meaning or usage to something in different contexts – specifically, to allow an entity such as a variable, a function, or an object to have more than one form.
29. Function overriding is an example of:
Answer:
Explanation:
Function overriding is a concept where two or more functions have the same name, return type, and parameters. The function that is to be called is determined at runtime, hence it is considered run-time polymorphism.
30. Which of the following concepts allows you to reuse code?
Answer:
Explanation:
Inheritance is a mechanism wherein a new class is derived from an existing class. This allows the derived class to inherit features from the base class, allowing for code reuse.
31. What function is used to open a file in C++?
Answer:
Explanation:
In C++, the open() function is used to open a file.
32. Which of the following modes is used to open a file in read-only mode in C++?
Answer:
Explanation:
The ‘r’ mode is used to open a file for reading.
33. What does a pointer variable store?
Answer:
Explanation:
A pointer variable in C++ stores the memory address of another variable.
34. What symbol is used to declare a pointer variable?
Answer:
Explanation:
The * symbol is used to declare a pointer variable.
35. What is the main use of templates in C++?
Answer:
Explanation:
Templates provide a way to create generic classes and functions, thereby providing flexibility.
36. Which loop is best suited for going through an array?
Answer:
Explanation:
A for loop is typically best suited for iterating through an array because of its structure, with initialization, condition, and increment/decrement in one line.
37. Which of the following loops is executed at least once?
Answer:
Explanation:
The do-while loop is executed at least once because the condition is checked after the loop body is executed.
38. What keyword is used for exception handling?
Answer:
Explanation:
In C++, the throw keyword is used to signal that an exception has occurred.
39. Which of the following is not a type of exception in C++?
Answer:
Explanation:
There isn’t an exception named input_error in the standard C++ library.
40. Which keyword is used to catch exceptions in C++?
Answer:
Explanation:
In C++, the catch block is used to handle the exceptions. It follows the try block which contains the code that might throw exceptions.
41. Which of the following keywords is used in C++ to allocate memory dynamically for a single value?
Answer:
Explanation:
In C++, the new operator is used to allocate memory dynamically on the heap for a single value or an array.
42. To deallocate memory allocated using the new operator, which operator is used?
Answer:
Explanation:
The delete operator is used to free up the memory that was previously allocated using the new operator.
43. What is the process of defining multiple functions with the same name but different parameters?
Answer:
Explanation:
Function overloading allows multiple functions to have the same name but different parameters, possibly with different types.
44. Which of the following is a container in the Standard Template Library (STL)?
Answer:
Explanation:
A vector is a dynamic array provided by the C++ Standard Template Library.
45. What is the primary use of the algorithm header in the STL?
Answer:
Explanation:
The algorithm header provides a set of function templates for common algorithms like sorting, searching, etc.
46. What is the full form of OOP?
Answer:
Explanation:
OOP stands for Object Oriented Programming, a paradigm that uses “objects” – data structures consisting of data fields and methods.
47. In C++, what is the scope resolution operator?
Answer:
Explanation:
The scope resolution operator (::) in C++ is used to define a function outside a class or to access a global variable within another variable.
48. Which of the following is the correct syntax to comment multiple lines in C++?
Answer:
Explanation:
In C++, /*…*/ is used to comment out multiple lines.
49. What does STL stand for in C++?
Answer:
Explanation:
STL stands for Standard Template Library in C++.
50. What is the correct way to declare a constant variable in C++?
Answer:
Explanation:
In C++, the const keyword is used before the datatype to declare a variable as constant.
51. What is a special type of function that has access to private members of a class but isn’t a member of that class?
Answer:
Explanation:
A friend function is not a member of the class, but it can access private and protected members of the class. It is declared using the keyword friend.
52. Which keyword is used in C++ to include a particular namespace in our program?
Answer:
Explanation:
The using keyword is used to specify that a program will be using a particular namespace, like using namespace std;.
53. Which function cannot be inline in C++?
Answer:
Explanation:
A virtual function in C++ is used for runtime polymorphism and it cannot be inline because its address is not known at compile time.
54. What is it called when a function calls itself?
Answer:
Explanation:
When a function calls itself, it’s known as recursion. It’s a powerful technique but one must ensure it has a base case to prevent infinite recursion.
55. Which among the following is a valid way to define a data member of a class as static?
Answer:
Explanation:
In C++, static data members of a class need to be declared within the class using the static keyword followed by data type and variable name.
56. What can be used to point to a function in C++?
Answer:
Explanation:
A function pointer points to the address of a function and can be used to call a function indirectly.
57. What characterizes the initialization of variables inside a class constructor?
Answer:
Explanation:
When the value of a variable is set during runtime (like inside a class constructor), it’s called dynamic initialization.
58. Which access specifier allows class members to be accessible only within the same class and by friend functions?
Answer:
Explanation:
Private access specifier ensures that class members are only accessible within the class and by friend functions.
59. What is a class called that cannot be instantiated and is meant to be subclassed?
Answer:
Explanation:
An abstract class is a class that cannot be instantiated and is intended to be subclassed. It can have abstract (without implementation) as well as concrete methods.
60. To read command line arguments in C++, which parameters should the main() function have?
Answer:
Explanation:
Command line arguments in C++ are read using (int argc, char *argv[]) where argc denotes the number of arguments and argv is an array of pointers to those arguments.