Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 8

3 / 3

Certainly! Here are 60 multiple-choice questions on various topics related to OOP


in C++. Each question is followed by the answer:

Familiarity with Functions:

Which of the following is not a valid function declaration in C++?


a) int add(int a, int b);
b) void multiply(int a, int b);
c) float divide(float a, float b);
d) double subtract(double a, b);
Answer: d) double subtract(double a, b);

What is the purpose of function overloading in C++?


a) To define multiple functions with the same name but different parameters
b) To restrict access to certain functions
c) To declare static member functions
d) To define functions inside a class
Answer: a) To define multiple functions with the same name but different parameters

What is the return type of the main() function in C++?


a) void
b) int
c) float
d) char
Answer: b) int

What is the difference between pass-by-value and pass-by-reference in C++?


a) Pass-by-value creates a copy of the argument, while pass-by-reference uses the
original argument
b) Pass-by-value is more efficient than pass-by-reference
c) Pass-by-reference is only used for built-in data types, while pass-by-value is
used for user-defined types
d) There is no difference; the terms are used interchangeably
Answer: a) Pass-by-value creates a copy of the argument, while pass-by-reference
uses the original argument

What is the purpose of the 'const' keyword in a function declaration?


a) To indicate that the function does not return a value
b) To prevent modifications to function parameters inside the function
c) To specify the data type of the function
d) To declare a function as a member of a class
Answer: b) To prevent modifications to function parameters inside the function

What is a recursive function in C++?


a) A function that calls itself
b) A function that takes a variable number of arguments
c) A function that is defined inside a class
d) A function that returns a value of type 'void'
Answer: a) A function that calls itself

Which of the following is true about function prototypes in C++?


a) They provide the definition of a function
b) They declare the existence and signature of a function
c) They are not required for functions to work
d) They are used to create objects from classes
Answer: b) They declare the existence and signature of a function
What is the purpose of function templates in C++?
a) To create multiple functions with the same name but different data types
b) To define functions that cannot be overridden
c) To define functions with default arguments
d) To define functions inside a class
Answer: a) To create multiple functions with the same name but different data types

How can you pass an array to a function in C++?


a) By using the array name as an argument
b) By using a pointer to the array as an argument
c) By using the size of the array as an argument
d) By using the array index as an argument
Answer: b) By using a pointer to the array as an argument

What is the purpose of function overloading resolution in C++?


a) To determine which overloaded function should be called based on the arguments
provided
b) To determine the return type of an overloaded function
c) To determine the order in which overloaded functions are defined
d) To determine the size of the memory allocated for an overloaded function
Answer: a) To determine which overloaded function should be called based on the
arguments provided

User-Defined Types (Classes):

What is a class in C++?


a) A built-in data type
b) A collection of functions
c) A user-defined data type that encapsulates data and functions
d) A reserved keyword in C++
Answer: c) A user-defined data type that encapsulates data and functions

What is the difference between a class and an object in C++?


a) A class is a blueprint for creating objects
b) An object is an instance of a class
c) A class defines the properties and behaviors of objects
d) All of the above
Answer: d) All of the above

What is encapsulation in object-oriented programming?


a) The process of creating objects from classes
b) The process of hiding implementation details and providing a public interface
c) The process of defining multiple functions with the same name but different
parameters
d) The process of using polymorphism to create multiple forms of a function
Answer: b) The process of hiding implementation details and providing a public
interface

What is the access specifier used to define members of a class that are accessible
only within the class?
a) private
b) protected
c) public
d) internal
Answer: a) private

What is the default access specifier for members of a class in C++?


a) private
b) protected
c) public
d) internal
Answer: a) private

What is the purpose of a constructor in C++?


a) To allocate memory for an object
b) To initialize the data members of an object
c) To define the behavior of an object
d) To define the interface of a class
Answer: b) To initialize the data members of an object

What is a destructor in C++?


a) A member function that is called when an object is created
b) A member function that is called when an object is destroyed
c) A type of loop used for iteration
d) A reserved keyword in C++
Answer: b) A member function that is called when an object is destroyed

What is the keyword used to create an instance of a class in C++?


a) instance
b) object
c) create
d) instantiate
Answer: b) object

Which of the following statements is true about static member functions in C++?
a) They can access only static data members of a class
b) They can be called using an object of the class
c) They cannot be declared const
d) They can access private data members of a class
Answer: a) They can access only static data members of a class

What is the difference between composition and inheritance in C++?


a) Composition involves creating objects of one class within another class, while
inheritance involves creating a new class based on an existing class
b) Composition is a form of static polymorphism, while inheritance is a form of
dynamic polymorphism
c) Composition allows for code reuse, while inheritance allows for defining new
classes based on existing classes
d) Composition is a one-to-one relationship, while inheritance is a one-to-many
relationship
Answer: a) Composition involves creating objects of one class within another class,
while inheritance involves creating a new class based on an existing class

Arrays:

What is an array in C++?


a) A collection of functions
b) A user-defined data type
c) A data structure that stores a fixed-size sequence of elements of the same type
d) A reserved keyword in C++
Answer: c) A data structure that stores a fixed-size sequence of elements of the
same type

How do you declare an array in C++?


a) int arr[];
b) int[] arr;
c) int arr[];
d) int arr[10];
Answer: d) int arr[10];

What is the index of the first element in an array in C++?


a) 0
b) 1
c) -1
d) The index depends on the size of the array
Answer: a) 0

How do you access elements of an array in C++?


a) By using the dot operator
b) By using the arrow operator
c) By using the index or subscript operator
d) By using the asterisk operator
Answer: c) By using the index or subscript operator

What is the size of an array in C++?


a) The number of elements in the array
b) The amount of memory allocated for the array
c) The index of the last element in the array
d) The index of the first element in the array
Answer: b) The amount of memory allocated for the array

How do you initialize an array in C++?


a) By assigning values to each element of the array individually
b) By using a loop to assign values to each element of the array
c) By using curly braces to specify the values for the elements of the array
d) All of the above
Answer: d) All of the above

What happens if you try to access an element outside the bounds of an array in C++?
a) The program crashes
b) The program continues to execute without any error
c) The program displays a warning message
d) The behavior is undefined
Answer: d) The behavior is undefined

What is the difference between a one-dimensional array and a multidimensional array


in C++?
a) A one-dimensional array stores a sequence of elements, while a multidimensional
array stores elements in multiple dimensions
b) A one-dimensional array can store elements of different types, while a
multidimensional array can store elements of the same type only
c) A one-dimensional array can be resized dynamically, while a multidimensional
array has a fixed size
d) There is no difference; the terms are used interchangeably
Answer: a) A one-dimensional array stores a sequence of elements, while a
multidimensional array stores elements in multiple dimensions

How do you pass an array to a function in C++?


a) By using the array name as an argument
b) By using a pointer to the array as an argument
c) By using the size of the array as an argument
d) By using the array index as an argument
Answer: b) By using a pointer to the array as an argument

What is the maximum number of dimensions an array can have in C++?


a) 1
b) 2
c) 3
d) There is no maximum limit
Answer: d) There is no maximum limit

References, Pointers, and Memory Management:

What is the difference between a reference and a pointer in C++?


a) References cannot be null, while pointers can be null
b) References cannot be reassigned, while pointers can be reassigned
c) References require dereferencing to access the value, while pointers do not
d) All of the above
Answer: d) All of the above

How do you declare a reference in C++?


a) int& ref;
b) int ref;
c) int* ref;
d) int&* ref;
Answer: a) int& ref;

What is the purpose of the 'this' pointer in C++?


a) It points to the current object within a member function
b) It points to the base class object within a derived class
c) It points to the first element of an array
d) It points to the last element of an array
Answer: a) It points to the current object within a member function

What is dynamic memory allocation in C++?


a) Allocating memory for global variables
b) Allocating memory for local variables
c) Allocating memory for objects
d) Allocating memory at compile-time
Answer: c) Allocating memory for objects

How do you allocate dynamic memory in C++?


a) By using the 'new' keyword
b) By using the 'malloc' function
c) By using the 'allocate' keyword
d) By using the 'create' keyword
Answer: a) By using the 'new' keyword

What is the difference between 'new' and 'malloc' in C++?


a) 'new' is used for dynamic memory allocation and object creation, while 'malloc'
is used for dynamic memory allocation only
b) 'new' requires the use of 'delete' for deallocation, while 'malloc' requires the
use of 'free' for deallocation
c) 'new' can call the constructor of an object, while 'malloc' cannot
d) All of the above
Answer: d) All of the above

What is memory deallocation in C++?


a) Releasing memory occupied by variables
b) Releasing memory occupied by objects
c) Releasing memory allocated dynamically
d) All of the above
Answer: d) All of the above

How do you deallocate dynamic memory in C++?


a) By using the 'delete' keyword
b) By using the 'free' function
c) By using the 'deallocate' keyword
d) By using the 'destroy' keyword
Answer: a) By using the 'delete' keyword

What is a memory leak in C++?


a) A situation where memory is allocated but never deallocated
b) A situation where memory is deallocated but never allocated
c) A situation where memory is accessed outside the bounds of an array
d) A situation where memory is deallocated multiple times
Answer: a) A situation where memory is allocated but never deallocated

What is the purpose of the 'nullptr' keyword in C++?


a) It is used to assign a null value to a pointer
b) It is used to assign a default value to a reference
c) It is used to check if a pointer is null
d) It is used to check if a reference is null
Answer: a) It is used to assign a null value to a pointer

Relationships among Classes:

What is inheritance in object-oriented programming?


a) The process of creating objects from classes
b) The process of defining multiple functions with the same name but different
parameters
c) The process of creating a new class based on an existing class
d) The process of hiding implementation details and providing a public interface
Answer: c) The process of creating a new class based on an existing class

What is the base class in inheritance?


