Who developed the C++ programming language?
- A
Dennis Ritchie
- B
Bjarne Stroustrup
- C
James Gosling
- D
Guido van Rossum
Master C++ with 120+ curated MCQs covering basics, OOP, templates, STL, exception handling, and more. Perfect for exams, interviews, and placements.
Who developed the C++ programming language?
Dennis Ritchie
Bjarne Stroustrup
James Gosling
Guido van Rossum
Which of the following is a valid C++ comment style?
// comment
/* comment */
Both A and B
# comment
Which header file is required for input/output operations in C++?
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <cstdio>
What is the extension of a C++ source file?
.c
.cpp
.java
.py
Which namespace is used for standard input/output in C++?
std
iostream
c++
standard
What is the correct syntax to output 'Hello World' in C++?
cout << 'Hello World';
cout << 'Hello World';
print('Hello World');
System.out.println('Hello World');
Which of the following is not a fundamental data type in C++?
int
float
string
char
What is the size of 'int' in C++ (on most 32-bit systems)?
1 byte
2 bytes
4 bytes
8 bytes
Which data type is used to store a single character?
char
string
int
bool
What is the size of 'bool' in C++?
1 byte
2 bytes
4 bytes
8 bytes
Which of the following is a user-defined data type in C++?
int
float
struct
char
What is the range of 'signed char' typically?
-128 to 127
0 to 255
-32768 to 32767
0 to 65535
Which operator is used to allocate memory dynamically in C++?
malloc
new
alloc
create
Which operator is used to deallocate memory in C++?
free
delete
dealloc
remove
What is the result of (5 / 2) in C++?
2.5
2
3
2.0
Which operator is used for logical AND in C++?
&
&&
|
||
What is the purpose of the scope resolution operator (::)?
Access global variables
Access namespace members
Define functions outside class
All of the above
Which operator is used to access a member of a pointer to an object?
.
->
::
*
Which statement is used to exit a loop prematurely in C++?
break
continue
exit
return
Which loop guarantees that the body executes at least once?
for
while
do-while
None
What is the correct syntax for an if-else statement in C++?
if (condition) { } else { }
if condition { } else { }
if (condition) { } elif { }
if (condition) then { } else { }
Which statement is used to skip the current iteration of a loop?
break
continue
goto
return
What is the output of the following? int x = 5; if (x > 3) cout << 'Yes'; else cout << 'No';
Yes
No
YesNo
Error
Which of the following is a valid switch expression in C++?
int
char
enum
All of the above
What is the default return type of a function in C++?
void
int
float
None (must specify)
Which of the following is used to pass arguments by reference in C++?
int &a
int *a
int a
const int a
What is function overloading in C++?
Multiple functions with the same name but different parameters
Multiple functions with same name and same parameters
Function that calls itself
Function that returns multiple values
What is the default value of a parameter in a function if not specified?
0
NULL
Depends on type (undefined)
No default; it's an error
Which keyword is used to define a function as inline?
inline
static
virtual
extern
What is a friend function in C++?
A non-member function that can access private members of a class
A member function of a class
A function that is declared in a namespace
A function that cannot be overloaded
What is the index of the first element in a C++ array?
0
1
-1
Depends on declaration
Which function is used to get the length of a C-style string?
strlen()
length()
size()
strlength()
How to declare an array of 10 integers in C++?
int arr[10];
array int[10];
int[10] arr;
arr int[10];
Which class in C++ is used for strings?
string
String
std::string
Both A and C
What is the output of the following code? char s[] = 'Hello'; cout << sizeof(s);
5
6
4
Error
Which function concatenates two strings in C++ (C-style)?
strcat
strcpy
strcmp
strlen
What is a pointer in C++?
A variable that stores the address of another variable
A variable that stores a value
A reference to a function
A type of array
Which operator is used to get the address of a variable?
&
*
->
::
Which operator is used to dereference a pointer?
&
*
->
.
What is a null pointer?
Pointer that points to address 0
Pointer that points to garbage value
Pointer that points to a valid memory location
Pointer that is not initialized
What is a pointer to a pointer?
Pointer that stores the address of another pointer
Pointer that stores a value
Pointer that is declared as void
Pointer that cannot be dereferenced
What is the size of a pointer on a 64-bit system?
4 bytes
8 bytes
2 bytes
16 bytes
What is a class in C++?
A user-defined data type that encapsulates data and functions
A built-in data type
A function that operates on data
A namespace
What is an object in C++?
An instance of a class
A function of a class
A data member of a class
A type of pointer
Which access specifier allows members to be accessible only within the class?
public
protected
private
internal
What is a constructor in C++?
A special member function that initializes objects
A function that destroys objects
A function that copies objects
A function that assigns objects
What is a destructor in C++?
A special member function that cleans up before object destruction
A function that creates objects
A function that copies objects
A function that overloads operators
What is the 'this' pointer in C++?
Pointer to the current object
Pointer to the base class
Pointer to the derived class
Pointer to the class itself
What is inheritance in C++?
Deriving a new class from an existing class
Creating multiple objects of a class
Combining multiple classes
Overloading functions
Which type of inheritance leads to the diamond problem?
Single inheritance
Multiple inheritance
Multilevel inheritance
Hierarchical inheritance
What is virtual inheritance used for?
To solve the diamond problem
To override functions
To define abstract classes
To create templates
Which specifier allows base class members to be accessible in derived classes but not outside?
public
protected
private
default
What is the order of constructor execution in inheritance?
Base class constructor first, then derived class
Derived class constructor first, then base class
Both execute simultaneously
Depends on the compiler
What is the order of destructor execution in inheritance?
Derived class destructor first, then base class
Base class destructor first, then derived class
Both execute simultaneously
Depends on the compiler
What is polymorphism in C++?
Ability to take many forms
Ability to inherit from multiple classes
Ability to overload operators
Ability to create templates
Which keyword is used to enable function overriding in C++?
virtual
override
final
abstract
What is a pure virtual function?
A virtual function with no implementation in base class
A virtual function that is overridden
A function that cannot be overridden
A function that is declared as static
What is a abstract class?
A class that has at least one pure virtual function
A class that cannot be inherited
A class that has no data members
A class that is declared as final
What is run-time polymorphism?
Achieved via virtual functions and inheritance
Achieved via function overloading
Achieved via templates
Achieved via operator overloading
What is compile-time polymorphism?
Achieved via function overloading and templates
Achieved via virtual functions
Achieved via inheritance
Achieved via abstract classes
What is a template in C++?
A blueprint for creating generic classes and functions
A class that cannot be instantiated
A function that cannot be overridden
A type of inheritance
Which keyword is used to define a template in C++?
template
typename
class
Both A and B or C
What is the difference between typename and class in template parameters?
No difference; they are interchangeable
typename is used for types, class for classes
class is used for types, typename for classes
typename is used for templates, class for classes
What is template specialization?
Defining a specific implementation for a particular type
Defining a template with multiple parameters
Defining a template without parameters
Defining a template as virtual
What is a function template?
A template that defines a generic function
A template that defines a generic class
A template that defines a variable
A template that defines a namespace
What is a class template?
A template that defines a generic class
A template that defines a generic function
A template that defines a variable
A template that defines a namespace
What does STL stand for?
Standard Template Library
Standard Type Library
System Template Library
Standard Tool Library
Which container provides fast random access and is dynamic in size?
vector
list
deque
set
Which container is implemented as a doubly linked list?
vector
list
deque
stack
Which algorithm is used to sort elements in a range?
std::sort
std::find
std::reverse
std::copy
What is an iterator in STL?
An object that points to an element in a container
A container type
An algorithm
A function object
Which container stores unique keys in sorted order?
set
map
unordered_set
multiset
Which keyword is used to throw an exception in C++?
throw
catch
try
exception
Which block is used to handle exceptions in C++?
catch
try
throw
finally
What is the purpose of the 'try' block?
To enclose code that might throw an exception
To handle exceptions
To throw exceptions
To define custom exceptions
Can you catch an exception using 'catch (...)'?
Yes, catches any exception
No, not allowed
Only if no other catch is used
Only for standard exceptions
What is a standard exception class in C++?
std::exception
std::error
std::runtime_error
Both A and C
What happens if an exception is thrown but not caught?
Program terminates with an error
Program continues execution
Exception is ignored
Compiler gives an error
Which header file is used for file input/output in C++?
#include <fstream>
#include <iostream>
#include <file>
#include <stdio.h>
Which class is used for writing to a file in C++?
ifstream
ofstream
fstream
filebuf
Which class is used for reading from a file in C++?
ifstream
ofstream
fstream
filebuf
Which mode is used to append to a file?
ios::app
ios::out
ios::in
ios::binary
How do you check if a file was opened successfully in C++?
file.is_open()
file.open()
file.good()
Both A and C
Which function is used to close a file in C++?
file.close()
file.end()
file.exit()
file.stop()
What is the main function in C++?
main()
Main()
int main()
void main()
What is the size of 'long double' in C++ (on most systems)?
8 bytes
10 bytes
16 bytes
4 bytes
What is the result of (5 % 2) in C++?
1
2
0
2.5
Which loop is best when the number of iterations is known?
for
while
do-while
All are equally suitable
What is a recursive function?
A function that calls itself
A function that is overloaded
A function that is virtual
A function that is inline
What is the output? int arr[] = {1,2,3}; cout << arr[2];
1
2
3
0
What is the type of a pointer to a function?
int (*func)()
int *func()
int func*()
function pointer
Which of the following is not a OOP concept in C++?
Encapsulation
Inheritance
Polymorphism
Procedural abstraction
Which type of inheritance is used to create a relationship like 'is-a'?
Composition
Aggregation
Inheritance
Association
What is the use of the 'final' specifier in C++?
Prevents a class from being inherited or function overridden
Marks a function as virtual
Marks a function as abstract
Marks a function as inline
What is the syntax for a template parameter with a default type?
template <typename T = int>
template <class T = int>
Both A and B
template <T = int>
Which container is a stack implemented as?
std::stack
std::queue
std::deque
std::list
Can you throw an exception of any type?
Yes, any type can be thrown
No, only derived from std::exception
Only built-in types
Only user-defined types
What is the default open mode for fstream?
ios::in | ios::out
ios::in
ios::out
ios::app
What is the use of the 'using' directive?
To bring a namespace into scope
To define a new type
To create a pointer
To allocate memory
What is the size of 'wchar_t' in C++?
2 bytes
4 bytes
1 byte
Depends on compiler
Which operator cannot be overloaded in C++?
:: (scope resolution)
+
->
[]
What is the output? int i = 0; while (i < 3) { cout << i; i++; }
012
123
0123
0 1 2
What is a static function in C++?
A function that belongs to a class but can be called without an object
A function that cannot be changed
A function that is not accessible outside
A function that returns void
What is the difference between C-style string and std::string?
std::string is a class with methods; C-style is character array
C-style is faster
std::string uses null terminator
No difference
What is a smart pointer in C++?
A pointer that automatically manages memory
A pointer that is faster
A pointer that is global
A pointer that cannot be dereferenced
Which of the following is a type of constructor?
Default constructor
Parameterized constructor
Copy constructor
All of the above
What is the difference between public, protected, and private inheritance?
They change the access levels of base class members in derived class
They change the speed of inheritance
They change the number of base classes
They are all the same
What is the use of the 'override' keyword (C++11)?
To explicitly mark a function as overriding a virtual function
To define a new function
To prevent overriding
To make a function pure virtual
Can a template have non-type parameters?
Yes, e.g., template <int N>
No, only types
Only for classes, not functions
Only for functions, not classes
Which STL algorithm is used to find an element in a range?
std::find
std::search
std::binary_search
All of the above
What is the purpose of the 'noexcept' specifier?
Indicates that a function does not throw exceptions
Indicates that a function always throws
Indicates that a function can throw any exception
Indicates that a function is virtual
How to read a line from a file in C++?
getline(file, line)
file >> line
file.readline(line)
Both A and B
What is the difference between #include <header> and #include 'header'?
<> searches system paths, '' searches local directories first
'' searches system paths, <> searches local
Both are the same
<> is for C, '' for C++
What is the size of 'long long' in C++?
8 bytes
4 bytes
16 bytes
2 bytes
Which operator is used to create a new object on the heap?
new
malloc
alloc
create
What is the output? int x = 10; if (x = 5) cout << 'Yes'; else cout << 'No';
Yes
No
Error
Undefined
What is a lambda expression in C++?
An anonymous function
A named function
A function template
A virtual function
What is the output? char str[] = 'Hello'; cout << strlen(str);
5
6
4
Error
What is a function pointer?
A pointer that stores the address of a function
A pointer that points to a variable
A pointer that points to a class
A pointer that cannot be dereferenced
What is the order of initialization of static members?
In the order they are defined
In reverse order
Random order
Depends on compiler
What is a virtual base class?
A base class that is inherited virtually to avoid duplication
A base class with virtual functions
A base class that is abstract
A base class that cannot be inherited
Which container uses a binary tree for storage?
set
vector
list
deque