You are on page 1of 54

OOPs Using C++(MCQ-150)

C++ OOPs Concepts | Set 2

11. Which of the following class allows to declare only one object of it?

a) Abstract class
b) Virtual class
c) Singleton class
d) Friend class

View Answer
Answer: C
Explanation: Singleton class allows the programmer to declare only one object of it, If
one tries to declare more than one object the program results into error.
12. Which of the following is not a type of Constructor?

a) Friend constructor
b) Copy constructor
c) Default constructor
d) Parameterized constructor

View Answer
Answer: A
Explanation: Friend function is not a constructor whereas others are a type of
constructor used for object initialization.
13. Which of the following is correct?

a) Base class pointer object cannot point to a derived class object


b) Derived class pointer object cannot point to a base class object
c) A derived class cannot have pointer objects
d) A base class cannot have pointer objects

View Answer
Answer: B
Explanation: C++ does not allow a derived class pointer to point a base class pointer
whereas Base class can point to a derived class object. Both base class and derived
class can have pointer objects.
14. Out of the following, which is not a member of the class?
a) Static function
b) Friend function
c) Constant function
d) Virtual function

View Answer
Answer: B
Explanation: Friend function is not a member of the class. They are given the same
access rights as the class member function have but they are not actual members of the
class.
15. What is the other name used for functions inside a class?

a) Member variables
b) Member functions
c) Class functions
d) Class variables

View Answer
Answer: B
Explanation: Functions of a class are also known as member functions of a class.
16. Which of the following cannot be a friend?

a) Function
b) Class
c) Object
d) Operator function

View Answer
Answer: C
Explanation: Objects of any class cannot be made a friend of any other or same class
whereas functions, classes and operator functions can be made a friend.
19. How many types of polymorphism are there in C++?

a) 1
b) 2
c) 3
d) 4

View Answer
Answer: B
Explanation: There are two types of polymorphism in C++ namely run-time and compile-
time polymorphisms.
20. How run-time polymorphisms are implemented in C++?
a) Using Inheritance
b) Using Virtual functions
c) Using Templates
d) Using Inheritance and Virtual functions

View Answer
Answer: D
Explanation: Run-time polymorphism is implemented using Inheritance and virtual in
which object decides which function to call.
21. How compile-time polymorphisms are implemented in C++?

a) Using Inheritance
b) Using Virtual functions
c) Using Templates
d) Using Inheritance and Virtual functions

View Answer
Answer: C
Explanation: Compile-time polymorphism is implemented using templates in which the
types(which can be checked during compile-time) are used decides which function to be
called.
22. Which of the following is an abstract data type?

a) int
b) float
c) class
d) string

View Answer
Answer: C
Explanation: Class is used as an abstract data type as it can be used to give
implementation independent view whereas no other data type can be used to provide
this.
25. Which of the following approach is used by C++?

a) Top-down
b) Bottom-up
c) Left-right
d) Right-left

View Answer
Answer: B
Explanation: C++ is an object-oriented language and OOL uses a bottom-up approach
to solve/view a problem.
26. Which operator is overloaded for a cout object?

a) >>
b) <<
c) <
d) >

View Answer
Answer: B
Explanation: cout in C++ uses << operator to print anything so << operator is
overloaded for a cout object.
27. Which of the following cannot be used with the virtual keyword?

a) Class
b) Member functions
c) Constructors
d) Destructors

View Answer
Answer: C
Explanation: Virtual keyword cannot be used with constructors as constructors are
defined to initialized an object of particular class hence no other class needs constructor
of other class.
28. Which concept is used to implement late binding?

a) Virtual functions
b) Operator functions
c) Constant functions
d) Static functions

View Answer
Answer: A
Explanation: Virtual functions are used to implement the concept of late binding i.e.
binding actual functions to their calls.

29. Which of the following is correct?

a) C++ allows static type checking

b) C++ allows dynamic type checking.

c) C++ allows static member function to be of type const.

d) C++ allows both static and dynamic type checking

View Answer
Answer: D

Explanation: C++ allows both static and dynamic type checking i.e. types are checked by the compiler.

31. Which of the following is a static polymorphism mechanism?

a) Function overloading

b) Operator overloading

c) Templates

d) All of the mentioned

View Answer

Answer: D

Explanation: All the options mentioned above uses static polymorphism mechanism. As the conflicts in
all these types of functions are resolved during compile-time.

33. Which of the following is not a type of inheritance?

a) Multiple

b) Multilevel

c) Distributive

d) Hierarchical

View Answer

Answer: C

Explanation: Distributive is not a type of inheritance whereas others are a type of inheritance having
their own meaning.

33. Which of the following is not a type of inheritance?

a) Multiple

b) Multilevel

c) Distributive

d) Hierarchical

View Answer
Answer: C

Explanation: Distributive is not a type of inheritance whereas others are a type of inheritance having
their own meaning.

39. Which of the following is used to make an abstract class?

a) By using virtual keyword in front of a class declaration

b) By using an abstract keyword in front of a class declaration

c) By declaring a virtual function in a class

d) By declaring a pure virtual function in a class

View Answer

Answer: D

Explanation: Abstract class should have at least one pure virtual function. Therefore to declare an
abstract class one should declare a pure virtual function in a class.

40. Which of the following is correct?

a) A class is an instance of its objects

b) An object is an instance of its class

c) A class is an instance of the data type that the class have

d) An object is an instance of the data type of the class

View Answer

Answer: B

Explanation: An object is an instance of a class i.e. an object represents a class i.e. what class has(data
members) and what it can do(member functions).

2. What are the essential operators in c++?

a) +

b) |

c) <=

d) All of the mentioned

View Answer
Answer: D

Explanation: Essential operators in c++ are +, |, <=.

5. What will be the output of the following C++ code?

#include <iostream>

using namespace std;

int main() {

int a, b;

a = 10;

b = 4;

a = b;

b = 7;

cout << "a: " << a;

cout << "\nb: " << b;

return 0;

a) a:4 b:7

b) a:10 b:4

c) a:4 b:10

d) a:4 b:6

View Answer

Answer: A

Explanation: In this program, we are reassigning the values of a and b because of this we got the output
as a:4 b:7

6. What will be the output of the following C++ code?


#include <iostream>

using namespace std;

int main() {

int a, b, c;

a = 2;

b = 7;

c = (a > b) ? a : b;

cout << "c: " << c;

return 0;

a) 2
b) 7
c) 9
d) 14

View Answer
Answer: B
Explanation: We are using the ternary operator to evaluate this expression. It will return
first option, if first condition is true otherwise it will return second
7. What will be the output of the following C++ code?

#include <iostream>

using namespace std;

int main() {

int a = 0;

int b = 10;
a = 2;

b = 7;

if (a && b) {

cout << "true: " << endl;

else

cout << "false: " << endl;

return 0;

a) true
b) false
c) error
d) 10

View Answer
Answer: B
Explanation: && is called as Logical AND operator, if there is no zero in the operand
means, it will be true otherwise false.
9. What is the name of | operator?

a) sizeof
b) or
c) and
d) modulus

View Answer
Answer: B
Explanation: | operator is used to find the ‘or’ of given values.

C++ OOPs Concepts | Set 1


1. Wrapping data and its related functionality into a single entity is known as _____________
a) Abstraction

b) Encapsulation

c) Polymorphism

d) Modularity

View Answer

Answer: B

Explanation: In OOPs, the property of enclosing data and its related functions into a single entity(in C++
we call them classes) is called encapsulation.

3. What does polymorphism in OOPs mean?

a) Concept of allowing overiding of functions

b) Concept of hiding data

c) Concept of keeping things in differnt modules/files

d) Concept of wrapping things into a single unit

View Answer

Answer: A

Explanation: In OOPs, Polymorphism is the concept of allowing a user to override functions either by
changing the types or number of parameters passed.

4. Which concept allows you to reuse the written code?

a) Encapsulation

b) Abstraction

c) Inheritance

d) Polymorphism

View Answer

Answer: C

Explanation: Inheritance allows you to reuse your already written code by inheriting the properties of
written code into other parts of the code, hence allowing you to reuse the already written code.

8. C++ is ______________
a) procedural programming language

b) object oriented programming language

c) functional programming language

d) both procedural and object oriented programming language

View Answer

Answer: D

Explanation: C++ supports both procedural(step by step instruction) and object oriented
programming(using concept of classes and objects).

Object Oriented Programming using C++ Questions


and Answers – Polymorphism
1. Which among the following best describes polymorphism?
a) It is the ability for a message/data to be processed in more than one form
b) It is the ability for a message/data to be processed in only 1 form
c) It is the ability for many messages/data to be processed in one way
d) It is the ability for undefined message/data to be processed in at least one way
View Answer
Answer: a
Explanation: It is actually the ability for a message / data to be processed in more than one
form. The word polymorphism indicates many-forms. So if a single entity takes more than
one form, it is known as polymorphism.

2. What do you call the languages that support classes but not polymorphism?
a) Class based language
b) Procedure Oriented language
c) Object-based language
d) If classes are supported, polymorphism will always be supported
View Answer
Answer: c
Explanation: The languages which support classes but doesn’t support polymorphism, are
known as object-based languages. Polymorphism is such an important feature, that is a
language doesn’t support this feature, it can’t be called as a OOP language.

3. Which among the following is the language which supports classes but not
polymorphism?
a) SmallTalk
b) Java
c) C++
d) Ada
View Answer
Answer: d
Explanation: Ada is the language which supports the concept of classes but doesn’t support
the polymorphism feature. It is an object-based programming language. Note that it’s not an
OOP language.

MCQs – C++ Polymorphism

Q) Compile time polymorphism in C++ language are

1. Operator overloading
2. Function overloading
3. Function overriding
4. B Only
5. A & B
Answer: 5

Q) C++ abstract class can contain

1. Pure virtual function


2. Non-virtual function
3. Only pure virtual function
4. Both pure virtual and non-virtual function
Answer: 4

Q) False statements about function overloading is

1. Defining multiple functions with same name in a class is called function


overloading
2. Overloaded function must differ in their order and types of arguments.
3. Overloaded functions should be preceded with virtual keyword
4. No statement is false
Answer: 3

Q) Following keyword is used before a function in a base class to be


overridden in derived class in C++

1. override
2. virtual
3. void
4. none
Answer: 2

Q) Which of the following cannot be overloaded in C++?

1. Increment operator
2. Constructor
3. Destructor
4. New and delete operator
Answer: 3

Destructor of a class cannot be overloaded in C++ programming. Increment


operator, constructor and new and delete can be overloaded.

Q) Which is the correct declaration of pure virtual function in C++

1. virtual void func = 0;


2. virtual void func() = 0;
3. virtual void func(){0};
4. void func() = 0;
Answer: 2

virtual void func() = 0; is the correct declaration.

Q) In a class, pure virtual functions in C++ is used

1. To create an interface
2. To make a class abstract
3. To force derived class to implement the pure virtual function
4. All the above
Answer: 4

All are correct. Recommended to read abstract class in c++ with example that
uses pure virtual function.

Q) Which public member of a base class cannot be inherited?

1. Constructor
2. Destructor
3. Both A & B
4. Only B
Answer: 3

In C++ constructor and destructor both cannot be inherited to child class.


Q) Interface class in C++ is created by

1. Using interface keyword before class


2. Using pure virtual function
3. Using pure virtual function and virtual function both
4. Using class keyword
Answer: 2

C++ does not have keyword interface like C# or java. Pure virtual function is used
to create an interface in C++ programs.

Q) Which statements are true about an abstract class

1. Abstract class has at least one pure virtual function.


2. Pointer for an abstract class can be created
3. Object of an abstract class cannot be created.
4. All are correct.
Answer: 4

Q) Run time polymorphism in C++ Program is

1. New and delete operator overloading


2. ++ and – – operator overloading
3. :: operator overloading
4. None
Answer: 4
Q) Run time binding is related to

1. Function overriding
2. Operator overloading
3. A & B
4. None
Answer: 1

Q) Which function cannot be overloaded in C++

1. Constructor
2. Class destructor
3. Both a & b
4. None
Answer: 2

Destructor of a class cannot be overloaded. Read in detail in the interview


question can we overload destructor in C++ program?

Q) Operators can be overloaded in C++ is/are

1. New
2. Delete
3. ++
4. All can be overloaded
Answer: 4

Polymorphism is achieved through ___


(A) Heritance

(B) Poly programming

(C) Encapsulation

(D) Overloading
View Answer
Ans: D

Overloading

The word polymorphism means ____


(A) Many programs

(B) Two forms

(C) Single form

(D) Many shapes


View Answer
Ans: D

Many shapes

Question: 3

The mechanism of giving special meaning to an


operator is called ____
(A) Object

(B) Inheritance

(C) Function overloading

(D) Operator Overloading


View Answer
Ans: D

Operator Overloading

Question: 4

In function overloading do not use the ___ function


name for two unrelated functions.
(A) Same

(B) Different

(C) Similar

(D) Complement
View Answer
Ans: A

Same
1. Wrapping data and its related functionality into a single entity is known as _____________

a) Abstraction

b) Encapsulation

c) Polymorphism

d) Modularity

View Answer

Answer: B

Explanation: In OOPs, the property of enclosing data and its related functions into a single entity(in C++
we call them classes) is called encapsulation.

The Prototype of the overloaded member function is


___
(A) Void operator – (negative)
(B) Void operator – ()

(C) Void operator minus

(D) Negative operator – ()


View Answer
Ans: B

Void operator – ()

Question: 3

Operator overloading ____


(A) Changes original definition

(B) Overrules original definition

(C) Does not overrule the original definition of the operator

(D) None of the given


View Answer
Ans: C

Does not overrule the original definition of the operator

Question: 4

The ___ function definitions are permitted for used


defined data type.
(A) Basic

(B) Overloaded

(C) Sizeof

(D) Friend
View Answer
Ans; B

Overloaded

Object Oriented Programming using C++ Questions


and Answers – Encapsulation
1. Which among the following best describes encapsulation?
a) It is a way of combining various data members into a single unit
b) It is a way of combining various member functions into a single unit
c) It is a way of combining various data members and member functions into a single unit
which can operate on any data
d) It is a way of combining various data members and member functions that operate on
those data members into a single unit
View Answer
Answer: d
Explanation: It is a way of combining both data members and member functions, which
operate on those data members, into a single unit. We call it a class in OOP generally. This
feature have helped us modify the structures used in C language to be upgraded into class
in C++ and other languages.

4. Which feature can be implemented using encapsulation?


a) Inheritance
b) Abstraction
c) Polymorphism
d) Overloading
View Answer
Answer: b
Explanation: Data abstraction can be achieved by using encapsulation. We can hide the
operation and structure of actual program from the user and can show only required
information by the user.

6. Encapsulation helps in writing ___________ classes in java.


a) Mutable
b) Abstract
c) Wrapper
d) Immutable
View Answer
Answer: d
Explanation: Immutable classes are used for caching purpose generally. And it can be
created by making the class as final and making all its members private.
8. How can Encapsulation be achieved?
a) Using Access Specifiers
b) Using only private members
c) Using inheritance
d) Using Abstraction
View Answer
Answer: a
Explanation: Using access specifiers we can achieve encapsulation. Using this we can in
turn implement data abstraction. It’s not necessary that we only use private access.

9. Which among the following violates the principle of encapsulation almost always?
a) Local variables
b) Global variables
c) Public variables
d) Array variables
View Answer
Answer: b
Explanation: Global variables almost always violates the principles of encapsulation.
Encapsulation says the data should be accessed only by required set of elements. But
global variable is accessible everywhere, also it is most prone to changes. It doesn’t hide
the internal working of program.

MCQs – C++ Classes and Objects


March 30, 2017 by rsingh

Q) Correct way of creating an object of a class called Car is

1. Car obj;
2. Car *obj = new Car();
3. Only B
4. A & B both
Answer: 4

Both Car obj; and Car *obj = new Car() are valid way to create an object of the
class.
Q) In C++, Class object created statically(e.g. Car obj; and dynamically (Car
*obj = new Car() ; ) are stored in memory

1. Stack, heap
2. Heap, heap
3. Heap, stack
4. Stack, stack
Answer: 1

Q) True statement about Class and structure in C++ is

1. Default access specifier is private in class and public in structure


2. Way of creating objects of class and structure are different
3. Way of inheriting class and structure are different
4. None
Answer: 1

Q) In C++ programming, cout is a/an

1. Function
2. Operator
3. Object
4. macro
Answer: 3
Q) Which is Abstract Data Type in C++

1. Class
2. Int
3. Float
4. array
Answer: 1

Q) Class allows only one object of it to be created though out the


program life cycle

1. Singleton class
2. Abstract class
3. Friend class
4. All classes
Answer: 1

Q) Statically allocated object for class A in C++ is

1. A *obj = new A();


2. A obj;
3. A obj = new A();
4. None
Answer: 2

Q) When you create an object of a class A like A obj ; then which one
will be called automatically

1. Constructor
2. Destructor
3. Copy constructor
4. Assignment operator
Answer: 1

Q) When you create an object of a derived class in C++

1. Derived class constructor is called first then the base class constructor
2. Base class constructor is called first then derived class constructor
3. base class constructor will not be called
4. none of the above
Answer: 2

Q) The class in C++ which act only as a base class and object of it
cannot be created is

