C++ Multiple Choice Question

Question : 1 What is the correct syntax to define a function in C++?

  • A

    function myFunction() {};

  • B

    void myFunction() {}

  • C

    def myFunction() {}

  • D

    def myFunction():

Answer:

Question : 2 Which data type in C++ is used to store a single character?

  • A

    char

  • B

    int

  • C

    string

  • D

    float

Answer:

Question : 3 What is the output of the following code snippet: int x = 5; cout << x++;

  • A

    5

  • B

    6

  • C

    Compiler Error

  • D

    Undefined Behavior

Answer:

Question : 4 Which operator is used to allocate memory for a variable in C++?

  • A

    new

  • B

    malloc

  • C

    alloc

  • D

    allocate

Answer:

Question : 5 What is the correct way to access the "age" member variable of an object "person" of class "Person" in C++?

  • A

    person.age;

  • B

    person->age;

  • C

    person::age;

  • D

    person[age];

Answer:

Question : 6 Which keyword is used to inherit a class in C++?

  • A

    extend

  • B

    inherits

  • C

    extend class

  • D

    inherit

Answer:

Question : 7 What is the output of the following code snippet: int x = 10; int y = x++ + ++x; cout << y;

  • A

    20

  • B

    21

  • C

    22

  • D

    Compiler Error

Answer:

Question : 8 Which of the following is NOT a valid C++ comment?

  • A

    // This is a comment

  • B

    /* This is a comment */

  • C

    # This is a comment

  • D

    // This is another comment

Answer:

Question : 9 What is the purpose of the "endl" manipulator in C++?

  • A

    It ends the program execution.

  • B

    It moves the cursor to the next line.

  • C

    It clears the output buffer.

  • D

    It inserts a new line character.

Answer:

Question : 10 What is the function of the "cin" object in C++?

  • A

    To display output on the console.

  • B

    To write data to a file.

  • C

    To read input from the console.

  • D

    To allocate memory.

Answer:

Question : 11 What does the "static" keyword mean when used with a class member in C++?

  • A

    It specifies that the member is accessible only within its class.

  • B

    It indicates that the member should not be initialized.

  • C

    It signifies that the member is shared among all objects of the class.

  • D

    It defines a constant member.

Answer:

Question : 12 Which operator is used to access the member functions and variables of a class in C++?

  • A

    ->

  • B

    .

  • C

    ::

  • D

    :

Answer:

Question : 13 What is the correct way to declare a reference variable in C++?

  • A

    int &x = 10;

  • B

    int* x;

  • C

    int& x = y;

  • D

    int x = &y;

Answer:

Question : 14 What is the output of the following code snippet: int arr[5] = {1, 2, 3}; cout << arr[3];

  • A

    0

  • B

    1

  • C

    2

  • D

    Undefined Behavior

Answer:

Question : 15 What does the "sizeof" operator return in C++?

  • A

    The address of a variable.

  • B

    The size of a datatype in bytes.

  • C

    The value of a variable.

  • D

    The datatype of a variable.

Answer:

Question : 16 What is the function of the "break" statement in a switch case statement in C++?

  • A

    To skip the current iteration of a loop.

  • B

    To exit the switch case block.

  • C

    To terminate the program execution.

  • D

    To continue to the next case.

Answer:

Question : 17 What is the purpose of the "continue" statement in C++?

  • A

    To exit the loop.

  • B

    To jump to the next iteration of the loop.

  • C

    To return from a function.

  • D

    To print the loop index.

Answer:

Question : 18 What is the difference between "++i" and "i++" in C++?

  • A

    "++i" increments the value of i and returns the new value, while "i++" returns the value of i and then increments it.

  • B

    "++i" increments the value of i, while "i++" decrements it.

  • C

    "++i" decrements the value of i and returns the new value, while "i++" returns the value of i and then decrements it.

  • D

    "++i" and "i++" have the same functionality.

Answer:

Question : 19 What is the output of the following code snippet: int x = 5, y = 10; cout << (x > y ? x : y);

  • A

    5

  • B

    10

  • C

    Compiler Error

  • D

    10

Answer:

