MCQ Related to C++ Language

  1. What is the correct syntax to include a header file in C++?
    • A) include <iostream>
    • B) #include <iostream>
    • C) #include iostream
    • D) import <iostream>
  2. Which of the following is a valid comment in C++?
    • A) /* This is a comment */
    • B) // This is a comment
    • C) # This is a comment
    • D) Both A and B
  3. Which keyword is used to declare a class in C++?
    • A) struct
    • B) class
    • C) object
    • D) type
  4. What will be the output of the following code?
    std::cout << 10 + 20 + " C++";
    • A) 30 C++
    • B) C++
    • C) error
    • D) 10 C++
  5. How do you declare a constant variable in C++?
    • A) const int x = 10;
    • B) constant int x = 10;
    • C) int const x = 10;
    • D) Both A and C
  6. What does std::endl do in C++?
    • A) Inserts a space
    • B) Ends a line and flushes the output buffer
    • C) Comments the line
    • D) Terminates the program
  7. In C++, what is the default access specifier for class members?
    • A) public
    • B) private
    • C) protected
    • D) friend
  8. Which operator is used for pointer dereferencing in C++?
    • A) *
    • B) &
    • C) ->
    • D) @
  9. Which of the following is true about constructors in C++?
    • A) They return values.
    • B) They are called automatically when an object is created.
    • C) They can be explicitly called.
    • D) They can have return types.
  10. How do you define a function in C++?
    • A) function int add(int a, int b) {}
    • B) def int add(int a, int b) {}
    • C) int add(int a, int b) {}
    • D) int add(int a, b): {}
  11. Which of the following methods can be used for memory allocation in C++?
    • A) malloc
    • B) calloc
    • C) new
    • D) All of the above
  12. How is inheritance implemented in C++?
    • A) By using the extends keyword
    • B) Through interface implementation
    • C) By using the : symbol
    • D) None of the above
  13. What is the purpose of the virtual keyword in C++?
    • A) To create virtual memory
    • B) To enable dynamic binding
    • C) To declare static members
    • D) To prevent overriding
  14. Which of the following correctly creates a derived class in C++?
    • A) class Derived : public Base {}
    • B) class Derived extends Base {}
    • C) class Derived inherits Base {}
    • D) class Derived(Base) {}
  15. What will be the result of the following code snippet?
    int a = 5;
    int b = 10;
    int c = a < b ? a : b;
    • A) 5
    • B) 10
    • C) error
    • D) false
  16. Which of the following is a standard library of C++?
    • A) iostream
    • B) cstdio
    • C) vector
    • D) All of the above
  17. How would you declare a function that takes a reference as an argument?
    • A) void func(int &x);
    • B) void func(int x&);
    • C) void func(int *x);
    • D) void func(int x);
  18. What is an example of a C++ keyword used for exception handling?
    • A) catch
    • B) throw
    • C) try
    • D) All of the above
  19. What is the output of std::cout << (5 + 3) * 2;?
    • A) 16
    • B) 8
    • C) 5
    • D) error
  20. Which of the following constructs are present in C++ for loop control?
    • A) for
    • B) while
    • C) do-while
    • D) All of the above
  21. What’s the difference between == and = in C++?
    • A) == is for assignment, = is for comparison.
    • B) == is for comparison, = is for assignment.
    • C) They are the same.
    • D) == is a logical operator, = is a bitwise operator.
  22. How do you declare a multi-dimensional array in C++?
    • A) int arr[3][4];
    • B) int[3][4] arr;
    • C) int arr[3, 4];
    • D) int arr(3, 4);
  23. What does the new operator do in C++?
    • A) It creates a new function.
    • B) It allocates memory dynamically.
    • C) It creates a new class.
    • D) It initializes static variables.
  24. Which of the following is NOT a valid identifier in C++?
    • A) var_1
    • B) 1var
    • C) _var
    • D) var
  25. What does the following C++ code snippet do?
    std::string str = "Hello";
    str += " World!";
    • A) It throws an error.
    • B) It concatenates “Hello” and ” World!”.
    • C) It initializes an integer.
    • D) It declares a constant string.
  26. Which keyword is used to exit a function immediately in C++?
    • A) stop
    • B) exit
    • C) return
    • D) break
  27. Which header file is required to use std::vector?
    • A) <vector.h>
    • B) <stdio.h>
    • C) <vector>
    • D) None of the above
  28. What is the purpose of the #define directive in C++?
    • A) To include header files
    • B) To define constants or macros
    • C) To declare functions
    • D) To create classes
  29. What is the result of the expression sizeof(int)?
    • A) The size of an integer variable in bytes.
    • B) The total number of integers.
    • C) An error.
    • D) Undefined behavior.
  30. What does polymorphism refer to in C++?
    • A) Overloading operators
    • B) Multiple forms of a function
    • C) Inheriting from multiple classes
    • D) None of the above
  31. Which one among the following can lead to memory leaks in C++?
    • A) Not using delete on dynamically allocated memory.
    • B) Using const for pointers.
    • C) Incorrect initializations.
    • D) Using stack variables.
  32. What is the typical return type of the main function in a C++ program?
    • A) int
    • B) void
    • C) float
    • D) char
  33. Which C++ feature allows one function to be used with different types?
    • A) Function overloading
    • B) Operator overloading
    • C) Inheritance
    • D) Encapsulation
  34. What does STL stand for in C++?
    • A) Standard Template Library
    • B) Standard Type Library
    • C) System Template Library
    • D) Simple Template Library
  35. Which of the following correctly initializes a string in C++?
    • A) string str = "Hello";
    • B) std::string str("Hello");
    • C) Both A and B
    • D) char str[] = "Hello";
  36. What is nullptr in C++?
    • A) A NULL pointer constant
    • B) Represents no value
    • C) A keyword for creating objects
    • D) A function of the pointer type
  37. Which operator is used for typecasting in C++?
    • A) #
    • B) ::
    • C) as
    • D) dynamic_cast
  38. What is the output of the following code?
    int x = 10;
    if (x == 10) {
    std::cout << "Ten";
    }
    • A) 10
    • B) Ten
    • C) error
    • D) Nothing
  39. What does the static keyword signify in C++?
    • A) A temporary variable
    • B) A global variable
    • C) A variable that maintains its value across function calls
    • D) A constant variable
  40. How do we pass an array to a function in C++?
    • A) By passing its first element
    • B) By using & operator
    • C) By sending it always by value
    • D) Directly, as a parameter
  41. Which of these is a valid destructor in C++?
    • A) ~ClassName()
    • B) ClassName::~
    • C) destructor ClassName {}
    • D) ClassName()~
  42. What will be the result of:
    int a = 2;
    std::cout << a++;
    • A) 2
    • B) 3
    • C) error
    • D) The program crashes
  43. What is the purpose of the friend keyword?
    • A) To declare a friendship between classes
    • B) To protect class members
    • C) To create constant variables
    • D) None of the above
  44. Which of the following operators can be overloaded in C++?
    • A) +
    • B) -
    • C) *
    • D) All of the above
  45. Which of these types of constructor is available in C++?
    • A) Default constructor
    • B) Copy constructor
    • C) Parameterized constructor
    • D) All of the above
  46. Which exception type is thrown if there is division by zero in C++?
    • A) std::invalid_argument
    • B) std::runtime_error
    • C) std::out_of_range
    • D) None of the above
  47. What does the sizeof() function return?
    • A) Memory space allocated to all variables
    • B) Total memory in use
    • C) The size of the data type or object in bytes
    • D) Total size of the program
  48. Which keyword is used for creating an instance of a class?
    • A) new
    • B) class
    • C) make
    • D) instance
  49. What is RAII in C++?
    • A) Resource Acquisition Is Initialization
    • B) Random Access Is Important
    • C) Reference Assignment Is Initiated
    • D) None of the above
  50. How can you declare an abstract class in C++?
    • A) By using abstract
    • B) By having at least one pure virtual function
    • C) By using virtual without defining any method
    • D) By inheriting from another abstract class.