1. parent class
2. super class
3. abstract class
4. none of the above
Answer: 3

Q) Data members and member functions of a class in C++ program


are by default

1. protected
2. public
3. private
4. None
Answer: 3

Q) Which operator is used to allocate an object dynamically of a class


in C++?

1. Scope resolution operator


2. Conditional operator
3. New operator
4. Membership access
Answer: 3

Q) Which is used to define the member function of a class externally?

1. :
2. ::
3. #
4. None
Answer: 2

Q) In C++, an object cannot be created for

1. An interface
2. An Abstract class
3. A singleton class
4. A&B
5. A, B & C
Answer: 4

Q) By default functions available in C++ language are

1. Constructor
2. Destructor
3. Copy constructor
4. Assignment operator
5. All
Answer: 5

MCQs – C++ Inheritance


Q)base class and derived class relationship
comes under
1. Inheritance
2. Polymorphism
3. encapsulation
4. None
Answer : 1

Explanation: Base class and derived class come under inheritance, one of the C++
oops principle. Also, it is known as parent child relationship.

Q) C++ Inheritance relationship is


1. Association
2. Is-A
3. Has-A
4. None
Answer: 2

Explanation: IS A Relationship is related to inheritance in C++.


Lets consider a relation ship between classes i.e. Dog IS A Animal, Apple IS A
Fruit, Car IS A Vehicle etc. Here, IS A relationship is valid, but, if we say Animal IS
A Dog, then it is wrong. So, Animal class should be the base class and Dog class
will be the derived class.

Q) Types of inheritance in C++ are


1. Multilevel
2. Multiple
3. Hierarchical
4. All the above
Answer: 4

Explanation: All are types of inheritance relationship in C++ oops.


Multilevel Inheritance: When a class is derived from a class which is also derived
from another class.
Multiple Inheritance: A class inherits multiple class. or say, A class has more than
one base class.
Hierarchical Inheritance: When multiple classes derived from a same base class.

Q) Inheritance allow in C++ Program?


1. Class Re-usability
2. Creating a hierarchy of classes
3. Extendibility
4. All
Answer: 4

Explanation: Advantage of inheritance are like re-usability- You can re-use


existing class in a new class that avoid re-writing same code and efforts.
We can make an application easily extensible.

Q) Functions that can be inherited from base


class in C++ program
1. Constructor
2. Destructor
3. Static function
4. None
Answer: 4

Explanation:

In C++, constructor and destruction of a class cannot be inherited to derived


class. However, when we create the object of derived class then constructor of
the base class is called automatically. You can read order of execution of
constructors and destructors call in c++ inheritance.

Static function is not the part of an object, Hence, it cannot be inherited in


derived classes. Note that all the class member functions that are the part of an
object, only they can be inherited. Read what is static function in C++
programming
Q)____________ members of base class are
inaccessible to derived class
1. Private
2. Protected
3. Public
4. None
Answer: 1

Q) Accessing functions from multiple classes to


a derived class is known as
1. multiple inheritance
2. single inheritance
3. Hybrid inheritance
4. multilevel inheritance
Answer: 1

Q) In inheritance, order of execution of base


class and derived class destructors are
1. Base to derived
2. Derived to base
3. Random order
4. none
Answer: 2

In inheritance, execution order of constructors are always from base to derived


and destructors call order is in reverse i.e. from derived to base. In polymorphic
classes, means the class that contain virtual functions, we need to make
destructor virtual in base class. Other wise the derived class destructor will not be
called. Read detail about C++ virtual destructor with example.

MCQs – C++ Virtual Concepts


Q) Which concept is not available in C++?

1. Virtual constructor
2. Virtual destructor
3. Virtual function
4. Virtual Table
Answer: 1
There is no concept of virtual constructor in C++ programming. Read why virtual
constructor is not possible in C++.

Q) What is output of the following C++ program?

class A{

public:

void f(){

cout<<"A::f()"<<endl;

}
};

class B:public A{

public:

void fb(){

cout<<"A::fb()"<<endl;

};

class C:public A{

public:

void fc(){

cout<<"A::fc()"<<endl;

};
class D: public B,public C{

public:

void fd(){

cout<<"A::fd()"<<endl;

};

int main(){

D obj;

obj.f();

return 0;

1. A::f()
2. A::f() A::f()
3. A::f() A::f()
4. Compiler error
Answer: 4
Explanation: The above class represent the virtual base class / diamond problem
in C++. The object D will try to access the function f() of base class via class B and
class C. as both B and C inherites A. This will lead to ambiguity call the function.
So, compiler will flash an error. i.e. ambiguous access of f().

If we make the class B and C virtual like below, then there would not be any ambiguity and
program will work fine.
class A { public: void f() {} };

class B : public virtual A {};

class C : public virtual A {};

class D : public B, public C {};

Q) What is purpose of Virtual destructor?

1. To maintain the call hierarchy of destructors of base and derived classes.


2. Destructor can be virtual so that we can override the destructor of base
class in derived class.
3. Virtual destructor is to destroy the VTable
4. None
Answer: 1
Base class destructor is made virtual in inheritance relationship to maintain the
call of destructors in base and derived class. Note that first derived class
destructor will be called then derived class. Read execution of order of
constructors and destructors in C++. Also, Read why and when virtual destructor
is useful ?

Q) When VTABLE (Virtual table) get created for a class?


1. Every class by default has virtual table
2. When a Class Overrides the function of Base class
3. When a class contains at least one virtual function.
4. When a class is derived from a base class.
Answer: 3

Q) Number of VTable created for following C++ program is ________

class A {

public:

virtual void f(){

cout<<"function :f()"<<endl;

};

int main(){

A obj1, obj2,obj3;

return 0;

1. 0
2. 1
3. 3
4. 4
Answer: 2
Compiler will create only 1 virtual table for the class. Virtual table does not
depend upon number of objects created.

Object Oriented Programming using C++ Questions


and Answers – Abstraction
1. Which among the following best defines abstraction?
a) Hiding the implementation
b) Showing the important data
c) Hiding the important data
d) Hiding the implementation and showing only the features
View Answer
Answer: d
Explanation: It includes hiding the implementation part and showing only the required data
and features to the user. It is done to hide the implementation complexity and details from
the user. And to provide a good interface in programming.