Question : 20 Which of the following statements is true about the "virtual" keyword in C++?

  • A

    It is used to declare a pure virtual function.

  • B

    It specifies that a function can be overridden in derived classes.

  • C

    It is used to define a function inside a class.

  • D

    It is used to prevent inheritance.

Answer:

Question : 21 What is the purpose of the "typeid" operator in C++?

  • A

    To determine the type of an object at runtime.

  • B

    To perform dynamic casting.

  • C

    To perform static casting.

  • D

    To determine the size of an object.

Answer:

Question : 22 What is the syntax for creating an object of class "MyClass" using dynamic memory allocation in C++?

  • A

    MyClass obj();

  • B

    MyClass obj = new MyClass();

  • C

    MyClass* obj = new MyClass();

  • D

    MyClass obj = new MyClass;

Answer:

Question : 23 What is the purpose of the "friend" keyword in C++?

  • A

    To specify the base class in inheritance.

  • B

    To declare a function or class as a friend, granting it access to private and protected members of the class.

  • C

    To declare a function as a member of a class.

  • D

    To specify the derived class in inheritance.

Answer:

Question : 24 Which of the following statements about templates in C++ is true?

  • A

    Templates are used to define generic classes and functions.

  • B

    Templates can only be used with built-in data types.

  • C

    Templates are used to define private members of a class.

  • D

    Templates are not supported in C++.

Answer:

Question : 25 What is the difference between "new" and "malloc()" in C++?

  • A

    "new" is an operator used for dynamic memory allocation and also calls the constructor, while "malloc()" is a function used for dynamic memory allocation without calling the constructor.

  • B

    "new" is used for static memory allocation, while "malloc()" is used for dynamic memory allocation.

  • C

    "new" is used for array allocation, while "malloc()" is used for single variable allocation.

  • D

    "new" is a function used for dynamic memory allocation and also calls the constructor, while "malloc()" is an operator used for dynamic memory allocation without calling the constructor.

Answer:

Question : 26 What is the purpose of the "delete" operator in C++?

  • A

    To deallocate memory allocated using "new".

  • B

    To deallocate memory allocated using "malloc()".

  • C

    To delete a file from the file system.

  • D

    To remove an element from a container.

Answer:

Question : 27 What is the difference between "private" and "protected" access specifiers in C++ classes?

  • A

    "private" members are accessible only within the same class, while "protected" members are accessible within the same class and its derived classes.

  • B

    "private" members are accessible within the same class and its derived classes, while "protected" members are accessible only within the same class.

  • C

    "private" members are accessible from anywhere in the program, while "protected" members are accessible only within the same class.

  • D

    "private" members are accessible only within the same class, while "protected" members are accessible from anywhere in the program.

Answer:

Question : 28 What is function overloading in C++?

  • A

    It refers to the ability to define multiple functions with the same name but different return types.

  • B

    It refers to the ability to define multiple functions with the same name but different access specifiers.

  • C

    It refers to the ability to define multiple functions with the same name but different parameter lists.

  • D

    It refers to the ability to define multiple functions with the same name but in different classes.

Answer:

Question : 29 What is a constructor in C++?

  • A

    It is a function that is called when an object is created.

  • B

    It is a function that is called when an object is destroyed.

  • C

    It is a function that is called when an object is modified.

  • D

    It is a function that is called when an object is copied.

Answer:

Question : 30 What is the purpose of the "this" pointer in C++?

  • A

    To store the address of the current object.

  • B

    To store the address of the previous object.

  • C

    To store the address of the next object.

  • D

    To store the address of the main function.

Answer:

Question : 31 What is the difference between "class" and "struct" in C++?

  • A

    There is no difference between "class" and "struct" in C++.

  • B

    In a "class", members are private by default, while in a "struct", members are public by default.

  • C

    In a "class", members are public by default, while in a "struct", members are private by default.

  • D

    In a "class", members are protected by default, while in a "struct", members are private by default.

Answer:

Question : 32 What is the purpose of inheritance in C++?

  • A

    To create multiple instances of a class.

  • B

    To allow a class to inherit properties and behaviors from another class.

  • C

    To prevent other classes from accessing certain members of a class.

  • D

    To restrict the access to certain members of a class.

Answer: