100 C++ MCQ (Multiple Choice Questions) with Answers

 

1) Who developed the C++ programming language?
  1. Dennis Ritchie
  2. Bjarne Stroustrup
  3. James Gosling
  4. Guido van Rossum
Show Answer
Answer: b
Explanation
Bjarne Stroustrup created C++ at Bell Labs starting in 1979, originally calling it “C with Classes” before renaming it C++ in 1983. Dennis Ritchie created C, James Gosling created Java, and Guido van Rossum created Python.

2) Which standard output stream is used in C++ to display text?
  1. printf()
  2. cout
  3. cin
  4. System.out.println
Show Answer
Answer: b
Explanation
cout (character output) is the standard output stream declared in <iostream>. printf() is a C library function, cin is the input stream, and System.out.println is Java syntax.

3) Which header file is needed to use std::cout and std::cin?
  1. <stdio.h>
  2. <stdlib.h>
  3. <iostream>
  4. <string>
Show Answer
Answer: c
Explanation
The <iostream> header declares the standard stream objects cin, cout, cerr and clog. <stdio.h> and <stdlib.h> are C headers, and <string> provides std::string.

4) What is the standard extension of a C++ source code file?
  1. .c
  2. .cpp
  3. .class
  4. .cp
Show Answer
Answer: b
Explanation
.cpp is the conventional C++ source extension (.cc, .cxx and .c++ also work). A .c file is treated as C by the compiler, and .class is a compiled Java file.

5) Which operator is used for input stream insertion in C++?
  1. <<
  2. >>
  3. ->
  4. ::
Show Answer
Answer: b
Explanation
The extraction operator >> pulls data out of a stream, e.g. cin >> x;. The insertion operator << pushes data into a stream (cout).

6) Which operator is used for scope resolution in C++?
  1. .
  2. ->
  3. ::
  4. :
Show Answer
Answer: c
Explanation
The :: operator resolves scope, e.g. std::cout or MyClass::method(). The dot accesses members of an object and -> accesses members through a pointer.

7) Which keyword is used to define a class in C++?
  1. struct
  2. class
  3. object
  4. interface
Show Answer
Answer: b
Explanation
The class keyword defines a class. struct also defines a class-like type but with public default access. “object” and “interface” are not C++ keywords.

8) What is the default access modifier for members of a class in C++?
  1. public
  2. protected
  3. private
  4. package-private
Show Answer
Answer: c
Explanation
Members of a class are private by default. This is one of the few differences between class and struct, whose members default to public. “package-private” is Java, not C++.

9) What is the default access specifier for members of a struct in C++?
  1. private
  2. public
  3. protected
  4. internal
Show Answer
Answer: b
Explanation
In C++, a struct defaults to public access (both for members and for inheritance). This is the key practical difference from class.

10) Which keyword is used to allocate memory dynamically in C++?
  1. malloc
  2. alloc
  3. new
  4. create
Show Answer
Answer: c
Explanation
The new operator allocates memory from the free store and calls the object’s constructor. malloc() is the C library function that only allocates raw bytes and does not run constructors.

11) Which operator is used to deallocate dynamic memory allocated with new?
  1. free
  2. delete
  3. remove
  4. clear
Show Answer
Answer: b
Explanation
delete releases memory obtained with new and invokes the destructor. free() is the C counterpart to malloc(). Every new must be matched by a delete, and every new[] by delete[].

12) Which keyword is used to pass a parameter by reference without creating a copy?
  1. *
  2. &
  3. ref
  4. pointer
Show Answer
Answer: b
Explanation
The ampersand & declares a reference, e.g. void f(int &x). A reference is an alias for the original object, so no copy is made. The * symbol declares a pointer instead.

13) What function is called automatically when an object is created?
  1. Destructor
  2. Constructor
  3. Virtual Function
  4. Inline Function
Show Answer
Answer: b
Explanation
A constructor runs automatically when an object is created and initialises it. A destructor runs automatically when the object goes out of scope or is deleted.

14) What symbol precedes the name of a destructor function in C++?
  1. #
  2. ~
  3. *
  4. !
Show Answer
Answer: b
Explanation
A destructor is written as ~ClassName(). For example, the destructor of class Car is ~Car(). It takes no parameters and has no return type.

15) Can a constructor be overloaded in C++?
  1. Yes
  2. No
  3. Only in derived classes
  4. Only if declared private
Show Answer
Answer: a
Explanation
Yes — a class can have many constructors as long as their parameter lists differ (default, parameterised, copy, move, etc.). This is called constructor overloading.

16) Can a destructor be overloaded in C++?
  1. Yes
  2. No
  3. Only if it takes no arguments
  4. Depends on the compiler
Show Answer
Answer: b
Explanation
No. A destructor takes no arguments and has no return type, so there is no way to give it a different signature. A class can have exactly one destructor.

17) Which feature allows defining multiple functions with the same name but different parameters?
  1. Function Overriding
  2. Function Overloading
  3. Virtual Functions
  4. Abstraction
Show Answer
Answer: b
Explanation
Function overloading means several functions share a name but differ in the number or types of parameters; the compiler picks the right one at compile time (static polymorphism).

18) What is function overriding?
  1. Defining multiple functions in the same class with different parameters
  2. Redefining a base class method in a derived class with the same signature
  3. Calling a function recursively
  4. Hiding global variables
Show Answer
Answer: b
Explanation
Overriding redefines a base class virtual method in a derived class using the same signature. It enables runtime polymorphism. Option (a) describes overloading instead.

19) Which keyword is used to make a method support dynamic binding / runtime polymorphism?
  1. inline
  2. virtual
  3. override
  4. static
Show Answer
Answer: b
Explanation
Marking a function virtual tells the compiler to resolve the call at runtime through the vtable, based on the actual object type rather than the pointer/reference type.

20) What is a class containing at least one pure virtual function called?
  1. Concrete class
  2. Abstract class
  3. Interface class
  4. Sealed class
Show Answer
Answer: b
Explanation
A class with at least one pure virtual function is abstract and cannot be instantiated. Derived classes must override every pure virtual function to become concrete.

21) What is the syntax for declaring a pure virtual function in C++?
  1. virtual void display() = 0;
  2. virtual void display();
  3. pure virtual void display();
  4. void display() = pure;
Show Answer
Answer: a
Explanation
The = 0 suffix makes a virtual function pure: virtual void display() = 0;. Option (b) is just an ordinary virtual function with a body expected elsewhere.

22) Which operator cannot be overloaded in C++?
  1. +
  2. ::
  3. []
  4. ()
Show Answer
Answer: b
Explanation
The scope resolution operator :: cannot be overloaded. The +, [] and () operators can all be overloaded.

23) Which of the following operators CANNOT be overloaded?
  1. sizeof
  2. ?:
  3. .
  4. All of the above
Show Answer
Answer: d
Explanation
sizeof, the ternary conditional ?: and the member access dot . are all non-overloadable. The full list also includes ::, .* and the preprocessor # and ##.

24) Which keyword is used to grant a non-member function access to private members of a class?
  1. public
  2. friend
  3. protected
  4. export
Show Answer
Answer: b
Explanation
A friend declaration inside a class gives a non-member function (or another class) full access to its private and protected members.

25) What pointer refers to the current invoking instance inside a member function?
  1. super
  2. this
  3. self
  4. base
Show Answer
Answer: b
Explanation
The this pointer holds the address of the object on which the member function was called. It is passed implicitly to every non-static member function.

26) Which type of inheritance allows a derived class to inherit from multiple base classes?
  1. Single Inheritance
  2. Multilevel Inheritance
  3. Multiple Inheritance
  4. Hierarchical Inheritance
Show Answer
Answer: c
Explanation
Multiple inheritance lets one class derive from two or more base classes, e.g. class C : public A, public B. Multilevel inheritance is a chain (A → B → C).

27) What problem arises when a class inherits from two classes that both inherit from the same base class?
  1. Diamond Problem
  2. Circular Dependency
  3. Stack Overflow
  4. Dangling Pointer
Show Answer
Answer: a
Explanation
This is the Diamond Problem: the bottom class ends up with two copies of the shared base sub-object, creating ambiguity when accessing base members.

28) Which keyword resolves the Diamond Problem when declaring base class inheritance?
  1. friend
  2. virtual
  3. override
  4. static
Show Answer
Answer: b
Explanation
Virtual inheritance (class B : virtual public A) makes the shared base exist only once in the final object, eliminating the duplicate sub-object.

29) What is the output of sizeof(bool) in standard C++?
  1. 1 byte
  2. 2 bytes
  3. 4 bytes
  4. Compiler-dependent
Show Answer
Answer: a
Explanation
sizeof(bool) is 1 on essentially every mainstream implementation. Strictly speaking the standard only requires at least 1, but in practice it is always exactly 1 byte.

30) Which keyword is used to define template functions or classes in C++?
  1. template
  2. generic
  3. typename
  4. Both a and c
Show Answer
Answer: d
Explanation
A template is introduced with the template keyword, and the type parameter is declared with typename (or class): template <typename T> T max(T a, T b);. “generic” is not a C++ keyword.

31) Which exception handling keyword is used to inspect a code block for errors?
  1. catch
  2. try
  3. throw
  4. finally
Show Answer
Answer: b
Explanation
The try block wraps code that might throw. Any exception raised inside it is passed to a matching catch block. C++ has no “finally” keyword.

32) Which keyword is used to signal the occurrence of an exception?
  1. raise
  2. throw
  3. catch
  4. error
Show Answer
Answer: b
Explanation
throw raises an exception, e.g. throw std::runtime_error("fail");. “raise” is Python and “catch” handles the exception rather than creating it.

33) Which block handles an exception thrown from a try block?
  1. catch
  2. except
  3. finally
  4. rescue
Show Answer
Answer: a
Explanation
The catch block receives the thrown object and handles it. “except” is Python, “rescue” is Ruby, and “finally” is Java/C#.

34) What does STL stand for in C++?
  1. System Type Library
  2. Standard Template Library
  3. Single Thread Logic
  4. Standard Text Language
Show Answer
Answer: b
Explanation
STL stands for Standard Template Library — the collection of generic containers, iterators, algorithms and function objects that later became part of the C++ standard library.

35) Which container in STL represents a dynamic contiguous array?
  1. std::list
  2. std::vector
  3. std::map
  4. std::set
Show Answer
Answer: b
Explanation
std::vector stores elements in one contiguous block of memory that grows automatically. list is a linked list and map/set are node-based trees.

36) Which STL container is implemented as a doubly linked list?
  1. std::vector
  2. std::deque
  3. std::list
  4. std::set
Show Answer
Answer: c
Explanation
std::list is a doubly linked list, so insertion and removal anywhere are O(1) once the position is known, but there is no random access by index.

37) Which STL container stores unique elements in sorted order?
  1. std::set
  2. std::vector
  3. std::list
  4. std::unordered_set
Show Answer
Answer: a
Explanation
std::set keeps unique elements automatically sorted (typically as a red-black tree). std::unordered_set also enforces uniqueness but uses a hash table with no ordering.

38) Which header file is required to use std::vector?
  1. <array>
  2. <vector>
  3. <list>
  4. <container>
Show Answer
Answer: b
Explanation
You must include <vector>. <array> provides the fixed-size std::array and <list> provides std::list.

39) What method returns the number of elements in a std::vector?
  1. length()
  2. size()
  3. count()
  4. capacity()
Show Answer
Answer: b
Explanation
size() returns the number of elements currently stored. capacity() returns how many elements can be held before reallocation — it is often larger than size().

40) What method adds an element to the end of a std::vector?
  1. append()
  2. push_back()
  3. insert()
  4. add()
Show Answer
Answer: b
Explanation
push_back() appends an element to the end (amortised O(1)). insert() adds at an arbitrary position but is generally O(n). C++ has no append() or add() on vector.

41) What is the output of 7 / 2 in C++ integer arithmetic?
  1. 3.5
  2. 3
  3. 4
  4. 3.0
Show Answer
Answer: b
Explanation
When both operands are integers, / performs integer division and truncates toward zero, so 7 / 2 = 3. Use 7.0 / 2 to get 3.5.

42) What is the result of 7 % 2 in C++?
  1. 1
  2. 3.5
  3. 0
  4. 2
Show Answer
Answer: a
Explanation
The modulus operator % returns the remainder: 7 divided by 2 is 3 with remainder 1. The % operator works only on integral types.

43) Which keyword is used to prevent modification of a variable’s value?
  1. static
  2. const
  3. constexpr
  4. Both b and c
Show Answer
Answer: d
Explanation
Both const and constexpr produce values that cannot be modified. constexpr goes further, requiring the value to be computable at compile time. “static” controls storage duration and linkage, not mutability.

44) What keyword was introduced in C++11 for automatic type deduction?
  1. var
  2. auto
  3. type
  4. decltype
Show Answer
Answer: b
Explanation
In C++11 the meaning of auto changed from “automatic storage” to type deduction from the initialiser, e.g. auto x = 5;. decltype is related but deduces the type of an expression without evaluating it.

45) Which value represents a null pointer constant introduced in C++11?
  1. NULL
  2. 0
  3. nullptr
  4. void*
Show Answer
Answer: c
Explanation
nullptr was added in C++11 as a proper null pointer literal of type std::nullptr_t. It avoids the ambiguities that NULL (usually just 0) creates in overload resolution.

46) What does RAII stand for in C++ programming?
  1. Resource Acquisition Is Initialization
  2. Runtime Access In Inheritance
  3. Random Allocation In Iteration
  4. Reference Access And Inspection
Show Answer
Answer: a
Explanation
Resource Acquisition Is Initialization ties a resource’s lifetime to an object’s lifetime: the constructor acquires, the destructor releases. It is the foundation of smart pointers and exception safety.

47) Which smart pointer provides exclusive ownership of an object?
  1. std::shared_ptr
  2. std::unique_ptr
  3. std::weak_ptr
  4. std::auto_ptr
Show Answer
Answer: b
Explanation
std::unique_ptr owns its object exclusively and cannot be copied (only moved). shared_ptr allows multiple owners, weak_ptr observes without owning, and auto_ptr was removed in C++17.

48) Which smart pointer uses reference counting to manage shared ownership?
  1. std::unique_ptr
  2. std::shared_ptr
  3. std::weak_ptr
  4. std::raw_ptr
Show Answer
Answer: b
Explanation
std::shared_ptr maintains a reference count and destroys the object only when the last owner is gone. std::weak_ptr observes a shared_ptr without incrementing that count.

49) Which header file is required for smart pointers like std::unique_ptr?
  1. <pointer>
  2. <memory>
  3. <utility>
  4. <stdlib.h>
Show Answer
Answer: b
Explanation
Smart pointers live in <memory>, along with std::make_unique and std::make_shared. <utility> provides std::move and std::pair instead.

50) What keyword suggests to the compiler to inline a function to reduce call overhead?
  1. inline
  2. fast
  3. macro
  4. static
Show Answer
Answer: a
Explanation
inline is a hint that the compiler should expand the function body at the call site instead of performing a call. Modern compilers usually decide on their own; the main guaranteed effect of inline is relaxed ODR rules for definitions in headers.

51) What type of casting is used for conversion between related class pointer types at runtime?
  1. static_cast
  2. dynamic_cast
  3. reinterpret_cast
  4. const_cast
Show Answer
Answer: b
Explanation
dynamic_cast performs a checked conversion between polymorphic types at runtime, returning nullptr (for pointers) if the conversion is invalid. It requires the base class to have at least one virtual function.

52) Which cast operator removes the const qualification from a variable?
  1. static_cast
  2. const_cast
  3. reinterpret_cast
  4. dynamic_cast
Show Answer
Answer: b
Explanation
const_cast adds or removes const (and volatile). Modifying an object that was originally declared const through such a cast is undefined behaviour.

53) Which cast is used for low-level bitwise reinterpretation of memory addresses?
  1. reinterpret_cast
  2. static_cast
  3. dynamic_cast
  4. const_cast
Show Answer
Answer: a
Explanation
reinterpret_cast reinterprets the bit pattern of a value as another type, e.g. converting a pointer to an integer. It is the most dangerous cast and is highly platform-dependent.

54) What is the scope of a variable declared with static inside a function?
  1. Local to the function, but retains value across function calls
  2. Global to all files
  3. Destroyed as soon as the function exits
  4. Restricted to class members
Show Answer
Answer: a
Explanation
A function-local static variable has static storage duration but local scope. It is initialised once and keeps its value between calls, yet is invisible outside the function.

55) How do you declare a constant expression evaluated at compile-time in C++11?
  1. const
  2. constexpr
  3. static const
  4. inline const
Show Answer
Answer: b
Explanation
constexpr, introduced in C++11, guarantees compile-time evaluation (when the initialiser is a constant expression). Plain const only means “read-only” and may be initialised at runtime.

56) What is an anonymous inline function introduced in C++11 called?
  1. Virtual function
  2. Lambda expression
  3. Macro
  4. Template function
Show Answer
Answer: b
Explanation
A lambda expression defines an unnamed function object inline: [](int x){ return x * 2; }. The compiler generates a unique closure type for it.

57) In a lambda expression [=]() { … }, what does [=] mean?
  1. Capture all external variables by value
  2. Capture all external variables by reference
  3. Capture nothing
  4. Return equal value
Show Answer
Answer: a
Explanation
[=] captures every variable used in the body by value, making a copy inside the closure. [&] captures by reference and [] captures nothing.

58) In a lambda expression [&]() { … }, what does [&] mean?
  1. Capture all external variables by value
  2. Capture all external variables by reference
  3. Pass arguments by reference
  4. Return address
Show Answer
Answer: b
Explanation
[&] captures all used external variables by reference, so the lambda sees and can modify the originals. Care is needed if the lambda outlives those variables.

59) What is the type of string literals like “Hello” in C++?
  1. std::string
  2. const char[]
  3. char*
  4. vector<char>
Show Answer
Answer: b
Explanation
A string literal has type const char[N] and decays to const char*. It is stored in read-only memory, so writing to it is undefined behaviour. It is not a std::string unless you construct one.

60) Which standard library header provides std::string?
  1. <string.h>
  2. <cstring>
  3. <string>
  4. <text>
Show Answer
Answer: c
Explanation
Explanation
<string> provides the std::string class. <string.h> and <cstring> are the C headers for the char* functions strlen, strcpy, etc.

61) Which method converts a std::string object to a C-style null-terminated string?
  1. to_char()
  2. c_str()
  3. str()
  4. data_str()
Show Answer
Answer: b
Explanation
c_str() returns a const char* pointing to a null-terminated copy of the string’s contents, valid only while the string is unchanged. data() does something similar but is not guaranteed to be null-terminated before C++11.

62) What does cin.get() do?
  1. Reads an entire line of text
  2. Reads a single character including whitespace
  3. Clears input stream
  4. Reads integers only
Show Answer
Answer: b
Explanation
cin.get() reads exactly one character, including whitespace such as spaces and newlines. To read a whole line use getline(cin, str).

63) Which function reads an entire line including spaces into a std::string?
  1. cin >> str
  2. getline(cin, str)
  3. gets(str)
  4. cin.read(str)
Show Answer
Answer: b
Explanation
getline(cin, str) reads everything up to the newline, preserving spaces. The extraction operator cin >> stops at the first whitespace. gets() was removed from C++17 as unsafe.

64) What happens if delete is used on a memory allocated with new[] (array allocation)?
  1. Everything is deallocated cleanly
  2. Undefined behavior (should use delete[])
  3. Compile-time error
  4. Memory leaks are prevented automatically
Show Answer
Answer: b
Explanation
Mismatching new[] with delete (or new with delete[]) is undefined behaviour — typically only the first element’s destructor runs and the heap may be corrupted. Always pair new[] with delete[].

65) What is the result of dereferencing a nullptr?
  1. Returns 0
  2. Undefined behavior / Segmentation Fault
  3. Returns NULL
  4. Throws a runtime_error
Show Answer
Answer: b
Explanation
Dereferencing a null pointer is undefined behaviour, which in practice almost always causes a segmentation fault. C++ does not throw an exception for this.

66) What is a pointer pointing to memory that has been deallocated called?
  1. Null pointer
  2. Dangling pointer
  3. Wild pointer
  4. Void pointer
Show Answer
Answer: b
Explanation
A dangling pointer still holds an address after the memory it points to has been freed or the object has gone out of scope. Using it is undefined behaviour; set pointers to nullptr after delete.

67) What is an uninitialized pointer called?
  1. Wild pointer
  2. Null pointer
  3. Dangling pointer
  4. Smart pointer
Show Answer
Answer: a
Explanation
An uninitialised pointer holds an indeterminate address and is called a wild pointer. A null pointer explicitly points to nothing, and a dangling pointer points to freed memory.

68) What class stream is used for reading from files in C++?
  1. ofstream
  2. ifstream
  3. fstream
  4. stringstream
Show Answer
Answer: b
Explanation
ifstream (input file stream) reads from files. ofstream writes, fstream does both, and stringstream works on in-memory strings.

69) What class stream is used for writing to files in C++?
  1. ifstream
  2. ofstream
  3. iostream
  4. fileout
Show Answer
Answer: b
Explanation
ofstream (output file stream) writes data to files. It creates or truncates the file by default unless you pass std::ios::app.

70) Which header file is required for file I/O operations in C++?
  1. <iostream>
  2. <fstream>
  3. <stdio.h>
  4. <file>
Show Answer
Answer: b
Explanation
<fstream> declares ifstream, ofstream and fstream. <iostream> only covers the console streams cin, cout, cerr and clog.

71) What mode flag in fstream appends output to the end of a file?
  1. std::ios::in
  2. std::ios::out
  3. std::ios::app
  4. std::ios::trunc
Show Answer
Answer: c
Explanation
std::ios::app opens the file in append mode, so all writes go to the end without erasing existing content. std::ios::trunc does the opposite — it clears the file.

72) Which keyword specifies that a class member cannot be modified inside a const member function?
  1. mutable
  2. volatile
  3. static
  4. Member variables can be modified by default
Show Answer
Answer: a
Explanation
By default a const member function cannot modify any data member. Declaring a member mutable is the exception — it may be modified even inside a const member function.

73) What key property allows mutable data members to be modified?
  1. Modified inside const member functions
  2. Modified across multiple threads safely
  3. Cannot be initialized
  4. Stored in stack memory
Show Answer
Answer: a
Explanation
A mutable member can be changed even when the enclosing object is const or the function is const — commonly used for caches, counters and lazy initialisation.

74) What is the scope of protected members of a base class?
  1. Accessible only inside base class
  2. Accessible inside base class and derived classes
  3. Accessible everywhere
  4. Accessible within the same package
Show Answer
Answer: b
Explanation
protected members are accessible inside the base class, inside derived classes, and by friends — but not by unrelated code that merely has an object of the class.

75) When a class inherits a base class publicly (class Derived : public Base), how do protected base members behave?
  1. Become private in derived
  2. Remain protected in derived
  3. Become public in derived
  4. Inaccessible
Show Answer
Answer: b
Explanation
With public inheritance the access levels carry over: public stays public, protected stays protected, and private members stay private to the base (but can still be reached through public/protected base methods).

76) What happens to public members when inheriting privately (class Derived : private Base)?
  1. Become private in derived class
  2. Remain public
  3. Become protected
  4. Inaccessible
Show Answer
Answer: a
Explanation
Private inheritance downgrades all inherited public and protected members to private in the derived class, so clients of Derived cannot use them. This models “implemented in terms of” rather than “is-a”.

77) What operator is used to access members using an object reference or instance?
  1. .
  2. ->
  3. ::
  4. *
Show Answer
Answer: a
Explanation
The dot operator . accesses members of an object or reference, e.g. obj.value. The arrow -> is used with pointers.

78) What operator is used to access members using an object pointer?
  1. .
  2. ->
  3. ::
  4. &
Show Answer
Answer: b
Explanation
The arrow operator -> dereferences a pointer and accesses a member in one step: ptr->value is shorthand for (*ptr).value.

79) What feature allows a constructor to be called with a single argument without implicit type conversion?
  1. explicit keyword
  2. implicit keyword
  3. override keyword
  4. final keyword
Show Answer
Answer: a
Explanation
Marking a single-argument constructor explicit prevents the compiler from silently converting a value into an object, so the constructor must be called directly.

80) What C++11 keyword prevents a class from being inherited or a virtual function from being overridden?
  1. stop
  2. final
  3. sealed
  4. const
Show Answer
Answer: b
Explanation
final can be applied to a class (it cannot be inherited from) or to a virtual function (it cannot be overridden further). “sealed” is the C# equivalent.

81) What C++11 keyword explicitly specifies that a method overrides a base class virtual function?
  1. virtual
  2. override
  3. overload
  4. extends
Show Answer
Answer: b
Explanation
The override specifier makes the compiler verify that a matching virtual function really exists in a base class; a mismatch becomes a compile-time error instead of silent non-overriding.

82) What is the size of a empty class in C++?
  1. 0 bytes
  2. 1 byte
  3. 4 bytes
  4. Compiler crash
Show Answer
Answer: b
Explanation
sizeof of an empty class is 1 byte. The standard requires every complete object to have a non-zero size so distinct objects have distinct addresses.

83) Why does an empty class have a non-zero size in C++?
  1. To ensure unique memory addresses for different object instances
  2. To store virtual table pointer
  3. Due to alignment padding
  4. For destructor memory
Show Answer
Answer: a
Explanation
If objects could be 0 bytes, an array of them would have all elements at the same address, breaking pointer arithmetic and identity. The 1 byte guarantees unique addresses.

84) What is VTABLE in C++?
  1. A table of function pointers created for classes with virtual functions
  2. A list of variable names
  3. Memory table for dynamic arrays
  4. Template lookup table
Show Answer
Answer: a
Explanation
The vtable is a per-class table of pointers to the virtual functions. Each object of such a class stores a hidden pointer (vptr) to its class’s vtable, which is how dynamic dispatch works.

85) What hidden pointer inside an object points to the VTABLE?
  1. vptr
  2. this
  3. vtable
  4. smart_ptr
Show Answer
Answer: a
Explanation
The vptr (virtual table pointer) is inserted by the compiler into every object of a polymorphic class. It is set by the constructor and points to the vtable of the object’s dynamic type.

86) When is the VTABLE resolution performed?
  1. Compile-time
  2. Run-time
  3. Preprocessing-time
  4. Linking-time
Show Answer
Answer: b
Explanation
The vtable itself is built at compile/link time, but the lookup — deciding which function to call — happens at run time via the vptr, which is what enables runtime polymorphism.

87) What is copy constructor signature for class A?
  1. A(A obj)
  2. A(const A &obj)
  3. A(A *obj)
  4. void A(A obj)
Show Answer
Answer: b
Explanation
The canonical copy constructor is A(const A &obj) — a reference (usually const) to the same class type, with no return type.

88) What occurs if a copy constructor takes parameter by value (A(A obj)) instead of reference?
  1. Infinite recursion / Compiler Error
  2. Normal copying
  3. Shallow copy
  4. Dynamic allocation
Show Answer
Answer: a
Explanation
Passing by value requires a copy to be made — which requires the copy constructor — which requires a copy… The compiler rejects the declaration as an error (infinite recursion).

89) What type of copy duplicates pointer addresses rather than pointing to new memory allocations?
  1. Deep Copy
  2. Shallow Copy
  3. Smart Copy
  4. Reference Copy