2. Hiding the implementation complexity can ____________


a) Make the programming easy
b) Make the programming complex
c) Provide more number of features
d) Provide better features
View Answer
Answer: a
Explanation: It can make programming easy. The programming need not know how the
inbuilt functions are working but can use those complex functions directly in the program. It
doesn’t provide more number of features or better features.
3. Class is _________ abstraction.
a) Object
b) Logical
c) Real
d) Hypothetical
View Answer
Answer: b
Explanation: Class is logical abstraction because it provides a logical structure for all of its
objects. It gives an overview of the features of an object.
4. Object is ________ abstraction.
a) Object
b) Logical
c) Real
d) Hypothetical
View Answer
Answer: c
Explanation: Object is real abstraction because it actually contains those features of class. It
is the implementation of overview given by class. Hence the class is logical abstraction and
its object is real.

5. Abstraction gives higher degree of ________


a) Class usage
b) Program complexity
c) Idealized interface
d) Unstable interface
View Answer
Answer: c
Explanation: It is to idealize the interface. In this way the programmer can use the
programming features more efficiently and can code better. It can’t increase the program
complexity, as the feature itself is made to hide it.

6. Abstraction can apply to ____________


a) Control and data
b) Only data
c) Only control
d) Classes
View Answer
Answer: a
Explanation: Abstraction applies to both. Control abstraction involves use of subroutines
and control flow abstraction. Data abstraction involves handling pieces of data in meaningful
ways.
7. Which among the following can be viewed as combination of abstraction of data and
code.
a) Class
b) Object
c) Inheritance
d) Interfaces
View Answer
Answer: b
Explanation: Object can be viewed as abstraction of data and code. It uses data members
and their functioning as data abstraction. Code abstraction as use of object of inbuilt class.

8. Abstraction principle includes___________


a) Use abstraction at its minimum
b) Use abstraction to avoid longer codes
c) Use abstraction whenever possible to avoid duplication
d) Use abstraction whenever possible to achieve OOP
View Answer
Answer: c
Explanation: Abstraction principle includes use of abstraction to avoid duplication (usually of
code). It this way the program doesn’t contain any redundant functions and make the
program efficient.

Object Oriented Programming using C++ Questions


and Answers – Access Specifiers
1. How many types of access specifiers are provided in OOP (C++)?
a) 1
b) 2
c) 3
d) 4
View Answer
Answer: c
Explanation: Only 3 types of access specifiers are available. Namely, private, protected and
public. All these three can be used according to the need of security of members.

2. Which among the following can be used together in a single class?


a) Only private
b) Private and Protected together
c) Private and Public together
d) All three together
View Answer
Answer: d
Explanation: All the classes can use any of the specifiers as needed. There is no restriction
on how many of them can be used together.

3. Which among the following can restrict class members to get inherited?
a) Private
b) Protected
c) Public
d) All three
View Answer
Answer: a
Explanation: Private members of a class can’t be inherited. These members can only be
accessible from members of its own class only. It is used to secure the data.
3. Which among the following can restrict class members to get inherited?
a) Private
b) Protected
c) Public
d) All three
View Answer
Answer: a
Explanation: Private members of a class can’t be inherited. These members can only be
accessible from members of its own class only. It is used to secure the data.
5. Which specifier allows a programmer to make the private members which can be
inherited?
a) Private
b) Default
c) Protected
d) Protected and default
View Answer
Answer: c
Explanation: Protected access is used to make the members private. But those members
can be inherited. This gives both security and code reuse capability to a program.

7. If a class has all the private members, which specifier will be used for its implicit
constructor?
a) Private
b) Public
c) Protected
d) Default
View Answer
Answer: b
Explanation: The implicit constructor will always be public. Otherwise the class wouldn’t be
able to have instances. In turn, no objects will be created and the class can only be used for
inheritance.

10. On which specifier’s data, does the size of a class’s object depend?
a) All the data members are added
b) Only private members are added
c) Only public members are added
d) Only default data members are added
View Answer
Answer: a
Explanation: All the data members are counted to calculate the size of an object of a class.
The data member access specifier doesn’t play any role here. Hence all the data size will
be added.
13. Which access specifier should be used so that all the parent class members can be
inherited and accessed from outside the class?
a) Private
b) Default or public
c) Protected or private
d) Public
View Answer
Answer: d
Explanation: All the members must be of public access. So that the members can be
inherited easily. Also, the members will be available from outside the class.

4. Which access specifier is usually used for data members of a class?


a) Private
b) Default
c) Protected
d) Public
View Answer
Answer: a
Explanation: All the data members should be made private to ensure the highest security of
data. In special cases we can use public or protected access, but it is advised to keep the
data members private always.

15. Which specifier should be used for member functions of a class?


a) Private
b) Default
c) Protected
d) Public
View Answer
Answer: d
Explanation: It is always advised that the member functions should be kept public so that
those functions can be used from out of the class. This is usually done to ensure that the
features provided by the class can be used at its maximum.

Object Oriented Programming using C++ Questions


and Answers – Public Access Specifier
1. Which among the following is true for the code given below?

class A
{
int marks;
public : disp()
{
cout&lt;&lt;marks;
}
}
class B: protected A
{
char name[20];
}
A a; a.disp();
B b; b.disp();

a) Only object of class A can access disp() function


b) Only object of class B can access disp() function
c) Both instances can access disp() function
d) Accessing disp() outside class is not possible
View Answer
Answer: a
Explanation: The object of class A can access the disp() function. This is because the disp()
function is public in definition of class A. But it can’t be accessed from instance of class B
because the disp() function is protected in class B, since it was inherited as protected.
2. If the members have to be accessed from anywhere in the program and other packages
also, which access specifier should be used?
a) Public
b) Private
c) Protected
d) Default
View Answer
Answer: a
Explanation: The access specifier must be public so as to access the members outside the
class and anywhere within the program without using inheritance. This is a rule, predefined
for the public members.
3. Which among the following have least security according to the access permissions
allowed?
a) Private
b) Default
c) Protected
d) Public
View Answer
Answer: d
Explanation: The public members are available to the whole program. This makes the
members most vulnerable to accidental changes, which may result in unwanted
modification and hence unstable programming.

4. Which among the following can be used for outermost class access specifier in java?
a) Private
b) Public
c) Default
d) Default or Public
View Answer
Answer: d
Explanation: Either default or public access specifier must be used for outermost classes.
Private can be used with inner classes. This is done so that all the members can access
and use the utmost class and that program execution can be done from anywhere. Inner
classes can be made private for security.
7. How many public class(s) (outermost) can be there in a java program?
a) 1
b) 2
c) 3
d) As required
View Answer
Answer: a
Explanation: There can be only one public class in a java program. The public class name
must match the name of file. And there can’t be more than one class with same name in a
single program in same scope. Hence it is not possible to have more than one public class
in java program.

8. What is the output of the following code?

package pack1;
class A
{
public A()
{
System.out.print(“object created”);
}
}
package pack2;
import pack1.*;
class B
{
A a=new A();
}

a) Output is: object created


b) Output is: object createdobject created
c) Compile time error
d) Run time error
View Answer
Answer: c
Explanation: The program will give compile time error. Class A is defined with default
access specifier. This directly means that class A will be available within package only.
Even if the constructor is public, the object will not be created.
11. Which specifier can be used to inherit protected members as protected in subclass but
public as public in subclass?
a) Private
b) Default
c) Public
d) Protected
View Answer
Answer: c
Explanation: The specifier that can make protected member’s protected in subclass and
public member’s public in subclass, is public. This is done to maintain the security level of
protected members of parent class.

15. Which specifier allows to secure the public members of base class in inherited classes?
a) Private
b) Protected
c) Public
d) Private and Protected
View Answer
Answer: d
Explanation: Both the private and protected specifiers can make the public members of the
base class more secure. This is useful if we stop using the parent class members and use
the classes which inherited the parent class, so as to secure data better.

Object Oriented Programming using C++ Questions


and Answers – Constructors
1. Which among the following is called first, automatically, whenever an object is created?
a) Class
b) Constructor
c) New
d) Trigger
View Answer
Answer: b
Explanation: Constructors are the member functions which are called automatically
whenever an object is created. It is a mandatory functions to be called for an object to be
created as this helps in initializing the object to a legal initial value for the class.
2. Which among the following is not a necessary condition for constructors?
a) Its name must be same as that of class
b) It must not have any return type
c) It must contain a definition body
d) It can contains arguments
View Answer
Answer: c
Explanation: Constructors are predefined implicitly, even if the programmer doesn’t define
any of them. Even if the programmer declares a constructor, it’s not necessary that it must
contain some definition.

3. Which among the following is correct?


a) class student{ public: int student(){} };
b) class student{ public: void student (){} };
c) class student{ public: student{}{} };
d) class student{ public: student(){} };
View Answer
Answer: d
Explanation: The constructors must not have any return type. Also, the body may or may
not contain any body. Defining default constructor is optional, if you are not using any other
constructor.

4. In which access should a constructor be defined, so that object of the class can be
created in any function?
a) Public
b) Protected
c) Private
d) Any access specifier will work
View Answer
Answer: a
Explanation: Constructor function should be available to all the parts of program where the
object is to be created. Hence it is advised to define it in public access, so that any other
function is able to create objects.

4. In which access should a constructor be defined, so that object of the class can be
created in any function?
a) Public
b) Protected
c) Private
d) Any access specifier will work
View Answer
Answer: a
Explanation: Constructor function should be available to all the parts of program where the
object is to be created. Hence it is advised to define it in public access, so that any other
function is able to create objects.

5. How many types of constructors are available for use in general (with respect to
parameters)?
a) 2
b) 3
c) 4
d) 5
View Answer
Answer: a
Explanation: Two types of constructors are defined generally, namely, default constructor
and parameterized constructor. Default constructor is not necessary to be defined always.

8. If class C inherits class B. And B has inherited class A. Then while creating the object of
class C, what will be the sequence of constructors getting called?
a) Constructor of C then B, finally of A
b) Constructor of A then C, finally of B
c) Constructor of C then A, finally B
d) Constructor of A then B, finally C
View Answer
Answer: d
Explanation: While creating the object of class C, its constructor would be called by default.
But, if the class is inheriting some other class, firstly the parent class constructor will be
called so that all the data is initialized that is being inherited.

. In multiple inheritance, if class C inherits two classes A and B as follows, which class
constructor will be called first?

class A{ };
class B{ };
class C: public A, public B{ };

a) A()
b) B()
c) C()
d) Can’t be determined
View Answer
Answer: a
Explanation: Constructor of class A will be called first. This is because the constructors in
multiple inheritance are called in the sequence in which they are written to be inherited.
Here A is written first, hence it is called first.

11. If the object is passed by value to a copy constructor?


a) Only public members will be accessible to be copied
b) That will work normally
c) Compiler will give out of memory error
d) Data stored in data members won’t be accessible
View Answer
Answer: c
Explanation: Compiler runs out of memory. This is because while passing the argument by
value, a constructor of the object will be called. That in turn called another object
constructor for values, and this goes on. This is like a constructor call to itself, and this goes
on infinite times, hence it must be passed by reference, so that the constructor is not called.
12. Which object will be created first?

class student
{
int marks;
};
student s1, s2, s3;

