Here's a C++ language quiz for you. Each question is followed by multiple-choice answers. Choose the correct option for each question.
(You will find the answers to the questions at the end of the quiz)
1. What is the correct way to declare a constant in C++?
a. const int x;
b. constant x;
c. int const x;
d. const x = 5;
2. How do you define a class named "Person" with a private member variable "name" in C++?
a. class Person { private: string name; };
b. class Person { private string name; };
c. class Person { private: name string; };
d. class Person private(name);
3. What is the purpose of the virtual keyword in C++?
a. Declares a variable as virtual
b. Declares a function as virtual
c. Allocates memory for a virtual object
d. Includes a virtual header file
4. How do you dynamically allocate memory for an integer in C++?
a. int* x = malloc(sizeof(int));
b. int* x = new int;
c. int* x = allocate(int);
d. int* x = new int();
5. What is the difference between function overloading and function overriding in C++?
a. Function overloading is for static polymorphism, and function overriding is for dynamic polymorphism.
b. Function overloading is for dynamic polymorphism, and function overriding is for static polymorphism.
c. They are the same concept with different names.
d. Function overloading is for encapsulation, and function overriding is for inheritance.
6. In C++, what is the purpose of the typeid operator?
a. Returns the type of an expression
b. Converts a type to another type
c. Checks if two types are the same
d. Retrieves the size of a type
7. What is the output of the following code snippet?
a. 5, 6
b. 6, 7
c. 5, 7
d. 6, 6
8. How do you catch all exceptions in a C++ catch block?
a. catch (all_exception e) { }
b. catch (...) { }
c. catch (Exception e) { }
d. catch (exception& e) { }
9. What is the purpose of the this pointer in C++?
a. Points to the current object
b. Points to the previous object
c. Points to the base class object
d. Points to the derived class object
10. How do you declare a pure virtual function in C++?
a. virtual function() = 0;
b. virtual function() pure;
c. abstract function();
d. pure virtual function();
11. What is the purpose of the namespace keyword in C++?
a. Declares a new class
b. Creates a new scope for identifiers
c. Defines a new function
d. Allocates memory for variables
12. What is the output of the following code snippet?
a. H
b. e
c. Hello
d. 72
13. How do you open a file named "example.txt" in C++ for reading and writing?
a. std::ofstream file("example.txt");
b. std::ifstream file("example.txt");
c. std::fstream file("example.txt");
d. std::file("example.txt");
14. What is the purpose of the static keyword in C++?
a. Declares a static variable
b. Declares a constant
c. Allocates memory for a variable
d. Defines a member function
15. What is the difference between new and malloc in C++?
a. new is used for dynamic memory allocation and initializes the memory, while malloc only allocates memory.
b. malloc is used for dynamic memory allocation and initializes the memory, while new only allocates memory.
c. They are the same; both are used for dynamic memory allocation.
d. new and malloc are used for static memory allocation.
Answers:
1. a,
2. a,
3. b,
4. b,
5. a,
6. a,
7. b,
8. b,
9. a,
10. a,
11. b,
12. a,
13. c,
14. a,
15. a