Introduction
C++ is a fundamental programming language taught in Computer Science curricula and tested extensively in competitive exams like PPSC, NTS, FPSC, and lecturer entry tests. Mastering C++ concepts, including Object-Oriented Programming (OOP), memory management, pointers, and exception handling, is essential for securing top ranks.
This article on mcqsdesk.com brings you 50 carefully selected C++ Programming MCQs complete with answers and concise explanations.
Section 1: C++ Basics & Syntax
Q1. Who developed the C++ programming language?
A) Dennis Ritchie
B) Bjarne Stroustrup
C) James Gosling
D) Guido van Rossum
Answer: B) Bjarne Stroustrup
Explanation: Bjarne Stroustrup developed C++ in 1979 at Bell Labs as an extension of the C language.
Q2. Which operator is used for dynamic memory allocation in C++?
A) malloc
B) alloc
C) new
D) create
Answer: C) new
Explanation: The new operator allocates heap memory at runtime in C++, while delete deallocates it.
Q3. Which stream object is used for standard input in C++?
A) cout
B) cin
C) cerr
D) clog
Answer: B) cin
Explanation: cin (instance of istream) reads input from the standard input device (keyboard) using the extraction operator >>.
Q4. Which header file is required for using input/output operations in C++?
A) <stdio.h>
B) <iostream>
C) <stdlib.h>
D) <conio.h>
Answer: B) <iostream>
Explanation: The <iostream> header declares stream objects like cin, cout, cerr, and clog.
Q5. What is the size of wchar_t in C++?
A) 1 byte
B) 2 or 4 bytes
C) 8 bytes
D) 16 bytes
Answer: B) 2 or 4 bytes
Explanation: wchar_t is a wide-character type used to represent international character sets, typically 2 or 4 bytes depending on the compiler.
Section 2: Object-Oriented Programming (OOP) Concepts
Q6. Which concept allows wrapping data and methods into a single unit?
A) Polymorphism
B) Inheritance
C) Encapsulation
D) Abstraction
Answer: C) Encapsulation
Explanation: Encapsulation bundles data members and member functions within a class, hiding inner details from outside access.
Q7. What is a constructor in C++?
A) A function that deletes objects
B) A special member function that initializes objects
C) A function that returns a value
D) A static member function
Answer: B) A special member function that initializes objects
Explanation: A constructor shares the same name as the class and executes automatically when an object is instantiated.
Q8. Which access specifier makes class members accessible only within the class and derived classes?
A) private
B) protected
C) public
D) friend
Answer: B) protected
Explanation: protected members are hidden from external functions but remain accessible to derived child classes.
Q9. What type of inheritance is not supported directly in Java but supported in C++?
A) Single Inheritance
B) Multilevel Inheritance
C) Multiple Inheritance
D) Hierarchical Inheritance
Answer: C) Multiple Inheritance
Explanation: C++ allows a derived class to inherit directly from more than one base class (Multiple Inheritance).
Q10. A class that contains at least one pure virtual function is called a/an:
A) Concrete Class
B) Abstract Class
C) Friend Class
D) Nested Class
Answer: B) Abstract Class
Explanation: Abstract classes cannot be instantiated directly and serve as base classes requiring implementation in derived classes.
Section 3: Pointers, References & Functions
Q11. What does a pointer variable store?
A) Value of a variable
B) Memory address of another variable
C) Function return type
D) Data type size
Answer: B) Memory address of another variable
Explanation: Pointers hold memory address locations where data values are stored.
Q12. Which symbol is used to declare a reference variable in C++?
A) *
B) &
C) ->
D) ::
Answer: B) &
Explanation: An ampersand & creates an alias/reference for an existing variable (e.g., int &ref = num;).
Q13. What is function overloading in C++?
A) Two functions with same name and different parameters
B) Two functions with different names and same parameters
C) Overriding virtual functions in derived classes
D) Defining global functions inside main
Answer: A) Two functions with same name and different parameters
Explanation: Function overloading enables multiple functions in the same scope to share a name if parameter lists differ.
Q14. Which keyword is used to define an inline function?
A) inline
B) macro
C) fast
D) static
Answer: A) inline
Explanation: The inline keyword suggests to the compiler to replace function calls directly with the function body code to reduce call overhead.
Q15. A pointer that points to no valid memory address is called a:
A) Wild Pointer
B) Null Pointer
C) Dangling Pointer
D) Void Pointer
Answer: B) Null Pointer
Explanation: A Null Pointer explicitly stores nullptr or 0, indicating it does not point to any memory location.
Section 4: Advanced OOP, Templates & STL
Q16. Which keyword is used to make a function virtual in C++?
A) override
B) virtual
C) abstract
D) interface
Answer: B) virtual
Explanation: Declaring a base class function virtual enables runtime polymorphism and dynamic binding.
Q17. How do you declare a pure virtual function in C++?
A) virtual void display() = 0;
B) virtual void display();
C) void display() = pure;
D) abstract void display();
Answer: A) virtual void display() = 0;
Explanation: Appending = 0 to a virtual function declaration turns it into a pure virtual function.
Q18. What is the scope resolution operator in C++?
A) .
B) ->
C) ::
D) :
Answer: C) ::
Explanation: :: (scope resolution operator) accesses global variables or defines class functions outside class declarations.
Q19. What does STL stand for in C++?
A) System Tool Library
B) Standard Template Library
C) Structural Text Language
D) Standard Thread Logic
Answer: B) Standard Template Library
Explanation: STL is a library providing generic containers (like vector, list), algorithms, and iterators.
Q20. Which STL container implements a dynamic array?
A) std::list
B) std::vector
C) std::stack
D) std::set
Answer: B) std::vector
Explanation: std::vector stores elements contiguously and automatically resizes when elements are inserted or deleted.
Section 5: Memory Management & Exception Handling
Q21. Which operator is used to deallocate memory allocated with new[] for arrays?
A) delete
B) delete[]
C) free()
D) remove
Answer: B) delete[]
Explanation: Arrays allocated dynamically using new[] must be released using delete[] to invoke destructors properly.
Q22. Which block in C++ contains code that might throw an exception?
A) catch
B) throw
C) try
D) finally
Answer: C) try
Explanation: Code capable of throwing runtime exceptions is enclosed inside a try block.
Q23. What keyword is used to explicitly raise an exception in C++?
A) raise
B) throw
C) catch
D) error
Answer: B) throw
Explanation: The throw statement signals an exception condition to an enclosing try/catch handler.
Q24. Destructors in C++ take how many parameters?
A) 0
B) 1
C) 2
D) Depends on class
Answer: A) 0
Explanation: Destructors cannot take any arguments, nor can they return values or be overloaded.
Q25. Destructor names are preceded by which symbol?
A) #
B) ~
C) *
D) &
Answer: B) ~
Explanation: The tilde ~ symbol precedes the class name to declare a destructor (e.g., ~MyClass()).
Section 6: Additional Core C++ MCQs (Q26 – Q50)
Q26. Which feature allows functions or classes to work with generic data types?
A) Macro
B) Template
C) Virtual function
D) Namespace
Answer: B) Template
Explanation: C++ templates allow writing generic code independent of specific data types.
Q27. What is the output of cout << (5 / 2); in C++?
A) 2.5
B) 2
C) 3
D) Error
Answer: B) 2
Explanation: Integer division truncates fractional components, returning 2.
Q28. Which keyword prevents a class from being inherited or a virtual function from being overridden?
A) static
B) const
C) final
D) sealed
Answer: C) final
Explanation: C++11 introduced final to stop further class inheritance or method overriding.
Q29. Which pointer operator is used to access members of a structure/class through a pointer?
A) .
B) ->
C) *
D) &
Answer: B) ->
Explanation: Arrow operator -> dereferences a pointer and accesses member variables or functions.
Q30. A function defined inside a class body is implicitly treated as:
A) Virtual
B) Inline
C) Static
D) Friend
Answer: B) Inline
Explanation: Member functions defined inside class declarations are automatically inline candidates.
(سوال نمبر 31 سے 50 اسی طرح مکمل سٹرکچرڈ فارمیٹ میں شامل کیے گئے ہیں)
Q31. Which feature prevents name collisions in large C++ projects?
A) Class
B) Namespace
C) Header
D) Macro
Answer: B) Namespace
Explanation: Namespaces group entities under distinct named scopes to resolve naming conflicts.
Q32. What is the default access specifier for members of a class in C++?
A) public
B) private
C) protected
D) internal
Answer: B) private
Explanation: Class members default to private, whereas struct members default to public.
Q33. What is the default access specifier for members of a struct in C++?
A) private
B) public
C) protected
D) friend
Answer: B) public
Explanation: Unlike class, all members of a struct default to public access.
Q34. A friend function can access which class members?
A) Only public members
B) Only protected members
C) Private, protected, and public members
D) None
Answer: C) Private, protected, and public members
Explanation: Declaring a non-member function as a friend grants full access to private and protected class data.
Q35. Copy constructor accepts its parameter as a:
A) Value
B) Reference to constant object
C) Pointer
D) Primitive int
Answer: B) Reference to constant object
Explanation: Copy constructor takes const ClassName &obj to prevent infinite recursive pass-by-value calls.
Q36. Which operator cannot be overloaded in C++?
A) +
B) ==
C) ::
D) []
Answer: C) ::
Explanation: Scope resolution ::, dot ., ternary ?:, and sizeof operators cannot be overloaded.
Q37. What is a dangling pointer?
A) Pointer initialized to NULL
B) Pointer pointing to freed/deallocated memory
C) Uninitialized pointer
D) Constant pointer
Answer: B) Pointer pointing to freed/deallocated memory
Explanation: Dangling pointers refer to memory blocks that have already been deleted/freed.
Q38. Runtime polymorphism in C++ is achieved using:
A) Function Overloading
B) Operator Overloading
C) Virtual Functions
D) Templates
Answer: C) Virtual Functions
Explanation: Virtual functions resolve call addresses dynamically at runtime (Dynamic Binding).
Q39. Compile-time polymorphism includes:
A) Function Overloading & Operator Overloading
B) Virtual Functions
C) Inheritance
D) Abstract classes
Answer: A) Function Overloading & Operator Overloading
Explanation: Overloading choices are resolved at compile time (Static Binding).
Q40. Which keyword is used to modify a variable inside a const member function?
A) static
B) mutable
C) volatile
D) register
Answer: B) mutable
Explanation: mutable allows class data members to be altered even within const functions.
Q41. Which algorithm sorting method is default in STL std::sort?
A) QuickSort
B) IntroSort
C) MergeSort
D) BubbleSort
Answer: B) IntroSort
Explanation: STL std::sort uses IntroSort (combination of QuickSort, HeapSort, and InsertionSort).
Q42. Which header is required to use STL algorithms like sort() and find()?
A) <algorithm>
B) <utility>
C) <numeric>
D) <vector>
Answer: A) <algorithm>
Explanation: The <algorithm> header contains utility template functions for searching, sorting, and manipulating collections.
Q43. What is the output of sizeof(char) in C++?
A) 1 byte
B) 2 bytes
C) 4 bytes
D) Compiler dependent
Answer: A) 1 byte
Explanation: Standard C++ guarantees sizeof(char) is strictly 1 byte.
Q44. Static data members belong to:
A) Individual objects
B) The entire class (shared among all objects)
C) Only main function
D) Heap memory
Answer: B) The entire class (shared among all objects)
Explanation: Static members exist once per class rather than per instantiated object.
Q45. Which keyword is used to declare a constant variable whose value cannot change?
A) static
B) const
C) fixed
D) immutable
Answer: B) const
Explanation: const prevents variable re-assignment post initialization.
Q46. What does RTTI stand for in C++?
A) Real-Time Thread Interface
B) Run-Time Type Information
C) Relative Type Transmission Index
D) Random Transmission Task Identifier
Answer: B) Run-Time Type Information
Explanation: RTTI provides mechanism (e.g., dynamic_cast, typeid) to determine object types at runtime.
Q47. Which casting operator is safest for downcasting in dynamic class hierarchies?
A) static_cast
B) dynamic_cast
C) reinterpret_cast
D) const_cast
Answer: B) dynamic_cast
Explanation: dynamic_cast checks types at runtime and returns nullptr if downcasting fails.
Q48. What is a Memory Leak in C++?
A) Stack overflow error
B) Dynamically allocated heap memory not freed before losing reference
C) Reading unassigned variable
D) Infinite loop execution
Answer: B) Dynamically allocated heap memory not freed before losing reference
Explanation: Unreleased heap memory allocated via new causes memory leaks.
Q49. Smart pointers in modern C++ (C++11 and later) are defined in which header?
A) <memory>
B) <pointer>
C) <smart>
D) <utility>
Answer: A) <memory>
Explanation: Header <memory> contains std::unique_ptr, std::shared_ptr, and std::weak_ptr.
Q50. Which operator is used to access members of a class using an object instance?
A) ->
B) .
C) ::
D) *
Answer: B) .
Explanation: Direct object instances use dot operator . to access public class members.
Conclusion
C++ Programming forms the foundational backbone of software development and Computer Science competitive exams. Understanding object-oriented principles, pointer arithmetic, and STL containers guarantees high performance in PPSC, NTS, and FPSC tests.
Stay tuned to mcqsdesk.com for more subject-wise quiz sets and exam preparation resources!
Related Subject-wise MCQs:
