C++ MCQs
C++ MCQs
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. 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) 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;
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