0% found this document useful (0 votes)
107 views

C++ MCQs

The document discusses important topics in C++ including basic syntax, data types, operators, control statements, functions, classes, inheritance, polymorphism and more. It provides multiple choice questions to test knowledge of these key concepts of C++ programming.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
107 views

C++ MCQs

The document discusses important topics in C++ including basic syntax, data types, operators, control statements, functions, classes, inheritance, polymorphism and more. It provides multiple choice questions to test knowledge of these key concepts of C++ programming.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Major topics in C++:

1. Basic syntax and semantics


2. Data types and variables
3. Operators and expressions
4. Control statements (if-else, switch-case, loops)
5. Functions and function overloading
6. Arrays and pointers
7. Strings and string handling functions
8. Classes and objects
9. Constructors and destructors
10.Inheritance and polymorphism
11.Virtual functions
12.Friend functions
13.Exception handling
14.Templates
15.Standard Template Library (STL)
16.File handling
17.Dynamic memory allocation
18.Namespaces

C++ Important Questions

MCQ's on Basic syntax and semantics of C++

1. What is C++? a) A high-level programming language. b) A low-level programming language.


c) A markup language. d) A scripting language.
Answer: a
2. What is the syntax used to declare a variable in C++? a) var x; b) x = var; c) int x; d) x = int;
Answer: c
3. What is the main function in C++? a) The entry point of a C++ program. b) A function used to
print output to the console. c) A function used to input data from the console. d) A function
used to declare variables.
Answer: a
4. What is the syntax used to print output to the console in C++? a) print("Hello World!"); b) cout
<< "Hello World!"; c) Console.WriteLine("Hello World!"); d) printf("Hello World!");
Answer: b
5. What is a function in C++? a) A block of code that performs a specific task. b) A variable that
holds a value. c) A keyword used to declare a variable. d) None of the above.
Answer: a
6. What is a header file in C++? a) A file that contains declarations for functions and variables. b)
A file that contains executable code. c) A file that contains data. d) None of the above.
Answer: a
7. What is the purpose of the #include directive in C++? a) To include a header file in the current
file. b) To declare a function. c) To declare a variable. d) None of the above.
Answer: a
8. What is a comment in C++? a) A block of code that is not executed. b) A keyword used to
declare a variable. c) A statement that explains the code. d) None of the above.
Answer: c
9. What is the syntax used to create a for loop in C++? a) for i = 0; i < 10; i++ b) for (i = 0; i < 10;
i++) c) loop i = 0 to 10 d) None of the above.
Answer: b
10.What is an if statement in C++? a) A statement used to loop through code. b) A statement used
to declare a function. c) A statement used to conditionally execute code. d) None of the above.
Answer: c

1. What does the term "++" in C++ stand for?