a) s1 then s2 then s3
b) s3 then s2 then s1
c) s2 then s3 then s1
d) all are created at same time
View Answer
Answer: a
Explanation: The objects are created in the sequence of how they are written. This happens
because the constructors are called in the sequence of how the objects are mentioned. This
is done in sequence.

15. For constructor overloading, each constructor must differ in ___________ and
__________
a) Number of arguments and type of arguments
b) Number of arguments and return type
c) Return type and type of arguments
d) Return type and definition
View Answer
Answer: a
Explanation: Each constructor must differ in the number of arguments it accepts and the
type of arguments. This actually defines the constructor signature. This helps to remove the
ambiguity and define a unique constructor as required.

1. How many types of constructors are available, in general, in any language?


a) 2
b) 3
c) 4
d) 5
View Answer
Answer: b
Explanation: There are 3 types of constructors in general, namely, default constructors,
parameterized constructors and copy constructors. Default one is called whenever an object
is created without arguments.

1. Copy constructor is a constructor which ________________


a) Creates an object by copying values from any other object of same class
b) Creates an object by copying values from first object created for that class
c) Creates an object by copying values from another object of another class
d) Creates an object by initializing it with another previously created object of same class
View Answer
Answer: d
Explanation: The object that has to be copied to new object must be previously created. The
new object gets initialized with the same values as that of the object mentioned for being
copied. The exact copy is made with values.

2. The copy constructor can be used to ____________


a) Initialize one object from another object of same type
b) Initialize one object from another object of different type
c) Initialize more than one object from another object of same type at a time
d) Initialize all the objects of a class to another object of another class
View Answer
Answer: a
Explanation: The copy constructor has the most basic function to initialize the members of
an object with same values as that of some previously created object. The object must be of
same class.

4. The copy constructors can be used to ________


a) Copy an object so that it can be passed to a class
b) Copy an object so that it can be passed to a function
c) Copy an object so that it can be passed to another primitive type variable
d) Copy an object for type casting
View Answer
Answer: b
Explanation: When an object is passed to a function, actually its copy is made in the
function. To copy the values, copy constructor is used. Hence the object being passed and
object being used in function are different.
7. If a class implements some dynamic memory allocations and pointers then
_____________
a) Copy constructor must be defined
b) Copy constructor must not be defined
c) Copy constructor can’t be defined
d) Copy constructor will not be used
View Answer
Answer: a
Explanation: In the case where dynamic memory allocation is used, the copy constructor
definition must be given. The implicit copy constructor is not capable of manipulating the
dynamic memory and pointers. Explicit definition allows to manipulate the data as required.

8. What is the syntax of copy constructor?


a) classname (classname &obj){ /*constructor definition*/ }
b) classname (cont classname obj){ /*constructor definition*/ }
c) classname (cont classname &obj){ /*constructor definition*/ }
d) classname (cont &obj){ /*constructor definition*/ }
View Answer
Answer: c
Explanation: The syntax must contain the class name first, followed by the classname as
type and &object within parenthesis. Then comes the constructor body. The definition can
be given as per requirements.
11. Copy constructor will be called whenever the compiler __________
a) Generates implicit code
b) Generates member function calls
c) Generates temporary object
d) Generates object operations
View Answer
Answer: c
Explanation: Whenever the compiler creates a temporary object, copy constructor is used to
copy the values from existing object to the temporary object.

Object Oriented Programming using C++ Questions


and Answers – Destructors
1. Which among the following describes a destructor?
a) A special function that is called to free the resources, acquired by the object
b) A special function that is called to delete the class
c) A special function that is called anytime to delete an object
d) A special function that is called to delete all the objects of a class
View Answer
Answer: a
Explanation: It is used to free the resources that the object might had used in its lifespan.
The destructors are called implicitly whenever an object’s life ends.

2. When a destructor is called?


a) After the end of object life
b) Anytime in between object’s lifespan
c) At end of whole program
d) Just before the end of object life
View Answer
Answer: d
Explanation: The destructor is called just before the object go out of scope or just before its
life ends. This is done to ensure that all the resources reserved for the object are used and
at last, are made free for others.

4. If in multiple inheritance, class C inherits class B, and Class B inherits class A. In which
sequence are their destructors called if an object of class C was declared?
a) ~C() then ~B() then ~A()
b) ~B() then ~C() then ~A()
c) ~A() then ~B() then ~C()
d) ~C() then ~A() then ~B()
View Answer
Answer: a
Explanation: The destructors are always called in the reverse order of how the constructors
were called. Here class A constructor would have been created first if Class C object is
declared. Hence class A destructor is called at last.

5. Choose the correct sequence of destructors being called for the following code.

class A{ };
class B{ };
class C: public A, public B{ };

a) ~A(), ~B(), ~C()


b) ~B(), ~C(), ~A()
c) ~A(), ~C(), ~B()
d) ~C(), ~B(), ~A()
View Answer
Answer: d
Explanation: In multiple inheritance, the constructors are called in the sequence of how they
are written in inheritance sequence. And the destructors will be called in the reverse order.
This can be cross verified just by printing a message from each destructor defined in
classes.

9. Destructors can be ________


a) Abstract type
b) Virtual
c) Void
d) Any type depending on situation
View Answer
Answer: b
Explanation: The destructors can be virtual. It is actually advised to keep the destructors
virtual always. This is done to suppress the problems that may arise if inheritance is
involved.
13. Which class destructor will be called first, when following code go out of scope?

class A{ };
class B{ };
class C: public B{ };
A a;
B b;
C c;

a) ~A()
b) ~B()
c) ~C()
d) ~B() and ~C()
View Answer
Answer: c
Explanation: The constructor that would have created at last, its destructor will be called first
when the code goes out of scope. This will help the program to manage the resources more
efficiently.
C++ Programming Questions and Answers – Operator
Overloading – 2
This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on
“Operator Overloading – 2”.

1. What is a binary operator?


a) Operator that performs its action on a single operand
b) Operator that performs its action on two operand
c) Operator that performs its action on three operand
d) Operator that performs its action on any number of operands
View Answer
Answer: b
Explanation: As the word binary itself means 2 therefore a binary operator operates on two
operands.

2. Which is the correct example of a binary operator?


a) ++
b) —
c) Dereferencing operator(*)
d) +
View Answer
Answer: d
Explanation: +(adding two operands) requires two operands whereas ++(increases value by
1), –(decreases value by 1) and *(dereferencing operator used for accessing value of
pointers) requires only one operand.

3. Which is the correct example of a unary operator?


a) &
b) ==
c) —
d) /
View Answer
Answer: c
Explanation: &, == and / requires two operands whereas — requires only one operand, in
general, it decreases the value of operand by 1.

4. Which is called ternary operator?


a) ?:
b) &&
c) |||
d) ===
View Answer
Answer: a
Explanation: ?: is called ternary operator because it separates three expressions. exp1 ?
exp2 : exp3.
5. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class complex
{

int i;
int j;
public:
complex(int a, int b)
{
i = a;
j = b;
}

complex operator+(complex c)
{
complex temp;
temp.i = this->i + c.i;
temp.j = this->j + c.j;
return temp;
}

void show(){
cout<<"Complex Number: "<<i<<" + i"<<j<<endl;
}
};

int main(int argc, char const *argv[])


{
complex c1(1,2);
complex c2(3,4);
complex c3 = c1 + c2;
c3.show();
return 0;
}
a) 4 + i6
b) 2 + i2
c) Error
d) Segmentation fault

Answer: c
Explanation: In the operator overloaded function we are trying to call default constructor of
the class complex but as we have overridden the constructor by our constructor therefore
the default constructor cannot be called hence the program gives error.

1. we can define a binary operator as :


A. The operator that performs its action on two operand
B. The operator that performs its action on three operand
C. The operator that performs its action on any number of operands
D. The operator that performs its action on a single operand
Answer - Click Here:
A
2. ______ is not an operator overloaded by the C++ language.
A. <<.
B. +.
C. pow
D. >>.
Answer - Click Here:
C
3. correct example of a binary operator is _______.
A. —
B. +
C. ++
D. Dereferencing operator(*)
Answer - Click Here:
B
5. ____ is a correct example of a unary operator.
A. /
B. —
C. &
D. ==
Answer - Click Here:
B
7. Ternary operator is shown as _____.
A. ===
B. &&
C. ?:
D. |||
Answer - Click Here:
C

8. Overloading the addition (+) operator is correct function name OF :


A. operator(+).
B. operator_+.
C. operator+.
D. operator:+. Answer - Click Here:

A
9. Choose the output of the following C++ code?
C++
1 #include <iostream>
2 #include <string>
3 using namespace std;
4 class complex
5 {
6 int i;
7 int j;
8 public:
9 complex(int a, int b)
10 {
11 i = a;
12 j = b;
13 }
14 complex operator+(complex c)
15 {
16 complex temp;
17 temp.i = this->i + c.i;
18 temp.j = this->j + c.j;
19 return temp;
20 }
21 void show(){
22 cout<<"Please note it that Complex Number is: "<<i<<" + i"<<j<<endl;
23 }
24 };
25 int main(int argc, char const *argv[])
26 {
27 complex c1(1,2);
28 complex c2(3,4);
29 complex c3 = c1 + c2;
30 c3.show();
31 return 0;
32 }
A. Error
B. 4 + i6
C. Segmentation fault
D. 2 + i2
Answer - Click Here:
A
0. ________ operators cannot be overloaded?
A. The [ ] operator.
B. The -> operator.
C. The . operator.
D. The & operator
Answer - Click Here:
C
1.In flowchart rectangle symbol indicates:
a.Input/Output
b.Connector
c.Process
d.Decision
Answer - Click Here:
C
2.C++ is a(n):
a.Object-oriented programming language.
b.Event-driven programming language.
c.Structured programming language.
d.None of these.
Answer - Click Here:
A
3.The arithmetic operators are:
a.Ternary operators
b.Unary operators
c.Binary operators
d.None of these
Answer - Click Here:
C
4.THe escape sequence for carrige return is:
a./t
b./f
c./n
d./r
Answer - Click Here:
D
5.In switch structure, each case label may be an integer constant or:
a.Real constant
b.Character constant
c.String constant
d.None of these
Answer - Click Here:
B
6.WHat will be the value of ‘x’ after executing for(x=1;x<=15;x++);?
a.15
b.1
c.14
d.16
Answer - Click Here:
C
7.The pointers are used for implementing the concept of:
a.Polymorphism
b.Array
c.Structure
d.Inheritance
Answer - Click Here:
A
8.The function stlen(“ABC”);will return value:
a.4
b.ABC
c.3
d.None of these
Answer - Click Here:
C
9.The data item of a structured are called:
a.Fields
b.Elements
c.Members
d.ALl of these
Answer - Click Here:
D
10.In C++,the functions of a class are called:
a.Attributes
b.Methods
c.Member function
d.Both a and b
Answer - Click Here:
C

11.The derived class is also called:


a.parent class
b.sub class
c.child class
d.both b and c

12.Which of the following type of inheritance is commonly used:


a.private
b.public
c.protected
d.none of these
Answer - Click Here:
B

Answer - Click Here:

D
13.Polymorphism is achieved through:
a.destructor
b.constructor
c.virtual function
d.overloading operator
Answer - Click Here:
C
14.Which of the following strem class is used to perform both input and output
file operation:
a.ofstream
b.ifstream
c.fstream
d.iostream
Answer - Click Here:
C

15.How many operations can be performe on stack:


a.4
b.5
c.3
d.2
Answer - Click Here:
D
20.Inheritance is the process of creating new classes called:
a.Super classes
b.Parent classes
c.Base classes
d.Derived classes
Answer - Click Here:
D

You might also like