OOPs MCQ (Multiple Choice Questions) - Sanfoundry

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

Object Oriented

Programming MCQ
(Multiple Choice Questions)
Here are 1000 MCQs on Object Oriented Programming
(Chapterwise).

1. Who invented OOP?


a) Andrea Ferro
b) Adele Goldberg
c) Alan Kay
d) Dennis Ritchie

View Answer

Answer: c
Explanation: Alan Kay invented OOP, Andrea Ferro was
a part of SmallTalk Development. Dennis invented C++
and Adele Goldberg was in team to develop SmallTalk
but Alan actually had got rewarded for OOP.

2. Which is not a feature of OOP in general definitions?


a) E!cient Code
b) Code reusability
c) Modularity
d) Duplicate/Redundant data

View Answer

Answer: d
Explanation: Duplicate/Redundant data is dependent on
programmer and hence can’t be guaranteed by OOP.
Code reusability is done using inheritance. Modularity is
supported by using di"erent code files and classes.
Codes are more e!cient because of features of OOP.

3. Which was the first purely object oriented programming


language developed?
a) Kotlin
b) SmallTalk
c) Java
d) C++

View Answer

Answer: b
Explanation: SmallTalk was the first programming
language developed which was purely object oriented.
It was developed by Alan Kay. OOP concept came into
the picture in 1970’s.

4. When OOP concept did first came into picture?


a) 1980’s
b) 1995
c) 1970’s
d) 1993

View Answer

Answer: c
Explanation: OOP first came into picture in 1970’s by
Alan and his team. Later it was used by some
programming languages and got implemented
successfully, SmallTalk was first language to use pure
OOP and followed all rules strictly.

5. Which feature of OOP indicates code reusability?


a) Abstraction
b) Polymorphism
c) Encapsulation
d) Inheritance

View Answer

Answer: d
Explanation: Inheritance indicates the code reusability.
Encapsulation and abstraction are meant to hide/group
data into one element. Polymorphism is to indicate
di"erent tasks performed by a single entity.

advertisement

6. Which header file is required in C++ to use OOP?


a) OOP can be used without using any header file
b) stdlib.h
c) iostream.h
d) stdio.h

View Answer

Answer: a
Explanation: We need not include any specific header
file to use OOP concept in C++, only specific functions
used in code need their respective header files to be
included or classes should be defined if needed.

7. Why Java is Partially OOP language?


a) It allows code to be written outside classes
b) It supports usual declaration of primitive data types
c) It does not support pointers
d) It doesn’t support all types of inheritance

View Answer

Answer: b
Explanation: As Java supports usual declaration of data
variables, it is partial implementation of OOP. Because
according to rules of OOP, object constructors must be
used, even for declaration of variables.

8. Which among the following doesn’t come under OOP


concept?
a) Data hiding
b) Message passing
c) Platform independent
d) Data binding

View Answer

Answer: c
Explanation: Platform independence is not feature of
OOP. C++ supports OOP but it’s not a platform
independent language. Platform independence
depends on the programming language.

9. Which is the correct syntax of inheritance?


a) class base_classname :access derived_classname{
/*define class body*/ };
b) class derived_classname : access base_classname{
/*define class body*/ };
c) class derived_classname : base_classname{ /*define
class body*/ };
d) class base_classname : derived_classname{ /*define
class body*/ };

View Answer

Answer: b
Explanation: Firstly, keyword class should come,
followed by the derived class name. Colon is must
followed by access in which base class has to be
derived, followed by the base class name. And finally
the body of class. Semicolon after the body is also must.

10. Which feature of OOP is indicated by the following


code?

class student{ int marks; };


class topper:public student{ int age; topper(int age){

a) Encapsulation and Inheritance


b) Inheritance and polymorphism
c) Polymorphism
d) Inheritance

View Answer

Answer: a
Explanation: Encapsulation is indicated by use of
classes. Inheritance is shown by inheriting the student
class into topper class. Polymorphism is not shown here
because we have defined the constructor in the topper
class but that doesn’t mean that default constructor is
overloaded.

11. The feature by which one object can interact with


another object is _____________
a) Message reading
b) Message Passing
c) Data transfer
d) Data Binding

View Answer

Answer: b
Explanation: The interaction between two object is
called the message passing feature. Data transfer is not
a feature of OOP. Also, message reading is not a feature
of OOP.

12. Which among the following, for a pure OOP language,


is true?
a) The language should follow at least 1 feature of OOP
b) The language must follow only 3 features of OOP
c) The language must follow all the rules of OOP
d) The language should follow 3 or more features of OOP

View Answer

Answer: c
Explanation: The language must follow all the rules of
OOP to be called a purely OOP language. Even if a single
OOP feature is not followed, then it’s known to be a
partially OOP language.

advertisement

13. How many types of access specifiers are provided in


OOP (C++)?
a) 4
b) 3
c) 2
d) 1

View Answer

Answer: b
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.

14. In multilevel inheritance, which is the most significant


feature of OOP used?
a) Code e!ciency
b) Code readability
c) Flexibility
d) Code reusability

View Answer

Answer: d
Explanation: The classes using multilevel inheritance
will use the code in all the subsequent subclasses if
available. Hence the most significant feature among the
options given is code reusability. This feature is
generally intended to use the data values and reuse the
redundant functions.

15. What is encapsulation in OOP?


a) It is a way of combining various data members and
member functions that operate on those data members
into a single unit
b) It is a way of combining various data members and
member functions into a single unit which can operate on
any data
c) It is a way of combining various data members into a
single unit
d) It is a way of combining various member functions into
a single unit

View Answer

Answer: a
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.

16. Which of the following is not true about


polymorphism?
a) Helps in redefining the same functionality
b) Increases overhead of function definition always
c) It is feature of OOP
d) Ease in readability of program

View Answer

Answer: b
Explanation: It never increases function definition
overhead, one way or another if you don’t use
polymorphism, you will use the definition in some other
way, so it actually helps to write e!cient codes.

17. Which constructor will be called from the object


created in the below C++ code?

advertisement

class A
{
int i;
A()
{
i=0; cout<<i;
}
A(int x=0)
{
i=x; cout<<I;
}
};
A obj1;

a) Parameterized constructor
b) Default constructor
c) Run time error
d) Compile time error

View Answer

Answer: d
Explanation: When a default constructor is defined and
another constructor with 1 default value argument is
defined, creating object without parameter will create
ambiguity for the compiler. The compiler won’t be able
to decide which constructor should be called, hence
compile time error.

18. What is an abstraction in object-oriented


programming?
a) Hiding the implementation and showing only the
features
b) Hiding the important data
c) Hiding the implementation
d) Showing the important data

View Answer

Answer: a
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.

19. Which among the following can show polymorphism?


a) Overloading &&
b) Overloading <<
c) Overloading ||
d) Overloading +=

View Answer

Answer: b
Explanation: Only insertion operator can be overloaded
among all the given options. And the polymorphism can
be illustrated here only if any of these is applicable of
being overloaded. Overloading is type of polymorphism.

20. In which access should a constructor be defined, so


that object of the class can be created in any function?
a) Any access specifier will work
b) Private
c) Public
d) Protected

View Answer

Answer: c
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.

21. Which among the following is correct for the class


defined below?

class student
{
int marks;
public: student(){}
student(int x)
{
marks=x;
}
};
main()
{
student s1(100);
student s2();
student s3=100;
return 0;
}

a) Program will give compile time error


b) Object s3, syntax error
c) Only object s1 and s2 will be created
d) Program runs and all objects are created

View Answer

Answer: d
Explanation: It is a special case of constructor with only
1 argument. While calling a constructor with one
argument, you are actually implicitly creating a
conversion from the argument type to the type of class.
Hence you can directly specify the value of that one
argument with assignment operator.

22. The copy constructors can be used to ________


a) Copy an object so that it can be passed to another
primitive type variable
b) Copy an object for type casting
c) Copy an object so that it can be passed to a function
d) Copy an object so that it can be passed to a class

View Answer

Answer: c
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 di"erent.

23. Which constructor will be called from the object obj2


in the following C++ program?

class A
{
int i;
A()
{
i=0;
}
A(int x)
{
i=x+1;
}
A(int y, int x)
{
i=x+y;
}
};
A obj1(10);
A obj2(10,20);
A obj3;

a) A(int y, int x)
b) A(int y; int x)
c) A(int y)
d) A(int x)

View Answer

Answer: a
Explanation: The two argument constructor will be
called as we are passing 2 arguments to the object
while creation. The arguments will be passed together
and hence compiler resolves that two argument
constructor have to be called.

24. Which among the following represents correct


constructor?
a) –classname()
b) classname()
c) ()classname
d) ~classname()

View Answer

Answer: b
Explanation: The constructors must contain only the
class name. The class name is followed by the blank
parenthesis or we can have parameters if some values
are to be passed.

25. What happens when an object is passed by reference?


a) Destructor is called at end of function
b) Destructor is called when called explicitly
c) Destructor is not called
d) Destructor is called when function is out of scope

View Answer

Answer: c
Explanation: The destructor is never called in this
situation. The concept is that when an object is passed
by reference to the function, the constructor is not
called, but only the main object will be used. Hence no
destructor will be called at end of function.

26. Which access specifier is usually used for data


members of a class?
a) Protected
b) Private
c) Public
d) Default

View Answer

Answer: b
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.

27. How to access data members of a class?


a) Dot, arrow or direct call
b) Dot operator
c) Arrow operator
d) Dot or arrow as required

View Answer

Answer: d
Explanation: The data members can never be called
directly. Dot operator is used to access the members
with help of object of class. Arrow is usually used if
pointers are used.

28. Which feature of OOP reduces the use of nested


classes?
a) Inheritance
b) Binding
c) Abstraction
d) Encapsulation

View Answer

Answer: a
Explanation: Using inheritance we can have the security
of the class being inherited. The subclass can access the
members of parent class. And have more feature than a
nested class being used.

29. Which keyword among the following can be used to


declare an array of objects in java?
a) allocate
b) arr
c) new
d) create

View Answer

Answer: c
Explanation: The keyword new can be used to declare
an array of objects in java. The syntax must be specified
with an object pointer which is assigned with a memory
space containing the required number of object space.
Even initialization can be done directly.

30. Which operator can be used to free the memory


allocated for an object in C++?
a) Unallocate
b) Free()
c) Collect
d) delete

View Answer

Answer: d
Explanation: The delete operator in C++ can be used to
free the memory and resources held by an object. The
function can be called explicitly whenever required. In
C++ memory management must be done by the
programmer. There is no automatic memory
management in C++.

31. Which of the following is not a property of an object?


a) Properties
b) Names
c) Identity
d) Attributes

View Answer

Answer: b
Explanation: The names are not property of an object.
The identity can be in any form like address or name of
object but name can’t be termed as only identity of an
object. The objects contain attributes that define what
type of data an object can store.

32. Which type of members can’t be accessed in derived


classes of a base class?
a) All can be accessed
b) Protected
c) Private
d) Public

View Answer

Answer: c
Explanation: The private members can be accessed only
inside the base class. If the class is derived by other
classes. Those members will not be accessible. This
concept of OOP is made to make the members more
secure.

33. Which among the following best describes the


Inheritance?
a) Using the data and functions into derived segment
b) Using already defined functions in a programming
language
c) Using the code already written once
d) Copying the code already written

View Answer

Answer: a
Explanation: It can only be indicated by using the data
and functions that we use in derived class, being
provided by parent class. Copying code is nowhere
similar to this concept, also using the code already
written is same as copying. Using already defined
functions is not inheritance as we are not adding any of
our own features.

34. Single level inheritance supports _____________


inheritance.
a) Language independency
b) Multiple inheritance
c) Compile time
d) Runtime

View Answer

Answer: d

You might also like