a) Increased by one b) Decreased by one c) Increased by two d) Decreased by two
Answer: a) Increased by one
2. Which of the following is a valid C++ identifier?
a) 2var b) var2 c) var-2 d) -var
Answer: b) var2
3. What is the output of the following code snippet?
int x = 5; cout << ++x << x++ << x;
a) 567 b) 657 c) 666 d) 576
Answer: b) 657
4. Which of the following is a valid data type in C++?
a) char* b) byte c) float64 d) bool
Answer: d) bool
5. What is the correct syntax for a for loop in C++?
a) for (int i = 0; i < 10; i++) b) for i = 0 to 10 c) for (i = 0; i < 10; i++) d) for (int i < 10; i++)
Answer: a) for (int i = 0; i < 10; i++)
6. What is the difference between pass by value and pass by reference in C++?
a) Pass by value creates a copy of the variable, while pass by reference does not b) Pass by reference
creates a copy of the variable, while pass by value does not c) Pass by value does not change the
original variable, while pass by reference does d) Pass by reference does not change the original
variable, while pass by value does
Answer: a) Pass by value creates a copy of the variable, while pass by reference does not
7. What is the difference between a while loop and a do-while loop in C++?
a) A while loop executes the code at least once, while a do-while loop may not b) A do-while loop
executes the code at least once, while a while loop may not c) Both loops execute the code at least
once d) There is no difference between the two loops
Answer: b) A do-while loop executes the code at least once, while a while loop may not
8. Which of the following is not a valid C++ operator?
a) && b) == c) ++ d) &-
Answer: d) &-
9. What is the purpose of the "using namespace std;" statement in C++?
a) It allows you to use standard C++ libraries without having to prefix them with "std::" b) It defines a
new namespace called "std" c) It imports all C++ libraries into your program d) It is not a valid
statement in C++
Answer: a) It allows you to use standard C++ libraries without having to prefix them with "std::"
10.What is the output of the following code snippet?
int x = 2; int y = 3; cout << x + y << x * y;
a) 56 b) 5 6 c) 6 5 d) 8 6
Answer: c) 6 5
1. Which of the following is not a principle of Object-Oriented Programming (OOP)?
a) Encapsulation b) Inheritance c) Polymorphism d) Multithreading
Answer: d) Multithreading
2. What is the purpose of a constructor in C++?
a) To create a new object of a class b) To initialize the data members of an object of a class c) To
destroy an object of a class d) To allocate memory for an object of a class
Answer: b) To initialize the data members of an object of a class
3. What is the difference between private and protected access modifiers in C++?
a) Private members can be accessed by the derived class, while protected members cannot b) Protected
members can be accessed by any other class, while private members cannot c) Private members can be
accessed only by the class that defines them, while protected members can be accessed by the class
that defines them and their derived classes d) There is no difference between private and protected
access modifiers
Answer: c) Private members can be accessed only by the class that defines them, while protected
members can be accessed by the class that defines them and their derived classes
4. What is inheritance in C++?
a) The process of creating a new class from an existing class b) The process of copying the data
members of one object to another object c) The process of creating a new object from an existing
object d) The process of deleting an object
Answer: a) The process of creating a new class from an existing class
5. What is polymorphism in C++?
a) The ability of a derived class to override a method of its base class b) The ability of a class to have
multiple constructors c) The ability of a class to have multiple data members with the same name d)
The ability of a class to have multiple methods with the same name
Answer: d) The ability of a class to have multiple methods with the same name
6. What is the difference between a class and an object in C++?
a) A class is a template for creating objects, while an object is an instance of a class b) A class is an
instance of an object, while an object is a template for creating classes c) A class and an object are the
same thing d) A class is a function, while an object is a variable
Answer: a) A class is a template for creating objects, while an object is an instance of a class
7. What is encapsulation in C++?
a) The ability to hide the implementation details of a class from the outside world b) The ability to
create multiple instances of a class c) The ability to inherit from multiple classes d) The ability to
declare a function as both virtual and pure
Answer: a) The ability to hide the implementation details of a class from the outside world
8. What is a virtual function in C++?
a) A function that can be overridden in a derived class b) A function that cannot be overridden in a
derived class c) A function that is automatically called when an object is created d) A function that can
be called without creating an object of a class
Answer: a) A function that can be overridden in a derived class
9. What is a pure virtual function in C++?
a) A virtual function that has no implementation in the base class b) A virtual function that has an
implementation in the base class c) A virtual function that can be called without creating an object of a
class d) A virtual function that cannot be overridden in a derived class
Answer A
1. What is a class in C++?
a) A data type that stores multiple values of the same type b) A group of functions that perform a
specific task c) A user-defined data type that encapsulates data and methods d) A block of code that can
be executed repeatedly
Answer: c) A user-defined data type that encapsulates data and methods
2. What is an object in C++?
a) A variable of a built-in data type b) A function that performs a specific task c) An instance of a class
d) A data structure that stores multiple values of the same type
Answer: c) An instance of a class
3. How is an object created in C++?
a) By declaring a variable of the class type b) By declaring a function of the class type c) By declaring
a pointer of the class type d) By declaring an array of the class type
Answer: a) By declaring a variable of the class type
4. What is a constructor in C++?
a) A function that returns a value b) A function that is called when an object is created c) A function
that is called when an object is destroyed d) A function that can only be called by other member
functions of the same class
Answer: b) A function that is called when an object is created
5. What is a destructor in C++?
a) A function that returns a value b) A function that is called when an object is created c) A function
that is called when an object is destroyed d) A function that can only be called by other member
functions of the same class
Answer: c) A function that is called when an object is destroyed
6. What is the difference between a private and a public member of a class in C++?
a) Private members can be accessed by any function outside the class, while public members can only
be accessed by member functions of the class b) Private members can only be accessed by member
functions of the class, while public members can be accessed by any function outside the class c)
Private members are declared before public members in the class definition d) There is no difference
between a private and a public member of a class in C++
Answer: b) Private members can only be accessed by member functions of the class, while public
members can be accessed by any function outside the class
7. What is data encapsulation in C++?
a) The ability of a class to inherit from multiple classes b) The ability of a class to hide its
implementation details from the outside world c) The ability of a class to have multiple constructors d)
The ability of a class to have multiple data members with the same name
Answer: b) The ability of a class to hide its implementation details from the outside world
8. What is the difference between a static and a non-static member of a class in C++?
a) Static members can only be accessed by member functions of the class, while non-static members
can be accessed by any function outside the class b) Static members are shared by all objects of the
class, while non-static members have separate copies in each object of the class c) Static members are
declared before non-static members in the class definition d) There is no difference between a static
and a non-static member of a class in C++
Answer: b) Static members are shared by all objects of the class, while non-static members have
separate copies in each object of the class
1. What is an inline function in C++?
a) A function that is called by reference b) A function that is called by value c) A function that is
expanded in place at the point of call d) A function that is expanded in a separate file
Answer: c) A function that is expanded in place at the point of call
2. How is an inline function defined in C++?
a) By using the inline keyword before the function declaration b) By using the static keyword before
the function declaration c) By using the virtual keyword before the function declaration d) By using
the const keyword before the function declaration
Answer: a) By using the inline keyword before the function declaration
3. What is the advantage of using an inline function in C++?
a) It reduces the size of the compiled code b) It improves the performance of the code by avoiding
function call overhead c) It allows the function to be called from multiple threads simultaneously d) It
allows the function to be redefined in a derived class
Answer: b) It improves the performance of the code by avoiding function call overhead
4. Can a recursive function be defined as inline in C++?
a) Yes, as long as the recursion depth is limited b) No, inline functions cannot be recursive c) Yes, but
only if the recursion depth is limited to a few levels d) Yes, but only if the function is tail-recursive
Answer: b) No, inline functions cannot be recursive
5. Is it always a good idea to use inline functions in C++?
a) Yes, inline functions should be used whenever possible to improve performance b) No, inline
functions can increase the size of the compiled code and reduce performance in some cases c) It
depends on the size and complexity of the function d) It depends on the type of function (e.g.,
recursive vs. non-recursive)
Answer: c) It depends on the size and complexity of the function. It is not always a good idea to use
inline functions because they can increase the size of the compiled code and reduce performance in
some cases. Inline functions should be used only for small, simple functions that are called frequently.
1. What is a virtual function in C++?
a) A function that can be called on an object of any class b) A function that can be called without
creating an object of a class c) A function that can be overridden in a derived class d) A function that
cannot be overridden in a derived class
Answer: c) A function that can be overridden in a derived class
2. How is a virtual function declared in C++?
a) By using the virtual keyword before the function declaration in the base class b) By using the static
keyword before the function declaration in the base class c) By using the const keyword before the
function declaration in the base class d) By using the friend keyword before the function declaration in
the base class
Answer: a) By using the virtual keyword before the function declaration in the base class
3. What is the purpose of a virtual destructor in C++?
a) To free the memory allocated for an object when it is deleted b) To prevent a derived class from
inheriting from the base class c) To allow a derived class to override the destructor of the base class d)
To allow a derived class to call the destructor of the base class
Answer: d) To allow a derived class to call the destructor of the base class
4. Can a constructor be virtual in C++?
a) Yes, a constructor can be virtual b) No, a constructor cannot be virtual c) It depends on the access
level of the constructor d) It depends on the type of class hierarchy
Answer: b) No, a constructor cannot be virtual
5. What is the difference between a virtual function and a pure virtual function in C++?
a) A virtual function has an implementation in the base class, while a pure virtual function has no
implementation in the base class b) A virtual function can be called on an object of any class, while a
pure virtual function can only be called on an object of a derived class c) A virtual function can be
overridden in a derived class, while a pure virtual function must be overridden in a derived class d) A
virtual function can be declared as const, while a pure virtual function cannot be declared as const
Answer: a) A virtual function has an implementation in the base class, while a pure virtual function has
no implementation in the base class. A pure virtual function is declared with the "= 0" syntax in the
base class and must be overridden in any derived class.
1. What is a friend function in C++?
a) A function that can access private and protected members of a class b) A function that can only
access public members of a class c) A function that can be called without creating an object of a class
d) A function that can only be defined in the same file as the class declaration
Answer: a) A function that can access private and protected members of a class
2. How is a friend function declared in C++?
a) By using the friend keyword before the function declaration in the class definition b) By using the
static keyword before the function declaration in the class definition c) By using the const keyword
before the function declaration in the class definition d) By using the virtual keyword before the
function declaration in the class definition
Answer: a) By using the friend keyword before the function declaration in the class definition
3. Can a friend function be a member of a class in C++?
a) Yes, a friend function can be a member of a class b) No, a friend function cannot be a member of a
class c) It depends on the access level of the member function d) It depends on the type of class
hierarchy
Answer: b) No, a friend function cannot be a member of a class
4. What is the advantage of using a friend function in C++?
a) It allows external functions to access private and protected members of a class b) It reduces the size
of the compiled code c) It improves the performance of the code by avoiding function call overhead d)
It allows the function to be called from multiple threads simultaneously
Answer: a) It allows external functions to access private and protected members of a class
5. Is it always a good idea to use friend functions in C++?
a) Yes, friend functions should be used whenever external functions need to access private and
protected members of a class b) No, friend functions can break encapsulation and make the code less
maintainable c) It depends on the size and complexity of the class d) It depends on the type of function
that needs to access private and protected members
Answer: b) No, friend functions can break encapsulation and make the code less maintainable. Friend
functions should be used only when necessary and with caution.
1. What is an exception in C++?
a) An error that occurs during program execution b) A condition that must be met before program
execution c) A type of data structure used to store information d) A keyword used to control program
flow
Answer: a) An error that occurs during program execution
2. How are exceptions handled in C++?
a) By using the try, catch, and throw keywords b) By using the if, else, and switch keywords c) By
using the for, while, and do-while loops d) By using the break, continue, and return keywords
Answer: a) By using the try, catch, and throw keywords
3. What is the purpose of the try block in C++?
a) To test a condition before executing a block of code b) To catch an exception and handle it c) To
throw an exception if a condition is met d) To define a function that can be called by other parts of the
program
Answer: b) To catch an exception and handle it
4. What is the purpose of the catch block in C++?
a) To test a condition before executing a block of code b) To catch an exception and handle it c) To
throw an exception if a condition is met d) To define a function that can be called by other parts of the
program
Answer: b) To catch an exception and handle it
5. Can exceptions be thrown from a destructor in C++?
a) Yes, exceptions can be thrown from a destructor b) No, exceptions cannot be thrown from a
destructor c) It depends on the type of exception d) It depends on the implementation of the destructor
Answer: a) Yes, exceptions can be thrown from a destructor, but it is generally not recommended as it
can cause unexpected behavior.
MCQs on Constructors in C++

1. What is a constructor in C++?


a) A function that creates an object of a class b) A function that destroys an object of a class c) A
function that updates an object of a class d) A function that is used to overload operators
Answer: a) A function that creates an object of a class
2. How is a constructor identified in C++?
a) By its name, which is always the same as the class name b) By its return type, which is always void
c) By the keyword constructor before the function declaration d) By the keyword this before the
function declaration
Answer: a) By its name, which is always the same as the class name
3. Can a constructor have parameters in C++?
a) Yes, a constructor can have parameters in C++ b) No, a constructor cannot have parameters in C++
c) It depends on the type of constructor d) It depends on the type of class hierarchy
Answer: a) Yes, a constructor can have parameters in C++
4. What is the purpose of a default constructor in C++?
a) To initialize all data members of a class to a default value b) To create an object of a class without
any arguments c) To copy an object of a class to another object d) To destroy an object of a class
Answer: b) To create an object of a class without any arguments
5. Can a class have more than one constructor in C++?
a) Yes, a class can have multiple constructors in C++ b) No, a class can only have one constructor in
C++ c) It depends on the type of constructor d) It depends on the type of class hierarchy
Answer: a) Yes, a class can have multiple constructors in C++

1. What is a destructor in C++?


a) A function that creates an object of a class b) A function that updates an object of a class c) A
function that destroys an object of a class d) A function that is used to overload operators
Answer: c) A function that destroys an object of a class
2. How is a destructor identified in C++?
a) By its name, which is always the same as the class name b) By its return type, which is always void
c) By the keyword destructor before the function declaration d) By the keyword this before the
function declaration
Answer: c) By the keyword destructor before the function declaration
3. When is a destructor called in C++?
a) When an object is created b) When an object is updated c) When an object is destroyed d) When an
object is copied to another object
Answer: c) When an object is destroyed
4. Can a destructor have parameters in C++?
a) Yes, a destructor can have parameters in C++ b) No, a destructor cannot have parameters in C++ c)
It depends on the type of destructor d) It depends on the type of class hierarchy
Answer: b) No, a destructor cannot have parameters in C++
5. Can a class have more than one destructor in C++?
a) Yes, a class can have multiple destructors in C++ b) No, a class can only have one destructor in C++
c) It depends on the type of destructor d) It depends on the type of class hierarchy
Answer: b) No, a class can only have one destructor in C++
1. What are templates in C++?
a) Functions that take multiple arguments b) Functions that can be used to create generic types and
functions c) Functions that can be used to override the default behavior of an operator d) Functions
that can be used to handle exceptions
Answer: b) Functions that can be used to create generic types and functions
2. What is the syntax for declaring a template function in C++?
a) template <typename T> void functionName(T arg); b) template <typename T> functionName(T
arg); c) template <typename T> void functionName(T); d) template <typename T> void
functionName();
Answer: a) template <typename T> void functionName(T arg);
3. What is the purpose of a template class in C++?
a) To create a generic class that can be used with multiple data types b) To create a class that can be
used to handle exceptions c) To create a class that can be used to override the default behavior of an
operator d) To create a class that can be used to handle dynamic memory allocation
Answer: a) To create a generic class that can be used with multiple data types
4. What is the difference between a function template and a class template in C++?
a) A function template is used to create generic functions, while a class template is used to create
generic classes b) A function template is used to handle exceptions, while a class template is used to
handle memory allocation c) A function template is used to override the default behavior of an
operator, while a class template is used to create generic classes d) There is no difference between a
function template and a class template in C++
Answer: a) A function template is used to create generic functions, while a class template is used to
create generic classes
5. Can a template function have default arguments in C++?
a) Yes, a template function can have default arguments in C++ b) No, a template function cannot have
default arguments in C++ c) It depends on the type of data being used with the template function d) It
depends on the type of class hierarchy being used with the template function
Answer: a) Yes, a template function can have default arguments in C++
MCQ's on STL in C++

1. What does STL stand for in C++?


a) Simple Template Library b) Standard Template Library c) Structured Type Library d) Systematic
Template Library
Answer: b) Standard Template Library
2. What are the three components of the STL in C++?
a) Containers, algorithms, and iterators b) Containers, classes, and functions c) Classes, objects, and
functions d) Pointers, classes, and templates
Answer: a) Containers, algorithms, and iterators
3. What is the purpose of containers in the STL in C++?
a) To store data in a specific format b) To perform operations on data stored in a container c) To iterate
through data stored in a container d) To sort data stored in a container
Answer: a) To store data in a specific format
4. What is the purpose of algorithms in the STL in C++?
a) To store data in a specific format b) To perform operations on data stored in a container c) To iterate
through data stored in a container d) To sort data stored in a container
Answer: b) To perform operations on data stored in a container
5. What is the purpose of iterators in the STL in C++?
a) To store data in a specific format b) To perform operations on data stored in a container c) To iterate
through data stored in a container d) To sort data stored in a container
Answer: c) To iterate through data stored in a container
6. What are the types of containers available in the STL in C++?
a) Sequence containers, associative containers, and container adaptors b) Vector containers, array
containers, and list containers c) Stack containers, queue containers, and priority queue containers d)
Dynamic containers, static containers, and fixed-size containers
Answer: a) Sequence containers, associative containers, and container adaptors
7. What is the difference between a vector and a list in the STL in C++?
a) A vector is a dynamically-sized array, while a list is a doubly-linked list b) A vector is a singly-
linked list, while a list is a doubly-linked list c) A vector and a list are the same data structure d) A
vector is a fixed-size array, while a list is a dynamically-sized array
Answer: a) A vector is a dynamically-sized array, while a list is a doubly-linked list
8. What is the purpose of the sort algorithm in the STL in C++?
a) To sort data stored in a container in ascending order b) To sort data stored in a container in
descending order c) To search for a specific value in a container d) To remove duplicates from a
container
Answer: a) To sort data stored in a container in ascending order
MCQ's on file handling in C++