a) The class that is derived from another class
b) The class that is used to create objects
c) The class that is being inherited from
d) The class that contains virtual functions
Answer: c) The class that is being inherited from

What is the derived class in inheritance?


a) The class that is derived from another class
b) The class that is used to create objects
c) The class that is being inherited from
d) The class that contains virtual functions
Answer: a) The class that is derived from another class

What is single inheritance in C++?


a) Inheriting from multiple base classes
b) Inheriting from a single base class
c) Inheriting from a derived class
d) Inheriting from an abstract class
Answer: b) Inheriting from a single base class

What is multiple inheritance in C++?


a) Inheriting from multiple base classes
b) Inheriting from a single base class
c) Inheriting from a derived class
d) Inheriting from an abstract class
Answer: a) Inheriting from multiple base classes

What is a virtual function in C++?


a) A function that is defined inside a class
b) A function that can be overridden in derived classes
c) A function that is called automatically when an object is created
d) A function that is declared as a member of a class
Answer: b) A function that can be overridden in derived classes

What is the purpose of the 'virtual' keyword in C++?


a) To indicate that a function is a member of a class
b) To indicate that a function should be called automatically when an object is
created
c) To indicate that a function can be overridden in derived classes
d) To indicate that a function is pure virtual
Answer: c) To indicate that a function can be overridden in derived classes

What is an abstract class in C++?


a) A class that cannot be instantiated
b) A class that is derived from another class
c) A class that contains only virtual functions
d) A class that cannot have member variables
Answer: a) A class that cannot be instantiated

What is a pure virtual function in C++?


a) A function that does not have a body
b) A function that is declared as a member of a class but not defined
c) A function that must be overridden in derived classes
d) A function that is called automatically when an object is created
Answer: c) A function that must be overridden in derived classes

What is the purpose of an interface class in C++?


a) To define the implementation details of a class
b) To provide a common interface for multiple classes
c) To restrict access to certain functions
d) To define a single function within a class
Answer: b) To provide a common interface for multiple classes

Polymorphism and Other Issues:

What is polymorphism in C++?


a) The process of creating objects from classes
b) The process of defining multiple functions with the same name but different
parameters
c) The process of creating a new class based on an existing class
d) The process of using a single interface to represent different types or forms
Answer: d) The process of using a single interface to represent different types or
forms

What are the two types of polymorphism in C++?


a) Static polymorphism and dynamic polymorphism
b) Single polymorphism and multiple polymorphism
c) Compile-time polymorphism and runtime polymorphism
d) Overloading polymorphism and overriding polymorphism
Answer: c) Compile-time polymorphism and runtime polymorphism

What is function overloading in C++?


a) Defining multiple functions with the same name but different parameters
b) Defining multiple functions with the same name and the same parameters
c) Defining multiple functions with the same name but different return types
d) Defining multiple functions with the same implementation
Answer: a) Defining multiple functions with the same name but different parameters
What is function overriding in C++?
a) Defining multiple functions with the same name but different parameters
b) Defining multiple functions with the same name and the same parameters
c) Redefining a base class function in a derived class with the same name and
parameters
d) Redefining a derived class function in a base class with the same name and
parameters
Answer: c) Redefining a base class function in a derived class with the same name
and parameters

What is the virtual keyword used for in function overriding?


a) To indicate that a function can be overridden in derived classes
b) To indicate that a function should be called automatically when an object is
created
c) To indicate that a function is pure virtual
d) To indicate that a function is static
Answer: a) To indicate that a function can be overridden in derived classes

What is the difference between compile-time polymorphism and runtime polymorphism


in C++?
a) Compile-time polymorphism is resolved at compile-time, while runtime
polymorphism is resolved at runtime
b) Compile-time polymorphism involves function overloading, while runtime
polymorphism involves function overriding
c) Compile-time polymorphism is faster than runtime polymorphism
d) There is no difference; the terms are used interchangeably
Answer: a) Compile-time polymorphism is resolved at compile-time, while runtime
polymorphism is resolved at runtime

What is a virtual destructor in C++?


a) A destructor that is declared as virtual
b) A destructor that is called automatically when an object is destroyed
c) A destructor that can be overridden in derived classes
d) A destructor that is pure virtual
Answer: a) A destructor that is declared as virtual

What is an abstract base class in C++?


a) A class that cannot be instantiated
b) A class that is derived from another class
c) A class that contains only virtual functions
d) A class that cannot have member variables
Answer: a) A class that cannot be instantiated

What is function hiding in C++?


a) The process of hiding implementation details and providing a public interface
b) The process of defining multiple functions with the same name but different
parameters
c) The process of overriding a base class function in a derived class
d) The process of redefining a derived class function in a base class
Answer: d) The process of redefining a derived class function in a base class

What is a pure virtual function in C++?


a) A function that does not have a body
b) A function that is declared as a member of a class but not defined
c) A function that must be overridden in derived classes
d) A function that is called automatically when an object is created
Answer: c) A function that must be overridden in derived classes

You might also like