Show Answer
Answer: b
Explanation
A shallow copy copies pointer values, so both objects end up sharing the same heap memory — leading to double deletion when both are destroyed.

90) What type of copy allocates new memory and duplicates actual data values?
  1. Deep Copy
  2. Shallow Copy
  3. Bitwise Copy
  4. Alias Copy
Show Answer
Answer: a
Explanation
A deep copy allocates fresh memory and duplicates the pointed-to data, so the two objects are fully independent and each can be safely destroyed.

91) What rule states that if a class requires a custom destructor, copy constructor, or copy assignment operator, it likely needs all three?
  1. Rule of Three
  2. Rule of Five
  3. Rule of Zero
  4. Rule of Inheritance
Show Answer
Answer: a
Explanation
The Rule of Three says that if a class manages a resource and needs one of destructor, copy constructor or copy assignment operator, it almost certainly needs all three.

92) With C++11 move semantics, Rule of Three expanded into which rule?
  1. Rule of Four
  2. Rule of Five
  3. Rule of Six
  4. Rule of Ten
Show Answer
Answer: b
Explanation
The Rule of Five adds the move constructor and move assignment operator to the original three special member functions, since declaring any of them affects the others.

93) Which operator was introduced in C++11 to support move semantics by referencing rvalues?
  1. &
  2. &&
  3. *
  4. ->*
Show Answer
Answer: b
Explanation
The double ampersand && declares an rvalue reference (e.g. int&&), which can bind to temporaries and enables move constructors and perfect forwarding.

94) Which standard library function converts an lvalue to an rvalue to enable moving?
  1. std::move()
  2. std::forward()
  3. std::copy()
  4. std::cast()
Show Answer
Answer: a
Explanation
std::move() is a cast to an rvalue reference — it does not move anything itself, it just marks the object as movable so a move constructor/assignment can be selected. std::forward() is used for perfect forwarding.

95) Which header file provides std::move and std::forward?
  1. <utility>
  2. <algorithm>
  3. <memory>
  4. <iostream>
Show Answer
Answer: a
Explanation
<utility> declares std::move, std::forward, std::pair and std::swap. <memory> has the smart pointers and <algorithm> the generic algorithms.

96) Which header file contains general algorithms like std::sort, std::find, and std::reverse?
  1. <algorithm>
  2. <utility>
  3. <numeric>
  4. <vector>
Show Answer
Answer: a
Explanation
<algorithm> holds sort, find, reverse, copy, transform and the rest of the non-numeric algorithms. <numeric> holds accumulate, inner_product and similar.

97) What is the average time complexity of std::sort() in STL?
  1. O(N)
  2. O(N log N)
  3. O(N^2)
  4. O(log N)
Show Answer
Answer: b
Explanation
std::sort is typically implemented as introsort (quicksort + heapsort + insertion sort) giving O(N log N) on average and in the worst case.

98) Which namespace contains all standard C++ library components?
  1. sys
  2. std
  3. cpp
  4. main
Show Answer
Answer: b
Explanation
Everything in the C++ standard library lives in the std namespace — std::cout, std::vector, std::string, std::sort and so on.

99) Which keyword allows using components of a namespace without qualifying them explicitly?
  1. using
  2. import
  3. include
  4. namespace
Show Answer
Answer: a
Explanation
A using directive — using namespace std; — brings all names from the namespace into the current scope. “include” pulls in a header and “namespace” declares one.

100) What is the output of sizeof(int*) on a 64-bit operating system architecture?
  1. 2 bytes
  2. 4 bytes
  3. 8 bytes
  4. 16 bytes
Show Answer
Answer: c
Explanation
On a 64-bit architecture a pointer holds a 64-bit address, so sizeof(int*) is 8 bytes. Note that sizeof(int) itself is still 4 bytes — the pointer size and the pointee size are independent.
100 C Programming MCQ (Multiple Choice Questions) with Answers
100 SQL MCQ (Multiple Choice Questions) with Answers
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment