1.
Which of the following is not a feature of Object-Oriented b) Multiple Inheritance
Programming? c) Hierarchical Inheritance
a) Encapsulation d) Hybrid Inheritance
b) Polymorphism Answer: b) Multiple Inheritance
c) Recursion 7. What is method overriding?
d) Inheritance a) Defining multiple methods with the same name in the same
Answer: c) Recursion class
2. What is the main advantage of Encapsulation? b) Changing the implementation of a method in a subclass
a) It helps in data hiding and security c) Overloading a constructor with different parameters
b) It allows direct access to the data members d) Using private methods within a class
c) It improves global variables usage Answer: b) Changing the implementation of a method in a
d) It allows multiple inheritances subclass
Answer: a) It helps in data hiding and security 8. Which keyword is used to refer to the parent class in Java?
3. Which OOP principle allows a derived class to provide a specific a) base
implementation of a method already defined in its base class? b) super
a) Abstraction c) this
b) Encapsulation d) extends
c) Inheritance Answer: b) super
d) Polymorphism Abstraction and Encapsulation
Answer: d) Polymorphism 9. Which of the following provides a blueprint for creating objects
Classes and Objects but cannot be instantiated?
4. What is an object in OOP? a) Object
a) A function that performs a task b) Abstract class
b) A variable that holds a single value c) Interface
c) An instance of a class d) Both b and c
d) A part of the operating system Answer: d) Both b and c
Answer: c) An instance of a class 10. What is the main purpose of an interface in Java?
5. In C++ or Java, how is an object created? a) To provide multiple inheritance
a) class obj; b) To store objects
b) obj = new Object(); c) To provide data hiding
c) Object obj = new Object(); d) To allow direct memory manipulation
d) create Object obj; Answer: a) To provide multiple inheritance
Answer: c) Object obj = new Object(); 1. Which of the following is NOT a pillar of Object-Oriented
Inheritance and Polymorphism Programming?
6. Which type of inheritance allows a class to inherit from multiple a) Abstraction
classes? b) Encapsulation
a) Single Inheritance c) Compilation
d) Inheritance a) Polymorphism
Answer: c) Compilation b) Encapsulation
2. Which feature of OOP allows bundling data and methods that c) Inheritance
operate on the data into a single unit? d) Abstraction
a) Inheritance Answer: d) Abstraction
b) Encapsulation 3. Inheritance & Polymorphism
c) Abstraction 8. What is Inheritance in Object-Oriented Programming?
d) Polymorphism a) Creating a new class from an existing class
Answer: b) Encapsulation b) Writing multiple classes with the same name
3. In OOP, what does an object represent? c) Overloading methods in the same class
a) A function d) A function that returns a value
b) An instance of a class Answer: a) Creating a new class from an existing class
c) A data type 9. What is Polymorphism in OOP?
d) A programming language a) The ability of a class to have multiple instances
Answer: b) An instance of a class b) The ability to take multiple forms (method
4. A class in Object-Oriented Programming is: overloading/overriding)
a) A function inside an object c) The process of hiding implementation details
b) A blueprint for creating objects d) The feature that prevents modification of objects
c) A programming language Answer: b) The ability to take multiple forms (method
d) A variable type overloading/overriding)
Answer: b) A blueprint for creating objects 10. Method Overloading is an example of:
2. Encapsulation & Abstraction a) Static Polymorphism
5. What is the purpose of Encapsulation? b) Dynamic Polymorphism
a) To break the program into functions c) Encapsulation
b) To hide details and only show essential features d) Abstraction
c) To inherit properties from another class Answer: a) Static Polymorphism
d) To allow direct modification of data 4. Miscellaneous
Answer: b) To hide details and only show essential features 11. Which of the following statements is TRUE about OOP?
6. In Java, which keyword is used to make a class member a) An object is a template for a class
accessible only within the same class? b) A class is an instance of an object
a) public c) Objects are created from classes
b) private d) OOP does not support code reuse
c) protected Answer: c) Objects are created from classes
d) static 12. In OOP, which term describes the concept of a child class
Answer: b) private inheriting properties from a parent class?
7. Which OOP concept helps to hide the implementation details a) Abstraction
and only shows the functionality to the user? b) Polymorphism
c) Encapsulation b) A class
d) Inheritance c) An object of ostream class
Answer: d) Inheritance d) A keyword
13. Which of these keywords is used to create a class in Java? Answer: c) An object of ostream class
a) function 4. Which header file is used for input-output operations in C++?
b) define a) <conio.h>
c) class b) <stdio.h>
d) object c) <iostream>
Answer: c) class d) <string.h>
14. Which type of relationship does Inheritance represent in OOP? Answer: c) <iostream>
a) Has-A 5. Which of the following is a correct way to print "Hello, World!"
b) Is-A in C++?
c) Uses-A a) print("Hello, World!");
d) Creates-A b) cout << "Hello, World!";
Answer: b) Is-A c) Console.WriteLine("Hello, World!");
15. Which of the following best describes an interface in OOP? d) echo "Hello, World!";
a) A class with implemented methods Answer: b) cout << "Hello, World!";
b) A blueprint for classes with only method declarations
c) A special type of object 2. Object-Oriented Programming in C++
d) A method that cannot be overridden 6. Which of the following is not a principle of Object-Oriented
Answer: b) A blueprint for classes with only method Programming (OOP)?
declarations a) Encapsulation
1. Basics of C++ b) Polymorphism
1. Who is the creator of C++? c) Abstraction
a) Dennis Ritchie d) Compilation
b) Bjarne Stroustrup Answer: d) Compilation
c) James Gosling 7. In C++, a class is a:
d) Guido van Rossum a) Blueprint for an object
Answer: b) Bjarne Stroustrup b) Type of function
2. What is the correct extension of C++ source files? c) Predefined data type
a) .c d) A variable
b) .cpp Answer: a) Blueprint for an object
c) .java 8. What is the correct syntax to define a class in C++?
d) .py a) class MyClass { public: int x; };
Answer: b) .cpp b) MyClass { int x; };
3. What is cout in C++? c) def class MyClass { int x; };
a) A function
d) class = MyClass { int x; }; 14. What is function overloading in C++?
Answer: a) class MyClass { public: int x; }; a) Defining multiple functions with the same name but different
9. How do you create an object of a class named Car? parameters
a) Car myCar; b) Redefining a function in a derived class
b) Car = myCar(); c) Using a function inside another function
c) object Car = myCar; d) Defining functions outside the class
d) new Car myCar(); Answer: a) Defining multiple functions with the same name but
Answer: a) Car myCar; different parameters
10. What is the default access specifier for members of a class in C+ 15. Which function will be executed when a derived class object is
+? created?
a) public a) Only base class constructor
b) private b) Only derived class constructor
c) protected c) Both base and derived class constructors
d) static d) None of the above
Answer: b) private Answer: c) Both base and derived class constructors
3. Inheritance & Polymorphism 4. Pointers & Memory Management
11. In C++, inheritance is used for: 16. What is a pointer in C++?
a) Code reusability a) A special type of variable that holds memory addresses
b) Data hiding b) A function that manipulates memory
c) Memory management c) A reference to an integer
d) None of the above d) A type of loop
Answer: a) Code reusability Answer: a) A special type of variable that holds memory
12. Which type of inheritance allows a class to inherit from multiple addresses
classes? 17. What is the correct syntax to declare a pointer?
a) Single Inheritance a) int ptr;
b) Multiple Inheritance b) int *ptr;
c) Multilevel Inheritance c) int ptr*;
d) Hierarchical Inheritance d) ptr int;
Answer: b) Multiple Inheritance Answer: b) int *ptr;
13. What does the virtual keyword indicate in C++? 18. Which operator is used to allocate memory dynamically in C++?
a) The function is only accessible inside the class a) malloc
b) The function can be overridden in derived classes b) allocate
c) The function cannot be accessed c) new
d) The function is static d) create
Answer: b) The function can be overridden in derived classes Answer: c) new
19. Which function is used to deallocate memory in C++? a) 10
a) delete b) 20
b) free c) 30
c) remove d) Compilation error
d) deallocate Answer: b) 20
Answer: a) delete 25. Which of the following is not a valid data type in C++?
20. What will happen if delete is used twice on the same pointer? a) int
a) Compilation error b) float
b) Memory leak c) bool
c) Undefined behavior d) string
d) Program crash Answer: d) string (string is part of <string> library, but not a
Answer: c) Undefined behavior built-in data type like int or float)
1. Basics of Data Types
5. Miscellaneous C++ Concepts 1. What is the size of an int data type in C++ (on a 32-bit system)?
21. Which keyword is used to define a constant variable in C++? a) 2 bytes
a) constant b) 4 bytes
b) const c) 8 bytes
c) static d) 1 byte
d) define Answer: b) 4 bytes
Answer: b) const 2. What is the default data type of a floating-point number in C+
22. What is the output of sizeof(int) on a typical 32-bit system? +?
a) 2 a) float
b) 4 b) double
c) 8 c) long double
d) 16 d) int
Answer: b) 4 Answer: b) double
23. Which operator is used to access a structure/class member using 3. Which of the following is not a fundamental data type in C++?
a pointer? a) int
a) . b) float
b) -> c) bool
c) * d) string
d) & Answer: d) string (string is part of <string> library but not a
Answer: b) -> fundamental data type)
24. What will be the output of: 4. The bool data type in C++ stores:
cpp a) Any integer value
CopyEdit b) Only 0 and 1
int a = 10, b = 20; c) Any positive number
cout << (a > b ? a : b);
d) Decimal values b) 4 bytes
Answer: b) Only 0 and 1 c) 8 bytes
5. What is the size of a char data type in C++? d) 16 bytes
a) 1 byte Answer: c) 8 bytes
b) 2 bytes
c) 4 bytes 3. Floating-Point Data Types
d) 8 bytes 11. What is the size of a float in C++?
Answer: a) 1 byte a) 2 bytes
b) 4 bytes
2. Integer Data Types c) 8 bytes
6. What is the range of a signed int on a 32-bit system? d) 16 bytes
a) -2,147,483,648 to 2,147,483,647 Answer: b) 4 bytes
b) 0 to 4,294,967,295 12. Which data type should be used for high-precision floating-point
c) -32,768 to 32,767 calculations?
d) 0 to 65,535 a) float
Answer: a) -2,147,483,648 to 2,147,483,647 b) double
7. The unsigned int data type in C++: c) long double
a) Can store negative values d) int
b) Can store only non-negative values Answer: c) long double
c) Occupies 2 bytes 13. What will be the output of sizeof(double) on a 64-bit system?
d) Always has a value of zero a) 2 bytes
Answer: b) Can store only non-negative values b) 4 bytes
8. Which data type is used to store very large integers? c) 8 bytes
a) int d) 16 bytes
b) short Answer: c) 8 bytes
c) long long 14. What is the difference between float and double in C++?
d) float a) No difference
Answer: c) long long b) double has higher precision than float
9. Which modifier is used to increase the range of an integer? c) float takes more memory than double
a) signed d) double is used for integer values
b) unsigned Answer: b) double has higher precision than float
c) long 15. What is the correct suffix for a long double literal in C++?
d) float a) F
Answer: c) long b) L
10. What will be the output of sizeof(long long) on a 64-bit c) D
system? d) LD
a) 2 bytes Answer: b) L
4. Character and Boolean Data Types 5. Derived & User-Defined Data Types
16. What is the ASCII value of 'A' in C++? 21. Which of the following is a derived data type in C++?
a) 65 a) int
b) 97 b) struct
c) 48 c) float
d) 255 d) bool
Answer: a) 65 Answer: b) struct
17. Which C++ data type is used to store a single character? 22. Which keyword is used to define a structure in C++?
a) string a) struct
b) char b) class
c) bool c) record
d) wchar_t d) object
Answer: b) char Answer: a) struct
18. Which of the following is a valid Boolean value in C++? 23. Which C++ keyword is used to define an enumeration?
a) True a) enum
b) False b) define
c) 1 c) enumerate
d) All of the above d) const
Answer: d) All of the above (In C++, true, false, and 1/0 are Answer: a) enum
valid boolean values) 24. What is the size of an enum in C++?
19. What will be the output of: a) 1 byte
cpp
b) 2 bytes
CopyEdit
bool b = -5; c) 4 bytes
cout << b; d) Depends on the compiler
a) 0 Answer: d) Depends on the compiler (Typically 4 bytes, but can
b) -5 vary)
c) 1 25. What is the difference between class and struct in C++?
d) Error a) struct members are public by default, class members are
Answer: c) 1 (Any nonzero value is treated as true in boolean private
context) b) class is faster than struct
20. What is the size of a bool in C++? c) struct does not support functions
a) 1 bit d) class cannot have member variables
b) 1 byte Answer: a) struct members are public by default, class
c) 2 bytes members are private
d) 4 bytes 1. Basics of Scope Resolution Operator
Answer: b) 1 byte
1. What is the purpose of the scope resolution operator (::) in C+ d) Garbage value
+? Answer: a) 10 (Using ::x accesses the global variable x)
a) To define a function inside a class 5. How can a global function be accessed if a local function has the
b) To access global variables or functions same name?
c) To allocate memory dynamically a) Using this->function_name();
d) To perform type conversion b) Using global.function_name();
Answer: b) To access global variables or functions c) Using ::function_name();
2. Which of the following correctly uses the scope resolution d) Using function_name::global();
operator? Answer: c) Using ::function_name();
a) class::function();
b) namespace::function(); 3. Scope Resolution Operator with Classes
c) object::function(); 6. The scope resolution operator (::) can be used to define a class
d) void function()::returnType; member function outside the class. Which syntax is correct?
Answer: b) namespace::function(); a) class_name::function_name() {}
3. In which of the following scenarios is the scope resolution b) class_name->function_name() {}
operator (::) NOT used? c) class_name::function_name;
a) Accessing a global variable when a local variable has the d) function_name::class_name() {}
same name Answer: a) class_name::function_name() {}
b) Accessing class static members 7. What is the output of the following program?
c) Calling a function inside the same class cpp
d) Defining a member function outside a class CopyEdit
Answer: c) Calling a function inside the same class #include <iostream>
using namespace std;
class Test {
2. Scope Resolution Operator with Global and Local Variables public:
4. What will be the output of the following program? static int x;
cpp };
CopyEdit int Test::x = 50;
#include <iostream> int main() {
using namespace std; cout << Test::x;
int x = 10; // Global variable return 0;
int main() { }
int x = 20; // Local variable a) 0
cout << ::x; b) 50
return 0;
} c) Compilation error
a) 10 d) Undefined behavior
b) 20 Answer: b) 50 (Test::x accesses the static member variable x of
c) Compilation error class Test)
8. The scope resolution operator is used for defining a static return 0;
}
member function outside a class. Which syntax is correct?
a) return_type class_name::static_function() {} a) 100
b) static return_type class_name::function_name() {} b) 200
c) return_type::class_name function() {} c) Compilation error
d) Undefined behavior
d) return_type function()::class_name {}
Answer: a) 100 (Using A::num accesses the variable from
Answer: a) return_type class_name::static_function() {}
namespace A)
9. Can a non-static class member be accessed using the scope
12. If a namespace has a function with the same name as a global
resolution operator (::)?
function, how can we call the global function?
a) Yes
a) ::function_name();
b) No
b) global::function_name();
Answer: b) No (Only static members of a class can be accessed
using ::) c) namespace::function_name();
d) function_name::global();
Answer: a) ::function_name();
4. Scope Resolution Operator with Namespaces
10. What is the correct way to access a function inside a namespace?
cpp 5. Miscellaneous Questions
CopyEdit 13. Which of the following cannot be accessed using the scope
namespace MyNamespace { resolution operator?
void display() { cout << "Hello"; }
a) Global variables
}
int main() { b) Class static members
_____ c) Local variables
} d) Namespace members
a) display(); Answer: c) Local variables (Local variables are accessed directly
b) MyNamespace->display(); without ::)
c) MyNamespace.display(); 14. The scope resolution operator (::) in C++ is a:
d) MyNamespace::display(); a) Unary operator
Answer: d) MyNamespace::display(); b) Binary operator
11. What will be the output of the following program? c) Ternary operator
cpp d) Assignment operator
CopyEdit Answer: a) Unary operator
#include <iostream>
using namespace std; 15. Can the scope resolution operator (::) be overloaded in C++?
namespace A { a) Yes
int num = 100; b) No
} Answer: b) No (Scope resolution operator cannot be overloaded in
int main() {
int num = 200;
C++)
cout << A::num;
16. The double colon (::) operator in C++: Answer: c) Integer (int) (It is a fundamental data type, not
a) Is used for input-output operations derived)
b) Is used to define class member functions outside the class 2. Which of the following is a derived data type in C++?
c) Is used for memory allocation a) Class
d) None of the above b) Enum
Answer: b) Is used to define class member functions outside the c) Pointer
class d) All of the above
17. What will be the output of the following code? Answer: d) All of the above
cpp 3. Derived data types in C++ are created using:
CopyEdit
a) Fundamental data types
#include <iostream>
using namespace std; b) Dynamic memory allocation
namespace A { c) Special operators
int x = 10; d) None of the above
} Answer: a) Fundamental data types
namespace B {
int x = 20;
} 2. Arrays in C++
int main() { 4. What will be the output of the following code?
cout << A::x << " " << B::x; cpp
return 0; CopyEdit
} #include <iostream>
a) 10 20 using namespace std;
b) 20 10 int main() {
int arr[5] = {1, 2, 3, 4, 5};
c) Compilation error cout << arr[2];
d) Undefined behavior return 0;
Answer: a) 10 20 }
a) 1
Conclusion b) 2
The scope resolution operator (::) is used for: c) 3
✅ Accessing global variables when local variables have the same name. d) Compilation error
✅ Accessing namespace members. Answer: c) 3 (Array index starts from 0, so arr[2] is 3)
✅ Defining class member functions outside a class. 5. What is the size of the following array in memory (assuming int
✅ Accessing static members of a class. is 4 bytes)?
cpp
1. Basics of Derived Data Types CopyEdit
1. Which of the following is not a derived data type in C++? int arr[10];
a) Array a) 10 bytes
b) Structure (struct) b) 20 bytes
c) Integer (int)
d) Pointer
c) 40 bytes 10. How do you allocate memory dynamically for an integer using a
d) 80 bytes pointer?
Answer: c) 40 bytes (Each int takes 4 bytes, so 10 * 4 = 40) a) int *ptr = new int;
6. What is the correct syntax for declaring a 2D array in C++? b) int ptr = new int;
a) int arr[][]; c) int ptr = (int) malloc(sizeof(int));
b) int arr[3][4]; d) int ptr = new(int);
c) array<int> arr[3][4]; Answer: a) int *ptr = new int;
d) int arr(3,4);
Answer: b) int arr[3][4]; 4. Structures and Classes in C++
11. Which keyword is used to define a structure in C++?
3. Pointers in C++ a) struct
7. What does a pointer store? b) class
a) Value of a variable c) record
b) Address of a variable d) table
c) Both a and b Answer: a) struct
d) None of the above 12. What is the difference between a struct and a class in C++?
Answer: b) Address of a variable a) struct members are public by default, class members are
8. What is the output of the following program? private
cpp b) class does not support functions
CopyEdit
#include <iostream> c) struct cannot have private members
using namespace std; d) struct is faster than class
int main() { Answer: a) struct members are public by default, class
int x = 10; members are private
int *ptr = &x;
cout << *ptr; 13. What will be the output of the following program?
return 0; cpp
} CopyEdit
#include <iostream>
a) Address of x using namespace std;
b) 10 struct Student {
c) Compilation error int id;
d) Garbage value string name;
};
Answer: b) 10 (Dereferencing the pointer gives the value of x) int main() {
9. What is the correct syntax for declaring a pointer to an integer? Student s1 = {1, "John"};
a) int ptr; cout << s1.name;
b) int *ptr; return 0;
}
c) pointer<int> ptr;
d) int &ptr; a) 1
b) John
Answer: b) int *ptr;
c) Compilation error b) int *ptr();
d) Undefined behavior c) int ptr*();
Answer: b) John d) int ptr();
Answer: a) int (*ptr)();
5. Enumerations in C++ 18. What will be the output of the following program?
14. What is an enum in C++? cpp
a) A user-defined data type that assigns names to integral values CopyEdit
#include <iostream>
b) A special function using namespace std;
c) A floating-point data type void hello() { cout << "Hello"; }
d) None of the above int main() {
Answer: a) A user-defined data type that assigns names to void (*ptr)() = hello;
ptr();
integral values return 0;
15. What is the default underlying type of an enum in C++? }
a) int a) Hello
b) char b) Compilation error
c) float c) Garbage value
d) bool d) None of the above
Answer: a) int Answer: a) Hello
16. What will be the output of the following code?
cpp Conclusion
CopyEdit
#include <iostream> Derived data types in C++ include:
using namespace std; ✅ Arrays → int arr[5]
enum Color { RED, GREEN, BLUE }; ✅ Pointers → int *ptr = &x;
int main() { ✅ Structures → struct Student { int id; };
Color c = GREEN;
cout << c; ✅ Classes → class MyClass { private: int data; };
return 0; ✅ Enumerations → enum Color { RED, GREEN, BLUE };
} ✅ Function Pointers → void (*ptr)();
a) GREEN 1. What is a function in C++?
b) 1 a) A block of code that performs a specific task
c) Compilation error b) A variable that stores data
d) Undefined behavior c) A special type of loop
Answer: b) 1 (enum values are assigned from 0 by default, so d) A header file
GREEN is 1) Answer: a) A block of code that performs a specific task
2. What is the correct syntax for declaring a function in C++?
6. Function Pointers in C++ a) void function_name;
17. Which of the following correctly declares a function pointer? b) return_type function_name();
a) int (*ptr)();
c) function_name return_type(); c) Parameters
d) void = function_name(); d) Function body
Answer: b) return_type function_name(); Answer: c) Parameters (Functions can have zero parameters)
3. What is the return type of a function that does not return any
value? 3. Function Parameters and Return Values
a) int 7. What will be the output of the following program?
b) void cpp
CopyEdit
c) float #include <iostream>
d) char using namespace std;
Answer: b) void int add(int a, int b) {
return a + b;
}
2. Function Definitions and Calls int main() {
4. What is the output of the following program? cout << add(5, 10);
cpp return 0;
CopyEdit }
#include <iostream>
a) 15
using namespace std;
void printMessage() { b) 5
cout << "Hello, World!"; c) 10
} d) Compilation error
int main() { Answer: a) 15
printMessage();
return 0;
8. How many values can a function return in C++?
} a) One
a) Hello, World! b) Two
b) Compilation error c) Multiple
c) No output d) Unlimited
d) Undefined behavior Answer: a) One (A function can return only one value directly but
Answer: a) Hello, World! can return multiple values using pointers, references, or
5. What is the correct way to call a function named calculate()? structures)
a) calculate; 9. What will happen if a function with a return type does not
b) calculate(); return any value?
c) void calculate(); a) Compilation error
d) int calculate(); b) Undefined behavior
Answer: b) calculate(); c) No output
6. Which of the following is not necessary when defining a d) The program runs normally
function? Answer: a) Compilation error
a) Function name
b) Return type
4. Function Overloading 14. Which keyword is used to declare an inline function?
10. What is function overloading in C++? a) inline
a) Defining multiple functions with the same name but different b) function
parameters c) static
b) Calling a function multiple times d) register
c) Using a function inside another function Answer: a) inline
d) Defining functions outside the main function 15. Which of the following is true about inline functions?
Answer: a) Defining multiple functions with the same name but a) They reduce function call overhead
different parameters b) They increase the size of the executable file
11. Which of the following function declarations correctly c) They may not be inlined if the function is too complex
represents function overloading? d) All of the above
a) int sum(int a, int b); and float sum(float x, float Answer: d) All of the above
y);
b) void print(int a); and void print(int a); 6. Recursion
c) char test(); and char test(int x); 16. What is recursion in C++?
d) Both (a) and (c) a) A function that calls itself
Answer: d) Both (a) and (c) b) A function that runs infinitely
12. Which of the following statements is true about function c) A function that is defined inside another function
overloading? d) A function that takes multiple arguments
a) Overloaded functions must have different return types Answer: a) A function that calls itself
b) Overloaded functions must have different numbers or types 17. What is required in a recursive function to prevent infinite
of parameters recursion?
c) Function overloading is not supported in C++ a) A return statement
d) Overloaded functions must have the same implementation b) A loop
Answer: b) Overloaded functions must have different numbers c) A base condition
or types of parameters d) A break statement
Answer: c) A base condition
5. Inline Functions 18. What is the output of the following recursive function?
13. What is an inline function in C++? cpp
a) A function with no parameters CopyEdit
#include <iostream>
b) A function that is defined inside main() using namespace std;
c) A function that is expanded at compile-time to reduce void countdown(int n) {
function call overhead if (n == 0) return;
d) A function that must return a value cout << n << " ";
Answer: c) A function that is expanded at compile-time to countdown(n - 1);
}
reduce function call overhead int main() {
countdown(3);
return 0; c) It is used to import header files
}
d) It is optional in every C++ program
a) 3 2 1 0 Answer: b) It is the entry point of execution for a C++ program
b) 3 2 1 2. Which of the following is the correct syntax for the main()
c) 1 2 3 function in C++?
d) Compilation error a) void main() { }
Answer: b) 3 2 1 b) int main() { return 0; }
c) main() { return 0; }
7. Default Arguments in Functions d) main(int argc, char argv) { }
19. What is the correct way to declare a function with default Answer: b) int main() { return 0; }
arguments?
3. What does return 0; in the main() function indicate?
a) void display(int a = 5);
a) The program encountered an error
b) void display(a = 5);
b) The program executed successfully
c) display(int a = 5); c) The program is incomplete
d) void display(int a) = 5; d) The program returns a garbage value
Answer: a) void display(int a = 5); Answer: b) The program executed successfully
20. In which part of the function definition are default arguments 4. What will happen if we do not include a return statement in the
specified? main() function?
a) Function call a) Compilation error
b) Function declaration b) Runtime error
c) Function body c) Implicit return of 0
d) Function return type d) The program will crash
Answer: b) Function declaration Answer: c) Implicit return of 0 (Starting from C++11, if main()
is declared as int main(), it will automatically return 0 if no
Conclusion explicit return statement is provided.)
C++ functions include:
✅ Function declaration & definition (return_type
2. Variants of main() Function
function_name(parameters))
5. Which of the following is a valid definition of main() in C++?
✅ Function overloading (same name, different parameters)
a) int main(int argc, char* argv[])
✅ Inline functions (inline keyword for efficiency)
b) void main()
✅ Recursion (a function calling itself)
c) float main()
✅ Default arguments (providing default values for parameters)
d) string main()
1. Basics of main() Function Answer: a) int main(int argc, char* argv[]) (It is the
1. What is the purpose of the main() function in a C++ program? correct signature for handling command-line arguments.)
a) It is used to define user-defined functions 6. What does argc represent in int main(int argc, char*
b) It is the entry point of execution for a C++ program argv[])?
a) Number of command-line arguments passed to the program
b) The first argument in the command line c) The main() function must always return an integer value
c) The total length of command-line arguments d) The main() function can be defined inside another function
d) The name of the program Answer: c) The main() function must always return an integer
Answer: a) Number of command-line arguments passed to the value (The standard definition of main() in C++ requires it to
program return int.)
7. What type of data is stored in argv[] in int main(int argc, 11. Which of the following statements is false about main()?
char* argv[])? a) main() function can have parameters
a) Integer values b) main() function can be overloaded
b) Floating-point numbers c) main() function is the first function executed in a C++
c) Strings (character arrays) program
d) Boolean values d) main() function can return an integer
Answer: c) Strings (character arrays) Answer: b) main() function can be overloaded (Overloading
8. If a program is run as follows: main() is not allowed in C++.)
bash
CopyEdit
./program Hello World 4. Special Cases Related to main()
What will be the value of argc? 12. Can main() be declared as static in C++?
a) 1 a) Yes
b) 2 b) No
c) 3 Answer: b) No (The main() function cannot be declared as
d) 4 static because it must be globally accessible for the operating
Answer: c) 3 (argc includes the program name as the first system to execute it.)
argument, so argc = 3.) 13. What is the correct way to handle command-line arguments in
main()?
3. Execution of main() Function a) int main(int args, string argv[])
9. What happens if we declare multiple main() functions in a b) int main(int argc, char** argv)
single C++ program? c) void main(char argc, int argv[])
a) The program executes successfully d) string main(int argc, char argv[][])
b) Compilation error occurs Answer: b) int main(int argc, char** argv)
c) Only the first main() function executes 14. What happens if main() is defined as void main() in C++?
d) The last main() function executes a) The program runs normally
Answer: b) Compilation error occurs b) The program generates a warning but still runs
10. Which of the following statements about main() is true? c) The program causes a compilation error
a) The main() function can be defined multiple times in a single d) The program runs but may behave unpredictably
program Answer: c) The program causes a compilation error (C++
b) The main() function cannot return a value standard does not allow void main(); it must return int.)
15. What is the default value returned by main() if no return
statement is provided?
a) 1 int main() {
cout << "Hello, C++!";
b) -1
main();
c) 0 return 0;
d) Undefined behavior }
Answer: c) 0 (In C++11 and later, main() implicitly returns 0 if a) Infinite recursion
no return statement is provided.) b) Hello, C++!
c) Compilation error
5. Advanced Concepts Related to main() d) Undefined behavior
16. What is the purpose of exit(0); in main()? Answer: a) Infinite recursion (Calling main() inside itself leads
a) It terminates the program execution immediately to infinite recursion until stack overflow occurs.)
b) It restarts the program
c) It returns control to the compiler Conclusion
d) It causes an infinite loop ✅ main() is the entry point of a C++ program.
Answer: a) It terminates the program execution immediately ✅ int main() is the correct syntax; void main() is incorrect in
17. Which header file is required for using exit() function in standard C++.
main()? ✅ argc and argv handle command-line arguments.
a) <iostream> ✅ Returning 0 means successful execution, while non-zero
b) <stdlib.h> values indicate errors.
c) <conio.h> ✅ main() cannot be overloaded or declared as static.
d) <stdio.h> 1. Basics of Function Prototyping
Answer: b) <stdlib.h> 1. What is a function prototype in C++?
18. If return 1; is used in main(), what does it indicate? a) A function definition
a) Program executed successfully b) A function declaration that specifies the function's return
b) Program terminated with an error type and parameters
c) Program ran but with warnings c) A function call
d) The return value is ignored d) A function with no return type
Answer: b) Program terminated with an error (Returning a non- Answer: b) A function declaration that specifies the function's
zero value usually indicates an error to the operating system.) return type and parameters
19. Can we call main() inside another function? 2. Where is the function prototype usually placed in a C++
a) Yes program?
b) No a) Inside the main() function
Answer: a) Yes (It is possible but not recommended as it can lead b) Before the main() function
to unexpected behavior.) c) After defining the function
20. What will be the output of the following program? d) At the end of the program
cpp Answer: b) Before the main() function
CopyEdit
#include <iostream> 3. What is the purpose of a function prototype?
using namespace std; a) To define the function logic
b) To inform the compiler about the function's existence before 8. What is the correct function prototype for the following function
it is used definition?
c) To execute a function cpp
CopyEdit
d) To allocate memory for the function
double multiply(int x, double y) {
Answer: b) To inform the compiler about the function's return x * y;
existence before it is used }
a) multiply(int x, double y);
2. Syntax of Function Prototyping b) double multiply(int, double);
4. Which of the following is a correct function prototype in C++? c) void multiply(int, double);
a) int sum(int, int); d) double multiply();
b) sum(int a, int b); Answer: b) double multiply(int, double);
c) void function() 9. Is it necessary to include variable names in a function
d) float (float, int); prototype?
Answer: a) int sum(int, int); a) Yes, always
5. What should a function prototype always include? b) No, only data types are needed
a) The function body c) Yes, otherwise the program won't compile
b) The function call d) Only for void functions
c) The function return type and parameter types Answer: b) No, only data types are needed
d) The function definition inside main() 10. Identify the incorrect function prototype:
Answer: c) The function return type and parameter types a) int add(int, int);
6. What happens if a function is called before its prototype is b) void display();
declared? c) float calculate(float, int x);
a) The program runs normally d) char getChar();
b) The program gives a compilation error Answer: c) float calculate(float, int x); (Variable names are
c) The function is ignored optional, but if one parameter has a name, all should have names or none
d) The function automatically defines itself should have names.)
Answer: b) The program gives a compilation error
7. Which of the following is incorrect about function prototypes? 4. Function Prototyping and Overloading
a) The function prototype can be placed inside main() 11. Which of the following correctly declares overloaded function
b) The function prototype must specify the return type prototypes?
c) The function prototype helps the compiler in function calls a)
d) The function prototype is not necessary for function cpp
definitions that come before main() CopyEdit
int display(int, int);
Answer: a) The function prototype can be placed inside main()
float display(float, float);
b)
3. Function Prototyping with Parameters cpp
CopyEdit
int display(int, int); #include <iostream>
int display(int, int); using namespace std;
c) int add(int a, int b = 5) {
cpp return a + b;
CopyEdit }
int display(int); int main() {
float display(int); cout << add(10);
return 0;
d) Both (a) and (c)
}
Answer: d) Both (a) and (c)
a) 10
12. What does function overloading mean in relation to function
b) 15
prototypes?
c) Compilation error
a) Declaring multiple functions with the same name but
d) Garbage value
different parameters
Answer: b) 15
b) Writing multiple function prototypes with different return
types
c) Writing multiple function definitions without prototypes
6. Function Prototype and Memory Management
d) Using the static keyword in function prototypes 16. Does a function prototype allocate memory for the function?
a) Yes
Answer: a) Declaring multiple functions with the same name but
b) No
different parameters
c) Only if it has default parameters
13. What will happen if two function prototypes have the same
d) Only if it is declared inside main()
name and parameters but different return types?
a) The compiler will choose the most appropriate return type Answer: b) No (A function prototype only informs the compiler about the
b) The compiler will generate an error function, but does not allocate memory.)
c) The function will be executed with both return types 17. Can a function prototype be declared multiple times in the same
d) The program will execute normally program?
Answer: b) The compiler will generate an error a) Yes, but only before defining the function
b) No, it will cause an error
c) Yes, but it is unnecessary
5. Function Prototyping and Default Arguments d) No, the program will crash
14. Which of the following function prototypes correctly uses
Answer: c) Yes, but it is unnecessary
default arguments?
18. What is the benefit of using function prototypes in large
a) int sum(int a = 10, int b);
projects?
b) int sum(int a, int b = 10);
a) Helps with modular programming
c) int sum(int a = 10, int b = 20); b) Reduces compilation time
d) Both (b) and (c) c) Makes debugging easier
Answer: d) Both (b) and (c) (Default arguments must be assigned from d) All of the above
right to left, not in between parameters.) Answer: d) All of the above
15. What will be the output of the following program?
cpp
CopyEdit 7. Special Cases Related to Function Prototyping
19. Can a function prototype be declared inside a class in C++? c) int func(int *x)
a) Yes d) Both (b) and (c)
b) No Answer: d) Both (b) and (c) (References and pointers can be used
Answer: a) Yes (Function prototypes are used in classes for declaring for call by reference.)
member functions.) 3. What happens when a function uses call by reference?
20. What will happen if a function prototype and its definition have a) The function operates on the actual arguments
different return types? b) The function works with copies of the arguments
cpp c) The function cannot modify the original values
CopyEdit
int square(int x);
d) The function does not return any value
float square(int x) { Answer: a) The function operates on the actual arguments
return x * x;
} 2. Syntax and Usage
a) Compilation error 4. Which of the following correctly implements call by reference?
b) The function executes normally a)
c) The function will return both int and float cpp
d) The program will run but return an incorrect result CopyEdit
void change(int x) {
Answer: a) Compilation error
x = 10;
}
Conclusion b)
✅ Function prototypes inform the compiler about function signatures cpp
✅ They are usually declared before main() CopyEdit
void change(int &x) {
✅ Overloading is allowed but must have different parameters
x = 10;
✅ Default arguments must be assigned from right to left }
✅ A function prototype does not allocate memory c)
1. Basics of Call by Reference cpp
1. What is call by reference in C++? CopyEdit
void change(int *x) {
a) Passing a copy of a variable to a function *x = 10;
b) Passing the memory address of a variable to a function }
c) Passing values using pointers only d) Both (b) and (c)
d) A way to create new variables inside a function Answer: d) Both (b) and (c)
Answer: b) Passing the memory address of a variable to a 5. What will be the output of the following program?
function cpp
2. Which of the following function parameter types enables call by CopyEdit
reference in C++? #include <iostream>
using namespace std;
a) int func(int x) void modify(int &x) {
b) int func(int &x) x = 20;
}
int main() { #include <iostream>
int num = 10; using namespace std;
modify(num); void change(int x) {
cout << num; x = 50;
return 0; }
} int main() {
a) 10 int num = 30;
change(num);
b) 20
cout << num;
c) Compilation error return 0;
d) Garbage value }
Answer: b) 20 (The function modifies num directly since it's passed a) 30
by reference.) b) 50
6. What is the correct way to declare a function that uses call by c) Compilation error
reference? d) Garbage value
a) void fun(int &a, int &b); Answer: a) 30 (Since the function uses call by value, it does not
b) void fun(int a&, int b&); modify num.)
c) void fun(int *a, int *b); 10. Which of the following statements is true?
d) void fun(int a, int b); a) Call by reference is slower than call by value
Answer: a) void fun(int &a, int &b); b) Call by reference can modify the original variable
7. Can we return a reference from a function in C++? c) Call by value uses less memory than call by reference
a) Yes d) Call by reference cannot use pointers
b) No Answer: b) Call by reference can modify the original variable
Answer: a) Yes (A function can return a reference, which can be
used to modify the original value.) 4. Call by Reference and Pointers
11. How is call by reference different from call by pointer?
3. Difference Between Call by Reference and Call by Value a) Call by reference requires dereferencing
8. What is the main difference between call by value and call by b) Call by reference uses & while call by pointer uses *
reference? c) Call by reference creates a new copy of a variable
a) Call by value does not modify the original variable, while call d) Call by reference cannot modify the original value
by reference does Answer: b) Call by reference uses & while call by pointer uses *
b) Call by value is faster than call by reference 12. Which of the following uses pointers instead of references for
c) Call by reference creates a new variable, whereas call by call by reference?
value does not a) void func(int &x);
d) Call by reference uses pointers, while call by value does not b) void func(int *x);
Answer: a) Call by value does not modify the original variable, c) void func(int x);
while call by reference does d) void func(int &x, int y);
9. What will be the output of this program? Answer: b) void func(int *x);
cpp
13. What will be the output of this program?
CopyEdit
cpp c) Only if specified using &
CopyEdit
#include <iostream>
d) Only if passed as a pointer
using namespace std; Answer: d) Only if passed as a pointer (Arrays decay into
void change(int *x) { pointers when passed to functions.)
*x = 100; 17. What happens if a function returns a reference to a local
} variable?
int main() {
int num = 50; a) The program crashes
change(&num); b) The program gives a compilation error
cout << num; c) It results in undefined behavior
return 0; d) The function executes normally
}
Answer: c) It results in undefined behavior (The local variable is
a) 50 destroyed when the function ends.)
b) 100 18. What will be the output of this program?
c) Compilation error cpp
d) Garbage value CopyEdit
Answer: b) 100 (The function modifies num using call by pointer #include <iostream>
using namespace std;
reference.) int& getValue() {
int x = 10;
5. Advantages and Disadvantages return x;
14. What is an advantage of using call by reference? }
int main() {
a) Saves memory by avoiding copies of variables int y = getValue();
b) Prevents modification of variables cout << y;
c) Makes debugging easier return 0;
d) Always results in faster execution }
Answer: a) Saves memory by avoiding copies of variables a) 10
15. What is a disadvantage of using call by reference? b) Compilation error
a) Cannot modify variables c) Undefined behavior
b) Requires more memory d) Garbage value
c) Increases risk of unintended modifications Answer: d) Garbage value (Returning a reference to a local
d) Slows down execution variable leads to undefined behavior.)
Answer: c) Increases risk of unintended modifications
7. Call by Reference with Objects
6. Special Cases in Call by Reference 19. Can objects be passed by reference?
16. Can arrays be passed by reference in C++? a) Yes
a) Yes, always b) No
b) No Answer: a) Yes (Passing objects by reference avoids unnecessary
copying.)
20. Which is the correct way to pass an object by reference? Answer: d) Both (b) and (c) (Overloading requires different
a) void modify(Object obj); parameter lists but not different return types.)
b) void modify(Object *obj); 3. Which of the following function declarations will cause a
c) void modify(Object &obj); compilation error?
d) Both (b) and (c)` a) void display(int x);
Answer: d) Both (b) and (c) b) void display(float y);
c) int display(int x);
Conclusion d) Both (b) and (c)
✅ Call by reference allows modifying original variables Answer: c) int display(int x); (Return type alone cannot
✅ Uses & in function parameters differentiate overloaded functions.)
✅ Can be implemented with references or pointers 4. What will be the output of the following program?
✅ Avoids memory overhead but may lead to unintended modifications cpp
CopyEdit
1. Function Overloading in C++ #include <iostream>
1. What is function overloading in C++? using namespace std;
a) Defining multiple functions with the same name but different void print(int x) {
return types cout << "Integer: " << x;
}
b) Defining multiple functions with the same name but different void print(double y) {
parameter lists cout << "Double: " << y;
c) Using a function with multiple return statements }
d) Writing a function inside another function int main() {
print(5);
Answer: b) Defining multiple functions with the same name but
return 0;
different parameter lists }
2. Which of the following correctly overloads a function? a) Integer: 5
a) b) Double: 5.0
cpp
CopyEdit c) Compilation error
int add(int a, int b); d) No output
float add(int a, int b); Answer: a) Integer: 5 (Integer value matches the print(int)
b) function.)
cpp
CopyEdit
int add(int a, int b); 2. Recursion in C++
float add(float a, float b); 5. What is recursion in C++?
c) a) A function that calls itself
cpp b) A function that calls another function
CopyEdit c) A function that runs in an infinite loop
int add(int a, int b);
int add(int a, int b, int c);
d) A function with multiple return statements
Answer: a) A function that calls itself
d) Both (b) and (c)
6. What is the base condition in a recursive function? Answer: c) O(2^n) (Each function call branches into two
a) A condition where recursion stops recursive calls, leading to exponential complexity.)
b) A condition where recursion continues indefinitely
c) The first function call in recursion 3. Inline Functions in C++
d) The last function call before returning 10. What is an inline function in C++?
Answer: a) A condition where recursion stops a) A function that runs in a loop
7. What is the output of the following program? b) A function that is expanded at the call site instead of being
cpp executed normally
CopyEdit
#include <iostream> c) A function that can be overloaded
using namespace std; d) A function that does not return any value
void count(int n) { Answer: b) A function that is expanded at the call site instead of
if (n == 0) being executed normally
return; 11. What is the main advantage of inline functions?
cout << n << " ";
count(n - 1); a) Reduces execution time by avoiding function call overhead
} b) Saves memory
int main() { c) Can be defined inside another function
count(3); d) Uses recursion
return 0;
}
Answer: a) Reduces execution time by avoiding function call
overhead
a) 3 2 1
12. Which of the following declares an inline function correctly?
b) 1 2 3
a)
c) Compilation error cpp
d) Infinite loop CopyEdit
Answer: a) 3 2 1 (Function calls itself, decreasing n until n == 0.) inline void display() {
8. What is tail recursion? cout << "Hello";
}
a) Recursion where the function call is the last operation before
returning b)
cpp
b) Recursion where the function is called at the beginning CopyEdit
c) Recursion that never terminates void inline display() {
d) Recursion that uses a loop internally cout << "Hello";
Answer: a) Recursion where the function call is the last }
operation before returning c)
cpp
9. What is the time complexity of the recursive Fibonacci function? CopyEdit
a) O(n) void display() inline {
b) O(log n) cout << "Hello";
c) O(2^n) }
d) O(n!) d) None of the above
Answer: a)
cpp ✅ Inline Functions: Functions expanded at the call site to improve
CopyEdit
performance.
inline void display() {
cout << "Hello"; 1. Basics of Inline Functions
} 1. What is an inline function in C++?
13. Can all functions be made inline? a) A function that is always compiled first
a) Yes b) A function that is expanded at the call site instead of making
b) No a function call
Answer: b) No (Recursive and large functions should not be c) A function that must return an integer value
inline due to memory overhead.) d) A function that is executed in parallel
14. What is the effect of an inline function on program size? Answer: b) A function that is expanded at the call site instead of
a) Decreases program size making a function call
b) Increases program size 2. Which keyword is used to define an inline function in C++?
c) No effect on program size a) fast
d) Random effect depending on compiler b) inline
Answer: b) Increases program size (Code expansion at multiple c) static
call sites increases binary size.) d) macro
15. What is the output of the following program? Answer: b) inline
cpp 3. What is the main advantage of using an inline function?
CopyEdit a) Reduces memory usage
#include <iostream>
using namespace std; b) Reduces function call overhead
inline int square(int x) { c) Increases execution time
return x * x; d) Saves disk space
} Answer: b) Reduces function call overhead
int main() {
cout << square(4);
4. Which of the following function definitions correctly declares an
return 0; inline function?
} a)
a) 8 cpp
CopyEdit
b) 16 void inline display() {
c) 4 cout << "Hello";
d) Compilation error }
Answer: b) 16 (Function square(4) expands to 4 * 4 at the call b)
site.) cpp
CopyEdit
inline void display() {
Conclusion cout << "Hello";
✅ Function Overloading: Multiple functions with the same name but }
different parameters. c)
✅ Recursion: A function that calls itself, must have a base case. cpp
CopyEdit cout << add(5, 10);
void display() inline { return 0;
cout << "Hello"; }
} a) 15
d) None of the above b) Compilation error
Answer: b) inline void display() { cout << "Hello"; } c) Undefined behavior
d) 5
2. Working of Inline Functions Answer: a) 15
5. When an inline function is called in a C++ program, what
happens? 3. Advantages and Disadvantages
a) The compiler replaces the function call with the function 9. Which of the following is not an advantage of inline functions?
definition a) Faster execution due to no function call overhead
b) The function is called normally like other functions b) Saves memory by reducing function size
c) The function executes in a separate memory space c) Improves program readability
d) The function executes in a loop d) Increases code size
Answer: a) The compiler replaces the function call with the Answer: d) Increases code size
function definition 10. What is a major disadvantage of inline functions?
6. Which of the following function calls benefits the most from a) They cannot be used in a program
being inline? b) They increase the execution time
a) A function that is called once in a program c) They increase the code size due to code expansion
b) A function that is large and complex d) They slow down the compiler
c) A small function that is called frequently Answer: c) They increase the code size due to code expansion
d) A function with recursion 11. When should you avoid using inline functions?
Answer: c) A small function that is called frequently a) When the function is small and frequently used
7. What happens if an inline function contains a loop or a complex b) When the function is complex or large
operation? c) When the function has a single return statement
a) The compiler will always make it inline d) When the function is used inside a loop
b) The compiler may ignore the inline request Answer: b) When the function is complex or large
c) The function will not compile 12. What is the effect of using an inline function inside a loop?
d) The function will execute twice a) It reduces execution time
Answer: b) The compiler may ignore the inline request b) It may increase code size due to multiple expansions
8. What is the output of the following program? c) It removes the loop execution overhead
cpp d) It forces the compiler to generate assembly code
CopyEdit Answer: b) It may increase code size due to multiple expansions
#include <iostream>
using namespace std;
inline int add(int a, int b) { 4. Compiler Behavior and Restrictions
return a + b; 13. Can an inline function be recursive?
} a) Yes, always
int main() {
b) No, never b) Use them only for simple, frequently used functions
c) Yes, but the compiler will likely ignore the inline request c) Use them only with static variables
d) Only for tail recursion d) Use them to replace macros in C++
Answer: c) Yes, but the compiler will likely ignore the inline request Answer: b) Use them only for simple, frequently used functions
14. What is the effect of using static variables inside an inline function? 19. What happens if an inline function is modified in a header file?
a) The function behaves like a normal function a) Only the modified function is recompiled
b) The function will not compile b) All files including the header file must be recompiled
c) The function can only be used once c) No changes occur in the program
d) The compiler will always make the function inline d) The function cannot be modified once declared inline
Answer: a) The function behaves like a normal function Answer: b) All files including the header file must be recompiled
15. Which of the following statements about inline functions is true? 20. Which of the following cannot be declared inline?
a) The compiler must always expand inline functions a) Member functions of a class
b) Inline functions increase execution time b) Friend functions
c) The compiler may or may not expand inline functions c) Virtual functions
d) Inline functions can contain static variables d) Both (b) and (c)
Answer: c) The compiler may or may not expand inline functions Answer: d) Both (b) and (c) (Friend functions and virtual functions
cannot be inline.)
5. Special Cases and Use Cases
16. Which of the following functions is not suitable for inlining? Conclusion
a) inline int sum(int a, int b) { return a + b; } ✅ Inline functions are used to reduce function call overhead.
b) inline void print() { cout << "Hello"; } ✅ They are best suited for small, frequently used functions.
c) ✅ The compiler may ignore inline requests if the function is too
cpp
CopyEdit
complex.
inline void loop() { ✅ Disadvantage: Increases code size if used excessively.
for(int i = 0; i < 1000; i++) 1. Basics of Default Arguments
cout << i << " "; 1. What are default arguments in C++?
} a) Arguments that are automatically passed to all functions
d) inline double multiply(double x, double y) { return x * y; b) Arguments that are assigned default values in the function
}
declaration
Answer: c) The function containing a loop (loop()) c) Arguments that must always be provided explicitly
17. Where are inline functions commonly used? d) Arguments that change their values at runtime
a) In large functions with multiple return values Answer: b) Arguments that are assigned default values in the
b) In small functions that are called frequently
function declaration
c) In functions that require dynamic memory allocation
2. Where can default arguments be specified?
d) In recursive functions
a) Only in the function definition
Answer: b) In small functions that are called frequently
b) Only in the function declaration
18. What is the best practice for using inline functions?
c) In both function definition and declaration
a) Use them for all functions to speed up execution
d) Inside the main() function
Answer: b) Only in the function declaration using namespace std;
void greet(string name = "Guest") {
3. What is the correct way to specify default arguments?
cout << "Hello, " << name;
a) }
cpp int main() {
CopyEdit greet();
void display(int a = 5) { return 0;
cout << a; }
}
a) Compilation error
b)
cpp b) No output
CopyEdit c) Hello, Guest
void display(int a) { d) Hello, (blank space)
a = 5; Answer: c) Hello, Guest
cout << a;
6. Can some parameters have default arguments while others
}
don’t?
c)
cpp a) Yes
CopyEdit b) No
void display(int a); Answer: a) Yes
void display(int a = 5) { 7. What is the output of the following program?
cout << a; cpp
} CopyEdit
d) #include <iostream>
cpp using namespace std;
CopyEdit void add(int a, int b = 10) {
void display(int a = 5); cout << a + b;
Answer: d) void display(int a = 5); (Default arguments }
should be specified in the function declaration.) int main() {
add(5);
return 0;
2. Working of Default Arguments }
4. What happens if a function with default arguments is called a) 15
without providing the argument? b) Compilation error
a) It results in a compilation error c) 5
b) The function assigns a random value d) 10
c) The function uses the provided default argument value Answer: a) 15 (The second parameter b is assigned the default
d) The function call is ignored value 10.)
Answer: c) The function uses the provided default argument
value 3. Rules and Restrictions
5. What is the output of the following program? 8. Which of the following is incorrect?
cpp
CopyEdit
a)
cpp
#include <iostream>
CopyEdit Answer: a) 5 15 30 (Only a and b are explicitly passed; c takes the default
void func(int a = 10, int b = 20);
value.)
b)
cpp
CopyEdit 4. Advanced Concepts
void func(int a = 10, int b); 12. Can constructor functions have default arguments?
c) a) Yes
cpp b) No
CopyEdit Answer: a) Yes
void func(int a, int b = 20); 13. Can a default argument be a function call?
d) Both (b) and (c) a) Yes
Answer: b) void func(int a = 10, int b); (Once a default b) No
argument is provided, all subsequent parameters must have Answer: a) Yes
default values.) 14. Which of the following is allowed?
9. Can a function have multiple default arguments? a)
a) Yes cpp
b) No CopyEdit
int sum(int a = 5, int b);
Answer: a) Yes
10. If a function has multiple parameters with default arguments, b)
cpp
how are the values assigned when calling the function? CopyEdit
a) From left to right void show(int a = 10, int b = 20);
b) From right to left c)
c) Randomly cpp
d) It depends on the compiler CopyEdit
void display(int x = 5, float y = 2.5);
Answer: a) From left to right
d) Both (b) and (c)
11. What is the output of the following program?
cpp Answer: d) Both (b) and (c)
CopyEdit 15. Can default arguments be overridden in derived classes?
#include <iostream> a) Yes
using namespace std; b) No
void display(int a = 10, int b = 20, int c = 30) {
cout << a << " " << b << " " << c;
Answer: a) Yes
}
int main() { 5. Comparison with Function Overloading
display(5, 15); 16. Which of the following is true regarding default arguments and function
return 0; overloading?
}
a) Default arguments and function overloading can be used together
a) 5 15 30 b) Function overloading cannot be used with default arguments
b) 10 15 30 c) Default arguments take precedence over function overloading
c) 5 20 30 d) Function overloading takes precedence over default arguments
d) Compilation error
Answer: a) Default arguments and function overloading can be used 20. What is best practice when using default arguments?
together a) Use them for frequently used default values
17. Which of the following function definitions will cause ambiguity if called b) Use them only with overloaded functions
as print(10)? c) Avoid them for performance reasons
a) d) Always use them in the function definition
cpp Answer: a) Use them for frequently used default values
CopyEdit
void print(int x, int y = 5);
void print(int x); Conclusion
b) ✅ Default arguments allow functions to be called with fewer
cpp parameters.
CopyEdit ✅ They must be declared in the function prototype, not the definition.
void print(int x, float y = 5.5); ✅ Default values are assigned from left to right.
void print(int x, int y);
✅ Used wisely, they reduce redundant function overloading.
c)
cpp 1. Basics of const Arguments
CopyEdit 1. What does the const keyword do when used with function
void print(int x); arguments in C++?
void print(float y); a) Allows the argument to be modified inside the function
d) None of the above b) Prevents the argument from being modified inside the
Answer: a) void print(int x, int y = 5); void print(int x); function
(Calling print(10) creates ambiguity between both function signatures.) c) Ensures the function always returns the same value
d) Forces the argument to be a constant literal
6. Miscellaneous Answer: b) Prevents the argument from being modified inside
18. What is the default argument for this function if called with func(2);? the function
cpp
2. What is the correct syntax for passing a constant integer
CopyEdit
void func(int a, int b = 7, int c = 9); argument to a function?
a) 2, 7, 9 a) void display(const int x);
b) 2, 9, 7 b) void display(int const x);
c) 2, 0, 9 c) void display(int x) const;
d) Compilation error d) Both (a) and (b)
Answer: a) 2, 7, 9 Answer: d) Both (a) and (b) (Both const int x and int const
19. What happens if you specify a default argument inside both the x are valid ways to declare a constant argument.)
function declaration and definition? 3. What happens if you try to modify a const argument inside a
a) Compilation error function?
b) The definition's default argument is used a) It gets modified successfully
c) The declaration's default argument is used b) It causes a compile-time error
d) Undefined behavior c) It produces undefined behavior
Answer: a) Compilation error d) The compiler ignores the modification
Answer: b) It causes a compile-time error b) No, it is not allowed
c) Yes, but it must be initialized inside the function
2. Working with const Arguments d) No, because pointers cannot be constant
4. What is the correct way to pass a constant reference argument Answer: a) Yes, but the value it points to cannot be modified
to a function? 8. How do you declare a constant pointer to an integer?
a) void show(int &x); a) int *const ptr;
b) void show(const int &x); b) const int *ptr;
c) void show(int *x); c) const int *const ptr;
d) void show(const int x); d) Both (a) and (c)
Answer: b) void show(const int &x); (Passing by const Answer: d) Both (a) and (c)
reference avoids unnecessary copies while preventing
modifications.) 3. const Functions with const Arguments
5. What is the advantage of passing arguments as const 9. What does it mean when a function is declared as const?
references? a) The function cannot return values
a) It avoids unnecessary copies of objects b) The function cannot modify member variables of the class
b) It allows modification of the argument c) The function cannot take arguments
c) It increases memory usage d) The function cannot be overloaded
d) It makes the function execute faster Answer: b) The function cannot modify member variables of the
Answer: a) It avoids unnecessary copies of objects class
6. What is the output of the following code? 10. Which function definition is correct when using const
cpp arguments and const member functions?
CopyEdit a)
#include <iostream> cpp
using namespace std; CopyEdit
void print(const int x) { class Example {
cout << x; void show(int x) const;
} };
int main() {
int num = 10; b)
print(num); cpp
return 0; CopyEdit
} class Example {
void show(const int x);
a) 10 };
b) Compilation error c)
c) Garbage value cpp
d) Undefined behavior CopyEdit
Answer: a) 10 class Example {
void show(const int x) const;
7. Can a const argument be passed by pointer? };
a) Yes, but the value it points to cannot be modified d) All of the above
Answer: d) All of the above 14. What happens if we try to modify a const argument passed as a
11. What is the output of the following code? pointer to const int?
cpp cpp
CopyEdit CopyEdit
#include <iostream> void show(const int *ptr) {
using namespace std; *ptr = 20;
class Example { }
public: a) Compilation error
void show(const int x) {
cout << x;
b) The value is modified
} c) The pointer is modified
}; d) No effect
int main() { Answer: a) Compilation error
Example obj;
obj.show(5);
return 0; 5. Special Cases and Miscellaneous
} 15. Can const arguments be used in constructor functions?
a) 5 a) Yes
b) Compilation error b) No
c) Undefined behavior Answer: a) Yes
d) No output 16. What is best practice when using const arguments?
Answer: a) 5 a) Use them when passing large objects by reference to avoid
modification
4. const Pointers and const Parameters b) Use them to reduce memory usage
12. What is the difference between const int *ptr and int *const c) Avoid them in all cases
ptr? d) Use them only for primitive data types
a) const int *ptr means the pointer cannot change Answer: a) Use them when passing large objects by reference to
b) int *const ptr means the value pointed to cannot change avoid modification
c) const int *ptr means the value pointed to cannot change 17. What is the correct way to pass a const object to a function?
d) Both are the same a) void func(Object obj);
Answer: c) const int *ptr means the value pointed to cannot b) void func(const Object obj);
change c) void func(const Object &obj);
13. What is the correct syntax for a constant pointer to a constant d) void func(Object &obj);
integer? Answer: c) void func(const Object &obj);
a) int const * const ptr; 18. What happens if a const function tries to modify a member
b) const int const *ptr; variable of a class?
c) const int *ptr; a) It will compile successfully
d) int *const ptr; b) It will cause a compile-time error
Answer: a) int const * const ptr; c) It will result in runtime error
d) The compiler will ignore the modification
Answer: b) It will cause a compile-time error b) Defining multiple functions with different names but the
19. Can const functions be overloaded with non-const functions? same parameters
a) Yes c) Using a function multiple times in the same program
b) No d) Overloading a function beyond its normal capacity
Answer: a) Yes Answer: a) Defining multiple functions with the same name but
20. What is the output of the following code? different parameters
cpp 2. What differentiates overloaded functions in C++?
CopyEdit a) The return type of the function
#include <iostream>
using namespace std; b) The number and type of parameters
class A { c) The name of the function
public: d) The scope of the function
void show() const { Answer: b) The number and type of parameters
cout << "Const Function";
}
3. Which of the following is correct function overloading?
void show() { a)
cout << "Non-Const Function"; cpp
} CopyEdit
}; void display(int x);
int main() { void display(float x);
A obj; b)
obj.show(); cpp
return 0; CopyEdit
} int display(int x);
a) Const Function void display(int x);
b) Non-Const Function c)
cpp
c) Compilation error CopyEdit
d) Undefined behavior void display();
Answer: b) Non-Const Function (Non-const version is called for void display();
non-const objects.) d) None of the above
Answer: a) void display(int x); void display(float x);
Conclusion 4. What is the output of the following code?
✅ const arguments prevent modification inside functions. cpp
CopyEdit
✅ Passing const references avoids unnecessary copies. #include <iostream>
✅ const functions prevent modifying class members. using namespace std;
✅ Using const improves code safety and optimization. void print(int x) {
cout << "Integer: " << x;
1. Basics of Function Overloading }
1. What is function overloading in C++? void print(double x) {
a) Defining multiple functions with the same name but different cout << "Double: " << x;
parameters }
int main() { Answer: a) void show(int a); void show(double b);
print(5);
return 0;
} 3. Function Overloading with Default Arguments
a) Integer: 5 8. What happens if a function is overloaded with only a default
b) Double: 5.0 argument difference?
c) Compilation error a) The program runs successfully
d) Undefined behavior b) The compiler gives an error
Answer: a) Integer: 5 c) Only the first function is used
d) The function with a default argument is ignored
2. Function Overloading Rules Answer: b) The compiler gives an error
5. Can function overloading be done by changing only the return 9. Identify the error in the following overloaded function
type? definitions:
cpp
a) Yes CopyEdit
b) No void show(int x, int y = 10);
Answer: b) No void show(int x);
6. What happens when two functions have the same name and a) There is no error
same parameter list, but different return types? b) The compiler cannot differentiate the function calls
a) The program runs successfully c) The default argument creates ambiguity
b) The compiler gives an error d) Both (b) and (c)
c) The function with a larger return type is called Answer: d) Both (b) and (c)
d) The compiler automatically chooses one function
Answer: b) The compiler gives an error 4. Function Overloading vs Function Overriding
7. Which of the following is valid function overloading? 10. What is the difference between function overloading and
a) function overriding?
cpp a) Function overloading occurs in the same class, function
CopyEdit
void show(int a); overriding occurs in inheritance
void show(double b); b) Function overloading occurs in inheritance, function
b) overriding occurs in the same class
cpp c) Function overloading uses virtual keyword
CopyEdit d) Function overriding cannot change the function signature
int show(int a);
void show(int a);
Answer: a) Function overloading occurs in the same class,
c) function overriding occurs in inheritance
cpp 11. What keyword is used for function overriding but NOT for
CopyEdit function overloading?
void show(int a); a) virtual
int show(int a, int b = 10); b) inline
d) None of the above
c) static b)
d) friend cpp
CopyEdit
Answer: a) virtual void show(int x);
void show(int x, int y = 5);
5. Function Overloading and Type Promotion c)
12. What happens if there is no exact match for an overloaded cpp
function call? CopyEdit
void show(int x);
a) The compiler selects the closest matching function int show(int x);
b) The compiler gives an error d) None of the above
c) The function with the largest parameter type is selected Answer: a) void show(int *ptr); void show(int x);
d) The program runs without output 15. What is the output of the following program?
Answer: a) The compiler selects the closest matching function cpp
13. What is the output of the following program? CopyEdit
cpp #include <iostream>
CopyEdit using namespace std;
#include <iostream> void show(int x) {
using namespace std; cout << "Integer";
void show(int x) { }
cout << "Integer"; void show(int *x) {
} cout << "Pointer";
void show(double x) { }
cout << "Double"; int main() {
} int num = 10;
int main() { show(&num);
show(5.5); return 0;
return 0; }
} a) Integer
a) Integer b) Pointer
b) Double c) Compilation error
c) Compilation error d) Undefined behavior
d) Undefined behavior Answer: b) Pointer
Answer: b) Double
7. Miscellaneous Questions
6. Function Overloading with Pointers 16. Can constructor functions be overloaded?
14. Which of the following function overloads is valid? a) Yes
a) b) No
cpp
CopyEdit
Answer: a) Yes
void show(int *ptr); 17. What is best practice when using function overloading?
void show(int x); a) Use it only when functions perform similar tasks
b) Use it for every function to reduce code size 2. Which of the following is required for a recursive function to
c) Use it to completely replace function overriding work properly?
d) Avoid it because it makes code difficult to read a) A loop inside the function
Answer: a) Use it only when functions perform similar tasks b) A goto statement
18. What happens if two overloaded functions have ambiguous c) A base condition to stop recursion
calls? d) Multiple return statements
a) The program runs successfully Answer: c) A base condition to stop recursion
b) The compiler gives an error 3. What happens if a recursive function does not have a base case?
c) The function with a larger data type is chosen a) It executes normally
d) The function with a smaller data type is chosen b) It runs indefinitely until the system crashes
Answer: b) The compiler gives an error c) It executes a finite number of times
19. Can main() be overloaded in C++? d) It stops automatically after 10 iterations
a) Yes Answer: b) It runs indefinitely until the system crashes
b) No 4. What is direct recursion?
Answer: b) No a) When a function calls another function
20. Which of the following cannot be overloaded? b) When a function calls itself
a) + operator c) When a function calls itself with different parameters
b) << operator d) When a function is executed in a loop
c) = operator Answer: b) When a function calls itself
d) :: (Scope resolution operator)
Answer: d) :: (Scope resolution operator) 2. Types of Recursion
5. What is indirect recursion?
Conclusion a) When a function calls another function, which then calls the
✅ Function overloading allows multiple functions with the same name first function
but different parameters. b) When a function calls itself directly
✅ The return type cannot differentiate overloaded functions. c) When a function is executed in a loop
✅ Default arguments can cause ambiguity when combined with d) When a function calls itself more than once
overloading. Answer: a) When a function calls another function, which then
✅ Overloading occurs in the same class, whereas overriding happens in calls the first function
inheritance. 6. What is tail recursion?
1. Basics of Recursion a) A recursive function where the recursive call is the last
1. What is recursion in C++? operation in the function
a) A function that calls itself b) A recursive function that calls multiple other functions
b) A function that is executed in a loop c) A recursive function that has a loop inside it
c) A function with multiple return statements d) A recursive function that does not return anything
d) A function that cannot be called directly Answer: a) A recursive function where the recursive call is the
Answer: a) A function that calls itself last operation in the function
7. Which of the following is not a type of recursion? return 0;
}
a) Tail recursion
b) Head recursion a) 3 2 1 0
c) Multi-thread recursion b) 0 1 2 3
d) Indirect recursion c) 3 2 1
Answer: c) Multi-thread recursion d) Compilation error
Answer: c) 3 2 1
11. What is the output of the following recursive function?
3. Recursion vs Iteration cpp
8. Which of the following is true about recursion and iteration? CopyEdit
a) Recursion is always better than iteration #include <iostream>
b) Iteration is always better than recursion using namespace std;
c) Recursion can sometimes be less efficient due to function call
void func(int n) {
overhead if (n == 0)
d) Recursion does not require extra memory return;
Answer: c) Recursion can sometimes be less efficient due to func(n - 1);
function call overhead cout << n << " ";
}
9. Which of the following uses more memory:
a) Recursion int main() {
b) Iteration func(3);
c) Both use the same memory return 0;
d) It depends on the function }
Answer: a) Recursion (Recursion uses extra stack memory for a) 3 2 1
function calls.) b) 1 2 3
c) 0 1 2 3
4. Recursive Function Execution d) Compilation error
10. What is the output of the following recursive function? Answer: b) 1 2 3
cpp 12. What will be the output of the following recursive function?
CopyEdit cpp
#include <iostream> CopyEdit
using namespace std; #include <iostream>
using namespace std;
void func(int n) {
if (n == 0) int factorial(int n) {
return; if (n == 0)
cout << n << " "; return 1;
func(n - 1); return n * factorial(n - 1);
} }
int main() { int main() {
func(3); cout << factorial(4);
return 0; 17. Which type of recursion can be easily converted into iteration?
}
a) Tail recursion
a) 24 b) Head recursion
b) 4 c) Indirect recursion
c) 10 d) Mutual recursion
d) 16 Answer: a) Tail recursion
Answer: a) 24 (Factorial of 4 = 4 × 3 × 2 × 1 = 24) 18. Why is tail recursion preferred over normal recursion?
a) It reduces stack usage
5. Recursion and Stack b) It increases execution time
13. What data structure is used for handling recursion in C++? c) It makes the function call itself multiple times
a) Queue d) It eliminates the need for recursion
b) Stack Answer: a) It reduces stack usage
c) Heap
d) Array 7. Miscellaneous Questions
Answer: b) Stack 19. Can a constructor call itself recursively?
14. What is stack overflow in recursion? a) Yes
a) When too many recursive calls consume all stack memory b) No
b) When a function returns an incorrect value Answer: b) No (Calling a constructor recursively leads to infinite
c) When an integer variable exceeds its limit object creation and stack overflow.)
d) When a function is called incorrectly 20. What is the best practice for using recursion in C++?
Answer: a) When too many recursive calls consume all stack a) Always use recursion instead of loops
memory b) Use recursion only when necessary and efficient
15. What happens if a function has infinite recursion? c) Never use recursion
a) It runs forever d) Use recursion in every function
b) It stops automatically Answer: b) Use recursion only when necessary and efficient
c) It leads to a stack overflow error
d) It slows down but does not crash Conclusion
Answer: c) It leads to a stack overflow error
✅ Recursion is a function calling itself.
✅ A base condition is required to stop recursion.
6. Recursion Optimization ✅ Recursion uses stack memory and can lead to stack overflow.
16. What is memoization in recursion? ✅ Tail recursion is preferred because it is more memory efficient.
a) A technique to optimize recursive functions by storing results ✅ Memoization can improve recursion performance by storing
b) A technique to create multiple recursive calls intermediate results.
c) A way to execute a function in a loop
1. Basics of Friend Function
d) A method to increase stack size
1. What is a friend function in C++?
Answer: a) A technique to optimize recursive functions by
a) A function that is a member of a class
storing results
b) A function that can access private and protected members of
a class Answer: b) Outside the class
c) A function that cannot access private members of a class
d) A function that is inherited by another class 2. Friend Function Access and Scope
Answer: b) A function that can access private and protected 6. Which of the following can a friend function access?
members of a class a) Only public members
2. Which keyword is used to define a friend function in C++? b) Only private members
a) private c) public, private, and protected members
b) protected d) Only protected members
c) friend Answer: c) public, private, and protected members
d) public 7. Can a friend function modify the private data members of a
Answer: c) friend class?
3. What is the correct syntax for declaring a friend function inside a) Yes
a class? b) No
a) Answer: a) Yes
cpp 8. Where is a friend function usually declared?
CopyEdit
friend void functionName(); a) Inside the class definition
b) b) Outside the class definition
cpp c) Inside and outside the class
CopyEdit d) None of the above
friend: void functionName(); Answer: a) Inside the class definition
c) 9. Can a friend function access members of a class without using
cpp
CopyEdit
an object?
void functionName(friend); a) Yes
d) b) No
cpp Answer: b) No (It needs an object of the class to access its
CopyEdit members.)
void friend functionName();
Answer: a) friend void functionName(); 3. Friend Function Implementation
4. Can a friend function be a member of a class? 10. What is the output of the following program?
a) Yes cpp
b) No CopyEdit
Answer: b) No #include <iostream>
using namespace std;
5. Where is a friend function defined?
a) Inside the class class A {
b) Outside the class int num;
c) Inside the class but executed outside public:
d) None of the above A() { num = 10; }
friend void show(A obj);
}; 15. What is the correct way to define a friend function that accesses
two classes?
void show(A obj) {
cout << obj.num; a)
} cpp
CopyEdit
int main() { friend void show(A, B);
A obj; b)
show(obj); cpp
return 0; CopyEdit
} void friend show(A, B);
a) Compilation error c)
b) 10 cpp
CopyEdit
c) Garbage value friend class show(A, B);
d) None of the above d)
Answer: b) 10 cpp
11. Can a friend function be called using a class object? CopyEdit
a) Yes show friend void(A, B);
b) No Answer: a) friend void show(A, B);
Answer: b) No (It is not a member of the class, so it cannot be
called with an object.) 5. Friend Function and Operator Overloading
12. Can a friend function be a private member of a class? 16. Can a friend function be used to overload an operator in C++?
a) Yes a) Yes
b) No b) No
Answer: b) No (Friend functions are not members of a class; they Answer: a) Yes
are declared inside but defined outside the class.) 17. What is the correct way to overload the + operator using a
friend function?
4. Friend Function with Multiple Classes a)
13. Can a friend function be shared among multiple classes? cpp
CopyEdit
a) Yes class A {
b) No int x;
Answer: a) Yes friend A operator+(A, A);
14. What happens when the same function is a friend of two classes? };
a) The function gets access to private members of both classes b)
cpp
b) The function can access only public members CopyEdit
c) The function cannot access protected members class A {
d) The program gives an error int x;
Answer: a) The function gets access to private members of both A friend operator+(A, A);
};
classes
c)
cpp Answer: b) No (Friend functions are not members of a class, so
CopyEdit
they are not inherited.)
class A {
int x; 22. Which is true about friend functions?
friend operator+(A, A); a) They reduce encapsulation
}; b) They increase security
d) c) They increase object-oriented principles
cpp d) They are mandatory in C++
CopyEdit
class A {
Answer: a) They reduce encapsulation (Because they allow
int x; external access to private data.)
void friend operator+(A, A);
}; Conclusion
Answer: a) class A { int x; friend A operator+(A, A); }; ✅ Friend functions allow non-member functions to access private and
protected members of a class.
6. Friend Function and Static Members ✅ They are declared inside a class but defined outside the class.
18. Can a friend function access static members of a class? ✅ They cannot be inherited and cannot be called using an object.
a) Yes ✅ Friend functions can be used for operator overloading and multiple
b) No class access.
Answer: a) Yes 1. Basics of Virtual Functions
19. What is true about friend functions and static members? 1. What is a virtual function in C++?
a) A friend function can access both static and non-static a) A function that cannot be overridden
members b) A function that is defined in a base class and overridden in a
b) A friend function can only access static members derived class
c) A friend function cannot access static members c) A function that does not have a body
d) A friend function must be static d) A function that can only be called using a pointer
Answer: a) A friend function can access both static and non- Answer: b) A function that is defined in a base class and
static members overridden in a derived class
2. Which keyword is used to declare a virtual function?
7. Miscellaneous Questions a) friend
20. What is not true about a friend function? b) static
a) It can access private members of a class c) virtual
b) It can be a member function of a class d) override
c) It is not called using an object of the class Answer: c) virtual
d) It is declared inside a class but defined outside 3. What is the main purpose of a virtual function?
Answer: b) It can be a member function of a class a) To allow function overloading
21. Can a friend function be inherited? b) To achieve dynamic (runtime) polymorphism
a) Yes c) To create static binding
b) No d) To define friend functions
Answer: b) To achieve dynamic (runtime) polymorphism cpp
CopyEdit
4. How is a virtual function called during runtime?
virtual void show();
a) Using the object name
b)
b) Using a pointer to the base class cpp
c) Using the new keyword CopyEdit
d) Using a friend function void virtual show();
Answer: b) Using a pointer to the base class c)
5. What happens if a virtual function is not overridden in the cpp
CopyEdit
derived class? friend virtual void show();
a) The base class version is used d)
b) The program gives an error cpp
c) The function call is ignored CopyEdit
d) The derived class does not compile void show() virtual;
Answer: a) The base class version is used Answer: a) virtual void show();
2. Virtual Function Behavior 3. Virtual Functions and Inheritance
6. What happens when a virtual function is called using a base 10. Can a virtual function be declared as private in a class?
class pointer pointing to a derived class object? a) Yes
a) The base class version of the function is executed b) No
b) The derived class version of the function is executed Answer: a) Yes (But it cannot be accessed directly, only through
c) Compilation error friend functions or member functions.)
d) Runtime error 11. Can a constructor be a virtual function?
Answer: b) The derived class version of the function is executed a) Yes
7. What is late binding in the context of virtual functions? b) No
a) The function call is resolved at compile time Answer: b) No (Constructors cannot be virtual because they are
b) The function call is resolved at runtime called only once during object creation.)
c) The function call is ignored 12. Can a destructor be virtual?
d) None of the above a) Yes
Answer: b) The function call is resolved at runtime b) No
8. What is early binding? Answer: a) Yes (To ensure proper deletion of derived class objects
a) The function call is resolved at compile time through base class pointers.)
b) The function call is resolved at runtime 13. What happens if a base class destructor is not declared virtual?
c) The function is ignored a) Only the base class destructor is called
d) The function cannot be used b) Both base and derived class destructors are called
Answer: a) The function call is resolved at compile time c) Only the derived class destructor is called
9. What is the correct way to declare a virtual function? d) The program crashes
a) Answer: a) Only the base class destructor is called
14. What is true about virtual functions and inheritance? Answer: c) It supports runtime polymorphism
a) A virtual function must be overridden in every derived class 19. What happens when a non-virtual function is called using a base
b) A virtual function can be overridden in a derived class but is class pointer?
not mandatory a) The base class function is executed
c) A virtual function cannot be inherited b) The derived class function is executed
d) A virtual function must be redefined with the same name c) The function call fails
Answer: b) A virtual function can be overridden in a derived d) The compiler gives an error
class but is not mandatory Answer: a) The base class function is executed
20. Which of the following is true?
4. Virtual Table (vtable) and Performance a) Virtual functions make early binding possible
15. How does C++ implement virtual functions internally? b) Virtual functions increase execution time due to indirect
a) Using function pointers function calls
b) Using vtable (virtual table) and vptr (virtual pointer) c) Virtual functions can be declared static
c) Using inline functions d) Virtual functions are called using friend functions
d) Using overloaded functions Answer: b) Virtual functions increase execution time due to
Answer: b) Using vtable (virtual table) and vptr (virtual indirect function calls
pointer)
16. What is vtable (virtual table)? Conclusion
a) A table storing base class pointers ✅ Virtual functions allow dynamic polymorphism by enabling function
b) A table storing function addresses for virtual function calls overriding in derived classes.
c) A table storing class names ✅ They are resolved at runtime (late binding) using a vtable and vptr
d) A table storing constructor addresses mechanism.
Answer: b) A table storing function addresses for virtual ✅ Constructors cannot be virtual, but destructors should be virtual to
function calls avoid memory leaks.
17. What is vptr (virtual pointer)? ✅ Base class pointers call derived class versions of virtual functions.
a) A pointer to the virtual table (vtable) 1. Basics of Classes in C++
b) A pointer to the first function in a class 1. What is a class in C++?
c) A pointer to the base class a) A function
d) A pointer to the last function in a class b) A user-defined data type
Answer: a) A pointer to the virtual table (vtable) c) A built-in data type
d) A pointer
5. Miscellaneous Questions Answer: b) A user-defined data type
18. What is true about a virtual function? 2. Which keyword is used to define a class in C++?
a) It must be defined in every derived class a) struct
b) It must always return void b) class
c) It supports runtime polymorphism c) define
d) It cannot be declared in the base class d) type
Answer: b) class b) Makes members accessible within the class and derived
3. What is an object in C++? classes
a) A function inside a class c) Makes members accessible everywhere
b) A variable of a class type d) Hides members from the program
c) A pointer to a class Answer: b) Makes members accessible within the class and
d) A structure derived classes
Answer: b) A variable of a class type 9. How do you define an object of a class named Car?
4. What is the default access specifier for members of a class in C+ a)
+? cpp
CopyEdit
a) public
Car();
b) private
b)
c) protected cpp
d) static CopyEdit
Answer: b) private Car myCar;
5. How many objects can be created from a class? c)
cpp
a) Only one CopyEdit
b) Multiple new Car;
c) Only three d)
d) None cpp
Answer: b) Multiple CopyEdit
create Car;
2. Class Members and Access Specifiers Answer: b) Car myCar;
6. Which of the following cannot be a member of a class? 10. What is the correct way to access a public member of a class?
a) Data members a)
cpp
b) Member functions CopyEdit
c) Constructors objectName.memberName;
d) A namespace b)
Answer: d) A namespace cpp
7. Which access specifier allows a member to be accessible only CopyEdit
objectName->memberName;
within the same class?
a) public c)
cpp
b) private CopyEdit
c) protected objectName::memberName;
d) static d)
Answer: b) private cpp
CopyEdit
8. What does the protected access specifier do? className.memberName;
a) Makes members accessible only within the same class Answer: a) objectName.memberName;
c) None
3. Constructors and Destructors d) As many as needed
11. What is a constructor in C++? Answer: a) Only one
a) A function used to initialize objects
b) A function used to delete objects 4. Static Members and Friend Functions
c) A function with a return type 16. What is a static data member in a class?
d) A static function a) A member function that can be called without an object
Answer: a) A function used to initialize objects b) A data member shared by all objects of the class
12. What is the correct syntax for defining a constructor? c) A data member that can only be modified once
a) d) A private member
cpp Answer: b) A data member shared by all objects of the class
CopyEdit
17. How do you define a static data member inside a class?
className() { }
a)
b) cpp
cpp CopyEdit
CopyEdit static int value;
void className() { }
b)
c) cpp
cpp CopyEdit
CopyEdit int static value;
static className() { }
c)
d) cpp
cpp CopyEdit
CopyEdit private static int value;
constructor className() { }
d)
Answer: a) className() { } cpp
13. Can a constructor be virtual? CopyEdit
a) Yes const static int value;
b) No Answer: a) static int value;
Answer: b) No 18. What is a friend function?
14. What is a destructor in C++? a) A function that can access private members of a class
a) A function used to initialize objects b) A function that can be called only by objects
b) A function used to delete objects c) A function that is part of the class
c) A function with a return type d) A virtual function
d) A constructor that takes parameters Answer: a) A function that can access private members of a
Answer: b) A function used to delete objects class
15. How many destructors can a class have? 19. Which keyword is used to declare a friend function?
a) Only one a) private
b) Multiple b) public
c) friend ✅ Classes are user-defined data types that help in object-oriented
d) static programming.
Answer: c) friend ✅ Objects are instances of classes.
✅ Constructors initialize objects, while destructors clean up memory.
5. Inheritance and Polymorphism ✅ Inheritance, polymorphism, and friend functions enhance the
20. What is inheritance in C++? flexibility of classes.
a) The ability to create new classes from existing ones 1. Basics of Function Prototype
b) The ability to create multiple functions with the same name 1. What is a function prototype in C++?
c) The ability to call functions dynamically a) A function definition
d) The ability to define friend functions b) A function declaration without a body
Answer: a) The ability to create new classes from existing ones c) A function call
21. Which access specifier allows a base class to be inherited as d) A function pointer
private? Answer: b) A function declaration without a body
a) public 2. Why is a function prototype used in C++?
b) protected a) To define a function
c) private b) To tell the compiler about the function before its actual
d) static definition
Answer: c) private c) To call the function
22. What is polymorphism in C++? d) To store function pointers
a) The ability to create multiple classes Answer: b) To tell the compiler about the function before its
b) The ability to have multiple functions with the same name but actual definition
different behavior 3. Where is a function prototype usually placed?
c) The ability to declare friend functions a) Inside the main() function
d) The ability to use static members b) At the end of the program
Answer: b) The ability to have multiple functions with the same c) Before the main() function or in a header file
name but different behavior d) After the function definition
23. What is function overloading? Answer: c) Before the main() function or in a header file
a) Defining multiple functions with the same name but different 4. What information does a function prototype provide to the
parameters compiler?
b) Overriding a base class function in a derived class a) Function name and return type
c) Using multiple return types in the same function b) Function name and parameter list
d) None of the above c) Return type, function name, and parameter list
Answer: a) Defining multiple functions with the same name but d) Only the return type
different parameters Answer: c) Return type, function name, and parameter list
5. What happens if a function is called without a prototype being
Conclusion declared beforehand?
a) Compilation error
b) The program runs successfully c) The function display takes one argument
c) Undefined behavior d) The function display returns an integer but takes no
d) The function executes but gives a warning arguments
Answer: a) Compilation error Answer: b) The function display does not take arguments and
does not return a value
2. Syntax and Examples 9. In a function prototype, what does the ellipsis (...) indicate?
6. Which of the following is a correct function prototype? cpp
CopyEdit
a)
cpp void print(...);
CopyEdit a) The function takes a variable number of arguments
int sum(int a, int b); b) The function takes no arguments
b) c) The function is an inline function
cpp d) The function must return an integer
CopyEdit
int sum { int a, int b };
Answer: a) The function takes a variable number of arguments
c)
cpp 3. Parameter Handling in Function Prototypes
CopyEdit 10. What happens if the function prototype has different
sum(int a, int b); parameters than the function definition?
d) a) The program runs normally
cpp
b) Compilation error
CopyEdit
int sum(int a, int b) { return a + b; } c) Only the prototype is used
Answer: a) int sum(int a, int b); d) Undefined behavior
7. What is the return type of the following function prototype? Answer: b) Compilation error
cpp 11. Consider the following prototype:
CopyEdit cpp
double calculate(int, float); CopyEdit
a) int int multiply(int, int);
b) float Which of the following function definitions is valid?
c) double a)
cpp
d) void CopyEdit
Answer: c) double int multiply(int a, int b) { return a * b; }
8. What does the following function prototype indicate? b)
cpp cpp
CopyEdit CopyEdit
void display(); int multiply(float a, float b) { return a * b; }
a) The function display returns an integer c)
b) The function display does not take arguments and does not cpp
CopyEdit
return a value int multiply(int a, int b, int c) { return a * b * c; }
d) int add(int, int);
cpp double add(double, double);
CopyEdit b)
multiply(int a, int b) { return a * b; } cpp
Answer: a) int multiply(int a, int b) { return a * b; } CopyEdit
int add(int a, int b);
12. Is it mandatory to specify parameter names in a function int add(int c, int d);
prototype?
c)
a) Yes cpp
b) No CopyEdit
Answer: b) No int add();
13. Which of the following is a valid function prototype for a int add();
function that takes an array as an argument? d)
cpp
a) CopyEdit
cpp
void add(int, int);
CopyEdit
int add(double, int);
void processArray(int arr[5]);
Answer: a) int add(int, int); double add(double,
b) double);
cpp
CopyEdit
void processArray(int arr[]); 5. Miscellaneous Questions
c) 16. If a function prototype is declared but not defined, what
cpp happens?
CopyEdit
void processArray(int *arr);
a) The program runs normally
d) b) Compilation error
cpp c) Linking error
CopyEdit d) Runtime error
All of the above Answer: c) Linking error
Answer: d) All of the above 17. What happens if a function prototype is declared inside a
function?
4. Function Prototype and Function Overloading a) The function becomes inline
14. Can function prototypes be overloaded? b) The function prototype is local to that function
a) Yes c) The function prototype is ignored
b) No d) The program does not compile
Answer: a) Yes Answer: b) The function prototype is local to that function
15. Which of the following is an example of function overloading 18. Can a function prototype include default arguments?
using function prototypes? a) Yes
a) b) No
cpp Answer: a) Yes
CopyEdit
19. Which of the following function prototype declarations is c) A function that does not belong to any class
correct when using default arguments? d) A function that only operates on static members
a) Answer: a) A function that is declared inside a class
cpp 2. Which keyword is used to define a member function outside a
CopyEdit
class?
int sum(int a, int b = 5);
a) extern
b)
cpp b) friend
CopyEdit c) :: (scope resolution operator)
int sum(int a = 5, int b); d) ->
c) Answer: c) :: (scope resolution operator)
cpp
CopyEdit
3. Where can member functions be defined in C++?
int sum(int a = 5, int b = 10, int c); a) Only inside the class
d) b) Only outside the class
cpp c) Either inside or outside the class
CopyEdit d) Inside a function
sum(int a, int b = 10); Answer: c) Either inside or outside the class
Answer: a) int sum(int a, int b = 5); 4. What happens when a member function is defined inside a
20. What is true about function prototypes in C++? class?
a) They are optional a) It is implicitly declared as virtual
b) They improve readability and structure b) It is treated as an inline function
c) They must always be declared in the main() function c) It cannot be called outside the class
d) They cannot include default arguments d) It must be declared static
Answer: b) They improve readability and structure Answer: b) It is treated as an inline function
5. What is the correct syntax for defining a member function
Conclusion outside the class?
✅ Function prototypes help declare functions before their actual a)
definition, improving code structure. cpp
✅ They specify the return type, function name, and parameter list. CopyEdit
✅ Function prototypes allow function overloading and default className::functionName() { }
arguments. b)
cpp
✅ Omitting a function prototype can lead to compilation or linking CopyEdit
errors. functionName::className() { }
1. Basics of Member Functions c)
1. What is a member function in C++? cpp
a) A function that is declared inside a class CopyEdit
className->functionName() { }
b) A function that is declared outside a class
d)
cpp
CopyEdit c) objectPointer::functionName();
functionName()::className { }
d) objectPointer->functionName;
Answer: a) className::functionName() { } Answer: b) objectPointer->functionName();
2. Accessing Member Functions 3. Static Member Functions
6. How can a member function of a class be called? 9. Which keyword is used to declare a static member function?
a) Using the dot (.) operator with an object a) const
b) Using the arrow (->) operator with a pointer to an object b) friend
c) Using the scope resolution operator (::) if it is static c) static
d) All of the above d) mutable
Answer: d) All of the above
Answer: c) static
7. Consider the following class:
cpp 10. What is true about static member functions?
CopyEdit a) They cannot access non-static members of the class
class Sample { b) They must be called using an object
public: c) They cannot be defined outside the class
void show();
d) They can only access member variables
};
Answer: a) They cannot access non-static members of the class
How do you correctly call the show() function?
11. How is a static member function called?
a)
cpp a) Using an object of the class
CopyEdit b) Using a class name and the scope resolution operator (::)
Sample.show(); c) Using an object pointer
b) d) Using this pointer
cpp Answer: b) Using a class name and the scope resolution operator (::)
CopyEdit
Sample->show(); 12. Which of the following statements about static member
c) functions is false?
cpp a) They can access only static data members
CopyEdit b) They do not have a this pointer
Sample obj; c) They can be accessed using an object of the class
obj.show();
d) They can access non-static data members
d) Answer: d) They can access non-static data members
cpp
CopyEdit
show(); 4. Constant Member Functions
Answer: c) Sample obj; obj.show(); 13. What does a const member function ensure?
8. How do you call a member function when using a pointer to an a) The function cannot be modified
object? b) The function does not modify any data members
a) objectPointer.functionName(); c) The function cannot be overloaded
b) objectPointer->functionName(); d) The function cannot be inherited
Answer: b) The function does not modify any data members b) A function that does not return any value
14. How do you declare a constant member function? c) A function that allows runtime polymorphism
a) d) A function that can only be declared in a derived class
cpp Answer: c) A function that allows runtime polymorphism
CopyEdit
19. Which of the following correctly declares a virtual function?
void show() const;
a)
b) cpp
cpp CopyEdit
CopyEdit virtual void display();
const void show();
b)
c) cpp
cpp CopyEdit
CopyEdit void virtual display();
void const show();
c)
d) cpp
cpp CopyEdit
CopyEdit static virtual void display();
show() const void;
d)
Answer: a) void show() const; cpp
15. Which statement about constant member functions is true? CopyEdit
a) They cannot modify non-static member variables friend virtual void display();
b) They cannot call other functions inside the class Answer: a) virtual void display();
c) They must be declared inside main() 20. What happens when a member function is declared as virtual?
d) They can modify member variables using pointers a) It becomes a friend function
Answer: a) They cannot modify non-static member variables b) It is not inherited
c) It supports dynamic binding (runtime polymorphism)
5. Friend Functions and Virtual Functions d) It cannot be overridden
16. Can a friend function be a member function of a class? Answer: c) It supports dynamic binding (runtime polymorphism)
a) Yes
b) No Conclusion
Answer: b) No ✅ Member functions allow operations on class members.
17. How is a friend function declared? ✅ Static functions can be called without an object and access only static
a) friend void functionName(); members.
b) static void functionName(); ✅ Const member functions prevent modification of class members.
c) private: functionName(); ✅ Virtual functions enable runtime polymorphism in inheritance.
d) protected: functionName(); 1. Basics of Static Data Members
Answer: a) friend void functionName(); 1. What is a static data member in C++?
18. What is a virtual function? a) A member variable that is shared among all objects of the
a) A function that is declared inside a class but defined outside class
b) A member variable that can be accessed only inside the class };
c) A member variable that changes automatically when an a) Test::count = 0;
object is created b) int Test::count = 0;
d) A member variable that is always constant c) static int Test::count = 0;
Answer: a) A member variable that is shared among all objects d) extern int Test::count = 0;
of the class Answer: b) int Test::count = 0;
2. How is a static data member declared in a class? 7. How can a static data member be accessed?
a) Using the keyword const a) Using an object of the class
b) Using the keyword static b) Using the class name and scope resolution operator (::)
c) Using the keyword friend c) Using a friend function only
d) Using the keyword extern d) It cannot be accessed
Answer: b) Using the keyword static Answer: b) Using the class name and scope resolution operator
3. Where is memory allocated for a static data member? (::)
a) In each object of the class 8. Consider the following code:
b) In the function stack cpp
CopyEdit
c) In the static storage area (global memory) class Counter {
d) In the heap memory public:
Answer: c) In the static storage area (global memory) static int count;
4. How many copies of a static data member exist for a class? };
int Counter::count = 0;
a) One per object int main() {
b) One per class Counter obj1, obj2;
c) One per function call obj1.count++;
d) Multiple copies, depending on the number of objects obj2.count++;
Answer: b) One per class std::cout << Counter::count;
}
5. Which of the following is true about static data members?
a) They must be initialized inside the class definition What will be the output?
b) They must be initialized outside the class definition a) 1
c) They cannot be initialized b) 2
d) They can be initialized in any member function c) 3
Answer: b) They must be initialized outside the class definition d) 4
Answer: b) 2
2. Initialization and Accessing Static Data Members 9. What happens if a static data member is not initialized outside
6. How do you correctly define a static data member outside a the class?
class? a) It is initialized to 0 by default
cpp b) It causes a compilation error
CopyEdit c) It causes a runtime error
class Test { d) It cannot be declared static
static int count;
Answer: b) It causes a compilation error
10. Can a static data member be accessed using an object? 15. What is true about static member functions?
a) Yes, but it is not recommended a) They can access non-static data members
b) No, it must be accessed only using the class name b) They do not have a this pointer
c) Yes, but only inside member functions c) They must be called using an object
d) No, it is a private member by default d) They must be declared as private
Answer: a) Yes, but it is not recommended Answer: b) They do not have a this pointer
3. Static Member Functions 4. Usage and Behavior of Static Functions
11. What is a static member function? 16. Can a static member function be const?
a) A function that can only access static data members a) Yes
b) A function that belongs to a single object b) No
c) A function that cannot access any class members Answer: b) No
d) A function that can access both static and non-static members 17. Can a static member function be virtual?
Answer: a) A function that can only access static data members a) Yes
12. Which keyword is used to declare a static member function? b) No
a) static Answer: b) No
b) const 18. Which of the following statements is false about static member
c) extern functions?
d) friend a) They do not require an object to be called
Answer: a) static b) They can be called using the class name
13. How is a static member function defined outside the class? c) They can access both static and non-static members
cpp d) They do not have a this pointer
CopyEdit
class Demo {
Answer: c) They can access both static and non-static members
public: 19. Consider the following code:
static void show(); cpp
}; CopyEdit
class Example {
a) void Demo::show() { } public:
b) static void Demo::show() { } static void display();
c) Demo::static show() { } };
d) void static Demo::show() { } void Example::display() {
std::cout << "Hello";
Answer: a) void Demo::show() { } }
14. How is a static member function called? int main() {
a) Using an object Example::display();
b) Using a pointer to an object }
c) Using the class name and scope resolution operator (::) What will be the output?
d) Using the this pointer a) Hello
Answer: c) Using the class name and scope resolution operator (::) b) Compilation error
c) Runtime error };
d) No output How do you correctly declare a pointer to the data member x?
Answer: a) Hello a) int *ptr = &Test::x;
20. Which statement about static data members and static member b) int Test::*ptr = &Test::x;
functions is correct? c) int ptr = Test::*x;
a) Static member functions can access only static data members d) int *ptr = x;
b) Static data members are unique for each object Answer: b) int Test::*ptr = &Test::x;
c) Static member functions require an object to be called 4. How do you access a member variable using a pointer-to-
d) Static data members are automatically initialized member?
Answer: a) Static member functions can access only static data a) object.*pointer
members b) object->*pointer
c) object.pointer
Conclusion d) Both a and b
✅ Static data members are shared across all objects of a class. Answer: d) Both a and b
✅ Static member functions can only access static data members. 5. Given:
✅ They are called using the class name and scope resolution operator cpp
CopyEdit
(::). class Sample {
✅ Static functions do not have a this pointer and cannot be virtual. public:
1. Basics of Pointers to Members int num;
1. What does a pointer to member in C++ allow? };
int Sample::*ptr = &Sample::num;
a) Accessing only public members of a class
What does ptr store?
b) Storing addresses of class members
a) Address of the Sample class
c) Creating a copy of class members
d) Allocating dynamic memory b) Address of an object of Sample
Answer: b) Storing addresses of class members c) Address of the member variable num
2. What is the correct syntax to declare a pointer to a data member d) Garbage value
of a class? Answer: c) Address of the member variable num
a) int *ptr;
b) int A::*ptr; 2. Using Pointers to Data Members
c) int *A::ptr; 6. Given:
cpp
d) int A::ptr; CopyEdit
Answer: b) int A::*ptr; class Example {
3. Given the class: public:
cpp int value;
CopyEdit };
class Test { int Example::*ptr = &Example::value;
public: Example obj;
int x; obj.value = 10;
cout << obj.*ptr; What will be the output?
What will be the output? a) Compilation error
a) Compilation error b) 20
b) 10 c) Address of data
c) Address of value d) Undefined behavior
d) Undefined behavior Answer: b) 20
Answer: b) 10
7. How do you access a pointer-to-member with a pointer to an 3. Pointers to Member Functions
object? 11. What is a pointer to a member function?
a) ptr->*memberPointer a) A pointer storing the address of an object
b) ptr.*memberPointer b) A pointer storing the address of a function inside a class
c) (*ptr).*memberPointer c) A pointer storing the address of a global function
d) Both a and c d) A pointer storing the address of a constructor
Answer: d) Both a and c Answer: b) A pointer storing the address of a function inside a class
8. Which operator is used to access a pointer to a data member? 12. What is the correct syntax for declaring a pointer to a member
a) -> function?
b) .* a) void (*ptr)();
c) & b) void A::*ptr();
d) ->* c) void (A::*ptr)();
Answer: b) .* d) A::*void ptr();
9. Which operator is used to access a pointer to a data member Answer: c) void (A::*ptr)();
when using a pointer to an object? 13. Given:
a) -> cpp
b) .* CopyEdit
class Example {
c) ->* public:
d) *-> void display() { cout << "Hello"; }
Answer: c) ->* };
10. Given: void (Example::*ptr)() = &Example::display;
cpp Example obj;
CopyEdit How do you call display() using the pointer-to-member function?
class A { a) obj.*ptr();
public:
int data;
b) obj->*ptr();
}; c) obj.ptr();
int A::*ptr = &A::data; d) ptr->display();
A *obj = new A(); Answer: a) obj.*ptr();
obj->data = 20;
cout << obj->*ptr;
14. Given:
cpp
CopyEdit
class Demo { Answer: d) Both a and b
public:
19. Can a pointer to member function be used to call an overloaded
void show() { cout << "Demo"; }
}; function?
How do you declare a pointer to the show function? a) Yes
a) void (*ptr)() = &Demo::show; b) No
b) void Demo::*ptr() = &Demo::show; Answer: a) Yes
c) void (Demo::*ptr)() = &Demo::show; 20. Which statement is true about pointers to members?
d) void ptr = &Demo::show; a) They can store addresses of static members
b) They can store addresses of non-static members
Answer: c) void (Demo::*ptr)() = &Demo::show;
c) They must be called using -> operator
15. How do you call a member function using a pointer to an
d) They store the address of an object, not a member
object?
Answer: b) They can store addresses of non-static members
a) obj->*ptr();
b) obj.*ptr();
c) ptr->obj(); Conclusion
d) (*ptr).obj(); ✅ Pointers to members allow indirect access to data members and
member functions of a class.
Answer: a) obj->*ptr();
✅ They use .* and ->* operators for access.
✅ Pointers to member functions require an object to be used.
4. Advanced Concepts of Pointers to Members ✅ They cannot store static functions or non-member functions.
16. Can a pointer to a member function store the address of a non-
member function?
1. Basics of Private Member Functions
a) Yes 1. What is a private member function in C++?
b) No a) A function that can only be accessed by objects outside the
Answer: b) No class
17. Which of the following cannot be used with a pointer-to- b) A function that can only be accessed by other member
member function? functions of the class
a) this pointer c) A function that must be declared as static
b) static function d) A function that can be accessed from anywhere in the
program
c) virtual function
Answer: b) A function that can only be accessed by other
d) Non-static member function
member functions of the class
Answer: b) static function
2. What happens when a private function is called directly from
18. What is the correct way to call a virtual function using a
outside the class?
pointer-to-member?
a) It executes normally
a) obj->*ptr();
b) It causes a compilation error
b) obj.*ptr();
c) It prints garbage values
c) (*obj).*ptr(); d) It can only be called using this pointer
d) Both a and b Answer: b) It causes a compilation error
3. Which access specifier is used to declare a private function? CopyEdit
class Test {
a) protected
private:
b) friend void show() { cout << "Private"; }
c) private public:
d) public void callShow() { show(); }
};
Answer: c) private int main() {
4. Given: Test obj;
cpp obj.callShow();
CopyEdit }
class Demo {
private: a) Compilation error
void display() { cout << "Hello"; } b) Private
}; c) Garbage value
int main() { d) Undefined behavior
Demo obj;
Answer: b) Private
obj.display();
} 8. Can a private member function be called using a pointer-to-
What will happen when this program is compiled? member function?
a) Prints "Hello" a) Yes
b) Compilation error b) No
c) Runtime error Answer: b) No
d) Undefined behavior 9. Can a friend function access a private member function?
Answer: b) Compilation error a) Yes
5. Where can a private member function be called? b) No
a) Inside the same class Answer: a) Yes
b) In derived classes 10. How can you access a private function in an inherited class?
c) Outside the class using an object a) By using the protected access modifier
d) Both a and b b) By using a friend function
Answer: a) Inside the same class c) By using a public function that calls the private function
d) Private functions cannot be inherited
2. Accessing Private Member Functions Answer: d) Private functions cannot be inherited
6. How can a private member function be accessed outside a class?
a) By using a pointer to the function 3. Private Member Functions in Inheritance
b) By using a friend function 11. Can a private function be inherited by a derived class?
c) By using inheritance a) Yes
d) It cannot be accessed at all b) No
Answer: b) By using a friend function Answer: b) No
7. What will be the output of the following code? 12. What happens when a private member function is declared in a
cpp base class?
a) It cannot be accessed by derived classes 16. Can a private function be called using this pointer inside the
b) It can be accessed using the protected keyword class?
c) It can be accessed using this pointer in derived classes a) Yes
d) It is automatically public in derived classes b) No
Answer: a) It cannot be accessed by derived classes Answer: a) Yes
13. What will happen if a private function is declared virtual in a 17. What happens if a class contains only private member
base class? functions?
cpp a) The class cannot be instantiated
CopyEdit b) The class is useless
class A {
private: c) The class can still be used with friend functions
virtual void show() { cout << "Base"; } d) Both a and c
}; Answer: d) Both a and c
class B : public A { 18. Given:
public: cpp
void show() { cout << "Derived"; } CopyEdit
}; class Example {
int main() { private:
B obj; static void show() { cout << "Private Static"; }
obj.show(); public:
} static void callShow() { show(); }
a) Prints "Base" };
b) Prints "Derived" int main() {
Example::callShow();
c) Compilation error }
d) Runtime error What will be the output?
Answer: c) Compilation error a) Compilation error
14. If a private member function needs to be accessible in a derived b) Private Static
class, what should be done?
c) Garbage value
a) Declare it as protected
d) Runtime error
b) Declare it as public Answer: b) Private Static
c) Declare a public function that calls the private function 19. Can a constructor be private?
d) Use a friend function a) Yes
Answer: a) Declare it as protected b) No
15. Can a private function be overloaded? Answer: a) Yes
a) Yes 20. What is the purpose of making a constructor private?
b) No a) To restrict object creation
Answer: a) Yes b) To prevent memory leaks
c) To make all functions private
4. Miscellaneous Questions d) To allow multiple instances of a class
Answer: a) To restrict object creation };
Local obj;
obj.display();
Conclusion }
✅ Private member functions can only be accessed inside the class or via int main() {
friend functions. test();
}
✅ They cannot be inherited, but can be accessed using public or
protected wrapper functions. What will be the output?
✅ Private static functions can be called inside the class without an a) Compilation error
object. b) Local Class
✅ Private constructors restrict object creation, useful for Singleton c) Undefined behavior
design patterns. d) Garbage value
1. Basics of Local Class Answer: b) Local Class
1. What is a local class in C++? 5. What is the scope of a local class?
a) A class defined inside another class a) Global
b) A class defined inside a function b) Block-level (inside function only)
c) A class defined inside a namespace c) File-level
d) A class defined globally d) Namespace-level
Answer: b) A class defined inside a function Answer: b) Block-level (inside function only)
2. Where can a local class be declared?
a) Inside a function 2. Restrictions on Local Classes
b) Inside another class 6. Can a local class access local variables of the function where it is
c) Inside a loop defined?
d) Outside the main function a) Yes
Answer: a) Inside a function b) No
3. Which of the following is true about a local class? Answer: b) No
a) It can be used anywhere in the program 7. Why can't a local class access local variables of the function?
b) It can only be used within the function where it is defined a) Because local variables have automatic storage duration
c) It must be declared as static b) Because local classes are global in scope
d) It cannot have member functions c) Because local classes cannot have constructors
Answer: b) It can only be used within the function where it is d) Because local variables are private
defined Answer: a) Because local variables have automatic storage
4. Given: duration
cpp 8. Can a local class have static data members?
CopyEdit a) Yes
void test() { b) No
class Local {
public:
Answer: b) No
void display() { cout << "Local Class"; }
9. Can a local class have member functions? 17. Can a local class access static variables of the function in which
a) Yes it is defined?
b) No a) Yes
Answer: a) Yes b) No
10. Can a local class have a constructor and destructor? Answer: a) Yes
a) Yes 18. What will happen if a local class tries to access a local variable
b) No of the function?
Answer: a) Yes a) Compilation error
b) Runtime error
3. Usage and Access of Local Classes c) Undefined behavior
11. Can a local class be returned from a function? d) It will execute normally
a) Yes Answer: a) Compilation error
b) No 19. Which of the following statements is true about local classes?
Answer: b) No a) They can access static variables of the function
12. Can a local class be passed as a function argument? b) They can be inherited
a) Yes c) They can have virtual functions
b) No d) They can be used as template arguments
Answer: b) No Answer: a) They can access static variables of the function
13. Can a local class have a friend function? 20. Can a local class be defined inside a loop?
a) Yes a) Yes
b) No b) No
Answer: a) Yes Answer: a) Yes
14. Can a local class be used outside the function in which it is
defined? Conclusion
a) Yes ✅ Local classes are defined inside functions and have function-level
b) No scope.
Answer: b) No ✅ They cannot access local variables, but they can access global and
15. Can a local class have virtual functions? static variables.
a) Yes ✅ No static data members, no virtual functions, and cannot be used
b) No outside the function.
Answer: b) No ✅ Useful for encapsulation and temporary class definitions in specific
functions.
4. Local Class with Static and Global Variables 1. Basics of Constructors
16. Can a local class access global variables? 1. What is a constructor in C++?
a) Yes a) A function that is explicitly called to initialize an object
b) No b) A function that initializes an object automatically when it is
Answer: a) Yes created
c) A function that can be called multiple times for the same c) Must be explicitly defined
object d) Must always initialize all data members
d) A function that is used to destroy objects Answer: a) Has no parameters or has default values for all
Answer: b) A function that initializes an object automatically parameters
when it is created 7. What is a parameterized constructor?
2. What is the name of a constructor function in C++? a) A constructor that takes arguments
a) Same as the class name b) A constructor that takes no arguments
b) Different from the class name c) A constructor that destroys objects
c) init() d) A constructor that is private
d) constructor() Answer: a) A constructor that takes arguments
Answer: a) Same as the class name 8. What is a copy constructor used for?
3. Which of the following statements is true about constructors? a) Copying data members from one object to another
a) They do not return a value b) Creating a new object from scratch
b) They must have a return type c) Destroying an object
c) They can be called explicitly multiple times d) Assigning one object to another using = operator
d) They can be inherited Answer: a) Copying data members from one object to another
Answer: a) They do not return a value 9. Which of the following is true about copy constructors?
4. How many constructors can a class have? a) It is used when an object is passed by value
a) Only one b) It is used when returning an object from a function
b) Multiple, if they have different parameter lists c) It is automatically created if not defined
c) Multiple, even with the same parameter list d) All of the above
d) None Answer: d) All of the above
Answer: b) Multiple, if they have different parameter lists 10. What is a move constructor introduced in C++11?
5. What happens if a constructor is not defined in a class? a) It moves resources from one object to another instead of
a) Compilation error copying
b) The object cannot be created b) It is the same as a copy constructor
c) A default constructor is automatically provided by the c) It is used to delete objects
compiler d) It is a private constructor
d) The program crashes Answer: a) It moves resources from one object to another instead of
Answer: c) A default constructor is automatically provided by copying
the compiler
3. Destructors
2. Types of Constructors 11. What is a destructor in C++?
6. A default constructor is a constructor that: a) A function called when an object is created
a) Has no parameters or has default values for all parameters b) A function called when an object goes out of scope
b) Takes at least one parameter c) A function used to create objects
d) A function that initializes the object
Answer: b) A function called when an object goes out of scope }
12. How is a destructor defined in C++? a) Compilation error
a) ~ClassName() b) Constructor Called
b) delete ClassName() c) Constructor Called followed by Destructor Called
c) Destructor() d) Destructor Called followed by Constructor Called
d) ClassName::~() Answer: c) Constructor Called followed by Destructor Called
Answer: a) ~ClassName() 17. What happens if a destructor is declared as private?
13. Which of the following is true about destructors? a) Object cannot be destroyed outside the class
a) They can take arguments b) Object is destroyed normally
b) They cannot be overloaded c) Program crashes
c) They must return an integer value d) It must be declared as public
d) They must be explicitly called Answer: a) Object cannot be destroyed outside the class
Answer: b) They cannot be overloaded 18. If a constructor is private, how can objects of the class be
14. When is a destructor called? created?
a) When an object goes out of scope a) Using a friend function
b) When an object is explicitly deleted b) Using a static function within the class
c) When the program terminates c) Using a singleton pattern
d) All of the above d) All of the above
Answer: d) All of the above Answer: d) All of the above
15. How many destructors can a class have? 19. What will be the output of the following code?
a) Only one cpp
CopyEdit
b) Multiple class Demo {
c) None public:
d) As many as needed Demo() { cout << "Constructor"; }
Answer: a) Only one ~Demo() { cout << "Destructor"; }
};
int main() {
4. Constructor and Destructor Execution Demo *obj = new Demo();
16. What will be the output of the following code? }
cpp a) ConstructorDestructor
CopyEdit
b) Constructor
#include <iostream>
using namespace std; c) Destructor
class Test { d) Compilation error
public: Answer: b) Constructor (Destructor is not called as delete obj; is
Test() { cout << "Constructor Called" << endl; } missing)
~Test() { cout << "Destructor Called" << endl; }
}; 20. What is the correct way to call a destructor explicitly?
int main() { a) delete obj;
Test obj; b) ~ClassName();
c) free(obj); cpp
CopyEdit
d) obj.~ClassName(); class Demo {
Answer: d) obj.~ClassName(); public:
int x;
Conclusion Demo(int val) { x = val; }
};
✅ Constructors initialize objects automatically, and destructors clean int main() {
up when objects go out of scope. Demo obj;
✅ Types of constructors: Default, Parameterized, Copy, and Move }
constructors. What will be the output?
✅ Destructors cannot be overloaded, and only one destructor exists per a) 0
class. b) Garbage value
✅ Private constructors are useful for Singleton patterns. c) Compilation error
✅ Explicit destructor calls are rare but possible using d) Runtime error
obj.~ClassName();. Answer: c) Compilation error (Because a parameterized
1. Parameterized Constructor constructor is defined, but no default constructor exists)
1. What is a parameterized constructor in C++? 5. Can a parameterized constructor have default arguments?
a) A constructor that takes arguments a) Yes
b) A constructor that does not take any arguments b) No
c) A constructor that copies values from another object Answer: a) Yes
d) A constructor that initializes pointers only
Answer: a) A constructor that takes arguments 2. Copy Constructor
2. What is the primary use of a parameterized constructor? 6. What is a copy constructor in C++?
a) To initialize an object with specific values a) A constructor that copies the values of an object into another
b) To create an empty object object
c) To delete objects from memory b) A constructor that assigns one object to another using =
d) To allocate memory dynamically operator
Answer: a) To initialize an object with specific values c) A constructor that takes a single integer argument
3. What will happen if a parameterized constructor is defined and d) A constructor that initializes an object with default values
no default constructor is provided? Answer: a) A constructor that copies the values of an object into
a) The compiler provides a default constructor another object
b) Object creation without arguments will cause an error 7. What is the correct syntax for defining a copy constructor?
c) The object initializes with garbage values a) ClassName(ClassName &obj);
d) The compiler automatically converts the parameterized b) ClassName(ClassName obj);
constructor into a default constructor c) ClassName &ClassName(obj);
Answer: b) Object creation without arguments will cause an d) ClassName obj(ClassName &);
error Answer: a) ClassName(ClassName &obj);
4. Consider the following code:
8. When is a copy constructor called? 12. Why should a copy constructor take its argument by reference
a) When an object is assigned another object's value using = instead of by value?
b) When an object is passed by value to a function a) To avoid infinite recursion
c) When an object is returned from a function b) To improve performance
d) Both b and c c) To prevent unnecessary object creation
Answer: d) Both b and c d) All of the above
9. What will happen if a class does not define a copy constructor? Answer: d) All of the above
a) Compiler provides a default copy constructor 13. What happens if a copy constructor is defined incorrectly,
b) The object cannot be copied taking the argument by value?
c) Compilation error a) It will work normally
d) Program crashes b) Infinite recursion occurs
Answer: a) Compiler provides a default copy constructor c) Compiler will optimize it automatically
10. Consider the following code: d) The object cannot be copied
cpp Answer: b) Infinite recursion occurs
CopyEdit 14. Which of the following situations does not call the copy
class Demo {
public: constructor?
int x; a) Passing an object by reference
Demo(int val) { x = val; } b) Passing an object by value
Demo(Demo &obj) { x = obj.x; } c) Returning an object by value
};
int main() {
d) Creating an object using another object
Demo obj1(10); Answer: a) Passing an object by reference
Demo obj2 = obj1; 15. What happens if a class has dynamic memory allocation but
cout << obj2.x; does not implement a copy constructor?
} a) Default copy constructor performs shallow copy
What will be the output? b) Memory leaks may occur
a) 0 c) Deleting one object may delete memory of another object
b) 10 d) All of the above
c) Garbage value Answer: d) All of the above
d) Compilation error
Answer: b) 10 4. Deep Copy vs Shallow Copy
16. What is the difference between a shallow copy and a deep copy?
3. Differences and Behavior a) Shallow copy duplicates pointers, deep copy duplicates
11. Which of the following is true about the copy constructor? memory
a) It is always required for all classes b) Shallow copy creates a new object, deep copy reuses the same
b) It is called when passing an object by reference object
c) It must accept an argument by reference c) Shallow copy takes more memory, deep copy takes less
d) It is never called automatically
Answer: c) It must accept an argument by reference
memory ✅ Shallow copy can lead to memory issues, so deep copy should be used
d) There is no difference for dynamic memory allocation.
Answer: a) Shallow copy duplicates pointers, deep copy duplicates ✅ Private copy constructors prevent object copying, useful for
memory Singleton Design Pattern.
17. When should you explicitly define a copy constructor? 1. Basics of Operator Overloading
a) When a class uses dynamic memory allocation 1. What is operator overloading in C++?
b) When a class has static data members a) Using operators with different data types
c) When a class has only primitive data types b) Giving additional meaning to an operator for user-defined
d) When a class does not have any constructor types
Answer: a) When a class uses dynamic memory allocation c) Writing multiple operators in one function
18. What happens if a copy constructor is declared as private? d) Using multiple operators in a single expression
a) Objects cannot be copied Answer: b) Giving additional meaning to an operator for user-
b) Objects can be copied using assignment operator defined types
c) Objects can be copied inside the class 2. Which of the following operators cannot be overloaded in C++?
d) It does not affect anything a) +
Answer: a) Objects cannot be copied b) ==
19. Which constructor is called when we create an object using c) ::
another existing object? d) *
a) Default constructor Answer: c) ::
b) Parameterized constructor 3. How is operator overloading implemented in C++?
c) Copy constructor a) Using normal functions
d) Move constructor b) Using operator keyword inside a class
Answer: c) Copy constructor c) Using inline functions only
20. How do we prevent shallow copy issues when using dynamic d) Using friend functions only
memory allocation? Answer: b) Using operator keyword inside a class
a) By defining a proper copy constructor 4. Which of the following operators must be overloaded as a
b) By avoiding constructors altogether member function?
c) By using malloc() instead of new a) =
d) By making the copy constructor private b) +
Answer: a) By defining a proper copy constructor c) -
d) *
Conclusion Answer: a) =
✅ Parameterized constructors allow object initialization with values. 5. What is the return type of an overloaded operator function?
✅ Copy constructors duplicate objects, either by shallow copy (default) a) Always void
or deep copy (user-defined). b) Always int
✅ Copy constructors take arguments by reference to prevent infinite c) Can be any type
recursion. d) Must return this pointer
Answer: c) Can be any type Answer: c) new
10. How do you overload a binary operator using a friend function?
2. Syntax and Rules for Operator Overloading a) friend ReturnType operator+(Arguments);
6. Which keyword is used to declare an overloaded operator b) ReturnType operator+(Arguments);
function? c) ReturnType friend operator+(Arguments);
a) operator d) friend operator+(Arguments);
b) function Answer: a) friend ReturnType operator+(Arguments);
c) overload
d) op 3. Unary and Binary Operator Overloading
Answer: a) operator 11. Which of the following correctly overloads the unary ++
7. How do you define an overloaded + operator inside a class? operator?
a) void operator+(); a) A operator++() { x++; return *this; }
b) void operator+(int x); b) A operator++(int) { A temp = *this; x++; return
c) ReturnType operator+(ClassName obj); temp; }
d) ClassName operator+(); c) Both a and b
Answer: c) ReturnType operator+(ClassName obj); d) None of the above
8. Consider the following code snippet: Answer: c) Both a and b
cpp 12. How do you differentiate between pre-increment and post-
CopyEdit increment when overloading ++?
class A { a) Using operator++ twice
public:
int x; b) Passing an int parameter for post-increment
A(int val) { x = val; } c) Using operator+(int)
A operator+(A obj) { d) No difference
return A(x + obj.x); Answer: b) Passing an int parameter for post-increment
}
}; 13. What is the correct way to overload the assignment = operator?
If A a1(10), a2(20); and A a3 = a1 + a2;, what is the value a) As a non-member function
of a3.x? b) As a member function
a) 10 c) As a friend function
b) 20 d) Cannot be overloaded
c) 30 Answer: b) As a member function
d) Compilation error 14. Which of the following correctly overloads the << (insertion)
Answer: c) 30 operator for cout?
cpp
9. Which of the following can be overloaded? CopyEdit
a) . (dot operator) class A {
b) sizeof public:
c) new int x;
};
d) ::
a) ostream& operator<<(ostream &out, A obj); Answer: a) operator Type();
b) void operator<<(ostream &out, A obj); 20. Consider the following code:
c) A operator<<(ostream &out, A obj); cpp
CopyEdit
d) ostream& friend operator<<(ostream &out, A obj); class A {
Answer: d) ostream& friend operator<<(ostream &out, A obj); public:
operator int() { return 10; }
4. Type Conversions };
int main() {
15. What is type conversion in C++? A obj;
a) Changing data types automatically int x = obj;
b) Explicitly defining a conversion function cout << x;
c) Converting between user-defined and primitive types }
d) All of the above What will be the output?
Answer: d) All of the above a) 10
16. Which type of conversion is performed automatically by the b) Garbage value
compiler? c) Compilation error
a) Implicit type conversion d) Undefined behavior
b) Explicit type conversion Answer: a) 10
c) User-defined conversion
d) Friend conversion Conclusion
Answer: a) Implicit type conversion ✅ Operator Overloading allows user-defined behavior for operators.
17. What is explicit type conversion in C++? ✅ Some operators (e.g., ::, sizeof, .) cannot be overloaded.
a) When the compiler converts types automatically ✅ Overloaded operators can be implemented using member or friend
b) When the user forces a conversion using type casting functions.
c) When an object is assigned to another object ✅ Type conversions can be implicit (automatic) or explicit (manual
d) When the compiler throws an error casting).
Answer: b) When the user forces a conversion using type casting ✅ Conversion constructors and conversion functions enable class-to-
18. What is the purpose of a conversion constructor in C++? class or class-to-primitive type
a) Converts one class type into another automatically 1. Basics of Inheritance
b) Converts one primitive type into another 1. What is inheritance in C++?
c) Converts objects into floating-point numbers a) A way to create multiple copies of a class
d) Converts int values into char values b) A mechanism where one class acquires properties of another
Answer: a) Converts one class type into another automatically c) A way to delete objects dynamically
19. How do you define a conversion function inside a class? d) A method to access private members of a class
a) operator Type(); Answer: b) A mechanism where one class acquires properties of
b) convert(Type); another
c) convertToType(); 2. Which keyword is used for inheritance in C++?
d) Type operator; a) inherits
b) base Answer: a) A derived class acts as a base class for another
c) extends derived class
d) : 8. Which of the following is an example of hierarchical
Answer: d) : inheritance?
3. What is the base class in inheritance? a) One base class and multiple derived classes
a) The class that is derived from another class b) One derived class inheriting from multiple base classes
b) The class that provides properties to the derived class c) A derived class acting as a base class for another class
c) A class that contains only private members d) None of the above
d) A class that cannot be inherited Answer: a) One base class and multiple derived classes
Answer: b) The class that provides properties to the derived 9. What is hybrid inheritance?
class a) A combination of multiple and multilevel inheritance
4. What is a derived class in inheritance? b) A combination of single and hierarchical inheritance
a) A class that is used only for pointers c) A combination of multiple and hierarchical inheritance
b) A class that cannot be instantiated d) A combination of multilevel and hierarchical inheritance
c) A class that inherits from a base class Answer: c) A combination of multiple and hierarchical
d) A class that contains only friend functions inheritance
Answer: c) A class that inherits from a base class 10. In multiple inheritance, if two base classes have a function with
5. What does single inheritance mean in C++? the same name, how does the derived class resolve the
a) A derived class inherits from only one base class ambiguity?
b) A base class is inherited by multiple derived classes a) Using scope resolution operator (::)
c) A class cannot be inherited b) Overloading the function
d) A derived class inherits from multiple base classes c) Renaming the function
Answer: a) A derived class inherits from only one base class d) It cannot be resolved
Answer: a) Using scope resolution operator (::)
2. Types of Inheritance
6. What is multiple inheritance? 3. Access Specifiers in Inheritance
a) When a class inherits from more than one base class 11. If a base class member is declared as private, what will be its
b) When multiple derived classes share the same base class access level in the derived class?
c) When a class cannot be inherited further a) Private
d) When a class has multiple objects b) Protected
Answer: a) When a class inherits from more than one base class c) Public
7. What is multilevel inheritance? d) Not inherited
a) A derived class acts as a base class for another derived class Answer: d) Not inherited
b) More than one derived class is inherited from a single base 12. If a base class member is declared as protected, what will be its
class access level in the derived class?
c) More than one base class is inherited by a single derived class a) Private
d) None of the above b) Protected
c) Public c) Destructors are not called in inheritance
d) Not inherited d) Random order
Answer: b) Protected Answer: b) Derived class destructor first, then base class
13. What happens when a class is inherited privately? 18. Can a derived class constructor explicitly call the base class
a) Public and protected members of the base class become constructor?
private in the derived class a) Yes, using base-class(parameters) in the derived class
b) Private members of the base class become protected in the constructor
derived class b) No, constructors are called automatically
c) Protected members of the base class become public in the c) Only for multiple inheritance
derived class d) Only for virtual inheritance
d) Public members remain public in the derived class Answer: a) Yes, using base-class(parameters) in the derived class
Answer: a) Public and protected members of the base class become constructor
private in the derived class 19. What happens if a base class does not have a default constructor
14. Which of the following is the default inheritance mode in C++? but a derived class tries to inherit from it?
a) Public a) Compilation error
b) Private b) Base class constructor is automatically created
c) Protected c) Derived class does not need to call the base class constructor
d) Depends on the class d) Derived class constructor works fine without any issue
Answer: b) Private Answer: a) Compilation error
15. If a base class is inherited publicly, what will be the access level 20. Consider the following code:
of its public members in the derived class? cpp
a) Private CopyEdit
class Base {
b) Protected public:
c) Public Base() { cout << "Base Constructor\n"; }
d) Not inherited ~Base() { cout << "Base Destructor\n"; }
Answer: c) Public };
class Derived : public Base {
public:
4. Constructor and Destructor in Inheritance Derived() { cout << "Derived Constructor\n"; }
16. In what order are constructors called in inheritance? ~Derived() { cout << "Derived Destructor\n"; }
a) Derived class constructor first, then base class };
int main() {
b) Base class constructor first, then derived class
Derived d;
c) Constructors are not called in inheritance }
d) Random order What will be the output?
Answer: b) Base class constructor first, then derived class a) Base Constructor → Derived Constructor → Derived
17. In what order are destructors called in inheritance? Destructor → Base Destructor
a) Base class destructor first, then derived class b) Derived Constructor → Base Constructor → Base Destructor →
b) Derived class destructor first, then base class Derived Destructor
c) Base Constructor → Derived Constructor → Base Destructor → 4. Which of the following is a valid public inheritance declaration?
Derived Destructor a) class A -> public B { };
d) Compilation error b) class A inherits public B { };
Answer: a) Base Constructor → Derived Constructor → Derived c) class A : public B { };
Destructor → Base Destructor d) class A, B { };
Answer: c) class A : public B { };
Conclusion 5. If a base class has a private member, how is it inherited in the
✅ Inheritance enables code reuse by allowing a derived class to use derived class?
properties and methods of a base class. a) As private
✅ Different types of inheritance include single, multiple, multilevel, b) As protected
hierarchical, and hybrid. c) As public
✅ Access specifiers control inheritance behavior (private members are d) Not inherited
not inherited, protected members remain protected). Answer: d) Not inherited
✅ Constructors are called from base to derived, while destructors are
called in reverse order. 2. Access Specifiers and Inheritance
✅ Multiple inheritance can cause ambiguity, resolved using the scope 6. If a base class is inherited as private, what happens to its public
resolution operator (::). members in the derived class?
1. Basics of Defining Derived Classes a) They remain public
1. What is the correct syntax for defining a derived class in C++? b) They become protected
a) class Derived inherits Base { }; c) They become private
b) class Derived extends Base { }; d) They are not accessible
c) class Derived : public Base { }; Answer: c) They become private
d) class Base -> Derived { }; 7. If a base class is inherited as protected, what happens to its
Answer: c) class Derived : public Base { }; protected members in the derived class?
2. What does the colon (:) symbol specify in class inheritance? a) They remain protected
a) Access specifier b) They become public
b) Scope resolution c) They become private
c) Inheritance relationship d) They are not accessible
d) Function overriding Answer: a) They remain protected
Answer: c) Inheritance relationship 8. Consider the following code:
3. What is the default mode of inheritance in C++? cpp
a) public CopyEdit
b) private class A {
public:
c) protected int x;
d) virtual };
Answer: b) private class B : private A {
public:
void show() { cout << x; } b) A class is inherited from more than one base class
};
c) A derived class acts as a base class for another class
What will happen if an object of B tries to access x directly? d) A base class is inherited more than once
a) x is public, so it is accessible Answer: c) A derived class acts as a base class for another class
b) x is private in B, so it is not accessible 14. What happens when a derived class is inherited from multiple
c) x is protected in B, so it is accessible base classes with the same function name?
d) Compilation error a) Compiler automatically resolves it
Answer: b) x is private in B, so it is not accessible b) Function overloading occurs
9. If a class is inherited as private, can its members be accessed c) Ambiguity error occurs
outside the class using an object? d) The function is hidden in the derived class
a) Yes Answer: c) Ambiguity error occurs
b) No 15. How can ambiguity in multiple inheritance be resolved?
c) Only for friend functions a) Using the scope resolution operator (::)
d) Only for public members b) Using function overloading
Answer: b) No c) Using protected access
10. What is the correct way to inherit a class as protected? d) Using friend functions
a) class Derived protected Base { }; Answer: a) Using the scope resolution operator (::)
b) class Derived : protected Base { };
c) class Derived { protected Base }; 4. Virtual Base Classes and Constructor Execution
d) class Derived (protected) Base { }; 16. What is the purpose of a virtual base class in inheritance?
Answer: b) class Derived : protected Base { }; a) To prevent inheritance
b) To avoid multiple copies of a base class in multiple
3. Multiple and Multilevel Inheritance inheritance
11. What is multiple inheritance? c) To make all members private
a) A derived class has multiple objects d) To prevent function overloading
b) A class has multiple constructors Answer: b) To avoid multiple copies of a base class in multiple
c) A derived class has multiple base classes inheritance
d) A base class has multiple derived classes 17. How do you declare a virtual base class?
Answer: c) A derived class has multiple base classes a) class Derived : virtual Base { };
12. Which syntax correctly represents multiple inheritance? b) class Derived -> virtual Base { };
a) class A : B, C { }; c) class Derived extends virtual Base { };
b) class A : public B, public C { }; d) class Derived inherits virtual Base { };
c) class A inherits B, C { }; Answer: a) class Derived : virtual Base { };
d) class A -> B, C { }; 18. In what order are constructors executed in multiple
Answer: b) class A : public B, public C { }; inheritance?
13. What is multilevel inheritance? a) Derived class first, then base class
a) A class is inherited by multiple derived classes b) Base class first, then derived class
c) Depends on the compiler operator.
d) Only derived class constructor is called ✅ Virtual base classes help avoid redundant copies of base class
Answer: b) Base class first, then derived class members.
19. What happens if a derived class does not explicitly call a base ✅ Constructors execute from base to derived, while destructors execute
class constructor? in reverse order.
a) The compiler automatically calls the default constructor 1. What is single inheritance in C++?
b) The base class constructor is not executed a) A class inherits from multiple base classes
c) Compilation error occurs b) A class inherits only from one base class
d) The base class constructor is skipped c) A class has multiple constructors
Answer: a) The compiler automatically calls the default constructor d) A class cannot inherit any class
20. Consider the following code: Answer: b) A class inherits only from one base class
cpp 2. What is the correct syntax for single inheritance?
CopyEdit
a) class Derived extends Base { };
class A {
public: b) class Derived : public Base { };
A() { cout << "A Constructor "; } c) class Base -> Derived { };
}; d) class Derived inherits Base { };
class B : public A {
Answer: b) class Derived : public Base { };
public:
B() { cout << "B Constructor "; } 3. What happens to private members of a base class in single
}; inheritance?
class C : public B { a) They are accessible in the derived class
public: b) They are inherited as private members
C() { cout << "C Constructor "; }
}; c) They are not inherited
int main() { d) They are inherited as public members
C obj; Answer: c) They are not inherited
} 4. If a base class is inherited privately, what will be the access level
What will be the output? of its public members in the derived class?
a) A Constructor B Constructor C Constructor a) Public
b) C Constructor B Constructor A Constructor b) Private
c) B Constructor A Constructor C Constructor c) Protected
d) Compilation error d) Not accessible
Answer: a) A Constructor B Constructor C Constructor Answer: b) Private
5. What will be the output of the following code?
Conclusion cpp
CopyEdit
✅ A derived class is defined using class Derived : access Base { }.
class A {
✅ Access specifiers (public, protected, private) control how base class public:
members are inherited. void show() { cout << "Base class"; }
✅ Multiple inheritance can cause ambiguity, resolved using the :: };
class B : public A { }; cpp
int main() { CopyEdit
B obj; class A {
obj.show(); public:
} void show() { cout << "Class A"; }
a) Base class };
class B {
b) Derived class public:
c) Compilation error void show() { cout << "Class B"; }
d) Base class Derived class };
Answer: a) Base class class C : public A, public B {
};
int main() {
2. Multiple Inheritance C obj;
6. What is multiple inheritance? obj.show();
a) A derived class inherits from multiple base classes }
b) A base class has multiple derived classes What will be the output?
c) A class has multiple member functions a) Class A
d) A class inherits from only one base class b) Class B
Answer: a) A derived class inherits from multiple base classes c) Ambiguity error
7. What is the correct syntax for multiple inheritance? d) Compilation error
a) class A : B, C { }; Answer: c) Ambiguity error
b) class A extends B, C { };
c) class A inherits B, C { }; 3. Hybrid (Multipath) Inheritance
d) class A -> B, C { }; 11. What is hybrid inheritance?
Answer: a) class A : B, C { }; a) Inheritance involving multiple levels
8. Which of the following is a potential issue in multiple b) A combination of multiple and multilevel inheritance
inheritance? c) A combination of private and public inheritance
a) Memory leak d) A class that has only private members
b) Constructor overloading Answer: b) A combination of multiple and multilevel inheritance
c) Ambiguity 12. Which of the following is an example of hybrid inheritance?
d) Encapsulation a) class A : public B, public C { };
Answer: c) Ambiguity b) class A : public B { }; class C : public A { };
9. How can ambiguity be resolved in multiple inheritance? c) class A : virtual public B, virtual public C { };
a) Using function overloading d) class A : public B { }; class C : public A, public B
{ };
b) Using protected access specifier
Answer: d) class A : public B { }; class C : public A, public
c) Using scope resolution operator :: B { };
d) Using friend functions 13. Which major issue does hybrid inheritance introduce?
Answer: c) Using scope resolution operator :: a) Code duplication
10. Consider the following code: b) Function overloading
c) Multiple copies of base class members cpp
d) Encapsulation CopyEdit
class A {
Answer: c) Multiple copies of base class members public:
14. How can multiple copies of a base class be avoided in hybrid A() { cout << "A Constructor "; }
inheritance? };
a) Using friend functions class B : virtual public A { };
b) Using virtual base classes class C : virtual public A { };
class D : public B, public C { };
c) Using default arguments int main() {
d) Using protected access specifier D obj;
Answer: b) Using virtual base classes }
15. Which type of inheritance is NOT included in hybrid inheritance? What will be the output?
a) Single a) A Constructor B Constructor C Constructor D Constructor
b) Multiple b) B Constructor C Constructor D Constructor
c) Multilevel c) A Constructor D Constructor
d) Encapsulated d) A Constructor
Answer: d) Encapsulated Answer: c) A Constructor D Constructor
20. Why does virtual inheritance solve the diamond problem?
4. Virtual Base Classes a) It prevents multiple base classes
16. What is the purpose of a virtual base class? b) It ensures that only one instance of the base class is inherited
a) To create an abstract class c) It removes function overloading
b) To prevent multiple copies of the base class in multiple inheritance d) It prevents constructors from executing
c) To override base class functions Answer: b) It ensures that only one instance of the base class is
d) To make all functions private inherited
Answer: b) To prevent multiple copies of the base class in multiple
inheritance Conclusion
17. How do you declare a virtual base class? ✅ Single inheritance involves one base and one derived class.
a) class Derived : public virtual Base { }; ✅ Multiple inheritance allows a derived class to inherit from multiple
b) class Derived -> virtual Base { }; base classes, leading to ambiguity.
c) class Derived extends virtual Base { }; ✅ Hybrid inheritance combines multiple and multilevel inheritance.
d) class Derived inherits virtual Base { }; ✅ Virtual base classes prevent redundant copies of base class members
Answer: a) class Derived : public virtual Base { }; in hybrid inheritance.
18. In virtual base class, when is the base class constructor executed?
1. Basics of Abstract Classes
a) When an object of the derived class is created
1. What is an abstract class in C++?
b) When an object of the base class is created
a) A class that has only private members
c) When an object of the grandchild class is created
d) Never executed
b) A class that contains at least one pure virtual function
Answer: c) When an object of the grandchild class is created c) A class that cannot have a constructor
19. Consider the following code: d) A class that has all functions declared as virtual
Answer: b) A class that contains at least one pure virtual 7. How can an abstract class be used in C++?
function a) By inheriting from it and overriding all pure virtual functions
2. How do you declare a pure virtual function in an abstract class? b) By creating an object of it directly
a) virtual void show() { } c) By using the new keyword
b) void show() = 0; d) By declaring it as static
c) virtual void show() = 0; Answer: a) By inheriting from it and overriding all pure virtual
d) void show(); functions
Answer: c) virtual void show() = 0; 8. Consider the following code:
3. What happens if a class has a pure virtual function but does not cpp
CopyEdit
override it in a derived class? class A {
a) It results in a compilation error public:
b) The derived class becomes an abstract class virtual void display() = 0;
c) The program crashes at runtime };
d) It works normally without issues int main() {
A obj;
Answer: b) The derived class becomes an abstract class }
4. Which of the following is true about abstract classes? What will happen?
a) They must have at least one virtual function a) Compilation succeeds and runs normally
b) They cannot have normal member functions b) Compilation fails due to the pure virtual function
c) They can have constructors and destructors c) The program crashes at runtime
d) Both a and c d) It gives a warning but runs
Answer: d) Both a and c Answer: b) Compilation fails due to the pure virtual function
5. Which of the following C++ keywords is used to make a class 9. How can we call a function of an abstract class without creating
abstract? its object?
a) virtual a) Using an object
b) abstract b) Using a pointer to the derived class
c) static c) Using the dot operator
d) final d) It is not possible
Answer: a) virtual Answer: b) Using a pointer to the derived class
10. What will happen if a derived class does not override a pure
2. Instantiation & Usage of Abstract Classes virtual function?
6. Can an abstract class be instantiated (i.e., can you create an a) The derived class remains abstract
object of an abstract class)? b) The derived class will be instantiated normally
a) Yes c) The program will crash at runtime
b) No d) The base class function will be called
c) Only if it contains a constructor Answer: a) The derived class remains abstract
d) Only if all functions are defined
Answer: b) No 3. Example-Based MCQs
11. What will be the output of the following program? c) No output
cpp d) Output: Derived class
CopyEdit
#include <iostream>
Answer: a) Compilation error
using namespace std;
4. Advanced Concepts in Abstract Classes
class Base { 13. Which of the following statements is true about abstract classes?
public:
virtual void show() = 0;
a) An abstract class must have at least one pure virtual function
}; b) An abstract class cannot have a constructor
c) Abstract classes can be instantiated
class Derived : public Base { d) An abstract class must override all its own functions
public: Answer: a) An abstract class must have at least one pure virtual
void show() { cout << "Derived class"; }
}; function
14. How can you prevent a class from being inherited?
int main() { a) Declare it as static
Derived d; b) Declare it as final
d.show();
return 0;
c) Declare it as abstract
} d) Declare it as sealed
a) Compilation error Answer: b) Declare it as final
b) Runtime error 15. Which of the following correctly defines an abstract class?
cpp
c) Output: Derived class
CopyEdit
d) No output class A {
Answer: c) Output: Derived class public:
12. What will be the output of the following code? virtual void show() = 0;
cpp };
CopyEdit a) This is an invalid abstract class
class Base { b) This is a valid abstract class
public:
virtual void show() = 0;
c) This class must implement show()
}; d) This class can only contain static functions
Answer: b) This is a valid abstract class
class Derived : public Base { };
int main() {
5. Virtual Destructors in Abstract Classes
Derived d; 16. Why should an abstract class have a virtual destructor?
return 0; a) To ensure proper deallocation of memory when deleting
} objects through a base class pointer
a) Compilation error b) To prevent memory allocation
b) Runtime error c) To allow multiple inheritance
d) It is not required
Answer: a) To ensure proper deallocation of memory when 4. Which access specifier allows a base class member to be
deleting objects through a base class pointer accessible anywhere in the program?
17. What happens if an abstract class has a non-virtual destructor? a) public
a) Memory leaks may occur b) private
b) Destructor overloading will happen c) protected
c) Base class destructor is not called d) None of the above
d) Nothing changes Answer: a) public
Answer: a) Memory leaks may occur
2. Access Specifiers in Inheritance
Conclusion 5. In public inheritance, what happens to the protected members
✅ Abstract classes contain at least one pure virtual function (virtual of the base class?
void func() = 0;). a) They become public in the derived class
✅ They cannot be instantiated directly but can be used as base classes. b) They remain protected in the derived class
✅ If a derived class does not override all pure virtual functions, it c) They become private in the derived class
remains abstract. d) They are not inherited
✅ Virtual destructors are necessary to ensure proper cleanup. Answer: b) They remain protected in the derived class
1. Basics of Access Specifiers 6. In private inheritance, what happens to the public members of
1. Which of the following are valid access specifiers in C++? the base class?
a) public, private, protected a) They become private in the derived class
b) default, private, protected b) They become protected in the derived class
c) internal, private, protected c) They remain public in the derived class
d) protected, secured, private d) They are not inherited
Answer: a) public, private, protected Answer: a) They become private in the derived class
2. What is the default access specifier for class members in C++? 7. In protected inheritance, what happens to the public members
a) public of the base class?
b) private a) They become public in the derived class
c) protected b) They become private in the derived class
d) default c) They become protected in the derived class
Answer: b) private d) They are not inherited
3. Which access specifier allows a base class member to be Answer: c) They become protected in the derived class
accessible only within the derived class but not outside? 8. Consider the following code:
a) public cpp
b) private CopyEdit
class A {
c) protected public:
d) friend int x;
Answer: c) protected protected:
int y;
private: cpp
int z; CopyEdit
}; class A {
class B : public A { }; public:
Which members of A are accessible inside B? int x;
protected:
a) x and y int y;
b) x, y, and z private:
c) x only int z;
d) None of them };
class B : protected A { };
Answer: a) x and y int main() {
9. Consider the following code: B obj;
cpp obj.x = 10;
CopyEdit }
class A {
public: What will happen?
int x; a) Compilation error
protected: b) It runs successfully
int y; c) Runtime error
private: d) None of the above
int z;
}; Answer: a) Compilation error (because x becomes protected in
class B : private A { }; B and is not accessible outside)
Which members of A are accessible inside B? 12. Consider the following code:
a) x and y cpp
CopyEdit
b) x, y, and z class A {
c) x only public:
d) None of them int x;
Answer: a) x and y (but both become private in B) };
class B : private A { };
10. Which of the following is true for private inheritance? int main() {
a) Base class public members become private in the derived B obj;
class obj.x = 5;
b) Base class protected members become private in the return 0;
}
derived class
c) Private inheritance is used to restrict access to base class What will happen?
members a) Compilation error
d) All of the above b) Runtime error
Answer: d) All of the above c) It runs successfully and prints 5
d) None of the above
Answer: a) Compilation error (because x becomes private in B
3. Example-Based MCQs
and is not accessible)
11. Consider the following code:
13. What will be the output of the following program? Derived obj;
cpp obj.display();
CopyEdit return 0;
class A { }
protected: a) 5
int x = 10; b) Compilation error
};
c) Undefined behavior
class B : public A {
public: d) None of the above
void show() { cout << x; } Answer: a) 5
}; 16. Can a derived class access private members of the base class
int main() { directly?
B obj;
obj.show(); a) Yes, always
return 0; b) No, never
} c) Only through public inheritance
a) 10 d) Only if declared as protected
b) Compilation error Answer: b) No, never
c) Runtime error 17. What happens if a derived class inherits a base class without
d) Undefined behavior specifying an access specifier?
Answer: a) 10 a) It is public by default
b) It is private by default
4. Advanced Concepts on Access Specifiers in Inheritance c) It is protected by default
14. Can a private member of a base class be accessed in a derived d) Compilation error
class? Answer: b) It is private by default (for class, public for
a) Yes struct)
b) No
c) Only through friend functions Conclusion
d) Only through protected inheritance ✅ Public inheritance keeps the same access levels as in the base class.
Answer: b) No ✅ Private inheritance makes base class members private in the derived
15. What will be the output of the following program? class.
cpp ✅ Protected inheritance makes public and protected members remain
CopyEdit
protected.
class Base {
protected: ✅ Private members of a base class are never inherited directly.
int x = 5; 1. Basics of Pointers
}; 1. What is a pointer in C++?
class Derived : public Base {
a) A variable that stores an address
public:
void display() { cout << x; } b) A reference to another variable
}; c) A special type of integer
int main() { d) A data type used for file handling
Answer: a) A variable that stores an address Answer: b) It moves to the next memory location based on the
2. What is the correct syntax to declare a pointer to an integer? data type
a) int ptr; 7. If an int takes 4 bytes and a pointer ptr is pointing to an integer
b) int *ptr; at memory location 1000, what will be the value of ptr + 2?
c) int &ptr; a) 1002
d) ptr int*; b) 1004
Answer: b) int *ptr; c) 1008
3. If ptr is a pointer to an integer, what does *ptr represent? d) 1006
a) The memory address stored in ptr Answer: c) 1008
b) The value stored at the memory address ptr points to
c) The size of the pointer 3. Pointer and Arrays
d) The reference of the pointer 8. What will be the output of the following code?
Answer: b) The value stored at the memory address ptr points cpp
to CopyEdit
int arr[] = {10, 20, 30, 40};
4. What will be the output of the following code? int *ptr = arr;
cpp cout << *(ptr + 2);
CopyEdit
int a = 10; a) 10
int *ptr = &a; b) 20
cout << *ptr; c) 30
a) 10 d) Compilation error
b) Address of a Answer: c) 30
c) Compilation error 9. Which of the following statements is true about array and
d) Undefined behavior pointer relation?
Answer: a) 10 a) arr is a pointer to the first element
b) arr stores the address of all elements
2. Pointer Arithmetic c) &arr gives the address of the last element
5. If ptr is a pointer to an int, what does ptr + 1 do? d) arr+1 moves to the last element
a) Moves the pointer to the next byte Answer: a) arr is a pointer to the first element
b) Moves the pointer to the next integer 10. Given int arr[5] = {1, 2, 3, 4, 5};, what does *(arr + 3)
c) Causes a compilation error return?
d) Resets the pointer a) 1
Answer: b) Moves the pointer to the next integer b) 3
6. What happens when you increment a pointer (ptr++) in C++? c) 4
a) It increases the value stored at the address d) 5
b) It moves to the next memory location based on the data type Answer: c) 4
c) It decrements the pointer
d) It causes a runtime error 4. Pointers and Functions
11. What is the advantage of passing pointers to functions? a) delete ptr;
a) Saves memory by avoiding copy operations b) delete ptr[];
b) Increases execution speed c) delete[] ptr;
c) Allows modification of original values d) free(ptr);
d) All of the above Answer: c) delete[] ptr;
Answer: d) All of the above
12. What will be the output of the following program? 6. Pointer to Pointer (Double Pointers)
cpp
CopyEdit
16. What does a double pointer store?
void fun(int *ptr) { a) Address of an integer
*ptr = 20; b) Address of another pointer
} c) Address of an array
int main() { d) Address of a function
int x = 10;
fun(&x); Answer: b) Address of another pointer
cout << x; 17. What will be the output of the following code?
} cpp
CopyEdit
a) 10
int a = 10;
b) 20 int *p = &a;
c) Compilation error int **q = &p;
d) Undefined behavior cout << **q;
Answer: b) 20 a) 10
b) Address of a
5. Dynamic Memory Allocation with Pointers c) Address of p
13. Which function is used to allocate memory dynamically in C++? d) Compilation error
a) alloc() Answer: a) 10
b) malloc()
c) new 7. Null and Wild Pointers
d) free() 18. What is a NULL pointer?
Answer: c) new a) A pointer that points to an invalid memory location
14. What happens if delete is not used after dynamic memory b) A pointer that stores 0
allocation? c) A pointer that points to the last memory location
a) Compilation error d) A pointer with a dynamic address
b) Memory leak Answer: b) A pointer that stores 0
c) Pointer reset 19. Which of the following statements is true about wild pointers?
d) Automatic deallocation a) They are properly initialized pointers
Answer: b) Memory leak b) They are pointers that are not initialized
15. What is the correct way to release dynamically allocated c) They store the value NULL
memory for an array? d) They always cause segmentation faults
Answer: b) They are pointers that are not initialized c) const ClassName*
d) ClassName*
8. Function Pointers Answer: d) ClassName*
20. What is a function pointer in C++? 4. What will be the output of the following code?
a) A pointer that points to a function’s return type cpp
CopyEdit
b) A pointer that stores a function’s address #include <iostream>
c) A pointer to a function variable using namespace std;
d) A pointer to a class class Test {
Answer: b) A pointer that stores a function’s address public:
21. What is the correct syntax to declare a function pointer? void show() {
cout << this;
a) void ptr(); }
b) void (*ptr)(); };
c) void &ptr(); int main() {
d) void *ptr(); Test obj;
obj.show();
Answer: b) void (*ptr)(); }
a) Address of the object obj
Conclusion b) Compilation error
✅ Pointers store memory addresses and allow direct access to memory. c) Address of show() function
✅ Pointer arithmetic depends on data type size. d) Runtime error
✅ Dynamic memory allocation requires new and delete. Answer: a) Address of the object obj
✅ Function pointers allow calling functions dynamically.
1. Basics of this Pointer 2. Usage of this Pointer
1. What is the this pointer in C++? 5. Why is the this pointer used in C++?
a) A pointer to the first element of a class a) To differentiate between local and class variables with the
b) A pointer to the current object same name
c) A pointer to the base class object b) To access private members of a class
d) A pointer to the function of a class c) To call a constructor explicitly
Answer: b) A pointer to the current object d) To create an instance of a class
2. The this pointer is available in: Answer: a) To differentiate between local and class variables
a) Static member functions only with the same name
b) Non-static member functions only 6. What will be the output of the following code?
c) Both static and non-static member functions cpp
d) Friend functions CopyEdit
Answer: b) Non-static member functions only #include <iostream>
using namespace std;
3. What type of pointer is this in C++? class Test {
a) int* private:
b) void* int x;
public: Test& setX(int x) {
void setX(int x) { this->x = x;
this->x = x; return *this;
} }
int getX() { void show() {
return x; cout << x;
} }
}; };
int main() { int main() {
Test obj; Test obj;
obj.setX(5); obj.setX(10).show();
cout << obj.getX(); }
} a) 10
a) 0 b) Compilation error
b) 5 c) Garbage value
c) Compilation error d) Runtime error
d) Undefined behavior Answer: a) 10
Answer: b) 5 10. Why is this used in method chaining?
7. Which of the following is true about the this pointer? a) To return the address of the class
a) It can be modified b) To return the current object
b) It points to the current object c) To create a new object
c) It is available in static member functions d) To prevent memory leaks
d) It must be explicitly defined in every class Answer: b) To return the current object
Answer: b) It points to the current object
8. The this pointer can be used to: 4. this Pointer in Constructor & Destructor
a) Return the current object 11. Can this pointer be used inside a constructor?
b) Modify static variables a) Yes
c) Change the address of an object b) No
d) Access private data members directly c) Only for static members
Answer: a) Return the current object d) Only in virtual constructors
Answer: a) Yes
3. this Pointer and Method Chaining 12. What is the purpose of this pointer inside a constructor?
9. What will be the output of the following code? a) To refer to the current object being constructed
cpp
b) To allocate memory dynamically
CopyEdit
#include <iostream> c) To call the destructor
using namespace std; d) To create multiple instances of a class
class Test { Answer: a) To refer to the current object being constructed
private: 13. What happens when this is used in a destructor?
int x;
public: a) It points to the current object being destroyed
b) It creates a copy of the object c) this pointer helps in method chaining
c) It causes a segmentation fault d) this pointer is available in constructors
d) It deletes the object from memory Answer: b) this pointer can be modified
Answer: a) It points to the current object being destroyed
6. this Pointer with Operator Overloading
5. Advanced Concepts 17. What is returned when this is used inside an overloaded
14. Can this pointer be used to call another constructor within the assignment operator?
same class? a) A new object
a) Yes, by using this explicitly b) The same object reference
b) No, constructor delegation must be used c) The base class object
c) Only if the constructor is static d) A pointer to NULL
d) It causes a segmentation fault Answer: b) The same object reference
Answer: b) No, constructor delegation must be used 18. What is the role of this in an assignment operator overload?
15. What will be the output of the following code? a) It returns the calling object
cpp b) It copies the object
CopyEdit
#include <iostream> c) It calls the destructor
using namespace std; d) It deletes the object
class Demo { Answer: a) It returns the calling object
public:
void display() {
cout << this; Conclusion
} ✅ The this pointer stores the address of the current object.
}; ✅ It is used in method chaining, constructors, and operator
int main() { overloading.
Demo obj1, obj2;
obj1.display();
✅ It is not available in static member functions.
cout << " "; ✅ It allows access to class members when local and member variables
obj2.display(); have the same name.
} 1. Basics of Polymorphism
a) Same address printed twice 1. What is polymorphism in C++?
b) Different addresses printed a) The ability to define multiple functions with the same name
c) Compilation error b) The ability to process objects differently based on their data
d) Undefined behavior type
Answer: b) Different addresses printed c) The ability to inherit from multiple classes
16. Which of the following statements about this pointer is d) The ability to use private members of a class
incorrect? Answer: b) The ability to process objects differently based on
a) this pointer can be used in non-static member functions their data type
b) this pointer can be modified 2. Which of the following is an example of compile-time
polymorphism?
a) Function overloading b) *
b) Operator overloading c) ::
c) Virtual functions d) +=
d) Both a and b Answer: c) ::
Answer: d) Both a and b 8. What will be the output of the following code?
3. Which of the following is an example of run-time cpp
polymorphism? CopyEdit
#include <iostream>
a) Function overloading using namespace std;
b) Operator overloading class Test {
c) Virtual functions public:
d) Inline functions void display() { cout << "No arguments" << endl; }
Answer: c) Virtual functions void display(int x) { cout << "Integer: " << x <<
endl; }
4. How is compile-time polymorphism achieved in C++? };
a) Using function overloading int main() {
b) Using operator overloading Test obj;
c) Using virtual functions obj.display();
obj.display(5);
d) Both a and b }
Answer: d) Both a and b
a) Compilation error
5. How is run-time polymorphism achieved in C++?
b) No arguments
a) Using function overloading
Integer: 5
b) Using virtual functions
c) No arguments
c) Using operator overloading
No arguments
d) Using static members
d) Integer: 5
Answer: b) Using virtual functions
Answer: b) No arguments
Integer: 5
2. Function Overloading and Operator Overloading 9. Which of the following is true about operator overloading?
6. What is function overloading? a) All operators can be overloaded
a) Defining multiple functions with the same name but different b) Only arithmetic operators can be overloaded
parameters c) Some operators cannot be overloaded
b) Using the same function in multiple classes d) Operators are overloaded automatically
c) Using a function inside another function Answer: c) Some operators cannot be overloaded
d) Defining multiple functions with the same name and same
parameters 3. Virtual Functions and Function Overriding
Answer: a) Defining multiple functions with the same name but
10. What is function overriding?
different parameters
a) A function with the same name and different parameters in
7. Which of the following operators cannot be overloaded?
different classes
a) +
b) A function with the same name and same parameters in a
derived class c) The program will not compile
c) A function inside another function d) The compiler will throw an error
d) Using the override keyword in function overloading Answer: b) The base class function is always called
Answer: b) A function with the same name and same 14. What is a pure virtual function?
parameters in a derived class a) A function with no definition
11. What keyword is used in C++ to achieve run-time b) A function that is not inherited
polymorphism? c) A function with a default implementation
a) virtual d) A function that cannot be overridden
b) static Answer: a) A function with no definition
c) inline 15. Which syntax correctly declares a pure virtual function in C++?
d) friend a) virtual void show();
Answer: a) virtual b) virtual void show() = 0;
12. What will be the output of the following code? c) void show() = 0;
cpp d) virtual show() {};
CopyEdit Answer: b) virtual void show() = 0;
#include <iostream>
using namespace std;
class Base { 4. Abstract Classes and Virtual Destructors
public: 16. What is an abstract class in C++?
virtual void show() { cout << "Base class" << endl; a) A class that cannot be inherited
}
}; b) A class with at least one pure virtual function
class Derived : public Base { c) A class with only static members
public: d) A class that must have a constructor
void show() { cout << "Derived class" << endl; } Answer: b) A class with at least one pure virtual function
};
int main() {
17. What happens if an abstract class is instantiated?
Base *b; a) The program compiles and runs normally
Derived d; b) The program does not compile
b = &d; c) The derived class function is called
b->show(); d) The base class function is called
}
Answer: b) The program does not compile
a) Base class
18. What is the purpose of a virtual destructor?
b) Derived class
a) To allow the destruction of base class objects only
c) Compilation error
b) To ensure the correct order of destructor calls in inheritance
d) Undefined behavior
c) To prevent memory leaks in operator overloading
Answer: b) Derived class
d) To automatically overload operators
13. What happens if a base class function is not declared as virtual?
Answer: b) To ensure the correct order of destructor calls in
a) The derived class function is always called
inheritance
b) The base class function is always called
5. Advanced Concepts c) virtual void show() = 0;
19. Which of the following best describes dynamic binding? d) pure virtual void show();
a) Function calls are resolved at compile time Answer: c) virtual void show() = 0;
b) Function calls are resolved at runtime 3. What is the effect of having at least one pure virtual function in
c) Function calls are resolved based on the number of a class?
parameters a) The class cannot have any objects
d) Function calls are determined based on static data members b) The class becomes an abstract class
Answer: b) Function calls are resolved at runtime c) The derived class must override all pure virtual functions
20. In C++ polymorphism, which table stores addresses of virtual d) All of the above
functions? Answer: d) All of the above
a) VMT (Virtual Method Table) 4. If a derived class does not override a pure virtual function from
b) OVT (Overloaded Virtual Table) its base class, what happens?
c) BMT (Base Method Table) a) The derived class remains an abstract class
d) CMT (Compile-time Method Table) b) The derived class compiles without error
Answer: a) VMT (Virtual Method Table) c) The derived class generates a runtime error
d) The base class function gets automatically invoked
Conclusion Answer: a) The derived class remains an abstract class
✅ Polymorphism allows objects to be treated as instances of their base 5. Which of the following statements about pure virtual functions
type. is false?
✅ Compile-time polymorphism is achieved using function overloading a) A class containing a pure virtual function is abstract
and operator overloading. b) A pure virtual function must be overridden in a derived class
✅ Run-time polymorphism is achieved using virtual functions. c) An abstract class cannot have normal (non-virtual) functions
✅ A pure virtual function makes a class abstract, preventing d) A pure virtual function can have a body in the base class
instantiation. Answer: c) An abstract class cannot have normal (non-virtual)
1. Pure Virtual Functions functions
1. What is a pure virtual function in C++?
a) A function with a default implementation 2. Virtual Constructors
b) A function that is inherited but cannot be overridden 6. Why can't a constructor be declared virtual in C++?
c) A function declared in a base class with = 0 and no definition a) Constructors do not participate in polymorphism
d) A function that cannot be called in a derived class b) Virtual functions require an object to exist, but constructors
Answer: c) A function declared in a base class with = 0 and no create objects
definition c) The VTable is not created until after the constructor runs
2. Which of the following correctly declares a pure virtual d) All of the above
function? Answer: d) All of the above
a) virtual void show(); 7. If virtual constructors are not allowed, how can we achieve
b) void show() = 0; constructor behavior in a polymorphic way?
a) Using a static factory method
b) Using a friend function c) To allow dynamic binding of destructors
c) Using a pointer to a constructor d) All of the above
d) Using a pure virtual function Answer: d) All of the above
Answer: a) Using a static factory method 13. Which of the following correctly declares a pure virtual
8. How can virtual behavior be mimicked in constructor-like destructor?
operations? a) virtual ~Base();
a) By defining a clone() function in the base class b) virtual ~Base() = 0;
b) By using a friend constructor c) pure virtual ~Base();
c) By using a copy constructor d) virtual void ~Base() = 0;
d) By overriding the constructor in the derived class Answer: b) virtual ~Base() = 0;
Answer: a) By defining a clone() function in the base class 14. If a base class has a pure virtual destructor, what must a
9. What happens if a constructor is declared virtual in a C++ derived class do?
class? a) It must implement the destructor
a) It works normally b) It must override all pure virtual functions
b) The program compiles but does nothing c) It must call the base class destructor explicitly
c) The compiler generates an error d) It must define a static function
d) The constructor is ignored at runtime Answer: a) It must implement the destructor
Answer: c) The compiler generates an error 15. What happens if a class has a pure virtual destructor but no
10. How can a base class create objects of a derived class without definition?
knowing the exact derived type? a) The program compiles but crashes at runtime
a) By defining a static factory method in the base class b) The compiler throws an error
b) By using a friend function c) The destructor is ignored
c) By directly instantiating the derived class d) The base class object gets deleted without calling the
d) By using a global function pointer destructor
Answer: a) By defining a static factory method in the base class Answer: b) The compiler throws an error
3. Pure Virtual Destructors 4. Advanced Concepts
11. Can a destructor be pure virtual in C++? 16. Why must a pure virtual destructor have a body?
a) Yes a) Because destructors are always called during object
b) No destruction
c) Only in derived classes b) Because virtual functions require a definition
d) Only in base classes c) Because abstract classes need at least one implemented
Answer: a) Yes function
12. What is the purpose of a pure virtual destructor? d) Because destructors can never be overridden
a) To make the base class abstract Answer: a) Because destructors are always called during object
b) To enforce deletion of derived class objects via a base class destruction
pointer
17. What happens when a virtual destructor is not used in an ✅ A pure virtual destructor must have a body and ensures proper
inheritance hierarchy? cleanup in polymorphic inheritance.
a) Memory leaks may occur 1. Which header file is required for console I/O operations in C++?
b) The base class destructor is called, but not the derived class a) #include <stdio.h>
destructor b) #include <conio.h>
c) The derived class destructor is called, but not the base class c) #include <iostream>
destructor d) #include <fstream>
d) Both a and b Answer: c) #include <iostream>
Answer: d) Both a and b 2. What is the standard output stream in C++?
18. What is the primary purpose of declaring a destructor virtual? a) cin
a) To allow overloading of destructors b) cout
b) To prevent object slicing c) cerr
c) To ensure the correct order of destructor calls in inheritance d) clog
d) To allocate memory dynamically Answer: b) cout
Answer: c) To ensure the correct order of destructor calls in 3. Which operator is used for output in C++?
inheritance a) <<
19. Which of the following statements about pure virtual b) >>
destructors is false? c) ==
a) A pure virtual destructor must have a definition d) **
b) A class with a pure virtual destructor becomes abstract Answer: a) <<
c) A pure virtual destructor cannot be inherited 4. What is the standard input stream in C++?
d) A pure virtual destructor ensures proper cleanup of derived a) cout
class objects b) cerr
Answer: c) A pure virtual destructor cannot be inherited
c) cin
20. What is the role of a virtual base class in multiple inheritance?
d) clog
a) To prevent redundant copies of base class members
Answer: c) cin
b) To ensure virtual destructors work correctly
5. What is the purpose of the endl manipulator?
c) To create a new VTable for polymorphism
d) To enable deep copy behavior a) To insert a new line and flush the output buffer
Answer: a) To prevent redundant copies of base class members b) To move the cursor back one space
c) To format floating-point numbers
d) To end the program
Conclusion
Answer: a) To insert a new line and flush the output buffer
✅ Pure virtual functions make a class abstract and force derived
classes to implement them.
✅ Virtual constructors are not allowed, but similar behavior can be 2. Using I/O Manipulators
achieved using factory methods. 6. Which header file is required to use manipulators like setw,
setprecision, and fixed?
a) #include <iostream>
b) #include <iomanip> c) Reads an integer value
c) #include <fstream> d) Clears the input buffer
d) #include <sstream> Answer: a) Reads a single character, including whitespace
Answer: b) #include <iomanip> 12. What does the cin.ignore(10, '\n') function do?
7. What is the purpose of the setw(n) manipulator? a) Ignores 10 characters or until a newline ('\n') is encountered
a) Sets the decimal precision b) Clears the entire input buffer
b) Sets the width of the next output field c) Reads 10 characters and stores them
c) Prints n blank spaces d) None of the above
d) None of the above Answer: a) Ignores 10 characters or until a newline ('\n') is
Answer: b) Sets the width of the next output field encountered
8. What does the fixed manipulator do? 13. What is the use of cin.fail()?
a) Fixes the width of the output a) Checks if the last input operation failed
b) Fixes the decimal precision to 2 b) Clears the input buffer
c) Displays floating-point numbers in fixed-point notation c) Terminates the program
d) Prevents modification of the output format d) None of the above
Answer: c) Displays floating-point numbers in fixed-point Answer: a) Checks if the last input operation failed
notation 14. What does the cin.clear() function do?
9. What does the showpoint manipulator do? a) Clears any error flags in cin
a) Shows trailing zeros in floating-point numbers b) Clears the input buffer
b) Forces the display of a decimal point for floating-point c) Reads an integer value
numbers d) Stops further input operations
c) Increases the precision of output Answer: a) Clears any error flags in cin
d) Removes the decimal point 15. How can you read an entire line of input, including spaces?
Answer: b) Forces the display of a decimal point for floating- a) cin >> str;
point numbers b) cin.read(str, size);
10. What is the default precision for floating-point output in C++? c) getline(cin, str);
a) 2 decimal places d) cin.getline(str, size);
b) 6 decimal places Answer: c) getline(cin, str);
c) 8 decimal places
d) It depends on the compiler 4. Error Handling and Special Streams
Answer: b) 6 decimal places 16. Which stream is used for error messages in C++?
a) cin
3. Input Handling and Formatting b) cout
11. What does the cin.get() function do? c) cerr
a) Reads a single character, including whitespace d) clog
b) Reads an entire string Answer: c) cerr
17. What is the difference between cerr and clog? 1. Which header file is required for file handling in C++?
a) cerr is buffered, clog is unbuffered a) #include <iostream>
b) cerr is unbuffered, clog is buffered b) #include <fstream>
c) cerr and clog are identical c) #include <iomanip>
d) cerr is for warnings, clog is for errors d) #include <string>
Answer: b) cerr is unbuffered, clog is buffered Answer: b) #include <fstream>
18. What will happen if we use cin >> int_variable; and enter a 2. Which of the following is the correct way to open a file for both
string instead of an integer? reading and writing?
a) The program will crash a) fstream file("data.txt");
b) cin will enter a fail state b) fstream file("data.txt", ios::in | ios::out);
c) The input buffer will be cleared automatically c) ifstream file("data.txt", ios::in | ios::out);
d) The first character of the string will be stored in the integer d) ofstream file("data.txt", ios::in | ios::out);
Answer: b) cin will enter a fail state Answer: b) fstream file("data.txt", ios::in | ios::out);
19. Which function can be used to reset the cin stream after an 3. What does ios::app do when opening a file?
error occurs? a) Opens a file for reading only
a) cin.reset(); b) Opens a file in append mode
b) cin.fail(); c) Deletes existing content before writing
c) cin.clear(); d) Opens the file in binary mode
d) cin.ignore(); Answer: b) Opens a file in append mode
Answer: c) cin.clear(); 4. What is the default mode in which an ifstream object opens a
20. If you want to flush the output buffer manually, which function file?
should you use? a) ios::in
a) flush(); b) ios::out
b) cout.clear(); c) ios::binary
c) cout.flush(); d) ios::app
d) cin.flush(); Answer: a) ios::in
Answer: c) cout.flush(); 5. What happens if a file is opened with ios::trunc mode?
a) It is opened at the end
Conclusion b) It is deleted after closing
✅ C++ console I/O operations use cin, cout, cerr, and clog. c) The file is truncated to zero length if it exists
✅ I/O manipulators (setw, fixed, setprecision) control formatting. d) The file is opened in binary mode
✅ Error handling in cin is done using cin.fail(), cin.clear(), and Answer: c) The file is truncated to zero length if it exists
cin.ignore().
✅ Buffered vs unbuffered output streams: clog is buffered, cerr is 2. File Handling Operations
unbuffered. 6. Which C++ class is used for writing data to a file?
1. Basics of File Handling in C++ a) ifstream
b) ofstream
c) fstream b) seekp() for input, seekg() for output
d) istream c) move()
Answer: b) ofstream d) set_pointer()
7. What does the following code do? Answer: a) seekg() for input, seekp() for output
cpp 12. How do you move the file pointer to the end of a file in input
CopyEdit
ofstream file("example.txt", ios::app);
mode?
file << "Hello, World!"; a) file.seekg(0, ios::end);
file.close(); b) file.seekp(0, ios::beg);
a) Overwrites "Hello, World!" in example.txt c) file.seekg(ios::end);
b) Deletes the file before writing d) file.move_to_end();
c) Appends "Hello, World!" to example.txt Answer: a) file.seekg(0, ios::end);
d) Opens the file in binary mode 13. Which function is used to find the current position of the file
Answer: c) Appends "Hello, World!" to example.txt pointer?
8. What function is used to check if a file was successfully opened? a) tellg() for input, tellp() for output
a) file.is_open() b) getpos()
b) file.open() c) tellpos()
c) file.exists() d) seekpos()
d) file.good() Answer: a) tellg() for input, tellp() for output
Answer: a) file.is_open() 14. What does ios::ate mode do when opening a file?
9. How do you close a file after working with it? a) Opens the file and moves the file pointer to the end
a) file.end(); b) Truncates the file before opening
b) file.close(); c) Opens the file in binary mode
c) file.stop(); d) Prevents any modifications to the file
d) file.quit(); Answer: a) Opens the file and moves the file pointer to the end
Answer: b) file.close(); 15. If a file is opened in ios::binary mode, what does it do?
10. What will happen if file.close(); is not called explicitly? a) Opens the file in read mode
a) The file will remain locked indefinitely b) Reads and writes data as binary, not text
b) The file will automatically close when the program ends c) Deletes the file before opening
c) The data written to the file will be lost d) Prevents modifications to the file
d) The program will crash Answer: b) Reads and writes data as binary, not text
Answer: b) The file will automatically close when the program
ends 4. Reading and Writing Files
16. Which of the following is a correct way to read a full line from a
3. File Pointers and Positioning file?
11. Which function is used to move the file pointer to a specific a) file.read(str, size);
position? b) getline(file, str);
a) seekg() for input, seekp() for output
c) file >> str; 1. What is the purpose of templates in C++?
d) file.get(); a) To define multiple functions with the same name
Answer: b) getline(file, str); b) To allow generic programming by creating functions or
17. How do you read a single character from a file? classes that work with any data type
a) file.read(ch, 1); c) To create derived classes automatically
b) file.get(ch); d) To define multiple constructors in a class
c) file.readchar(); Answer: b) To allow generic programming by creating functions
d) file >> ch; or classes that work with any data type
Answer: b) file.get(ch); 2. Which keyword is used to define a template in C++?
18. What function is used to write binary data to a file? a) generic
a) write() b) template
b) put() c) typename
c) push() d) class
d) send() Answer: b) template
Answer: a) write() 3. What is the correct syntax for defining a function template?
19. What happens when eof() function returns true? a) template <T> void func(T a) {}
a) The end of the file has been reached b) template <typename T> void func(T a) {}
b) The file has been closed c) template (T) void func(T a) {}
c) The file is empty d) template class T void func(T a) {}
d) The file pointer is at the beginning Answer: b) template <typename T> void func(T a) {}
Answer: a) The end of the file has been reached 4. What is the difference between typename and class in a
20. Which function can be used to reset error states after an input template parameter?
failure? a) class can only be used for classes, typename can be used for
a) file.reset(); any data type
b) file.clear(); b) There is no difference; both work the same way in template
c) file.restart(); definitions
d) file.ignore(); c) typename is mandatory for function templates
Answer: b) file.clear(); d) class can only be used for template specialization
Answer: b) There is no difference; both work the same way in
Conclusion template definitions
✅ C++ uses ifstream, ofstream, and fstream for file handling. 5. Which of the following correctly declares a function template
✅ Modes like ios::app, ios::binary, and ios::trunc control file that swaps two values?
behavior. a)
cpp
✅ File positioning functions (seekg, seekp, tellg, tellp) manage file CopyEdit
pointers. template <class T>
✅ Use getline() for reading full lines and write() for binary data. void swap(T &a, T &b) {
T temp = a;
1. Basics of Templates
a = b; CopyEdit
b = temp; template <class T>
} class Box {
b) T data;
cpp public:
CopyEdit void setData(T d) { data = d; }
template <T> T getData() { return data; }
void swap(T &a, T &b) { };
T temp = a; b)
a = b; cpp
b = temp; CopyEdit
} template (T)
c) class Box {
cpp T data;
CopyEdit public:
template <typename> void setData(T d) { data = d; }
void swap(T &a, T &b) { T getData() { return data; }
T temp = a; };
a = b; c)
b = temp; cpp
} CopyEdit
d) template <>
cpp class Box<T> {
CopyEdit T data;
template <> public:
void swap(T &a, T &b) { void setData(T d) { data = d; }
T temp = a; T getData() { return data; }
a = b; };
b = temp; d)
} cpp
Answer: a) Correct function template declaration CopyEdit
class template Box<T> {
T data;
2. Class Templates public:
6. How do you define a class template? void setData(T d) { data = d; }
a) template <typename T> class MyClass {} T getData() { return data; }
};
b) class template MyClass<T> {}
c) class MyClass<typename T> {} Answer: a) Correct class template declaration
d) template class MyClass<T> {} 8. How do you create an object of a class template Box<T> for int
Answer: a) template <typename T> class MyClass {} type?
a) Box<int> obj;
7. Which of the following correctly defines a class template?
a) b) Box obj<int>;
cpp
c) Box<int>(); class Box {
public:
d) Box<T> obj<int>; void display() { cout << "Specialized for char"; }
Answer: a) Box<int> obj; };
9. What happens if a template class is instantiated without c)
specifying a type? cpp
a) It results in a compilation error CopyEdit
b) It assumes int as the default type template <char>
class Box {
c) It creates a generic object public:
d) It generates an exception at runtime void display() { cout << "Specialized for char"; }
Answer: a) It results in a compilation error };
10. Can a class template have multiple template parameters? d)
a) Yes, using template <typename T, typename U> cpp
CopyEdit
b) No, templates can only have one parameter template <>
c) Yes, but only in function templates class Box<T> {
d) No, C++ does not support multiple template parameters public:
Answer: a) Yes, using template <typename T, typename U> void display() { cout << "Specialized for char"; }
};
Answer: a) Correct template specialization syntax
3. Template Specialization
11. What is template specialization in C++?
a) Creating a specialized version of a template for a specific data 4. Function Templates vs Class Templates
type 13. Which of the following is true about function templates?
b) Overloading a template function a) They allow writing generic functions that work with multiple
c) Using multiple template parameters data types
d) Defining multiple template functions in one program b) They cannot be overloaded
Answer: a) Creating a specialized version of a template for a specific c) They must have at least two template parameters
data type d) They are faster than normal functions
12. How do you specialize a template for char type? Answer: a) They allow writing generic functions that work with
a) multiple data types
cpp 14. Can function templates be overloaded?
CopyEdit a) Yes
template <> b) No
class Box<char> { c) Only if they have different return types
public:
void display() { cout << "Specialized for char"; } d) Only if they have different numbers of parameters
}; Answer: a) Yes
b) 15. Can a template function be defined inside a class?
cpp a) Yes
CopyEdit b) No
template <typename char>
c) Only if it's a friend function Answer: c) try
d) Only if the function is static 3. Which keyword is used to catch an exception in C++?
Answer: a) Yes a) handle
b) catch
5. Advanced Template Concepts c) throw
16. What are variadic templates in C++? d) error
a) Templates that accept a variable number of template Answer: b) catch
parameters 4. What is the purpose of the throw keyword in C++?
b) Templates that can only accept integer types a) To declare an exception
c) Templates that specialize in runtime polymorphism b) To handle an exception
d) Templates that work only with arrays c) To generate an exception
Answer: a) Templates that accept a variable number of template d) To prevent exceptions from occurring
parameters Answer: c) To generate an exception
17. Can a template class inherit from another template class? 5. What happens if an exception is thrown but not caught in C++?
a) Yes a) The program continues execution
b) No b) The program stops executing immediately
c) Only for function templates c) The exception is ignored
d) Only for class specialization d) The compiler provides a warning
Answer: a) Yes Answer: b) The program stops executing immediately
18. What is the benefit of using templates in C++?
a) Code reusability 2. Handling Exceptions
b) Faster execution speed 6. Which of the following is the correct syntax for handling
c) Reduced memory consumption exceptions?
d) All of the above a)
Answer: a) Code reusability cpp
1. Basics of Exception Handling CopyEdit
try {
1. What is the purpose of exception handling in C++? // Code
a) To handle syntax errors } handle(ExceptionType e) {
b) To detect and handle runtime errors // Handle exception
c) To prevent compilation errors }
d) To optimize program execution b)
cpp
Answer: b) To detect and handle runtime errors
CopyEdit
2. Which keyword is used to define an exception block in C++? try {
a) error // Code
b) exception } catch(ExceptionType e) {
c) try // Handle exception
}
d) catch
c)
cpp c) rethrow;
CopyEdit
handle {
d) throw exception();
// Code Answer: a) throw;
} try(ExceptionType e) {
// Handle exception 3. Exception Classes and Handling Mechanisms
}
11. Which of the following is a standard exception class in C++?
d) a) exception
cpp
CopyEdit b) runtime_error
catch { c) out_of_range
// Code d) All of the above
} try(ExceptionType e) {
// Handle exception
Answer: d) All of the above
} 12. Which standard exception class is used to handle invalid
Answer: b) Correct syntax for exception handling memory access?
7. What is the purpose of the catch(...) block? a) bad_alloc
a) To catch all types of exceptions b) bad_cast
b) To catch only runtime errors c) out_of_range
c) To catch only syntax errors d) runtime_error
d) To handle memory leaks Answer: a) bad_alloc
Answer: a) To catch all types of exceptions 13. Which of the following standard exceptions is thrown when an
8. In which order should multiple catch blocks be arranged? invalid type conversion occurs?
a) From specific to general a) bad_cast
b) From general to specific b) bad_typeid
c) In any order c) invalid_argument
d) Only one catch block is allowed d) logic_error
Answer: a) From specific to general Answer: a) bad_cast
9. What will happen if an exception is thrown inside a try block 14. What will happen if throw is called inside a destructor?
but there is no matching catch block? a) The program will crash
a) The program will compile successfully b) The exception will be caught normally
b) The program will terminate with an error c) It will result in std::terminate() being called
c) The program will execute normally d) It will be ignored
d) The program will skip the try block Answer: c) It will result in std::terminate() being called
Answer: b) The program will terminate with an error 15. What is the purpose of std::exception class?
10. What is the correct way to rethrow an exception inside a catch a) It provides a base class for all standard exceptions
block? b) It is used to catch only logic errors
a) throw; c) It cannot be used directly
b) throw new; d) It is used only for runtime errors
Answer: a) It provides a base class for all standard exceptions
21. What happens if an exception is thrown inside a noexcept
4. Exception Handling with Constructors and Destructors function?
16. Can a constructor throw an exception in C++? a) The exception is ignored
a) Yes b) The program calls std::terminate()
b) No c) The exception is caught automatically
c) Only if it's a virtual constructor d) The compiler generates a warning
d) Only in derived classes Answer: b) The program calls std::terminate()
Answer: a) Yes 22. Can exceptions be used for normal program flow control?
17. What happens if an exception is thrown inside a constructor? a) Yes, but it is not recommended due to performance overhead
a) The object is partially created b) No, exceptions should only be used for error handling
b) The object is not created c) Only if they are caught immediately
c) The destructor is automatically called d) Only in large programs
d) The program terminates immediately Answer: a) Yes, but it is not recommended due to performance
Answer: b) The object is not created overhead
18. How can we prevent memory leaks when an exception is thrown 1. goto Statement in C++
in a constructor? 1. Which of the following is true about the goto statement in C++?
a) Using delete before throwing the exception a) It is used to jump from one function to another
b) Using smart pointers (std::unique_ptr, std::shared_ptr) b) It can only be used inside loops
c) Ignoring the exception c) It allows an unconditional jump to a labeled statement
d) Declaring the constructor as virtual d) It replaces the switch statement
Answer: b) Using smart pointers (std::unique_ptr, Answer: c) It allows an unconditional jump to a labeled
std::shared_ptr) statement
2. What is a major drawback of using goto in C++?
5. Exception Handling and Performance Considerations a) It increases program readability
19. What is the disadvantage of using exceptions in C++? b) It makes debugging easier
a) They make the program slower c) It leads to "spaghetti code" and reduces readability
b) They cannot be caught d) It improves execution speed
c) They make debugging easier Answer: c) It leads to "spaghetti code" and reduces readability
d) They are required for all C++ programs 3. What is the correct syntax for using goto in C++?
Answer: a) They make the program slower a)
20. How does the noexcept keyword affect a function? cpp
CopyEdit
a) It specifies that the function does not throw exceptions label:
b) It forces the function to catch all exceptions goto label;
c) It disables exception handling in the function b)
d) It allows throwing only standard exceptions cpp
Answer: a) It specifies that the function does not throw CopyEdit
exceptions goto label;
label:
c) Answer: b) The program terminates abnormally
cpp 8. Which of the following is a valid way to throw an exception?
CopyEdit
a) throw 10;
label:
cout << "Hello"; b) throw "Error!";
goto label; c) throw std::runtime_error("Runtime Error");
d) d) All of the above
cpp Answer: d) All of the above
CopyEdit 9. Can we throw an object of a user-defined class using throw?
goto 10;
10: cout << "Hello"; a) No, only standard exceptions can be thrown
Answer: b) goto label; followed by label: b) Yes, any type of object can be thrown
4. Which of the following is not a valid use of goto? c) No, user-defined exceptions must be caught in main()
a) Jumping to a label within the same function d) Only pointers can be thrown
b) Jumping forward in the code Answer: b) Yes, any type of object can be thrown
c) Jumping backward in the code 10. What is the correct way to rethrow an exception inside a catch
d) Jumping from one function to another block?
Answer: d) Jumping from one function to another a) throw;
5. What will happen if a goto statement causes a jump into a b) rethrow();
different scope? c) throw new;
a) The program executes normally d) throw exception();
b) The compiler gives an error Answer: a) throw;
c) The variable scope is extended
d) The program runs but produces an undefined result 3. catch Statement in C++
Answer: b) The compiler gives an error 11. Which keyword is used to catch an exception in C++?
a) catch
2. throw Statement in C++ b) handle
6. What is the purpose of the throw keyword in C++? c) try
a) To handle an exception d) exception
b) To declare an exception Answer: a) catch
c) To generate (raise) an exception 12. What is the purpose of catch(...) in C++?
d) To terminate the program a) To catch all types of exceptions
Answer: c) To generate (raise) an exception b) To catch only integer exceptions
7. What happens when an exception is thrown using throw but is c) To catch only standard exceptions
not caught? d) It is not valid in C++
a) The program continues execution Answer: a) To catch all types of exceptions
b) The program terminates abnormally 13. What happens if a catch block does not match a thrown
c) The exception is ignored exception?
d) The compiler resolves the error automatically a) The program executes normally
b) The program terminates with an error b) It will result in a compilation error
c) The exception is ignored c) It will execute but might cause undefined behavior
d) The exception is handled by the operating system d) It will execute normally without any issues
Answer: b) The program terminates with an error Answer: b) It will result in a compilation error
14. Consider the following code: 18. Can a throw statement be used inside a goto statement?
cpp a) Yes, but it is not recommended
CopyEdit
try {
b) No, goto cannot contain throw
throw 5; c) Only if goto jumps to an exception block
} catch (double d) { d) Only if throw is inside a loop
cout << "Caught a double"; Answer: a) Yes, but it is not recommended
} catch (...) {
19. What is the best way to handle errors in C++?
cout << "Caught an exception";
} a) Using goto for error handling
What will be the output? b) Using if-else statements
a) Caught a double c) Using try, catch, and throw
b) Caught an exception d) Ignoring errors and continuing execution
c) Compilation error Answer: c) Using try, catch, and throw
d) No output 20. What will be the output of the following code?
cpp
Answer: b) Caught an exception CopyEdit
15. Can a catch block be written without a corresponding try #include <iostream>
block? using namespace std;
a) Yes
int main() {
b) No
try {
c) Only if it catches all exceptions cout << "Before throwing an exception" << endl;
d) Only in nested try blocks throw 10;
Answer: b) No cout << "This line will not be executed" <<
endl;
} catch (int e) {
4. Combination of goto, throw, and catch cout << "Exception caught: " << e << endl;
16. Can we use goto to jump from a catch block to outside the try }
block? return 0;
a) Yes }
b) No a) Before throwing an exception
Exception caught: 10
c) Only if the label is in the same function
d) Only in nested try-catch blocks b) This line will not be executed
Answer: b) No c) Exception caught: 10
17. What happens if a goto statement is used inside a try block and d) Compilation error
jumps outside the block? Answer: a) Before throwing an exception followed by
Exception caught: 10
a) It will compile successfully
1. Exception Handling in C++ c) Compilation error
1. What is the purpose of exception handling in C++? d) No output
a) To prevent runtime errors Answer: b) Caught an exception
b) To handle runtime errors gracefully 6. Which of the following is the correct syntax for rethrowing an
c) To increase program execution speed exception inside a catch block?
d) To make code longer a) throw;
Answer: b) To handle runtime errors gracefully b) rethrow();
2. Which of the following keywords are used in C++ exception c) throw new;
handling? d) throw exception();
a) try, catch, throw Answer: a) throw;
b) catch, handle, throw 7. What type of exceptions does std::exception handle?
c) try, throw, finally a) Only integer exceptions
d) catch, raise, finally b) Only floating-point exceptions
Answer: a) try, catch, throw c) Standard exceptions in the C++ Standard Library
3. What happens if an exception is thrown and not caught in C++? d) All types of exceptions
a) The program executes normally Answer: c) Standard exceptions in the C++ Standard Library
b) The program crashes 8. What is the output of the following code?
c) The program ignores the exception cpp
CopyEdit
d) The exception is automatically resolved try {
Answer: b) The program crashes throw std::runtime_error("Error occurred");
4. What does catch(...) do in a C++ program? } catch (std::exception &e) {
a) Catches only integer exceptions cout << e.what();
b) Catches only standard exceptions }
c) Catches all types of exceptions a) Error occurred
d) Ignores exceptions b) Caught an exception
Answer: c) Catches all types of exceptions c) Compilation error
5. Consider the following code: d) No output
cpp Answer: a) Error occurred
CopyEdit
try {
throw 10;
2. File Handling in C++
} catch (double d) { 9. Which header file is used for file handling in C++?
cout << "Caught a double"; a) <file>
} catch (...) { b) <fstream>
cout << "Caught an exception";
c) <iostream>
}
d) <fstream.h>
What will be the output?
a) Caught a double Answer: b) <fstream>
b) Caught an exception
10. Which of the following classes is used to write to a file in C++? c) To close a file
a) ifstream d) To delete a file
b) ofstream Answer: b) To move the read pointer
c) fstream
d) file 3. Standard Template Library (STL) in C++
Answer: b) ofstream 16. What does STL stand for in C++?
11. Which mode is used to append data to an existing file? a) System Template Library
a) ios::in b) Standard Template Library
b) ios::app c) Software Template Library
c) ios::out d) Structured Type Library
d) ios::trunc Answer: b) Standard Template Library
Answer: b) ios::app 17. Which of the following is not a component of STL?
12. What is the correct way to open a file for both reading and a) Containers
writing? b) Algorithms
a) fstream file("data.txt", ios::in); c) Iterators
b) fstream file("data.txt", ios::out); d) Headers
c) fstream file("data.txt", ios::in | ios::out); Answer: d) Headers
d) fstream file("data.txt", ios::binary); 18. Which of the following is a sequential container in STL?
Answer: c) fstream file("data.txt", ios::in | ios::out); a) map
13. What happens if you try to read from a file that does not exist b) set
using ifstream? c) vector
a) A new file is created d) unordered_map
b) The program throws an exception Answer: c) vector
c) The stream state becomes invalid 19. What is the correct syntax to declare a vector of integers?
d) The program exits immediately a) vector<int> v;
Answer: c) The stream state becomes invalid b) int vector v;
14. Which function is used to check if a file has been successfully c) array<int> v;
opened? d) list<int> v;
a) file.good() Answer: a) vector<int> v;
b) file.exists() 20. Which of the following is used to insert an element at the end of
c) file.opened() a vector?
d) file.check() a) push()
Answer: a) file.good() b) insert()
15. What is the purpose of seekg() in file handling? c) push_back()
a) To move the write pointer d) add_element()
b) To move the read pointer Answer: c) push_back()
21. What does begin() function of a vector return? 2. Which keyword is used to explicitly generate an exception in C++?
a) A pointer to the last element a) raise
b) A pointer to the first element b) catch
c) The size of the vector c) throw
d) The number of elements d) try
Answer: b) A pointer to the first element Answer: c) throw
22. Which STL container does not allow duplicate values?
a) vector 3. What will happen if an exception is thrown inside a try block but no
b) list corresponding catch block is found?
c) set a) The program executes normally
d) deque b) The program terminates abruptly
Answer: c) set c) The exception is ignored
23. Which of the following is an associative container in STL? d) The program automatically handles the exception
a) vector Answer: b) The program terminates abruptly
b) queue
c) map 4. What is the correct syntax for a catch block that catches all types of
d) stack exceptions?
Answer: c) map a) catch { ... }
24. How can we remove all elements from an STL container? b) catch (...) { ... }
a) clear() c) catch_all { ... }
b) eraseAll() d) catch {exception e} { ... }
c) delete() Answer: b) catch (...) { ... }
d) removeAll()
5. What happens if multiple catch blocks are defined, and an exception is
Answer: a) clear()
thrown?
25. Which STL algorithm is used to sort elements?
a) The first matching catch block is executed
a) sort()
b) All catch blocks are executed
b) order()
c) The last catch block is executed
c) arrange()
d) The program ignores all catch blocks
d) orderBy()
Answer: a) The first matching catch block is executed
Answer: a) sort()
1. What is the purpose of the try block in C++?
6. Consider the following code:
a) To check for syntax errors cpp
b) To define a block of code where an exception might occur CopyEdit
c) To catch exceptions thrown by a function try {
d) To handle runtime errors automatically throw 5;
}
Answer: b) To define a block of code where an exception might occur catch (double d) {
cout << "Caught a double";
}
catch (...) { 11. What is the output of the following program?
cout << "Caught an exception"; cpp
} CopyEdit
What will be the output? #include <iostream>
a) Caught a double using namespace std;
b) Caught an exception void test() {
throw "Error occurred!";
c) No output }
d) Compilation error int main() {
Answer: b) Caught an exception try {
test();
} catch (const char* msg) {
7. Which of the following can be thrown as an exception in C++?
cout << msg;
a) Integers }
b) Objects return 0;
c) Pointers }
d) All of the above a) Error occurred!
Answer: d) All of the above b) No output
c) Compilation error
8. What is the correct way to rethrow an exception inside a catch block? d) Runtime error
a) rethrow(); Answer: a) Error occurred!
b) throw;
c) throw new; 12. Can a try block be empty in C++?
d) throw exception(); a) Yes, but it is useless
Answer: b) throw; b) No, it must contain at least one statement
c) Only if there is a catch block
9. What happens if an exception is thrown outside of a try block? d) Only in C++11 or later
a) It will be caught by the nearest catch block Answer: b) No, it must contain at least one statement
b) It results in a runtime error
c) It is ignored by the compiler 13. What is the main advantage of using exception handling with try,
d) The program automatically resolves it catch, and throw?
Answer: b) It results in a runtime error a) It makes the code faster
b) It allows handling errors without crashing the program
10. Can a catch block have multiple parameters? c) It reduces code size
a) Yes, always d) It improves security
b) No, a catch block can only handle one type at a time Answer: b) It allows handling errors without crashing the program
c) Only if they are pointers
d) Only in certain compilers
Answer: b) No, a catch block can only handle one type at a time
14. Can a catch block throw another exception? return 0;
a) Yes }
b) No a) HelloWorld
c) Only if the catch block is inside another try block b) Hello World
d) Only in C++17 and later c) Hello + World
Answer: a) Yes d) Compilation error
Answer: b) Hello World
15. What will happen if a throw statement is used inside a destructor and
not caught? 4. Which of the following function is used to find the length of a C++ string?
a) The program terminates a) length()
b) The destructor executes successfully b) size()
c) The exception is ignored c) Both length() and size()
d) The compiler generates a warning but continues execution d) strlen()
Answer: a) The program terminates Answer: c) Both length() and size()
1. Which header file is required to use the string class in C++?
a) <cstring> 5. How can we compare two strings in C++?
b) <iostream> a) Using == operator
c) <string> b) Using compare() function
d) <stdlib.h> c) Using strcmp() function
Answer: c) <string> d) All of the above
Answer: d) All of the above
2. What is the correct way to declare a string in C++?
a) char str[] = "Hello"; 6. What will be the output of the following code?
cpp
b) string str = "Hello"; CopyEdit
c) char *str = "Hello"; #include <iostream>
d) All of the above #include <string>
Answer: d) All of the above using namespace std;
int main() {
string str = "Programming";
3. What is the output of the following code? cout << str.substr(3, 4);
cpp return 0;
CopyEdit }
#include <iostream>
#include <string> a) gram
using namespace std; b) ogra
int main() { c) ramm
string s1 = "Hello"; d) mming
string s2 = "World";
Answer: b) ogra
string s3 = s1 + " " + s2;
cout << s3;
7. Which function is used to insert a string into another string? cout << s;
a) insert() return 0;
}
b) replace()
a) HelloWorld
c) substr()
b) Hello World
d) append()
c) Compilation error
Answer: a) insert() d) Hello+World
Answer: b) Hello World
8. What is the output of the following code?
cpp
CopyEdit 11. How can we access the first character of a string in C++?
#include <iostream> a) s[0]
#include <string> b) s.at(0)
using namespace std; c) s.front()
int main() {
string s = "C++ Programming"; d) All of the above
s.erase(3, 4); Answer: d) All of the above
cout << s;
return 0; 12. Which function is used to remove all characters from a string?
}
a) clear()
a) C++ gramming b) erase()
b) C++ing c) remove()
c) C++Programming d) delete()
d) Compilation error Answer: a) clear()
Answer: a) C++ gramming
13. What is the correct way to convert a C++ string to a C-style string?
9. Which function is used to concatenate two strings in C++? a) s.to_char()
a) strcat() b) s.data()
b) append() c) s.c_str()
c) push_back() d) s.convert()
d) concatenate() Answer: c) s.c_str()
Answer: b) append()
14. What is the function of push_back() in a string?
10. What will be the output of the following code? a) Adds a character at the end of the string
cpp
CopyEdit b) Removes a character from the end of the string
#include <iostream> c) Appends a string to another string
#include <string> d) Inserts a string at a given position
using namespace std; Answer: a) Adds a character at the end of the string
int main() {
string s = "Hello";
s += " World";
15. What will be the output of the following code? 19. Which function is used to reverse a string in C++?
cpp a) reverse()
CopyEdit
#include <iostream>
b) strrev()
#include <string> c) std::reverse()
using namespace std; d) flip()
int main() { Answer: c) std::reverse()
string str = "abcdef";
str.pop_back();
cout << str; 20. What is the output of the following code?
return 0; cpp
} CopyEdit
#include <iostream>
a) abcde #include <string>
b) abcdef using namespace std;
c) f int main() {
d) Compilation error string s1 = "abcd";
string s2 = "abcd";
Answer: a) abcde if (s1 == s2)
cout << "Equal";
16. Which function is used to find the position of a substring in a string? else
a) find() cout << "Not Equal";
b) search() return 0;
}
c) locate()
a) Equal
d) strpos()
b) Not Equal
Answer: a) find()
c) Compilation error
d) Undefined behavior
17. What happens if find() does not find the substring?
Answer: a) Equal
a) It throws an exception
1. What is the shortcut key to compile a C++ program in most IDEs (e.g.,
b) It returns -1 Dev-C++, Code::Blocks)?
c) It returns string::npos a) F9
d) It causes a segmentation fault b) Ctrl + B
Answer: c) It returns string::npos c) F5
d) Alt + C
18. What is the purpose of the replace() function in C++ strings? ✅ Answer: a) F9
a) To replace a character in a string
b) To replace a substring within a string 2. Which shortcut key is used to run a C++ program after compilation in
c) To replace all occurrences of a character in a string Code::Blocks?
d) To remove a character from a string a) F9
Answer: b) To replace a substring within a string b) F8
c) Ctrl + F9 7. Which shortcut is used to comment/uncomment multiple lines of code in
d) Ctrl + Shift + R most C++ IDEs?
✅ Answer: c) Ctrl + F9 a) Ctrl + /
b) Ctrl + Shift + C
3. What is the shortcut key to stop the execution of a running C++ program c) Ctrl + K + C
in most IDEs? d) Alt + /
a) Esc ✅ Answer: c) Ctrl + K + C (in Visual Studio) and Ctrl + / (in many other
b) Ctrl + C IDEs like Code::Blocks, VS Code)
c) Alt + F4
d) Shift + S 8. What is the shortcut key for debugging in Visual Studio?
✅ Answer: b) Ctrl + C a) F7
b) F8
4. Which shortcut key is used to format or indent code properly in Visual c) F5
Studio? d) Shift + D
a) Ctrl + K + D ✅ Answer: c) F5
b) Ctrl + I
c) Shift + F 9. How can you place a breakpoint in a C++ program while debugging in
d) Alt + Tab Visual Studio?
✅ Answer: a) Ctrl + K + D a) Ctrl + Shift + B
b) F9
5. What is the shortcut key to open a new C++ file in most IDEs? c) Alt + B
a) Ctrl + N d) Ctrl + B
b) Ctrl + Shift + N ✅ Answer: b) F9
c) Alt + N
d) Shift + F2 10. What is the shortcut key to find and replace text in most C++ IDEs?
✅ Answer: a) Ctrl + N a) Ctrl + R
b) Ctrl + Shift + F
6. What is the shortcut to save a file in C++ programming environments c) Ctrl + H
like Visual Studio and Code::Blocks? d) Alt + F3
a) Ctrl + S ✅ Answer: c) Ctrl + H
b) Shift + S
c) Ctrl + P 11. What is the shortcut key to switch between open files in Visual Studio
d) Alt + S and Code::Blocks?
✅ Answer: a) Ctrl + S a) Ctrl + Tab
b) Alt + Shift + Tab
c) Ctrl + Shift + N
d) Ctrl + Alt + F
✅ Answer: a) Ctrl + Tab
17. What is the shortcut key to open the console output window in Visual
12. How do you close the current file in most C++ IDEs? Studio?
a) Ctrl + W a) Ctrl + Alt + O
b) Alt + F4 b) Shift + Alt + O
c) Shift + W c) Ctrl + Shift + O
d) Ctrl + Q d) Ctrl + W + O
✅ Answer: a) Ctrl + W ✅ Answer: a) Ctrl + Alt + O
13. What is the shortcut to quickly open a file in Visual Studio? 18. How can you rename a variable or function name across a file in Visual
a) Ctrl + O Studio?
b) Ctrl + P a) Ctrl + R + R
c) Alt + F4 b) Ctrl + Shift + F
d) Shift + O c) Alt + R
✅ Answer: b) Ctrl + P (in Visual Studio Code), Ctrl + O *(in many IDEs d) Ctrl + Shift + R
like Dev-C++) ✅ Answer: a) Ctrl + R + R
14. What is the shortcut key for selecting an entire line in most C++ IDEs? 19. What is the shortcut to open the Solution Explorer in Visual Studio?
a) Ctrl + L a) Ctrl + Alt + L
b) Shift + End b) Ctrl + Shift + E
c) Ctrl + Shift + Left Arrow c) Alt + S
d) Ctrl + A d) Ctrl + Alt + E
✅ Answer: a) Ctrl + L ✅ Answer: a) Ctrl + Alt + L
15. How do you redo an action in a C++ editor? 20. What is the shortcut to toggle full-screen mode in Visual Studio?
a) Ctrl + Z a) F11
b) Ctrl + Shift + Z b) Shift + Enter
c) Ctrl + Y c) Alt + Shift + F
d) Shift + R d) Ctrl + Alt + F
✅ Answer: c) Ctrl + Y (in most IDEs) ✅ Answer: a) F11
1. What is C++ primarily known for?
16. What shortcut is used to compile and run a C++ program in Dev-C++? a) Functional programming
a) F9 b) Object-oriented programming
b) F5 c) Scripting language
c) Ctrl + Shift + F9 d) Database management
d) Ctrl + F11 ✅ Answer: b) Object-oriented programming
✅ Answer: b) F5
2. Who developed the C++ language? 7. What is the correct syntax to define a constant variable in C++?
a) Dennis Ritchie a) const int x = 10;
b) Bjarne Stroustrup b) int const x = 10;
c) James Gosling c) #define x 10
d) Guido van Rossum d) Both a and b
✅ Answer: b) Bjarne Stroustrup ✅ Answer: d) Both a and b
3. What is the extension of a C++ source file? 8. Which header file is used for input and output operations in C++?
a) .cpl a) #include <stdio.h>
b) .cp b) #include <conio.h>
c) .cpp c) #include <iostream>
d) .cls d) #include <stdlib.h>
✅ Answer: c) .cpp ✅ Answer: c) #include <iostream>
4. What is the default access specifier for class members in C++? 9. How do you declare a pointer in C++?
a) Public a) int *ptr;
b) Private b) ptr = &x;
c) Protected c) int ptr;
d) Friend d) pointer int ptr;
✅ Answer: b) Private ✅ Answer: *a) int ptr;
5. Which of the following is NOT a fundamental data type in C++? 10. Which operator is used for dynamic memory allocation in C++?
a) int a) malloc
b) float b) calloc
c) double c) new
d) string d) alloc
✅ Answer: d) string (Because string is a class in C++) ✅ Answer: c) new
6. Which of the following allows defining multiple functions with the same 11. What does the this pointer represent in C++?
name but different parameters? a) A pointer to the current object
a) Function overriding b) A pointer to the base class
b) Function overloading c) A pointer to the previous object
c) Operator overloading d) A pointer to the derived class
d) Virtual functions ✅ Answer: a) A pointer to the current object
✅ Answer: b) Function overloading
12. What is the purpose of a virtual function in C++? 17. What is the default return type of main() function in C++?
a) To allow function overloading a) int
b) To enable dynamic (runtime) polymorphism b) void
c) To prevent function overriding c) float
d) To make the function execute faster d) char
✅ Answer: b) To enable dynamic (runtime) polymorphism ✅ Answer: a) int
13. Which of the following is NOT a feature of C++? 18. What is the use of friend function in C++?
a) Object-oriented programming a) To access private and protected members of a class
b) Multi-threading support by default b) To declare a function inside a class
c) Operator overloading c) To prevent function overloading
d) Exception handling d) To restrict inheritance
✅ Answer: b) Multi-threading support by default ✅ Answer: a) To access private and protected members of a class
14. What is the output of sizeof(char) in C++? 19. Which of the following is NOT a type of inheritance in C++?
a) 1 a) Single inheritance
b) 2 b) Multiple inheritance
c) 4 c) Hybrid inheritance
d) Depends on the system d) Virtual inheritance
✅ Answer: a) 1 ✅ Answer: d) Virtual inheritance (Virtual is a concept, not a type of
inheritance)
15. How can a class in C++ be made abstract?
a) By declaring at least one pure virtual function 20. What happens if a constructor is declared private in C++?
b) By defining a constructor as private a) The class cannot be instantiated from outside
c) By making all functions static b) The constructor can only be called by derived classes
d) By using friend functions c) The constructor becomes a virtual function
✅ Answer: a) By declaring at least one pure virtual function d) The constructor is ignored
✅ Answer: a) The class cannot be instantiated from outside
16. Which keyword is used to prevent a function from being overridden in
C++? Would you like more MCQs on Advanced C++ to
a) static 1. Which of the following keywords was introduced in C++11 for type
b) final inference?
c) sealed a) auto
d) override b) var
✅ Answer: b) final (Introduced in C++11) c) typeof
d) dynamic
✅ Answer: a) auto
✅ Answer: b) It determines the type of an expression at compile-time
2. What is the purpose of the nullptr keyword introduced in C++11?
a) To initialize null values in int variables 7. Which of the following keywords was introduced in C++11 to indicate
b) To replace NULL and provide a type-safe null pointer that an object cannot be copied or moved?
c) To initialize uninitialized pointers a) restrict
d) To replace void* pointers b) delete
✅ Answer: b) To replace NULL and provide a type-safe null pointer c) removed
d) omit
3. Which new C++ keyword allows defining an immutable variable similar ✅ Answer: b) delete
to const, but with more flexibility in initialization?
a) let 8. Which C++ keyword is used to declare a thread-local storage variable in
b) constexpr C++11?
c) final a) threadsafe
d) sealed b) thread_local
✅ Answer: b) constexpr c) static_thread
d) local
4. What does the override keyword do in C++11? ✅ Answer: b) thread_local
a) Prevents function overriding
b) Ensures that a function correctly overrides a base class function 9. Which of the following new keywords was introduced in C++17?
c) Enables function overloading a) co_await
d) Declares a function as virtual b) register
✅ Answer: b) Ensures that a function correctly overrides a base class c) volatile
function d) alignas
✅ Answer: a) co_await (For coroutines in C++20)
5. Which of the following C++ keywords was introduced to prevent further
derivation from a class? 10. What is the purpose of the noexcept keyword introduced in C++11?
a) sealed a) It prevents exceptions from being thrown in a function
b) stop b) It forces the function to throw an exception
c) final c) It makes functions run faster
d) restrict d) It prevents segmentation faults
✅ Answer: c) final ✅ Answer: a) It prevents exceptions from being thrown in a function
6. What is the purpose of the decltype keyword introduced in C++11? 11. What does the explicit keyword do in C++11 and later?
a) It declares a class as abstract a) It forces a constructor to be used only explicitly
b) It determines the type of an expression at compile-time b) It allows implicit conversions
c) It dynamically changes the type of a variable c) It makes a function virtual
d) It creates function templates d) It prevents function overloading
✅ Answer: a) It forces a constructor to be used only explicitly
12. Which of the following is NOT a new keyword introduced in C++11 or
later?
a) concept
b) module
c) restrict
d) co_yield
✅ Answer: c) restrict (Not a standard C++ keyword, but used in some
compilers like MSVC for optimization)
13. What does the alignas keyword introduced in C++11 do?
a) It aligns variables to a specific memory address
b) It aligns function calls
c) It allows defining custom memory allocators
d) It forces memory compaction
✅ Answer: a) It aligns variables to a specific memory address
14. Which of the following was introduced in C++20?
a) constexpr
b) concept
c) thread_local
d) nullptr
✅ Answer: b) concept (Used in C++20 for defining template constraints)
15. What is the purpose of the co_return keyword introduced in C++20?
a) It returns a coroutine value
b) It forces a function to exit immediately
c) It declares a coroutine function
d) It prevents exceptions
✅ Answer: a) It returns a coroutine value