1. Which header file is required to perform file handling in C++?


a) <iostream> b) <fstream> c) <cstring> d) <cstdlib>
Answer: b) <fstream>
2. Which mode is used to open a file for reading in C++?
a) write b) append c) read d) both a and b
Answer: c) read
3. Which mode is used to open a file for writing in C++?
a) write b) append c) read d) both a and b
Answer: a) write
4. Which mode is used to append data to an existing file in C++?
a) write b) append c) read d) both a and b
Answer: b) append
5. What is the difference between text mode and binary mode in file handling in C++?
a) Text mode is used for reading and writing text files, while binary mode is used for reading and
writing binary files. b) Text mode is used for reading and writing binary files, while binary mode is
used for reading and writing text files. c) There is no difference between text mode and binary mode in
file handling in C++. d) Text mode is used for reading and writing files, while binary mode is used for
creating directories.
Answer: a) Text mode is used for reading and writing text files, while binary mode is used for reading
and writing binary files.
6. Which function is used to check whether a file has been successfully opened in C++?
a) fopen() b) open() c) is_open() d) close()
Answer: c) is_open()
7. Which function is used to write data to a file in C++?
a) read() b) write() c) getline() d) seekg()
Answer: b) write()
8. Which function is used to read data from a file in C++?
a) read() b) write() c) getline() d) seekg()
Answer: a) read()

MCQ's on namespaces in C++


1. What is a namespace in C++?
a) A namespace is a data type that can hold a single value of a specified type. b) A namespace is a
mechanism for avoiding naming conflicts in large programs. c) A namespace is a special function that
can be called without an object. d) A namespace is a way to create classes with shared data members.
Answer: b) A namespace is a mechanism for avoiding naming conflicts in large programs.
2. Which keyword is used to declare a namespace in C++?
a) using b) namespace c) typedef d) class
Answer: b) namespace
3. Which of the following statements about nested namespaces in C++ is true?
a) Nested namespaces are not allowed in C++. b) Nested namespaces can only be declared inside
classes. c) Nested namespaces are used to create hierarchical namespace structures. d) Nested
namespaces can only be accessed using the scope resolution operator (::).
Answer: c) Nested namespaces are used to create hierarchical namespace structures.
4. Which of the following is a valid way to access a namespace in C++?
a) Using the keyword "using" b) Declaring an object of the namespace c) Using the scope resolution
operator (::) d) All of the above
Answer: c) Using the scope resolution operator (::)
5. What is the purpose of the "using" directive in C++?
a) To import all the functions and classes from a namespace b) To declare a new namespace c) To
define a new data type d) To create an alias for a namespace or a member of a namespace
Answer: d) To create an alias for a namespace or a member of a namespace
6. Which of the following is an example of a standard namespace in C++?
a) std b) math c) string d) vector
Answer: a) std
7. Which of the following is a disadvantage of using namespaces in C++?
a) They can make code more readable and organized. b) They can reduce the likelihood of naming
conflicts. c) They can increase the size of the compiled program. d) They can slow down the execution
of the program.
Answer: c) They can increase the size of the compiled program.
1. What is dynamic memory allocation in C++?
a) A process of allocating memory during program execution b) A process of allocating memory during
program compilation c) A process of freeing memory during program execution d) A process of freeing
memory during program compilation
Answer: a) A process of allocating memory during program execution
2. Which of the following operators is used for dynamic memory allocation in C++?
a) new b) malloc c) both a and b d) none of the above
Answer: a) new
3. What is the purpose of the "delete" operator in C++?
a) To free up memory allocated by the "new" operator b) To free up memory allocated by the "malloc"
function c) To free up memory allocated by the "calloc" function d) To free up memory allocated by
the "realloc" function
Answer: a) To free up memory allocated by the "new" operator
4. What happens if we forget to free dynamically allocated memory using the "delete" operator?
a) The program crashes b) The memory is automatically freed when the program terminates c) The
memory is leaked and cannot be used again d) The memory is moved to a garbage collector
Answer: c) The memory is leaked and cannot be used again
5. What is a memory leak in C++?
a) A situation where too much memory is allocated b) A situation where not enough memory is
allocated c) A situation where dynamically allocated memory is not freed d) A situation where
dynamically allocated memory is freed too early
Answer: c) A situation where dynamically allocated memory is not freed
6. What is the difference between static and dynamic memory allocation in C++?
a) Static memory allocation is done during program execution, while dynamic memory allocation is
done during program compilation. b) Static memory allocation is done using the "new" operator, while
dynamic memory allocation is done using the "malloc" function. c) Static memory allocation is done
on the stack, while dynamic memory allocation is done on the heap. d) Static memory allocation is
used for global variables, while dynamic memory allocation is used for local variables.
Answer: c) Static memory allocation is done on the stack, while dynamic memory allocation is done
on the heap.
7. What is the maximum amount of memory that can be dynamically allocated using the "new"
operator in C++?
a) It depends on the size of the data type being allocated. b) It depends on the available memory in the
system. c) It is limited by the maximum value of the "size_t" data type. d) There is no maximum limit.
Answer: b) It depends on the available memory in the system.
MCQ's on String Handling functions in C++

1. Which of the following header files is used for string handling in C++?
a) <iostream> b) <cstring> c) <string> d) <cstdio>
Answer: c) <string>
2. Which of the following functions is used to get the length of a string in C++?
a) strrev() b) strlen() c) strupr() d) strlwr()
Answer: b) strlen()
3. Which of the following functions is used to concatenate two strings in C++?
a) strcat() b) strncat() c) strrev() d) strstr()
Answer: a) strcat()
4. Which of the following functions is used to compare two strings in C++?
a) strcmp() b) strncmp() c) strstr() d) strchr()
Answer: a) strcmp()
5. Which of the following functions is used to convert a string to an integer in C++?
a) stoi() b) atoi() c) strtoi() d) itoa()
Answer: b) atoi()
6. Which of the following functions is used to convert an integer to a string in C++?
a) to_string() b) itoa() c) sprintf() d) strrev()
Answer: a) to_string()
7. Which of the following functions is used to extract a substring from a string in C++?
a) substr() b) strrev() c) strupr() d) strlwr()
Answer: a) substr()
Overloading in C++

1. Which of the following statements about function overloading in C++ is true?


a) Two functions can have the same name but different return types b) Two functions can have the
same name, same return type, but different parameter types c) Two functions can have the same name,
same parameter types, but different return types d) Two functions cannot have the same name in C++
Answer: b) Two functions can have the same name, same return type, but different parameter types
2. Which of the following is an example of function overloading in C++?
a) int add(int a, int b) b) float add(float a, float b) c) char add(char a, char b) d) All of the above
Answer: d) All of the above
3. Which of the following is not considered when determining which overloaded function to call
in C++?
a) Number of parameters b) Order of parameters c) Data types of parameters d) Return type of the
function
Answer: d) Return type of the function
4. Which of the following is not allowed when overloading functions in C++?
a) Overloading a function with a different number of parameters b) Overloading a function with a
different order of parameters c) Overloading a function with the same name and parameter types, but
different return types d) Overloading a function with a different access specifier
Answer: d) Overloading a function with a different access specifier
5. Which of the following is true about default arguments and function overloading in C++?
a) Default arguments cannot be used in overloaded functions b) Default arguments can be used in
overloaded functions, but the default argument must be the same for all versions of the function c)
Default arguments can be used in overloaded functions, and different versions of the function can have
different default arguments d) None of the above
Answer: c) Default arguments can be used in overloaded functions, and different versions of the
function can have different default arguments
MCQ's on pointers in C++

1. What is a pointer? a) A variable that stores the address of another variable. b) A variable that
stores the value of another variable. c) A variable that stores the type of another variable. d) A
variable that stores the size of another variable.
Answer: a
2. What is the output of the following code?
cppCopy code
int x = 10;
int* p = &x;
cout << p;

a) 10 b) The address of x in memory c) An error message d) None of the above


Answer: b
3. What is the output of the following code?
cppCopy code
int arr[3] = {1, 2, 3};
int* p = &arr[0];
cout << *p;

a) 1 b) 2 c) 3 d) None of the above


Answer: a
4. What is the difference between the following two lines of code?
cppCopy code
int* p;
int *p;

a) There is no difference. b) The first line declares a pointer to an int, while the second line declares a
pointer to a pointer to an int. c) The first line declares a pointer to an int, while the second line declares
a pointer to a char. d) The first line declares a pointer to an int, while the second line declares a
reference to an int.
Answer: a
5. What is the correct way to allocate memory for an array of integers using new?
a) int arr = new int[10]; b) int* arr = new int[10]; c) int arr[10] = new int; d) None of the above
Answer: b
6. What is the output of the following code?
cppCopy code
int x = 10;
int* p = &x;
*p = 20;
cout << x;

a) 10 b) 20 c) An error message d) None of the above


Answer: b
7. What is the correct way to free the memory allocated using new?
a) delete arr; b) delete[] arr; c) free(arr); d) None of the above
Answer: b
8. What is a null pointer?
a) A pointer that points to nothing. b) A pointer that points to a valid memory location. c) A pointer that
points to the end of an array. d) A pointer that points to a function.
Answer: a
9. What is a dangling pointer?
a) A pointer that points to a valid memory location. b) A pointer that points to nothing. c) A pointer that
points to a deleted memory location. d) A pointer that points to a function.
Answer: c
10.What is the output of the following code?
cppCopy code
int* p = nullptr;
cout << p;

a) 0 b) An error message c) The address of a null pointer d) None of the above


