C++ MCQ: Test Your Knowledge with Multiple Choice Questions

Welcome to your C++ PART 1

1. 
Which of the following is the correct way to define a derived class in C++?

2. 
. What is the output of the following code snippet? ```cpp int x = 5; int y = 2; cout < y ? x : y) << endl; ```

3. 
What is the output of the following code snippet? ```cpp int x = 5; cout < 10 ? "Yes" : "No"); ```

4. 
What is the output of the following code snippet? ```cpp int arr[] = {1, 2, 3}; cout << arr[3]; ```

5. 
Which of the following is not a valid type of function parameter passing in C++?

6. 
What is the output of the following code snippet? ```cpp int x = 5; cout < 2 && x < 10); ```

7. 
. What is the output of the following code snippet? ```cpp int x = 5; int y = 2; cout << x / y << endl; cout << static_cast(x) / y << endl; ```

8. 
Which of the following is the correct syntax for a lambda function in C++?

9. 
What is the purpose of the "try-catch" block in C++?

10. 
Which of the following is not a fundamental data type in C++?

11. 
What is the output of the following code snippet? ```cpp int arr[] = {1, 2, 3, 4, 5}; int* ptr = arr + 2; cout << *ptr << endl; cout << ptr[1] << endl; ```

12. 
. What is the output of the following code snippet? ```cpp int arr[] = {1, 2, 3, 4, 5}; cout << *(arr + 2);

13. 
. What is the output of the following code snippet? ```cpp int arr[5]; cout << sizeof(arr); ```

14. 
Which of the following is the correct way to declare a function template in C++?

15. 
Which of the following is the correct way to declare a constant member function in a class?

16. 
What is the output of the following code snippet? ```cpp int x = 5; int* ptr = &x; int& ref = *ptr; ref = 10; cout << x; ```

17. 
Which of the following is the correct way to include a header file in C++?

18. 
What is the output of the following code snippet? ```cpp int arr[3] = {1, 2}; cout << arr[2]; ```

19. 
. Which of the following is not a valid access specifier in C++?

20. 
What is the output of the following code snippet? ```cpp int x = 5; int y = x++; cout << y; ```

21. 
. What is the purpose of the "inline" keyword in C++?

22. 
Which of the following is the correct way to define a destructor in a class?

23. 
What is the output of the following code snippet? ```cpp int arr[] = {1, 2, 3, 4, 5}; cout << *arr << endl; cout << *(arr + 2) << endl; ```

24. 

25. 
Which of the following is the correct way to define a static member variable in a class?

26. 
Which of the following is not a valid way to define a constant in C++?

27. 
. What is the output of the following code snippet? ```cpp int x = 5; int& ref = x; ref = 10; cout << x; ```

28. 
. What is the output of the following code snippet? ```cpp int arr[] = {1, 2, 3, 4, 5}; int* ptr = arr; cout << sizeof(arr) / sizeof(arr[0]) << endl; cout << sizeof(ptr) << endl; ```

29. 
What is the output of the following code snippet? ```cpp int arr[] = {1, 2, 3, 4, 5}; int* ptr = arr; cout << *ptr++ << endl; cout << *ptr << endl; ```

30. 
What is the output of the following code snippet? ```cpp int x = 10; int* ptr = &x; cout << *ptr << endl; *ptr = 20; cout << x << endl; ```

31. 
Which of the following is not a valid type of loop in C++?

32. 
Which of the following is not a valid arithmetic operator in C++?

33. 
What is the purpose of the "virtual" keyword in C++?

34. 
. What is the output of the following code snippet? ```cpp int x = 5; int* ptr = &x; cout << ptr << endl; cout << *ptr << endl; cout << &ptr << endl; ```

35. 
Which of the following is the correct way to define a constructor in a class?

36. 
What is the purpose of the "using" keyword in C++?

37. 
Which of the following is not a valid way to define a string in C++?

38. 
. What is the output of the following code snippet? ```cpp int x = 5; int* ptr = &x; int** ptr2 = &ptr; cout << **ptr2; ```

39. 
. Which of the following is the correct way to allocate memory for an array in C++?

40. 
What is the purpose of the "const_cast" operator in C++?

41. 
What is the purpose of the "delete" keyword in C++?

42. 
Which of the following operators is used to access a member of a class using a pointer?

43. 
What does the "const" keyword indicate in C++?

44. 
What is the output of the following code snippet? ```cpp int x = 5; int y = 2; int z = x + y++; cout << z << endl; cout << y << endl; ```

45. 
What is the purpose of the "break" statement in a switch statement?

46. 
Which of the following is a valid way to declare a constant pointer in C++?

47. 
What is the output of the following code snippet? ```cpp cout << sizeof('a');

48. 
What is the output of the following code snippet? ```cpp int x = 10; int* ptr = nullptr; if (ptr) cout << *ptr; else cout << x; ```

49. 
Which of the following is not a valid storage class specifier in C++?

More C++ MCQ

20 C++ questions and answers

Q: What is the difference between a variable and a constant in C++?

A: A variable can be assigned a new value during program execution, while a constant has a fixed value that cannot be changed.

Q: What is the syntax for declaring a variable in C++?

A: The syntax for declaring a variable in C++ is: <data_type> <variable_name>;

Q: What are the basic data types in C++?

A: C++ supports several basic data types, including int, float, double, char, and bool.

Q: What is the difference between an int and a float data type?

A: An int data type stores whole numbers, while a float data type stores decimal numbers with a limited precision.

Q: How can you determine the size of a data type in C++?

A: You can use the sizeof operator to determine the size of a data type in bytes. For example, sizeof(int) returns the size of an int.

Q: What is the purpose of an if statement in C++?

A: An if statement allows you to perform certain actions based on a condition. If the condition is true, the code within the if block is executed.

Q: What is the difference between the pre-increment and post-increment operators?

A: The pre-increment operator (++i) increments the value of the variable and returns the updated value, while the post-increment operator (i++) returns the current value and then increments it.

Q: How do you define a function in C++?

A: The syntax for defining a function in C++ is: <return_type> <function_name>(<parameter_list>) { <function_body> }

Q: What is recursion in programming?

A: Recursion is a technique where a function calls itself to solve a problem. It involves breaking a problem into smaller subproblems until a base case is reached.

Q: What is the purpose of the return statement in a function?

A: The return statement is used to return a value from a function back to the caller. It also terminates the function’s execution.

Q: What is a pointer in C++?

A: A pointer is a variable that stores the memory address of another variable. It allows you to indirectly access and manipulate the value of that variable.

Q: How do you declare a pointer variable in C++?

A: The syntax for declaring a pointer variable in C++ is: <data_type>* <pointer_name>;

Q: What is the difference between the * and & operators when working with pointers?

A: The * operator is used to declare a pointer and to dereference a pointer (access the value it points to). The & operator is used to get the address of a variable.

Q: What is the purpose of the while loop in C++?

A: The while loop repeatedly executes a block of code as long as a given condition is true. It is used when the number of iterations is not known in advance.

Q: What is the switch statement used for in C++?

A: The switch statement allows you to select one of many possible paths of execution based on the value of a variable or an expression.

Q: What is the scope of a variable in C++?

A: The scope of a variable defines where it can be accessed within a program. Variables can have local scope (limited to a specific block) or global scope (accessible throughout the program).

Q: Can you pass an array to a function in C++?

A: Yes, you can pass an array to a function in C++. You can either pass it by reference or by pointer.

Q: What is the purpose of the break statement in a loop or switch statement?

A: The break statement is used to exit a loop or switch statement prematurely. It transfers control to the statement immediately following the loop or switch.

Q: How do you dynamically allocate memory in C++?

A: You can dynamically allocate memory in C++ using the new keyword. For example, int* ptr = new int; allocates memory for an integer.

Q: What is function overloading in C++?

A: Function overloading allows you to define multiple functions with the same name but different parameter lists. The compiler determines which function to call based on the arguments used.

More Questions – > Click