Basics of C++
Sample MCQ: What is the correct file extension for a C++ program?
o A) .cpp
o B) .cs
o C) .c
o D) .py
Answer: A) .cpp
2. Data Types
Sample MCQ: Which of the following is a valid C++ data type?
o A) int
o B) double
o C) float
o D) All of the above
Answer: D) All of the above
3. Variables and Constants
Sample MCQ: How do you declare a constant in C++?
o A) constant int x = 5;
o B) const int x = 5;
o C) int const x = 5;
o D) Both B and C
Answer: D) Both B and C
4. Operators
Sample MCQ: What is the result of the expression 10 % 3 in C++?
o A) 0
o B) 1
o C) 3
o D) 10
Answer: B) 1
5. Control Flow Statements
Sample MCQ: Which of the following is the correct if statement in C++?
o A) if x = 5
o B) if (x == 5)
o C) if x == 5
o D) if (x = 5)
Answer: B) if (x == 5)
6. Loops
Sample MCQ: Which loop in C++ executes at least once, regardless of the
condition?
o A) for
o B) while
o C) do-while
o D) foreach
Answer: C) do-while
7. Functions
Sample MCQ: Which of the following correctly declares a function in C++ that
returns an integer?
o A) int myFunction()
o B) function int myFunction()
o C) myFunction()
o D) def int myFunction()
Answer: A) int myFunction()
8. Classes and Objects
Sample MCQ: How do you define a class in C++?
o A) class MyClass { int x; };
o B) define MyClass { int x; }
o C) MyClass class { int x; };
o D) class: MyClass { int x; };
Answer: A) class MyClass { int x; };
9. Constructors and Destructors
Sample MCQ: What is the purpose of a constructor in C++?
o A) To initialize objects.
o B) To define methods.
o C) To manage memory.
o D) To destroy objects.
Answer: A) To initialize objects.
Sample MCQ: Which of the following is a destructor in C++?
o A) ~MyClass()
o B) delete MyClass()
o C) MyClass::~()
o D) ~MyClass()
Answer: A) ~MyClass()
10. Inheritance
Sample MCQ: How do you declare that a class Child inherits from class Parent
in C++?
o A) class Child extends Parent
o B) class Child : public Parent
o C) class Child inherits Parent
o D) class Child = Parent
Answer: B) class Child : public Parent
11. Polymorphism
Sample MCQ: Which keyword is used to allow a derived class to override a base
class method in C++?
o A) override
o B) virtual
o C) abstract
o D) new
Answer: B) virtual
12. Encapsulation
Sample MCQ: Which of the following access specifiers in C++ allows members to
be accessible only within the same class?
o A) public
o B) private
o C) protected
o D) global
Answer: B) private
13. Abstraction
Sample MCQ: How do you define an abstract class in C++?
o A) class MyClass
o B) class MyClass abstract
o C) abstract class MyClass
o D) class MyClass { virtual void myFunction() = 0; }
Answer: D) class MyClass { virtual void myFunction() = 0; }
14. Templates
Sample MCQ: What is a template in C++ used for?
o A) To define functions that work with different data types.
o B) To define abstract classes.
o C) To handle runtime polymorphism.
o D) To manage memory dynamically.
Answer: A) To define functions that work with different data types.
15. Exception Handling
Sample MCQ: Which keyword is used to handle exceptions in C++?
o A) throw
o B) catch
o C) try
o D) All of the above
Answer: D) All of the above
16. Operator Overloading
Sample MCQ: Which operator can be overloaded in C++?
o A) +
o B) &&
o C) sizeof
o D) ::
Answer: A) +
17. Friend Functions
Sample MCQ: What is a friend function in C++?
o A) A function that can access private members of a class.
o B) A function that cannot access private members.
o C) A function that inherits another class.
o D) A function that operates only within its own class.
Answer: A) A function that can access private members of a class.
18. STL (Standard Template Library)
Sample MCQ: Which of the following is not a component of the Standard
Template Library (STL) in C++?
o A) Algorithms
o B) Iterators
o C) Functions
o D) Containers
Answer: C) Functions
19. Memory Management
Sample MCQ: Which operator is used to allocate memory dynamically in C++?
o A) malloc()
o B) calloc()
o C) new
o D) alloc()
Answer: C) new
Sample MCQ: Which operator is used to deallocate memory in C++?
o A) free()
o B) delete
o C) dealloc()
o D) remove()
Answer: B) delete
20. Pointers
Sample MCQ: What does the & symbol represent in C++?
o A) Address of operator
o B) Dereference operator
o C) Logical AND operator
o D) Bitwise AND operator
Answer: A) Address of operator
21. References
Sample MCQ: How do you declare a reference variable in C++?
o A) int &ref = x;
o B) ref int = x;
o C) &int ref = x;
o D) ref int x = &x;
Answer: A) int &ref = x;
22. File Handling
Sample MCQ: Which library in C++ is used for file handling?
o A) <fstream>
o B) <iostream>
o C) <fileio>
o D) <files>
Answer: A) <fstream>
23. Multithreading
Sample MCQ: Which library is used for multithreading in C++?
o A) <thread>
o B) <pthread>
o C) <threading>
o D) <process>
Answer: A) <thread>
24. Namespaces
Sample MCQ: What is the purpose of a namespace in C++?
o A) To organize code and avoid name conflicts.
o B) To define a class.
o C) To manage memory.
o D) To declare global variables.