C++ MCQ ( 1-30)

1. Which of the following is the correct syntax for defining a class in C++?
a) class MyClass {}
b) class MyClass[] {}
c) MyClass class {}
d) class = MyClass {}

Answer: a) class MyClass {}


2. What is the output of the following code?

#include<iostream>  
using namespace std;  
int main() {  
    int x = 10;  
    cout << ++x << " ";  
    cout << x++ << " ";  
    cout << --x << " ";  
    cout << x-- << endl;  
    return 0;  
}  

a) 11 10 9 10
b) 11 11 10 9
c) 10 11 9 9
d) 10 10 11 10

Answer: a) 11 10 9 10


3. Which of the following is the correct way to declare a pointer in C++?
a) int ptr;
b) ptr int
;
c) int ptr*;
d) *int ptr;

Answer: a) int *ptr;


4. What is the output of the following code?

#include <iostream>  
using namespace std;  
int main() {  
    int x = 5;  
    int *p = &x;  
    cout << *p << endl;  
    return 0;  
}  

a) 5
b) x
c) &x
d) error

Answer: a) 5


5. Which of the following is true about constructors in C++?
a) Constructors can have a return type.
b) A constructor can be private.
c) A constructor cannot be overloaded.
d) A constructor must be called explicitly.

Answer: b) A constructor can be private.


6. What does the ‘new’ keyword do in C++?
a) Allocates memory dynamically
b) Deallocates memory
c) Initializes variables
d) Creates a new function

Answer: a) Allocates memory dynamically


7. Which of the following is used to handle exceptions in C++?
a) try-catch
b) exception-throw
c) throw-catch
d) handle-exception

Answer: a) try-catch


8. What is the default access specifier for members of a class in C++?
a) private
b) public
c) protected
d) private and public

Answer: a) private


9. Which of the following operators is used to allocate memory dynamically in C++?
a) malloc()
b) new()
c) alloc()
d) create()

Answer: b) new()


10. What is the output of the following code?

#include<iostream>  
using namespace std;  
int main() {  
    int arr[3] = {1, 2, 3};  
    cout << arr[1] << endl;  
    return 0;  
}  

a) 1
b) 2
c) 3
d) arr[1]

Answer: b) 2


11. Which of the following is true about destructors in C++?
a) Destructors are called explicitly.
b) A class can have multiple destructors.
c) A destructor has the same name as the class but with a tilde (~) prefix.
d) Destructors cannot be virtual.

Answer: c) A destructor has the same name as the class but with a tilde (~) prefix.


12. What is the purpose of the ‘this’ pointer in C++?
a) It points to the current function.
b) It points to the current object.
c) It points to the base class object.
d) It is used to return the value of an object.

Answer: b) It points to the current object.


13. Which of the following is correct about static data members in C++?
a) Static data members are shared among all objects of a class.
b) Static data members can only be accessed by non-static member functions.
c) Static data members are initialized in the constructor.
d) A static data member cannot be initialized in the class definition.

Answer: a) Static data members are shared among all objects of a class.


14. What is the size of the following data types in C++?
int, float, char

a) 2, 4, 1
b) 4, 4, 1
c) 4, 8, 1
d) 2, 8, 1

Answer: b) 4, 4, 1


15. What will be the output of the following code?

#include<iostream>  
using namespace std;  
int main() {  
    int x = 10;  
    int &y = x;  
    y = 20;  
    cout << x << endl;  
    return 0;  
}  

a) 10
b) 20
c) error
d) undefined

Answer: b) 20


16. What does the keyword ‘virtual’ do in C++?
a) It declares a virtual function.
b) It allows dynamic binding of function calls.
c) It is used to define a virtual class.
d) It is used for virtual inheritance.

Answer: b) It allows dynamic binding of function calls.


17. Which of the following is the correct way to declare a constant pointer in C++?
a) const int *ptr;
b) int const *ptr;
c) int *const ptr;
d) both a and b

Answer: c) int *const ptr;


18. Which of the following is NOT a valid C++ keyword?
a) virtual
b) namespace
c) exception
d) friend

Answer: c) exception


19. What is the result of the following code?

#include <iostream>  
using namespace std;  
int main() {  
    int a = 5;  
    int b = 10;  
    cout << a & b << endl;  
    return 0;  
}  

a) 15
b) 0
c) 10
d) error

Answer: b) 0


20. Which operator is used to access members of a class in C++?
a) . (dot)
b) -> (arrow)
c) & (address)
d) both a and b

Answer: d) both a and b


21. Which of the following is used to terminate a loop or a switch statement in C++?
a) break
b) exit
c) stop
d) terminate

Answer: a) break


22. Which of the following is the correct syntax for defining a destructor in C++?
a) ClassName();
b) ClassName();
c) ClassName();
d) destructor ClassName();

Answer: a) ~ClassName();


23. What does the ‘continue’ statement do in a loop?
a) Terminates the loop.
b) Skips the current iteration and proceeds to the next iteration.
c) Skips the next iteration and goes back to the beginning of the loop.
d) Breaks the loop.

Answer: b) Skips the current iteration and proceeds to the next iteration.


24. Which of the following is the correct way to define a friend function in C++?
a) friend int func();
b) friend func();
c) int friend func();
d) friend function func();

Answer: a) friend int func();


25. What is the primary function of the ‘new’ and ‘delete’ operators in C++?
a) ‘new’ allocates memory dynamically, and ‘delete’ deallocates memory.
b) ‘new’ is used for defining new objects, and ‘delete’ destroys objects.
c) ‘new’ allocates memory for arrays, and ‘delete’ deallocates arrays.
d) ‘new’ and ‘delete’ are used for exception handling.

Answer: a) ‘new’ allocates memory dynamically, and ‘delete’ deallocates memory.


26. What will be the output of the following code?

#include<iostream>  
using namespace std;  
int main() {  
    for(int i=0; i<5; i++) {  
        if(i == 3)  
            continue;  
        cout << i << " ";  
    }  
    return 0;  
}  

a) 0 1 2 3 4
b) 0 1 2 4
c) 1 2 3 4
d) 0 1 2 3

Answer: b) 0 1 2 4


27. What is the purpose of the ‘inline’ keyword in C++?
a) It defines a function that cannot be overridden.
b) It reduces function call overhead by replacing function calls with the function body.
c) It makes a function static.
d) It defines a function that is automatically called during runtime.

Answer: b) It reduces function call overhead by replacing function calls with the function body.


28. Which of the following is used to handle files in C++?
a) fstream
b) file
c) io
d) filesystem

Answer: a) fstream


29. Which of the following is the correct way to declare a reference variable in C++?
a) int &x;
b) int x&;
c) &int x;
d) reference int x;

Answer: a) int &x;


30. What does the ‘sizeof’ operator do in C++?
a) It returns the size of a variable or object in bytes.
b) It returns the size of a data type in bits.
c) It returns the size of an array in elements.
d) It returns the total size of a class.

Answer: a) It returns the size of a variable or object in bytes.

Leave a Reply