Answer: a
MCQ's on inheritance in C++
1. What is inheritance? a) The process of creating a new class from an existing class. b) The
process of creating an object from a class. c) The process of deleting a class from memory. d)
The process of changing the access level of a class member.
Answer: a
2. What are the access specifiers used in inheritance? a) public, private, protected b) only public
c) only private d) only protected
Answer: a
3. Which access specifier allows derived classes to access a base class member? a) public b)
private c) protected d) None of the above
Answer: c
4. What is the syntax for inheriting a class in C++? a) class derivedClass : baseClass b) class
derivedClass -> baseClass c) class baseClass : derivedClass d) None of the above
Answer: a
5. What is a derived class? a) A class that is inherited from another class. b) A class that has no
base class. c) A class that is not related to any other class. d) None of the above
Answer: a
6. What is a base class? a) The class that is inherited from. b) The class that inherits from another
class. c) A class that has no derived class. d) None of the above
Answer: a
7. What is multiple inheritance? a) The process of inheriting multiple classes into a single derived
class. b) The process of inheriting a single class into multiple derived classes. c) The process of
inheriting a class from multiple base classes. d) None of the above
Answer: c
8. What is the order of constructor and destructor invocation in inheritance? a) Derived class
constructor, base class constructor, base class destructor, derived class destructor. b) Base class
constructor, derived class constructor, derived class destructor, base class destructor. c) Base
class constructor, derived class constructor, base class destructor, derived class destructor. d)
Derived class constructor, base class destructor, base class constructor, derived class destructor.
Answer: c
9. Can a derived class access the private members of a base class? a) Yes, always. b) Yes, but only
through a friend function. c) Yes, but only through a public member function. d) No, never.
Answer: b
10.What is the purpose of virtual functions? a) To prevent inheritance. b) To override base class
functions in derived classes. c) To hide base class functions in derived classes. d) None of the
above.
Answer: b
MCQ's on inheritance in C++

1. What is inheritance? a) The process of creating a new class from an existing class. b) The
process of creating an object from a class. c) The process of deleting a class from memory. d)
The process of changing the access level of a class member.
Answer: a
2. What are the access specifiers used in inheritance? a) public, private, protected b) only public
c) only private d) only protected
Answer: a
3. Which access specifier allows derived classes to access a base class member? a) public b)
private c) protected d) None of the above
Answer: c
4. What is the syntax for inheriting a class in C++? a) class derivedClass : baseClass b) class
derivedClass -> baseClass c) class baseClass : derivedClass d) None of the above
Answer: a
5. What is a derived class? a) A class that is inherited from another class. b) A class that has no
base class. c) A class that is not related to any other class. d) None of the above
Answer: a
6. What is a base class? a) The class that is inherited from. b) The class that inherits from another
class. c) A class that has no derived class. d) None of the above
Answer: a
7. What is multiple inheritance? a) The process of inheriting multiple classes into a single derived
class. b) The process of inheriting a single class into multiple derived classes. c) The process of
inheriting a class from multiple base classes. d) None of the above
Answer: c
8. What is the order of constructor and destructor invocation in inheritance? a) Derived class
constructor, base class constructor, base class destructor, derived class destructor. b) Base class
constructor, derived class constructor, derived class destructor, base class destructor. c) Base
class constructor, derived class constructor, base class destructor, derived class destructor. d)
Derived class constructor, base class destructor, base class constructor, derived class destructor.
Answer: c
9. Can a derived class access the private members of a base class? a) Yes, always. b) Yes, but only
through a friend function. c) Yes, but only through a public member function. d) No, never.
Answer: b
10.What is the purpose of virtual functions? a) To prevent inheritance. b) To override base class
functions in derived classes. c) To hide base class functions in derived classes. d) None of the
above.
Answer: b
MCQ's on Access specifiers in C++

1. What are access specifiers in C++? a) Keywords used to specify the visibility of class
members. b) Keywords used to specify the size of class members. c) Keywords used to specify
the type of class members. d) Keywords used to specify the scope of class members.
Answer: a
2. What are the three access specifiers in C++? a) public, private, protected b) read, write, execute
c) in, out, inout d) none of the above
Answer: a
3. Which access specifier allows a class member to be accessed from anywhere in the program? a)
public b) private c) protected d) None of the above
Answer: a
4. Which access specifier allows a class member to be accessed only from within the class? a)
public b) private c) protected d) None of the above
Answer: b
5. Which access specifier allows a class member to be accessed from within the class and its
derived classes? a) public b) private c) protected d) None of the above
Answer: c
6. What is the default access specifier in C++? a) public b) private c) protected d) None of the
above
Answer: b
7. What is the purpose of making class members private? a) To prevent them from being accessed
outside the class. b) To allow them to be accessed from anywhere in the program. c) To allow
them to be accessed only from within the class and its derived classes. d) None of the above
Answer: a
8. What is the purpose of making class members public? a) To prevent them from being accessed
outside the class. b) To allow them to be accessed from anywhere in the program. c) To allow
them to be accessed only from within the class and its derived classes. d) None of the above
Answer: b
9. What is the purpose of making class members protected? a) To prevent them from being
accessed outside the class. b) To allow them to be accessed from anywhere in the program. c)
To allow them to be accessed only from within the class and its derived classes. d) None of the
above
Answer: c
10.What is the syntax for declaring a private class member? a) private: int x; b) public: int x; c)
protected: int x; d) None of the above
Answer: a

You might also like