Object Oriented Programming MCQ focuses on “OOP Basic Concepts”.

1. Which was the first purely object oriented programming language developed?

a) Java
b) C++
c) SmallTalk
d) Kotlin

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

2. Which of the following best defines a class?
a) Parent of an object
b) Instance of an object
c) Blueprint of an object
d) Scope of an object

Answer: c
Explanation: A class is Blueprint of an object which describes/ shows all the functions and data that are provided by an object of a specific class. It can’t be called as parent or instance of an object. Class in general describes all the properties of an object.

3. Who invented OOP?
a) Alan Kay
b) Andrea Ferro
c) Dennis Ritchie
d) Adele Goldberg

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

4. What is the additional feature in classes that was not in structures?
a) Data members
b) Member functions
c) Static data allowed
d) Public access specifier

Answer: b
Explanation: Member functions are allowed inside a class but were not present in structure concept. Data members, static data and public access specifiers were present in structures too.

5. Which is not feature of OOP in general definitions?
a) Code reusability
b) Modularity
c) Duplicate/Redundant data
d) Efficient Code

Answer: c
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 different code files and classes. Codes are more efficient because of features of OOP.

6. Pure OOP can be implemented without using class in a program. (True or False)
a) True
b) False

Answer: b
Explanation: It’s false because for a program to be pure OO, everything must be written inside classes. If this rule is violated, the program can’t be labelled as purely OO.

7. Which Feature of OOP illustrated the code reusability?
a) Polymorphism
b) Abstraction
c) Encapsulation
d) Inheritance

Answer: d
Explanation: Using inheritance we can reuse the code already written and also can avoid creation of many new functions or variables, as that can be done one time and be reused, using classes.

8. Which language does not support all 4 types of inheritance?
a) C++
b) Java
c) Kotlin
d) Small Talk

Answer: b
Explanation: Java doesn’t support all 4 types of inheritance. It doesn’t support multiple inheritance. But the multiple inheritance can be implemented using interfaces in Java.

9. How many classes can be defined in a single program?
a) Only 1
b) Only 100
c) Only 999
d) As many as you want

Answer: d
Explanation: Any number of classes can be defined inside a program, provided that their names are different. In java, if public class is present then it must have the same name as that of file.

10. When OOP concept did first came into picture?
a) 1970’s
b) 1980’s
c) 1993
d) 1995

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

11. Why Java is Partially OOP language?
a) It supports usual declaration of primitive data types
b) It doesn’t support all types of inheritance
c) It allows code to be written outside classes
d) It does not support pointers

Answer: a
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.
Answer: b
Explanation: In C++, it’s not necessary to use classes, and hence codes can be written without using OOP concept. Classes may or may not contain member functions, so it’s not a necessary condition in C++. And, an object can only be declared in a code if its class is defined/included via header file.

13. Which header file is required in C++ to use OOP?
a) iostream.h
b) stdio.h
c) stdlib.h
d) OOP can be used without using any header file

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

14. Which of the two features match each other?
a) Inheritance and Encapsulation
b) Encapsulation and Polymorphism
c) Encapsulation and Abstraction
d) Abstraction and Polymorphism

Answer: c
Explanation: Encapsulation and Abstraction are similar features. Encapsulation is actually binding all the properties in a single class or we can say hiding all the features of object inside a class. And Abstraction is hiding unwanted data (for user) and showing only the data required by the user of program.

15. Which feature allows open recursion, among the following?
a) Use of this pointer
b) Use of pointers
c) Use of pass by value
d) Use of parameterized constructor

Answer: a
Explanation: Use of this pointer allows an object to call data and methods of itself whenever needed. This helps us call the members of an object recursively, and differentiate the variables of different scopes.

This set of Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) focuses on “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

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

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

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.

4. If same message is passed to objects of several different classes and all of those can respond in a different way, what is this feature called?
a) Inheritance
b) Overloading
c) Polymorphism
d) Overriding

Answer: c
Explanation: The feature defined in question defines polymorphism features. Here the different objects are capable of responding to the same message in different ways, hence polymorphism.

5. Which class/set of classes can illustrate polymorphism in the following code?

abstract  student

      marks
   calc_grade

 topper student

      calc_grade
     
          
    

 average student
 
       calc_grade
     
           
     

 failed  marks 

a) Only class student can show polymorphism
b) Only class student and topper together can show polymorphism
c) All class student, topper and average together can show polymorphism
d) Class failed should also inherit class student for this code to work for polymorphism

Answer: c
Explanation: Since Student class is abstract class and class topper and average are inheriting student, class topper and average must define the function named calc_grade(); in abstract class. Since both the definition are different in those classes, calc_grade() will work in different way for same input from different objects. Hence it shows polymorphism.

6. Which type of function among the following shows polymorphism?
a) Inline function
b) Virtual function
c) Undefined functions
d) Class member functions

Answer: b
Explanation: Only virtual functions among these can show polymorphism. Class member functions can show polymorphism too but we should be sure that the same function is being overloaded or is a function of abstract class or something like this, since we are not sure about all these, we can’t say whether it can show polymorphism or not.

7. In case of using abstract class or function overloading, which function is supposed to be called first?
a) Local function
b) Function with highest priority in compiler
c) Global function
d) Function with lowest priority because it might have been halted since long time, because of low priority

Answer: b
Explanation: Function with highest priority is called. Here, it’s not about the thread scheduling in CPU, but it focuses on whether the function in local scope is present or not, or if scope resolution is used in some way, or if the function matches the argument signature. So all these things define which function has the highest priority to be called in runtime. Local function could be one of the answer but we can’t say if someone have used pointer to another function or same function name.

8. Which among the following can’t be used for polymorphism?
a) Static member functions
b) Member functions overloading
c) Predefined operator overloading
d) Constructor overloading

Answer: a
Explanation: Static member functions are not property of any object. Hence it can’t be considered for overloading/overriding. For polymorphism, function must be property of object, not only of class.

9. What is output of the following program?

 student
 
       marks 
	 disp 
	 
		”its base ”
	
	 topper student
	
		  
		 disp
		 
			”Its derived ” 
		
	
	 main  student s topper t
	s.
	t.

a) Its base classIts derived class
b) Its base class Its derived class
c) Its derived classIts base class
d) Its derived class Its base class

Answer: a
Explanation: You need to focus on how the output is going to be shown, no space will be given after first message from base class. And then the message from derived class will be printed. Function disp() in base class overrides the function of base class being derived.
Answer: c
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.

11. Find the output of the following program.

 education
 
	 name
	  disp
	 
		”Its education ”
	
	 school education
	
		  dsip
		 
			”Its school education ”
		
    
	 main
	
                school s
                s.
	

a) Its school education system
b) Its education system
c) Its school education systemIts education system
d) Its education systemIts school education system

Answer: a
Explanation: Notice that the function name in derived class is different from the function name in base class. Hence when we call the disp() function, base class function is executed. No polymorphism is used here.

12. Polymorphism is possible in C language.
a) True
b) False

Answer: a
Explanation: It is possible to implement polymorphism in C language, even though it doesn’t support class. We can use structures and then declare pointers which in turn points to some function. In this way we simulate the functions like member functions but not exactly member function. Now we can overload these functions, hence implementing polymorphism in C language.

13. Which problem may arise if we use abstract class functions for polymorphism?
a) All classes are converted as abstract class
b) Derived class must be of abstract type
c) All the derived classes must implement the undefined functions
d) Derived classes can’t redefine the function

Answer: c
Explanation: The undefined functions must be defined is a problem, because one may need to implement few undefined functions from abstract class, but he will have to define each of the functions declared in abstract class. Being useless task, it is a problem sometimes.

14. Which among the following is not true for polymorphism?
a) It is feature of OOP
b) Ease in readability of program
c) Helps in redefining the same functionality
d) Increases overhead of function definition always

Answer: d
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 efficient codes.

15. If 2 classes derive one base class and redefine a function of base class, also overload some operators inside class body. Among these two things of function and operator overloading, where is polymorphism used?
a) Function overloading only
b) Operator overloading only
c) Both of these are using polymorphism
d) Either function overloading or operator overloading because polymorphism can be applied only once in a program

Answer: d
Explanation: Both of them are using polymorphism. It is not necessary that polymorphism can be used only once in a program, it can be used anywhere, any number of times in a single program.

This set of Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) focuses on “Constructors”.

1. Which among the following is called first, automatically, whenever an object is created?
a) Class
b) Constructor
c) New
d) Trigger

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

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(){} };

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

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

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.

6. If a programmer defines a class and defines a default value parameterized constructor inside it.
He has not defined any default constructor. And then he try to create the object without passing arguments, which among the following will be correct?
a) It will not create the object (as parameterized constructor is used)
b) It will create the object (as the default arguments are passed)
c) It will not create the object (as the default constructor is not defined)
d) It will create the object (as at least some constructor is defined)

Answer: b
Explanation: It will create the object without any problem, because the default arguments use the default value if no value is passed. Hence it is equal to default constructor with zero parameters. But it will not create the object if signature doesn’t match.

7. Default constructor must be defined, if parameterized constructor is defined and the object is to be created without arguments.
a) True
b) False

Answer: a
Explanation: If the object is create without arguments and only parameterized constructors are used, compiler will give an error as there is no default constructor defined. And some constructor must be called so as to create an object in memory.

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

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.

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

 A 
 B 
 C  A,  B  

a) A()
b) B()
c) C()
d) Can’t be determined

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.
Answer: b
Explanation: It can’t be defined with zero number of arguments. This is because to copy one object to another, the object must be mentioned so that compiler can take values from that object.
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?

 student 
  
     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

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.

13. Which among the following helps to create a temporary instance?
a) Implicit call to a default constructor
b) Explicit call to a copy constructor
c) Implicit call to a parameterized constructor
d) Explicit call to a constructor

Answer: d
Explanation: Explicit call to a constructor can let you create a temporary instance. This is because the temporary instances doesn’t have any name. Those are deleted from memory as soon as their reference is removed.

14. Which among the following is correct for the class defined below?

 student

     marks
     student
    student x
     
         marksx 
    

main

    student s1
    student s2
    student s3
     

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

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

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

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.

This set of Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) focuses on “Overloading Constructors”.

1. Which among the following best describes constructor overloading?
a) Defining one constructor in each class of a program
b) Defining more than one constructor in single class
c) Defining more than one constructor in single class with different signature
d) Defining destructor with each constructor

Answer: c
Explanation: If more than one constructors are defined in a class with same signature, then that results in error. The signatures must be different. So that the constructors can be differentiated.

2. Can constructors be overloaded in derived class?
a) Yes, always
b) Yes, if derived class has no constructor
c) No, programmer can’t do it
d) No, never

Answer: d
Explanation: The constructor must be having the same name as that of a class. Hence a constructor of one class can’t even be defined in another class. Since the constructors can’t be defined in derived class, it can’t be overloaded too, in derived class.

3. Does constructor overloading include different return types for constructors to be overloaded?
a) Yes, if return types are different, signature becomes different
b) Yes, because return types can differentiate two functions
c) No, return type can’t differentiate two functions
d) No, constructors doesn’t have any return type

Answer: d
Explanation: The constructors doesn’t have any return type. When we can’t have return type of a constructor, overloading based on the return type is not possible. Hence only parameters can be different.

4. Which among the following is possible way to overload constructor?
a) Define default constructor, 1 parameter constructor and 2 parameter constructor
b) Define default constructor, zero argument constructor and 1 parameter constructor
c) Define default constructor, and 2 other parameterized constructors with same signature
d) Define 2 default constructors

Answer: a
Explanation: All the constructors defined in a class must have a different signature in order to be overloaded. Here one default and other parameterized constructors are used, wherein one is of only one parameter and other accepts two. Hence overloading is possible.

5. Which constructor will be called from the object created in the code below?

 A
 
	 i
	A
	 
		i ltlti 
	
	A x
	 
		ix  ltltI  
	

A obj1

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

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

6. Which among the following is false for a constructor?
a) Constructors doesn’t have a return value
b) Constructors are always user defined
c) Constructors are overloaded with different signature
d) Constructors may or may not have any arguments being accepted

Answer: b
Explanation: The constructors are not always user defined. The construct will be provided implicitly from the compiler if the used doesn’t defined any constructor. The implicit constructor makes all the string values null and allocates memory space for each data member.

7. When is the constructor called for an object?
a) As soon as overloading is required
b) As soon as class is derived
c) As soon as class is created
d) As soon as object is created

Answer: d
Explanation: The constructor is called as soon as the object is created. The overloading comes into picture as to identify which constructor have to be called depending on arguments passed in the creation of object.

8. Which among the following function can be used to call default constructor implicitly in java?
a) this()
b) that()
c) super()
d) sub()

Answer: a
Explanation: The function this() can be used to call the default constructor from inside any other constructor. This helps to further reuse the code and not to write the redundant data in all the constructors.

9. Why do we use constructor overloading?
a) To use different types of constructors
b) Because it’s a feature provided
c) To initialize the object in different ways
d) To differentiate one constructor from another

Answer: c
Explanation: The constructors are overloaded to initialize the objects of a class in different ways. This allows us to initialize the object with either default values or used given values. If data members are not initialized then program may give unexpected results.

10. If programmer have defined parameterized constructor only, then __________________
a) Default constructor will not be created by the compiler implicitly
b) Default constructor will be created by the compiler implicitly
c) Default constructor will not be created but called at runtime
d) Compile time error

Answer: a
Explanation: When the programmer doesn’t specify any default constructor and only defines some parameterized constructor. The compiler doesn’t provide any default constructor implicitly. This is because it is assumed that the programmer will create the objects only with constructors.
Answer: b
Explanation: Java doesn’t provide the feature to recursively call the constructor. This is to eliminate the out of memory error in some cases that arises unexpectedly. That is an predefined condition for constructors in java.

12. Which constructor will be called from the object obj2 in the following program?

 A

	 i
	A
	  
		i  
	
	A x
	  
		ix  
	
	A y,  x
	  
		ixy  
	

A obj1
A obj2,
A obj3

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

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

13. What are we only create an object but don’t call any constructor for it in java?
a) Implicit constructor will be called
b) Object is initialized to some null values
c) Object is not created
d) Object is created but points to null

Answer: d
Explanation: The object becomes a reference object which can be initialized will another object. Then this object will indirectly become another name of the object being assigned. If not assigned, it simply points to null address.

14. Which among the following is false?
a) Constructor can’t be overloaded in Kotlin
b) Constructors can’t be called recursively in java
c) Constructors can be overloaded in C++
d) Constructors overloading depends on different signatures

Answer: a
Explanation: Kotlin language allows constructor overloading. This is a basic feature for the constructors. The constructor overloading allows the object to be initialized according to the user.

15. Which is correct syntax?
a) classname objectname= new() integer;
b) classname objectname= new classname;
c) classname objectname= new classname();
d) classname objectname= new() classname();

Answer: c
Explanation: The syntax for object creating in java with calling a constructor for is it is as in option c. The syntax must contain the classname followed by the object name. The keyword new must be used and then the constructor call with or without the parameters as required.

This set of Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) focuses on “Access Specifiers”.

1. How many types of access specifiers are provided in OOP (C++)?
a) 1
b) 2
c) 3
d) 4

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

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

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.

4. Which access specifier is used when no access specifier is used with a member of class (java)?
a) Private
b) Default
c) Protected
d) Public

Answer: b
Explanation: Default access is used if the programmer doesn’t specify the specifier. This acts in a similar way as that of private. But since nothing is specified we call it to default access.

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

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.

6. Which among the following is false?
a) Private members can be accessed using friend functions
b) Member functions can be made private
c) Default members can’t be inherited
d) Public members are accessible from other classes also

Answer: c
Explanation: The default members can be inherited. Provided that they are in same package. It works in a little different way from private access specifier.

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

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.

8. If class A has add() function with protected access, and few other members in public. Then class B inherits class A privately. Will the user will not be able to call _________ from the object of class B.
a) Any function of class A
b) The add() function of class A
c) Any member of class A
d) Private, protected and public members of class A

Answer: d
Explanation: Class B object will not be able to call any of the private, protected and public members of class A. It is not only about the function add(), but all the members of class A will become private members of class B.

9. Which access specifier should be used in a class where the instances can’t be created?
a) Private default constructor
b) All private constructors
c) Only default constructor to be public
d) Only default constructor to be protected

Answer: b
Explanation: All the constructors must be made private. This will restrict the instance of class to be made anywhere in the program. Since the constructors are private, no instance will be able to call them and hence won’t be allocated with any memory space.

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

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.

11. If class B inherits class A privately. And class B has a friend function. Will the friend function be able to access the private member of class A?
a) Yes, because friend function can access all the members
b) Yes, because friend function is of class B
c) No, because friend function can only access private members of friend class
d) No, because friend function can access private member of class A also

Answer: c
Explanation: The friend function of class B will not be able to access private members of class A. Since B is inheriting class A privately, the members will become private in class B. But private members of class A won’t be inherited at all. Hence it won’t be accessible.
Answer: a
Explanation: The classes which inherit the abstract class, won’t be able to implement the members of abstract class. The private members will not be inherited. This will restrict the subclasses to implement those members.

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

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.

14. Which access specifier is usually used for data members of a class?
a) Private
b) Default
c) Protected
d) Public

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

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.

This set of Object Oriented Programming Question Bank focuses on “Protected Access Specifier”.

1. Which among the following best describes the protected specifier?
a) Members are most secure and can’t be used outside class
b) Members are secure but can be used outside the class
c) Members are secure as private, but can be inherited
d) Members are secure like private, but can’t be inherited

Answer: c
Explanation: The members which are made protected, are most secure if inheritance is not used. But, this facility is provided to keep those members private and with that, they can be inherited by other classes. This is done to make the code more flexible.

2. If a constructor is defined in protected access, then?
a) It’s instance can be created inside the subclasses
b) It’s instance can be created anywhere in the program
c) It’s instance can be created inside the subclasses and main() function
d) It’s instance can be created inside the parent class only

Answer: a
Explanation: The instances will be allowed to be created only inside the sub classes. This is because the protected members will be inherited and hence the constructor too. This will allow the subclasses to call the constructor whenever an object is created.

3. For the following code, choose the correct option.

 A
  
	 marks
	  A
	 
		marks 
	
	  A  x
	 
		marksx  
	

a) The instances can be created only in subclasses
b) The instances can be created only in main() function
c) The instances can be created only in parent class
d) The instances can be created anywhere in the program

Answer: d
Explanation: The instances can be created anywhere in the program. The only restriction will be on which constructor will have to be called. The instances with zero arguments will be allowed to be created only inside the subclasses, but the instances with one argument can be created anywhere in the program.

4. If the protected members are to be made accessible only to the nearest subclass and no further subclasses, which access specifier should be used in inheritance?
a) The sub class should inherit the parent class privately
b) The sub class should inherit the parent class as protected
c) The sub class should inherit the parent class as public
d) The sub class can use any access modifier

Answer: a
Explanation: The sub class should use private inheritance. This will allow only the nearest sub classes to inherit the protected members and then those members will become private. Hence further inheritance of those members will not be possible.

5. What will be the output of the following code (all header files and required things are included)?

 A

	 marks
	  A x
	 
		marksx 
	
	  A
	 
		marks 
	

 B

	A a
	A b

main

	A a, b
	B c

a) Program runs fine
b) Program gives runtime error
c) Program gives compile time error
d) Program gives logical error

Answer: c
Explanation: The objects being created with assignment value are allowed, if the constructor accepts only 1 argument. But main() function will not be able to create the object here with assignment, as the constructor which accepts one argument is in protected mode in the class.

6. Which among the following is true for the given code below?

 A

	   marks
	  
	A
	 
		marks 
	
	disp
	 
		ltlt”marks”ltltmarks 
	

 B  A


B b
b.

a) Object b can’t access disp() function
b) Object b can access disp() function inside its body
c) Object b can’t access members of class A
d) Program runs fine

Answer: a
Explanation: The object of class B can’t access the members of A outside the class. This is because the class is being inherited in protected access, so all the members will become protected in subclass B.

7. Protected members differ from default members as _______
a) Protected members can be accessed outside package using inheritance, but default can’t
b) Default members can be accessed outside package using inheritance, but protected can’t
c) Protected members are allowed for inheritance but Default members are not allowed
d) Both are same

Answer: a
Explanation: The protected members are allowed in the same package but can also be accessed in other packages using inheritance. But the default members can never be accessible in other packages.

8. If all the members are defined in protected specifier then? (Constructors not considered)
a) Instance of class can’t be created
b) Instance of class can be created anywhere
c) Instance of class can be created only in subclasses
d) Instance of class can be created only in main() function

Answer: b
Explanation: The instances can be created anywhere in the program. This is because the constructors are not considered among the members defined in protected mode. Hence the default implicit constructor will be used whenever an object is created.
 A

      marks
    A
     
    
    Public  disp
     
          ltlt marks 
     

 B  A


B b

a) Instance of B will not be created
b) Instance of B will be created
c) Program gives compile time error
d) Program gives runtime error

Answer: a
Explanation: Instance of B will not be created. When you try to create an instance of B, First the constructor of parent class will be called, but the parent class constructor is private, hence it won’t be able to initialize and allocate memory for parent class members. In turn, the object of B will not be created.

10. If protected inheritance is used then _____
a) Public members become public in subclass
b) Protected members become public in subclass
c) Protected members become protected in subclass
d) Protected and Public members become protected in subclass

Answer: d
Explanation: The protected and public members of the parent class will become the protected members in subclass. This is predefined rule of inheritance. The reason behind is to maintain the level of security in subclass too.

11. If protected members are to be accessed from outside the class then__________
a) Members must be inherited publicly in subclass
b) Members must accessed using class pointers
c) Members must be accessed as usual
d) Members must be made public

Answer: d
Explanation: The members must be made public, otherwise it is not possible. In every case, the protected members will act as private members if it’s about access specifier. It will only be inherited, that too will lead to make those members protected again, in subclasses.

12. Which among the following can use protected access specifier?
a) Members which may be used in other packages
b) Members which have to be secure and should be used by other packages/subclass
c) Members which have to be accessed from anywhere in the program
d) Members which have to be as secure as private but can be used by main() function

Answer: b
Explanation: The members which have to be secure and might get used in other packages or subclasses can use protected access. This also allows the members to be safe from accidental modification.

13. Protected access is same as default access that is given implicitly in java if no specifier is mentioned.
a) True
b) False

Answer: b
Explanation: The statement given is true. The clear difference is protected members are available in other packages also, but the default members are available within the package only.

14. If a class have default constructor defined in private access, and one parameter constructor in protected mode, how will it be possible to create instance of object?
a) Define a constructor in public access with different signature
b) Directly create the object in the subclass
c) Directly create the object in main() function
d) Not possible

Answer: a
Explanation: If a new constructor is defined in public access. That will be available to the whole program. Only restriction will be of the way to use it.

15. What will be the output of the program given below?

 A

	Public  A a
	 
		ltlt” A”
        

A a
A b
A c

a) new A new A new A
b) newAnewAnewA
c) new Anew Anew A
d) new A new Anew A

Answer: c
Explanation: The constructor has a default argument. Whenever the object is created, the constructor will be called and print the message in its definition. Since the argument is default valued, it is not mandatory to pass anything to the new object.

This set of Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) focuses on “Data Members”.

1. What is the term used to indicate the variable and constants of a class?
a) Data members
b) Variables of class
c) Data characters
d) Constants

Answer: a
Explanation: The variables inside a class are termed data members of the class. It is not a mandatory rule but variables are used to refer to usual variables used in functions or globally. The term is given because the values stored in those variables represent some kind of data related to class.

2. Data members ________________ (C++)
a) Can be initialized with declaration in classes
b) Can be initialized only with help of constructors
c) Can be initialized either in declaration or by constructor
d) Can’t be initialized

Answer: b
Explanation: The data members are not property of class, those are property of the instances of the class. And the memory for the data members are not reserved until a constructor is called. Hence we use constructors for their initialization after the memory is reserved.

3. Which among the following is true for data members?
a) Private data members can be initialized with declaration in class
b) Static members are initialized in constructors
c) Protected data members can be initialized in class directly
d) Static data members are defined outside class, not in constructor

Answer: d
Explanation: Static members are not property of instances of classes. Those are shared by all the object of classes. Hence those are defined outside the constructor, so as to make them common for all the objects.

4. What should be done for data member to be of user defined structure type?
a) The structure must have been defined before class.
b) The structure must have been defined after the class definition
c) The structure must be predefined
d) The structure type data members can’t be used

Answer: a
Explanation: The structure must have been defined prior to its use. If the structure is not defined, then the memory space will not be allocated for its members. This leads to undefined use of new data types.

5. How many data members can a class contain?
a) 27
b) 255
c) 1024
d) As many as required

Answer: d
Explanation: Any class can have as many data members as required. The only restriction that may arise is when there is not enough memory space. This gives flexibility to define a class with best properties possible.

6. How to access data members of a class?
a) Dot operator
b) Arrow operator
c) Dot or arrow as required
d) Dot, arrow or direct call

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

7. To create a pointer to a private data member of a class, outside the class, which among the following is correct?
a) Return the address of the private data member using a member function
b) Access the private member using a pointer outside class
c) Declare the member as pointer inside the class
d) Not possible to create pointer to a private member

Answer: a
Explanation: We can call a public member function and return the address of any private data member. Though the pointer being returned must be defined inside class itself. And the returned address can be stored in a pointer.

8. Which among the following is true for use of setter() and getter() function?
a) Considered best for manipulating data values
b) Considered the only proper way to manipulate the values
c) Considered specially for private members manipulation
d) Considered a red flag, and not recommended for large scale use

Answer: d
Explanation: This concept of getter and setter functions is not acceptable if used too much. This is considered to be inappropriate in OOP perspective. Though it is commonly used, it doesn’t work according to OOP concepts at some higher level of understanding.

9. What is the output of following code?

 n		
 A

	   n
	   m
	A
	 
		n m
	
 disp

	”n”mn

a) 1050100
b) 1005010
c) n5010
d) n50100

Answer: d
Explanation: In cout we have specified n as a string to be printed. And m is a variable so its value gets printed. And global variable will not be used since local variable have more preference.

10. The static member functions can only use ________
a) Static data members
b) Private data members
c) Protected data members
d) Constant data members

Answer: a
Explanation: The static member functions can only access static data members. This is because the static member function can’t work with the properties that change object to object. It is mandatory that only the common properties of all the objects be used. And only static data members are common to all as those are property of class.
Answer: b
Explanation: The data members in a class can never refer to own class type. This is not possible because the data members should have some memory allocated for its object before the self-reference is used, but class must call constructor for that. Hence not possible.

12. What is the keyword used to make data members have same value?
a) static
b) const
c) double
d) abstract

Answer: b
Explanation: The keyword const can be used anywhere to make the variable have same value all the time. This restriction is made to use the same value whenever required. Also, this can restrict accidental changes.

13. Which data members can be inherited but are private to a class?
a) Private
b) Protected
c) Protected and Static
d) Privately inherited

Answer: b
Explanation: Static members inheritance also depends on the type of specifier they have. Only the protected members can be inherited but remain private to class. If static members are defined in private access, they won’t be allowed for inheritance.

14. The arguments passed to member functions by reference are considered as data members of class.
a) True
b) False

Answer: b
Explanation: This is a wrong statement. As only the data defined inside class is considered as its member. But even if a variable is passed by reference it would be the same variable that is outside the class. Hence it can’t be considered class member.

15. Which among the following is not allowed for data member declaration?
a) int a;
b) static int a;
c) abstract a;
d) Boolean a;

Answer: c
Explanation: The abstract keyword in the declaration of data members is not allowed. This is because the abstract keyword features can’t be used with the data members of the class. We can have all other syntax given, but not abstract.

This set of Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) focuses on “Local Class”.

1. What are local classes?
a) Classes declared inside a package
b) Classes declared inside a function
c) Classes declared inside a class
d) Classes declared inside structure

Answer: b
Explanation: The classes declared inside a package are available to all the functions and classes, hence can’t be called local. This is somewhat similar concept that we use to denote variables of a function. The classes declared inside functions will be local to them.

2. All member functions of a local class must be ___________
a) Defined outside class body
b) Defined outside the function definition
c) Defined inside the class body
d) Defined at starting of program

Answer: c
Explanation: There is a restriction on where the member functions of the local class should be define. Those must be defined inside the class body only. This is to reduce the ambiguity and complexity of program.

3. Can local class members access/use the general local variables (except static, abstract etc.) of the function in which it is defined?
a) Yes, it can access with arrow operator
b) No, it can’t access with dot operator
c) Yes, it can access using dot operator
d) No, it can’t access In anyway

Answer: d
Explanation: The local variables of the functions are not available to the member functions of the class. This is done to reduce the ambiguity in variables and their access rules.

4. Which type of data can a local class access from the function in which it is defined?
a) Static and extern
b) Abstract and static
c) Void and extern
d) Const and static

Answer: a
Explanation: The local classes have this feature to access the static and extern variables of the function in which those are defined. This feature is available since these type of data are common to the program and is created only one time. Run time creation and destruction of these variables is not done. The only restriction that may apply is those members must be constants.

5. Local classes can access the type names and enumerators defined by the enclosing function.
a) True
b) False

Answer: a
Explanation: This is a little tricky part with local classes. Though the local class can’t access the general variables of the function but can access the types that are defined inside the function. This is because the whole definition of that type would be existing inside the class.

6. Can static variables be declared inside a local class?
a) Yes, with public access specifier
b) Yes, anywhere as required
c) No, not possible in private access specifier
d) No, not possible anyway

Answer: d
Explanation: No, the static variables can’t be declared inside a local class. This is because each time the function is called, all the variables get created again and are destroyed as soon as the function is returned. This would have been possible id the static variable was of function.

7. All the member functions of local classes are __________ by default.
a) Static
b) Inline
c) Abstract
d) Virtual

Answer: c
Explanation: All the members are defined inside the class body. And when the member functions are defined inside the class body, they are made inline by default. If the definition is too complex, those are made normal functions.

8. The enclosing function has no special access to the members of the local class.
a) True
b) False

Answer: a
Explanation: This is a rule that the enclosing function doesn’t have any special access to the members of the local class. This is done to maintain the security of class members. And to adhere to the rules of OOP.

9. Which language can use inheritance with local classes?
a) Kotlin
b) Java
c) SmallTalk
d) SAP ABAP

Answer: d
Explanation: Other language might support inheritance with local classes but those doesn’t provide all the proper features of inheritance. Language SAP ABAP provides a way to implement inheritance with local classes efficiently.

10. How many local classes can be defined inside a single function?
a) Only 1
b) Only 3
c) Only 5
d) As many as required

Answer: d
Explanation: The local classes can be defined as required. There is no restriction on the number of local classes that can be defined inside a function. But all those classes must follow the rules and restrictions.

11. All the data members of local class must be ___________
a) Defined with declaration
b) Defined in constructor
c) Declared and defined in constructor
d) Declared using a member function

Answer: b
Explanation: The data members follow the same rules as of simple classes. Hence the data members must be declared first. Then their definition must be given using the constructors.
Answer: a
Explanation: The local classes can have same name if they belong to different functions. The classes would be local to those specific functions and hence can have same name. This is same as that of local variables concept.

13. What is the scope of local class?
a) Within the class only
b) Within the function
c) Within the program
d) One time creation and live till end of program

Answer: b
Explanation: The scope of a local class is limited only within the function definition. The function can use the class as usual as local variables. The class gets destroyed as soon as the function is returned.

14. Can a function, other than the enclosing function of local class, access the class members?
a) Yes, using object
b) Yes, using direct call
c) Yes, using pointer
d) No, can’t access

Answer: d
Explanation: The local classes are local to the specific enclosing function. Other functions can’t access the class. Even if the pointers are used, the class must be alive when the pointer is used. But this will not happen if the enclosing function is returned.

15. Which among the following is the main advantage of using local classes?
a) Make program more efficient
b) Makes program execution faster
c) Helps to add extra functionality to a function
d) Helps to add more members to a function

Answer: c
Explanation: The closest answer is to add more functionalities to a function or to make some specific functions to be generic. Adding more members to a function can be done directly but to add some special functionality that are encapsulated, can be done using local classes.

This set of Object Oriented Programming Interview Questions and Answers focuses on “Passing and Returning Object with Functions”.

1. In how many ways can an object be passed to a function?
a) 1
b) 2
c) 3
d) 4

Answer: c
Explanation: The objects can be passed in three ways. Pass by value, pass by reference and pass by address. These are the general ways to pass the objects to a function.

2. If an object is passed by value _____________
a) A new copy of object is created implicitly
b) The object itself is used
c) Address of the object is passed
d) A new object is created with new random values

Answer: a
Explanation: When an object is passed by value, a new object is created implicitly. This new object uses the implicit values assignment, same as that of the object being passed.

3. Pass by address passes the address of object _________ and pass by reference passes the address of the object _________
a) Explicitly, explicitly
b) Implicitly, implicitly
c) Explicitly, Implicitly
d) Implicitly, explicitly

Answer: c
Explanation: Pass by address uses the explicit address passing to the function whereas pass by reference implicitly passes the address of the object.

4. If an object is passed by reference, the changes made in the function ___________
a) Are reflected to the main object of caller function too
b) Are reflected only in local scope of the called function
c) Are reflected to the copy of the object that is made during pass
d) Are reflected to caller function object and called function object also

Answer: a
Explanation: When an object is passed by reference, its address is passed implicitly. This will make changes to the main function whenever any modification is done.

5. Constructor function is not called when an object is passed to a function, will its destructor be called when its copy is destroyed?
a) Yes, depending on code
b) Yes, must be called
c) No, since no constructor was called
d) No, since same object gets used

Answer: b
Explanation: Even though the constructor is not called when the object is passed to a function, the copy of the object is still created, where the values of the members are same. When the object have to be destroyed, the destructor is called to free the memory and resources that the object might have reserved.

6. When an object is returned by a function, a _______________ is automatically created to hold the return value.
a) Temporary object
b) Virtual object
c) New object
d) Data member

Answer: a
Explanation: The temporary object is created. It holds the return value. The values gets assigned as required, and the temporary object gets destroyed.

7. Is the destruction of temporary object safe (while returning object)?
a) Yes, the resources get free to use
b) Yes, other objects can use the memory space
c) No, unexpected side effects may occur
d) No, always gives rise to exceptions

Answer: c
Explanation: The destruction of temporary variable may give rise to unexpected logical errors. Consider the destructor which may free the dynamically allocated memory. But this may abort the program if another is still trying to copy the values from that dynamic memory.

8. How to overcome the problem arising due to destruction of temporary object?
a) Overloading insertion operator
b) Overriding functions can be used
c) Overloading parenthesis or returning object
d) Overloading assignment operator and defining copy constructor

Answer: d
Explanation: The problem can be solved by overloading the assignment operator to get the values that might be getting returned while the destructor free the dynamic memory. Defining copy constructor can help us to do this in even simpler way.

9. How many objects can be returned at once?
a) Only 1
b) Only 2
c) Only 16
d) As many as required

Answer: a
Explanation: Like any other value, only one object can be returned at ones. The only possible way to return more than one object is to return address of an object array. But that again comes under returning object pointer.

10. What will be the output of the following code?

Class A
 
	 i 
	  A n
	 
		in ltlt”inside constructor ”
	 
	~A
	
		ltlt”destroying  ”ltlti
	
	 seti n
	
		in
	
	 geti
	
		 I
	

 tA ob
 
	ltlt”something ”

 main

	A a
	ta
	ltlt” is i in main ”
	ltlta.

a) inside constructor something destroying 2this is i in main destroying 1
b) inside constructor something this is i in main destroying 1
c) inside constructor something destroying 2this is i in main
d) something destroying 2this is i in main destroying 1

Answer: a
Explanation: Although the object constructor is called only ones, the destructor will be called twice, because of destroying the copy of the object that is temporarily created. This is the concept of how the object should be passed and manipulated.
Answer: c
Explanation: Having the address being passed to the function, the changes are automatically made to the main function. In all the cases if the address is being used, the same memory location will be updated with new values.

12. How many objects can be passed to a function simultaneously?
a) Only 1
b) Only an array
c) Only 1 or an array
d) As many as required

Answer: d
Explanation: There is no limit to how many objects can be passed. This works in same way as that any other variable gets passed. Array and object can be passed at same time also.

13. If an object is passed by address, will be constructor be called?
a) Yes, to allocate the memory
b) Yes, to initialize the members
c) No, values are copied
d) No, temporary object is created

Answer: c
Explanation: A copy of all the values is created. If the constructor is called, there will be a compile time error or memory shortage. This happens because each time a constructor is called, it try to call itself again and that goes infinite times.

14. Is it possible that an object of is passed to a function, and the function also have an object of same name?
a) No, Duplicate declaration is not allowed
b) No, 2 objects will be created
c) Yes, Scopes are different
d) Yes, life span is different

Answer: a
Explanation: There can’t be more than one variable or object with the same name in same scope. The scope is same, since the object is passed, it becomes local to function and hence function can’t have one more object of same name.

15. Passing an object using copy constructor and pass by value are same.
a) True
b) False

Answer: b
Explanation: The copy constructor is used to copy the values from one object to other. Pass by values is not same as copy constructor method. Actually the pass by value method uses a copy constructor to copy the values in a local object.

This set of Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) focuses on “Object Array”.

1. What is an array of objects?
a) An array of instances of class represented by single name
b) An array of instances of class represented by more than one name
c) An array of instances which have more than 2 instances
d) An array of instances which have different types

Answer: a
Explanation: The array of objects an array of instances of a class. The array is represented by a single name. The array name is itself a pointer. Array name represents the first object.

2. Which among the following is a mandatory condition for array of objects?
a) All the objects should be of different class
b) All the objects should be of same program classes
c) All the objects should be of same class
d) All the objects should have different data

Answer: c
Explanation: The objects of an array must be of same class. This is mandatory because array is set of same type of elements. The objects of same class are considered to be of same type.

3. What is the type of elements of array of objects?
a) Class
b) Void
c) String
d) Null

Answer: a
Explanation: The class itself is the type of elements of array of objects. All the objects possess the same properties. Like any other primitive data type, the objects are of their respective class type.

4. If array of objects is declared as given below, which is the limitation on objects?

Class_name arrayName[size];

a) The objects will have same values
b) The objects will not be initialized individually
c) The objects can never be initialized
d) The objects will have same data

Answer: b
Explanation: If the syntax given, is used to declare the array of objects, then the objects can’t be initialized individually. All the objects will have to be initialized after this declaration.

5. Which is the condition that must be followed if the array of objects is declared without initialization, only with size of array?
a) The class should have separate constructor for each object
b) The class must have no constructors
c) The class should not have any member function
d) The class must have a default or zero argument constructor

Answer: d
Explanation: The class must have a default/zero argument constructor. Since the declaration is done by only specifying the size of array, the class must have default a construct to be called by default to reserve memory for each object. Also, we can’t specify the arguments in this type of declaration hence the class should provide a default initialization.

6. When are the array of objects without any initialization useful?
a) When object data is not required just after the declaration
b) When initialization of object data is to be made by the compiler
c) When object data doesn’t matter in the program
d) When the object should contain garbage data

Answer: a
Explanation: Sometimes the object data is not mandatory to be used just after the declaration or may be the program requires the data to be updated according to what user inputs. Hence only declaration us also useful.

7. If constructor arguments are passed to objects of array then ____________ if the constructors are overloaded.
a) It is mandatory to pass same number of arguments to all the objects
b) It is mandatory to pass same type of arguments to all the objects
c) It is not mandatory to call same constructor for all the objects
d) It is mandatory to call same constructor for all the constructors

Answer: c
Explanation: It is not mandatory to call the same constructor for all the objects in an array if initialized with the declaration. The objects can be passed with different set of arguments in the same syntax, separated by commas.

8. How the objects of array can be denoted?
a) Indices
b) Name
c) Random numbers
d) Alphabets

Answer: a
Explanation: Different objects in an array can be denoted with the indices of array. The first object is denoted by 0. And the further indices denote the next objects in sequence of array.

9. The objects in an object array _______________________
a) Can be created without use of constructor
b) Can be created without calling default constructor
c) Can’t be created with use of constructor
d) Can’t be created without calling default constructor

Answer: b
Explanation: The objects need some constructor to get the memory spaced reserved for those. If the default constructor is not used then we can use some arguments constructor which will reserve the memory for the objects. The objects can be passed with constructor arguments during declaration.

10. The Object array is created in _____________________
a) Heap memory
b) Stack memory
c) HDD
d) ROM

Answer: a
Explanation: If the object arrays are declared dynamically, then the memory will be reserved on heap. The memory for objects will be on stack only if some constructor or some call and return tasks are happening. The program doesn’t run on HDD and ROM is not used for the execution of programs.
Answer: a
Explanation: The array name with the index of fifth element is called, i.e. index 4. Then the dot operator is used to access the data member of that object. This Allows us to access the data members of all the objects in an object array.

12. Can we have two dimensional object array?
a) Yes, always
b) Yes, only if primitive type array
c) No, since two indices are impossible
d) No, never

Answer: a
Explanation: A two dimensional array can always be created. There is no rule that only primitive type objects can have more than one dimension. The object array can also be made 2 dimensional.

13. From which index does the array of objects start?
a) 0
b) 1
c) 2
d) 3

Answer: a
Explanation: The index must start from 0. The index ends at size – 1 index. This is because the index is always till n-1 where n is the total number of beads.

14. Two dimensional array can’t be initialized with the declaration.
a) True
b) False

Answer: b
Explanation: The two dimensional arrays can also be initialized using curly brackets. For each set, values in curly bracket. And then another bracket is added at first and end. This ensures that all the code belongs to the user.

15. Is an array of characters always a string?
a) Yes, always
b) Yes, if each character is terminated by null
c) No, since each character is terminated by null
d) No, never

Answer: d
Explanation: The character arrays are not the same as string. The string once created then remains the same. The character array values may change.

This set of Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) focuses on “Abstract Class”.

1. Which among the following best describes abstract classes?
a) If a class has more than one virtual function, it’s abstract class
b) If a class have only one pure virtual function, it’s abstract class
c) If a class has at least one pure virtual function, it’s abstract class
d) If a class has all the pure virtual functions only, then it’s abstract class

Answer: c
Explanation: The condition for a class to be called abstract class is that it must have at least one pure virtual function. The keyword abstract must be used while defining abstract class in java.

2. Can abstract class have main() function defined inside it?
a) Yes, depending on return type of main()
b) Yes, always
c) No, main must not be defined inside abstract class
d) No, because main() is not abstract function

Answer: b
Explanation: This is a property of abstract class. It can define main() function inside it. There is no restriction on its definition and implementation.

3. If there is an abstract method in a class then, ________________
a) Class must be abstract class
b) Class may or may not be abstract class
c) Class is generic
d) Class must be public

Answer: a
Explanation: It is a rule that if a class have even one abstract method, it must be an abstract class. If this rule was not made, the abstract methods would have got skipped to get defined in some places which are undesirable with the idea of abstract class.

4. If a class is extending/inheriting another abstract class having abstract method, then _______________________
a) Either implementation of method or making class abstract is mandatory
b) Implementation of the method in derived class is mandatory
c) Making the derived class also abstract is mandatory
d) It’s not mandatory to implement the abstract method of parent class

Answer: a
Explanation: Either of the two things must be done, either implementation or declaration of class as abstract. This is done to ensure that the method intended to be defined by other classes gets defined at every possible class.

5. Abstract class A has 4 virtual functions. Abstract class B defines only 2 of those member functions as it extends class A. Class C extends class B and implements the other two member functions of class A. Choose the correct option below.
a) Program won’t run as all the methods are not defined by B
b) Program won’t run as C is not inheriting A directly
c) Program won’t run as multiple inheritance is used
d) Program runs correctly

Answer: d
Explanation: The program runs correctly. This is because even class B is abstract so it’s not mandatory to define all the virtual functions. Class C is not abstract but all the virtual functions have been implemented will that class.

6. Abstract classes can ____________________ instances.
a) Never have
b) Always have
c) Have array of
d) Have pointer of

Answer: a
Explanation: When an abstract class is defined, it won’t be having the implementation of at least one function. This will restrict the class to have any constructor. When the class doesn’t have constructor, there won’t be any instance of that class.

7. We ___________________ to an abstract class.
a) Can create pointers
b) Can create references
c) Can create pointers or references
d) Can’t create any reference, pointer or instance

Answer: c
Explanation: Even though there can’t be any instance of abstract class. We can always create pointer or reference to abstract class. The member functions which have some implementation inside abstract itself can be used with these references.

8. Which among the following is an important use of abstract classes?
a) Header files
b) Class Libraries
c) Class definitions
d) Class inheritance

Answer: b
Explanation: The abstract classes can be used to create a generic, extensible class library that can be used by other programmers. This helps us to get some already implemented codes and functions that might have not been provided by the programming language itself.

9. Use of pointers or reference to an abstract class gives rise to which among the following feature?
a) Static Polymorphism
b) Runtime polymorphism
c) Compile time Polymorphism
d) Polymorphism within methods

Answer: b
Explanation: The runtime polymorphism is supported by reference and pointer to an abstract class. This relies upon base class pointer and reference to select the proper virtual function.

10. The abstract classes in java can _________________
a) Implement constructors
b) Can’t implement constructor
c) Can implement only unimplemented methods
d) Can’t implement any type of constructor

Answer: a
Explanation: The abstract classes in java can define a constructor. Even though instance can’t be created. But in this way, only during constructor chaining, constructor can be called. When instance of concrete implementation class is created, it’s known as constructor chaining.

11. Abstract class can’t be final in java.
a) True
b) False

Answer: a
Explanation: If an abstract class is made final in java, it will stop the abstract class from being extended. And if the class is not getting extended, there won’t be another class to implement the virtual functions. Due to this contradicting fact, it can’t be final in java.
Answer: a
Explanation: There is no restriction on declaring static methods. The only condition is that the virtual functions must have some definition in the program.

13. It is _________________________ to have an abstract method.
a) Not mandatory for an static class
b) Not mandatory for a derived class
c) Not mandatory for an abstract class
d) Not mandatory for parent class

Answer: c
Explanation: Derived, parent and static classes can’t have abstract method (We can’t say what type of these classes is). And for abstract class it’s not mandatory to have abstract method. But if any abstract method is there inside a class, then class must be abstract type.

14. How many abstract classes can a single program contain?
a) At most 1
b) At least 1
c) At most 127
d) As many as required

Answer: d
Explanation: There is no restriction on the number of abstract classes that can be defined inside a single program. The programs can use as many abstract classes as required. But the functions with no body must be implemented.

15. Is it necessary that all the abstract methods must be defined from an abstract class?
a) Yes, depending on code
b) Yes, always
c) No, never
d) No, if function is not used, no definition is required

Answer: b
Explanation: That is the rule of programming language that each function declared, must have some definition. There can’t be some abstract method that remains undefined. Even if it’s there, it would result in compile time error.

This set of Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) focuses on “Derived Class”.

1. Which among the following is best definition of a derived class?
a) A child class
b) A class which inherits one or more classes
c) A class with keyword derived
d) A class with more than one constructor

Answer: b
Explanation: Any class which inherits one or more classes is a derived class. The only condition is it must inherit at least one class in order to be called as a derived class.

2. Which among the following is inherited by a derived class from base class?
a) Data members only
b) Member functions only
c) All the members except private members
d) All the members of base class

Answer: c
Explanation: The class inheriting another class, inherits all the data members and member functions that are not private. This is done to ensure the security features with maximum flexibility.

3. If there is a derived class in a program, how many classes must be in that program?
a) 1
b) 2
c) 3
d) 4

Answer: b
Explanation: If there is a derived class in a program, there must be at least 2 classes in that program. One is a base class and another derived class. Hence at least 2 classes must be there.

4. Which members can never be accessed in derived class from the base class?
a) Private
b) Protected
c) Public
d) All except private

Answer: d
Explanation: There is no restriction for a derived class to access the members of the base class until and unless the members are private. Private member are declared so that those members are not accessible outside the class.

5. How many types of inheritance are supported in C++ for deriving a class?
a) 1
b) 2
c) 3
d) 4

Answer: c
Explanation: There are three types of inheritance possible. Private inheritance, protected inheritance, and public inheritance. The inheritance defines the access specifier to be used with the inherited members in the derived class.

6. How many derived class can a single base class have?
a) 1
b) 2
c) 3
d) As many are required

Answer: d
Explanation: There is no restriction on how many classes can inherit a single base class. Hence there can be as many derived classes as required in a program from a single base class.

7. Which among the following is correct?
a) Friend function of derived class can access non-private members of base class
b) Friend function of base class can access derived class members
c) Friend function of derived class can access members of only derived class
d) Friend function can access private members of base class of a derived class

Answer: a
Explanation: The friend function of a class can access the non-private members of base class. The reason behind is that the members of base class gets derived into the derived class and hence become members of derived class too. Hence a friend function can access all of those.

8. If a class is being derived using more than two base classes, which inheritance will be used?
a) Single
b) Multi-level
c) Hierarchical
d) Multiple

Answer: d
Explanation: The statement given is the definition of multiple inheritance with respect to the derived class. The concept can be illustrated with many other samples but the main aspects are base class and derived class only.

9. Derived class is also known as ______________ class.
a) Subclass
b) Small class
c) Big class
d) Noticeable class

Answer: a
Explanation: It is just another name given to the derived classes. This is used while denoting all the derived classes subsequent to a superclass.

10. If class A is derived from another derived class B which is derived from class C, which class will have maximum level of abstraction?
a) Class A
b) Class B
c) Class C
d) All have the same level of abstraction

Answer: c
Explanation: The abstraction level of class C will be maximum. This is because the parent class have higher level of abstraction. Hence the parent of all other class will have maximum level of abstraction.

11. If base class is an abstract class then derived class ______________ the undefined functions.
a) Must define
b) Must become another abstract class or define
c) Must become parent class for
d) Must implement 2 definitions of

Answer: b
Explanation: The function must be defined in the program which are not defined in the base class. Hence the class must be defined as abstract of implement the function definition in it.
Answer: d
Explanation: When a class is to be derived from another derived class, the derived class behaves as a normal base class hence there are no restriction on how many class can be derived from a derived class. The derived class again behaves as a normal superclass.

13. The members of a derived class can never be derived.
a) True
b) False

Answer: b
Explanation: This is not true that the members of a derived class can’t be derived. All the classes are considered to be a normal class when used for derivation. The members can be derived with respect to their access specifiers.

14. Which feature is not related to the derived classes among the following?
a) Inheritance
b) Encapsulation
c) Run time memory management
d) Compile time function references

Answer: c
Explanation: The memory management is the feature that is not necessary for derived classes that will be a part of whole program. The functions references must be resolved for their proper use if inheritance is used.

15. Deriving a class in such a way that that the base class members are not available for further inheritance is known as ___________________
a) Public inheritance
b) Protected inheritance
c) Protected or private inheritance
d) Private inheritance

Answer: d
Explanation: The private members of a class can never be derived to another class. When a class derives another class using private inheritance, all the members become private members of the derived class. Hence these member won’t be available for further inheritance.

This set of Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) focuses on “Inheritance”.

1. Which among the following best describes the Inheritance?
a) Copying the code already written
b) Using the code already written once
c) Using already defined functions in programming language
d) Using the data and functions into derived segment

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

2. How many basic types of inheritance are provided as OOP feature?
a) 4
b) 3
c) 2
d) 1

Answer: a
Explanation: There are basically 4 types of inheritance provided in OOP, namely, single level, multilevel, multiple and hierarchical inheritance. We can add one more type as Hybrid inheritance but that is actually the combination any types of inheritance from the 4 basic ones.

3. Which among the following best defines single level inheritance?
a) A class inheriting a derived class
b) A class inheriting a base class
c) A class inheriting a nested class
d) A class which gets inherited by 2 classes

Answer: b
Explanation: A class inheriting a base class defines single level inheritance. Inheriting an already derived class makes it multilevel inheritance. And if base class is inherited by 2 other classes, it is multiple inheritance.

4. Which among the following is correct for multiple inheritance?
a) class student{public: int marks;}s; class stream{int total;}; class topper:public student, public stream{ };
b) class student{int marks;}; class stream{ }; class topper: public student{ };
c) class student{int marks;}; class stream:public student{ };
d) class student{ }; class stream{ }; class topper{ };

Answer: a
Explanation: Class topper is getting derived from 2 other classes and hence it is multiple inheritance. Topper inherits class stream and class student publicly and hence can use its features. If only few classes are defined, there we are not even using inheritance (as in option class student{ }; class stream{ }; class topper{ };).

5. Which programming language doesn’t support multiple inheritance?
a) C++ and Java
b) C and C++
c) Java and SmallTalk
d) Java

Answer: d
Explanation: Java doesn’t support multiple inheritance. But that feature can be implemented by using the interfaces concept. Multiple inheritance is not supported because of diamond problem and similar issues.

6. Which among the following is correct for a hierarchical inheritance?
a) Two base classes can be used to be derived into one single class
b) Two or more classes can be derived into one class
c) One base class can be derived into other two derived classes or more
d) One base class can be derived into only 2 classes

Answer: c
Explanation: One base class can be derived into the other two derived classes or more. If only one class gets derived by only 2 other classes, it is also hierarchical inheritance, but it is not a mandatory condition, because any number of derived classes can be there.

7. Which is the correct syntax of inheritance?
a) class derived_classname : base_classname{ /*define class body*/ };
b) class base_classname : derived_classname{ /*define class body*/ };
c) class derived_classname : access base_classname{ /*define class body*/ };
d) class base_classname :access derived_classname{ /*define class body*/ };

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

8. Which type of inheritance leads to diamond problem?
a) Single level
b) Multi-level
c) Multiple
d) Hierarchical

Answer: c
Explanation: When 2 or more classes inherit the same class using multiple inheritance and then one more class inherits those two base classes, we get a diamond like structure. Here, ambiguity arises when same function gets derived into 2 base classes and finally to 3rd level class because same name functions are being inherited.

9. Which access type data gets derived as private member in derived class?
a) Private
b) Public
c) Protected
d) Protected and Private

Answer: a
Explanation: It is a rule, that when a derived class inherits the base class in private access mode, all the members of base class gets derived as private members of the derived class.

10. If a base class is inherited in protected access mode then which among the following is true?
a) Public and Protected members of base class becomes protected members of derived class
b) Only protected members become protected members of derived class
c) Private, Protected and Public all members of base, become private of derived class
d) Only private members of base, become private of derived class

Answer: a
Explanation: As the programming language rules apply, all the public and protected members of base class becomes protected members of derived class in protected access mode. It can’t be changed because it would hinder the security of data and may add vulnerability in the program.

11. Members which are not intended to be inherited are declared as ________________
a) Public members
b) Protected members
c) Private members
d) Private or Protected members

Answer: c
Explanation: Private access specifier is the most secure access mode. It doesn’t allow members to be inherited. Even Private inheritance can only inherit protected and public members.
Answer: c
Explanation: If the access mode is not specified during inheritance, the class is inherited privately by default. This is to ensure the security of data and to maintain OOP features. Hence it is not mandatory to specify the access mode if we want the class to be inherited privately.

13. If a derived class object is created, which constructor is called first?
a) Base class constructor
b) Derived class constructor
c) Depends on how we call the object
d) Not possible

Answer: a
Explanation: First the base class constructor is invoked. When we create a derived class object, the system tries to invoke its constructor but the class is derived so first the base class must be initialized, hence in turn the base class constructor is invoked before the derived class constructor.

14. The private members of the base class are visible in derived class but are not accessible directly.
a) True
b) False

Answer: a
Explanation: Consider that a variable is private in base class and the derived class uses public inheritance to inherit that class. Now if we also have a global variable of same name as that of base class private variable, neither the global variable nor the base class private variable will be accessible from derived class. This is because we can’t have 2 variables with same name in same local scope. Hence the private members are accessible but not directly.

15. How can you make the private members inheritable?
a) By making their visibility mode as public only
b) By making their visibility mode as protected only
c) By making their visibility mode as private in derived class
d) It can be done both by making the visibility mode public or protected

Answer: d
Explanation: It is not mandatory that you have to make the visibility mode either public or protected. You can do either of those. That will give you permission to inherit the private members of base class.

This set of Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) focuses on “Multiple Inheritance”.

1. Multiple inheritance is ____________________
a) When a class is derived from another class
b) When a class is derived from two or more classes
c) When a class is derived from other two derived classes
d) When a class is derived from exactly one class

Answer: b
Explanation: The multiple inheritance is used when a class is being derived using two base classes or more. This way a single class can have features of more than one classes inherited into a single unit. This lets us combine two class members into a single class.

2. Which problem arises due to multiple inheritance, if hierarchical inheritance is used previously for its base classes?
a) Diamond
b) Circle
c) Triangle
d) Loop

Answer: a
Explanation: The diamond problem arises when multiple inheritance is used. This problem arises because the same name member functions get derived into a single class. Which in turn creates ambiguity in calling those methods.

3. How many classes should a program contain to implement the multiple inheritance?
a) Only 1
b) At least 1
c) At least 3
d) Exactly 3

Answer: c
Explanation: For the implementation of multiple inheritance, there must be at least 3 classes in a program. At least 2 base classes and one class to inherit those two classes. If lesser, it becomes single level inheritance.

4. Which programming language restricts the use of multiple inheritance?
a) C++
b) PHP
c) SmallTalk
d) Java

Answer: d
Explanation: Java doesn’t allow use of multiple inheritance with classes. But this can be done by using the interfaces. This is more secure and unambiguous way to implement multiple inheritance.

5. Is it possible to have all the abstract classes as base classes of a derived class from those?
a) Yes, always
b) Yes, only if derived class implements all the methods
c) No, because abstract classes doesn’t have constructors
d) No, never

Answer: b
Explanation: The condition for abstract class applies same here too. All the undefined functions must be defined. Hence all the base classes can be abstract but derived class must implement all those undefined functions.

6. If class A inherits class B and class C as “class A: public class B, public class C {// class body ;}; ”, which class constructor will be called first?
a) Class A
b) Class B
c) Class C
d) All together

Answer: b
Explanation: The constructors of parent class will be called first. In that, the constructor of the classes will be called in the same sequence as that mentioned in class definition inheritance. Since class B is mentioned first for inheritance, its constructor will be called first.

7. Why does diamond problem arise due to multiple inheritance?
a) Methods with same name creates ambiguity and conflict
b) Methods inherited from the superclass may conflict
c) Derived class gets overloaded with more than two class methods
d) Derived class can’t distinguish the owner class of any derived method

Answer: a
Explanation: All the derived classes can distinguish the base class members, but if a method is being inherited to the base classes from another class which again gets inherited into same class (diamond shape), that may create conflict in using the function from two available.

8. How many base classes can a derived class have which is implementing multiple inheritance?
a) Only 2
b) At least 2
c) At most 2
d) As many as required

Answer: d
Explanation: The classes can derive from as many classes as required since the multiple inheritance feature is made to combine or group together the functions that are from different classes. This make the derived class stronger in terms of its flexibility.

9. How to overcome diamond problem?
a) Using alias name
b) Using seperate derived class
c) Using virtual keyword with same name function
d) Can’t be done

Answer: c
Explanation: To overcome the ambiguity and conflict we can use keyword virtual. This will help us to differentiate the functions with same name that came to last derived class in diamond problem.

10. When multiple inheritance is used, which class object should be used in order to access all the available members of parent and derived class?
a) Derived class object
b) Parent class objects
c) Use Abstract derived class
d) Derive a class from derived class

Answer: a
Explanation: The derived class object can access all of its own members. It can also access the available members of the parent classes, because the members are derived into the derived class.

11. If all the members of all the base classes are private then _____________
a) There won’t be any use of multiple inheritance
b) It will make those members public
c) Derived class can still access them in multiple inheritance
d) Compile time error

Answer: a
Explanation: The derived class will not be able to access any members of the base classes. Since private member’s are not inheritable. It leads to no use of multiple inheritance.
Answer: b
Explanation: The constructors must be defined in every class. If class is abstract, it won’t have any constructor but other classes must have constructor. Either implicit or explicit.

13. If a class contains 2 nested class and is being inherited by another class, will there be any multiple inheritance?
a) No, only single level inheritance is used
b) No, only multilevel inheritance is used
c) Yes, because 3 classes are involved
d) Yes, because more than 1 classes are being derived

Answer: a
Explanation: When a class having nested classes is being derived into another class. It indirectly means a simple class is being inherited to another class. This is single level inheritance.

14. Which members can’t be accessed in derived class in multiple inheritance?
a) Private members of base
b) Public members of base
c) Protected members of base
d) All the members of base

Answer: a
Explanation: The private member’s are available for only the class containing those members. Derived classes will have access to protected and public members only.

15. Can the derived class be made abstract if multiple inheritance is used?
a) No, because other classes must be abstract too
b) Yes, if all the functions are implemented
c) Yes, if all the methods are predefined
d) No, since constructors won’t be there

Answer: d
Explanation: The derived class must not be abstract. This is because the abstract classes doesn’t have constructor and hence we won’t be having the capability to have instances. This will restrict the use of multiple inheritance.

This set of Object Oriented Programming Questions and Answers for Entrance exams focuses on “Virtual Functions”.

1. Virtual function is ______ class function which expected to be redefined in ______ class, so that when reference is made to derived class object using pointer then we can call virtual function to execute ________ class definition version.
a) Base, derived, derived
b) Derived, Derived, Derived
c) Base, derived, base
d) Base, base, derived

Answer: a
Explanation: The functions which may give rise to ambiguity due to inheritance, can be declared virtual. So that whenever derived class object is referred using pointer or reference to the base class methods, we can still call the derived class methods using virtual function. Hence this differentiates those methods from each other.

2. What does a virtual function ensure for an object, among the following?
a) Correct method is called, regardless of the class defining it
b) Correct method is called, regardless of the object being called
c) Correct method is called, regardless of the type of reference used for function call
d) Correct method is called, regardless of the type of function being called by objects

Answer: c
Explanation: It is property of the virtual function and one of their main use. Its use ensure that the correct method is called even though it is been called from different pointer or references. This also decreases chance of mistakes in program.

3. Virtual functions are mainly used to achieve _____________
a) Compile time polymorphism
b) Interpreter polymorphism
c) Runtime polymorphism
d) Functions code polymorphism

Answer: c
Explanation: It is used to achieve runtime polymorphism. The functions which are inherited and overridden, so at runtime the correct function is executed. The correct function call is made from the intended class.

4. Which keyword is used to declare virtual functions?
a) virtual
b) virt
c) anonymous
d) virtually

Answer: a
Explanation: The virtual keyword is used to declare virtual functions. Anonymous keyword is used with classes and have a different meaning. The virtual functions are used to call the intended function of the derived class.

5. Where the virtual function should be defined?
a) Twice in base class
b) Derived class
c) Base class and derived class
d) Base class

Answer: d
Explanation: The virtual function should be declared in base class. So that when the derived class inherits from the base class, the functions can be differentiated from the one in base class and another in derived class.

6. The resolving of virtual functions is done at ______________
a) Compile time
b) Interpret time
c) Runtime
d) Writing source code

Answer: c
Explanation: The resolving of virtual functions that are to be called is done at run time. The base class and the derived classes may contain different definitions and different variables, so all these things are resolved at run time and decided which function is to be called.

7. In which access specifier should a virtual function be defined?
a) Private
b) Public
c) Protected
d) Default

Answer: b
Explanation: The virtual functions must be defined in public section of a class. This is to ensure that the virtual function is available everywhere in the program. Also to avoid any error while resolving the method.

8. Virtual functions can never be made _______________
a) Static function
b) Parameterized function
c) Default argument function
d) Zero parameter function

Answer: a
Explanation: The virtual function must not be static. Those functions are the property of individual objects and not of a class as a whole. The functions should not be made common for all the objects of that class.

9. Virtual functions can’t be made friend function of other classes.
a) True
b) False

Answer: a
Explanation: The friend functions can access the private members also. This may hinder the security of class members. This is why the functions should not be made friend functions of other class.

10. Which is a must condition for virtual function to achieve runtime polymorphism?
a) Virtual function must be accessed with direct name
b) Virtual functions must be accessed using base class object
c) Virtual function must be accessed using pointer or reference
d) Virtual function must be accessed using derived class object only

Answer: c
Explanation: The virtual functions must be called using pointer or reference. This is mandatory so that the intended function gets executed while resolving the method at runtime. The must not be any ambiguity between the method of parent class and derived class.

11. Which among the following is true for virtual functions?
a) Prototype must be different in base and derived class
b) Prototype must be same in base class and derived class
c) Prototype must be given only in base class
d) Prototype must have different signature in base and derived class

Answer: b
Explanation: The prototype must be the same. Because the function is to be overridden in the derived class. If the function prototype is different in derived class then it will not override the base class function and hence virtual function concept won’t work here.
Answer: d
Explanation: The virtual functions must be declared and defined in base class. The functions can be redefined in derived class. If redefined in derived class then it overrides the base class function definition.

13. It is __________ to redefine the virtual function in derived class.
a) Necessary
b) Not necessary
c) Not acceptable
d) Good practice

Answer: b
Explanation: It is not necessary to redefine the virtual function in the derived class. If not defined, the base class function definition is used but if defined, the intended definition is used according to need. It is not about good coding practice as it should be redefined only if needed.

14. Which among the following is true?
a) A class may have virtual destructor but not virtual constructor
b) A class may have virtual constructor but not virtual destructor
c) A class may have virtual constructor and virtual constructor
d) A class may have either virtual destructor or virtual constructor

Answer: a
Explanation: Any class can contain virtual destructor. But is not possible to define a virtual constructor. The reason behind is that the destructors can be overridden but constructors should not be.

15. If virtual function of base class is redefined in derived class then ________________
a) It must be declared virtual in derived class also
b) It may or may not be declared virtual in derived class
c) It can must not be declared virtual in derived class
d) It must be declared normally in derived class

Answer: b
Explanation: The virtual functions may or may not be declared virtual in derived class. This is because if the overriding function defined in derived class is not declared virtual explicitly, the compiler makes it virtual implicitly.

This set of Object Oriented Programming Interview Questions and Answers for freshers focuses on “Overriding Member Functions”.

1. Which among the following best describes member function overriding?
a) Member functions having same name in base and derived classes
b) Member functions having same name in base class only
c) Member functions having same name in derived class only
d) Member functions having same name and different signature inside main function

Answer: a
Explanation: The member function which is defined in base class and again in the derived class, is overridden by the definition given in the derived class. This is because the preference is given more to the local members. When derived class object calls that function, definition from the derived class is used.

2. Which among the following is true?
a) Inheritance must not be using when overriding is used
b) Overriding can be implemented without using inheritance
c) Inheritance must be done, to use overriding are overridden
d) Inheritance is mandatory only if more than one functions

Answer: c
Explanation: The inheritance must be used in order to use function overriding. If inheritance is not used, the functions can only be overloaded. There must be a base class and a derived class to override the function of base class.

3. Which is the correct condition for function overriding?
a) The declaration must not be same in base and derived class
b) The declaration must be exactly the same in base and derived class
c) The declaration should have at least 1 same argument in declaration of base and derived class
d) The declaration should have at least 1 different argument in declaration of base and derived class

Answer: b
Explanation: For a function to be over ridden, the declaration must be exactly the same. There must not be any different syntax used. This will ensure that the function to be overridden is only the one intended from to be overridden from the derived class.

4. Exactly same declaration in base and derived class includes______________
a) Only same name
b) Only same return type and name
c) Only same return type and argument list
d) All the same return type, name and parameter list

Answer: d
Explanation: Declaration includes the whole prototype of the function. The return type name and the parameter list must be same in order to confirm that the function is same in derived and the base class. And hence can be overridden.

5. Which among function will be overridden from the function defined in derived class below:

 A

	 i
	 show
	 
		ltlti 
	
	 print
	 
		 ltlti 
	

 B

	 j
	 show
	 
		ltltj 
	

a) show()
b) print()
c) show() and print()
d) Compile time error

Answer: a
Explanation: The declaration must be exactly same in the derived class and base class. The derived class have defined show() function with exactly same declaration. This then shows that the function in base class is being overridden if show() is called from the object of class B.

6. How to access the overridden method of base class from the derived class?
a) Using arrow operator
b) Using dot operator
c) Using scope resolution operator
d) Can’t be accessed once overridden

Answer: c
Explanation: Scope resolution operator :: can be used to access the base class method even if overridden. To access those, first base class name should be written followed by the scope resolution operator and then the method name.

7. The functions to be overridden _____________
a) Must be private in base class
b) Must not be private base class
c) Must be private in both derived and base class
d) Must not be private in both derived and base class

Answer: b
Explanation: If the function is private in the base class, derived class won’t be able to access it. When the derived class can’t access the function to be overridden then it won’t be able to override it with any definition.

8. Which language doesn’t support the method overriding implicitly?
a) C++
b) C#
c) Java
d) SmallTalk

Answer: b
Explanation: The feature of method overriding is not provided in C#. To override the methods, one must use override or virtual keywords explicitly. This is done to remove accidental changes in program and unintentional overriding.

9. In C# ____________________
a) Non – virtual or static methods can’t be overridden
b) Non – virtual and static methods only can be overridden
c) Overriding is not allowed
d) Overriding must be implemented using C++ code only

Answer: a
Explanation: The non-virtual and static methods can’t be overridden in C# language. The restriction is made from the language implicitly. Only the methods that are abstract, virtual or override can be overridden.

10. In Delphi ______________
a) Method overriding is done implicitly
b) Method overriding is not supported
c) Method overriding is done with directive override
d) Method overriding is done with the directive virtually

Answer: c
Explanation: This is possible but only if the method to be overridden is marked as dynamic or virtual. It is inbuilt restriction of programming language. This is done to reduce the accidental or unintentional overriding.
Answer: a
Explanation: The keyword super must be used to access base class members. Even when overriding is used, super must be used with the dot operator. The overriding is possible.

12. In Kotlin, the function to be overridden must be ______________
a) Private
b) Open
c) Closed
d) Abstract

Answer: b
Explanation: The function to be overridden must be open. This is a condition in Kotlin for any function to be overridden. This avoids accidental overriding.

13. Abstract functions of a base class _________________
a) Are overridden by the definition in same class
b) Are overridden by the definition in parent class
c) Are not overridden generally
d) Are overridden by the definition in derived class

Answer: d
Explanation: The functions declared to be abstract in base class are redefined in derived classes. That is, the functions are overridden by the definitions given in the derived classes. This must be done to give at least one definition to each undefined function.

14. If virtual functions are defined in the base class then _______________
a) It is not necessary for derived classes to override those functions
b) It is necessary for derived classes to override those functions
c) Those functions can never be derived
d) Those functions must be overridden by all the derived classes

Answer: a
Explanation: The derived classes doesn’t have to redefine and override the base class functions. If one definition is already given it is not mandatory for any derived class to override those functions. The base class definition will be used.

15. Which feature of OOP is exhibited by the function overriding?
a) Inheritance
b) Abstraction
c) Polymorphism
d) Encapsulation

Answer: c
Explanation: The polymorphism feature is exhibited by function overriding. Polymorphism is the feature which basically defines that same named functions can have more than one functionalities.

This set of Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) focuses on “Exception Handling”.

1. What is an exception?
a) Problem arising during compile time
b) Problem arising during runtime
c) Problem in syntax
d) Problem in IDE

Answer: b
Explanation: The problems that might occur during execution of a program are known as exceptions. The exceptions are unexpected sometimes and can be predicted. Also, the exceptions should be always considered for a better program.

2. Why do we need to handle exceptions?
a) To prevent abnormal termination of program
b) To encourage exception prone program
c) To avoid syntax errors
d) To save memory

Answer: a
Explanation: The exceptions should be handled to prevent any abnormal termination of a program. The program should keep running even if it gets interrupted in between. The program should preferable show the error occurred and then retry the process or just continue the program further.

3. An exception may arise when _______________
a) Input is fixed
b) Input is some constant value of program
c) Input given is invalid
d) Input is valid

Answer: c
Explanation: The exceptions may arise because the input given by the user might not be of the same type that a program can manage. If the input is invalid the program gets terminated.

4. If a file that needs to be opened is not found in the target location then _____________
a) Exception will be produced
b) Exceptions are not produced
c) Exception might get produced because of syntax
d) Exceptions are not produced because of logic

Answer: a
Explanation: The exceptions are produced when anything unexpected happened. The program might not be able to find a file in the target location and hence program produces an exceptions. The exception produced, then terminates the program.

5. Which is the universal exception handler class?
a) Object
b) Math
c) Errors
d) Exceptions

Answer: d
Explanation: Any type of exception can be handled by using class Exceptions. An object of this class is created which can manipulate the exception data. The data can be used to display the error or to run the program further based on error produced.

6. What are two exception classes in hierarchy of java exceptions class?
a) Runtime exceptions only
b) Compile time exceptions only
c) Runtime exceptions and other exceptions
d) Other exceptions

Answer: c
Explanation: The exceptions class is having two other derived classes which are of runtime exception handler and for other type of exceptions handling. The runtime exception handler is used to handle the exceptions produced during run time and same with case of other exceptions.

7. Which are the two blocks that are used to check error and handle the error?
a) Try and catch
b) Trying and catching
c) Do and while
d) TryDo and Check

Answer: a
Explanation: Two blocks that are used to check for errors and to handle the errors are try and catch block. The code which might produce some exceptions is placed inside the try block and then the catch block is written to catch the error that is produced. The error message or any other processing can be done in catch block if the error is produced.

8. There can be a try block without catch block but vice versa is not possible.
a) True
b) False

Answer: a
Explanation: The try block may or may not have any catch block. But a catch block can’t be there in a program if there is no try block. It is like else-block can only be written if and only if if-block is present in the program.

9. How many catch blocks can a single try block can have?
a) Only 1
b) Only 2
c) Maximum 127
d) As many as required

Answer: d
Explanation: There is no limit on the number of catch blocks corresponding to a try block. This is because the error can be of any type and for each type, a new catch block can be defined. This is to make sure all type of exceptions can be handled.

10. Which among the following is not a method of Throwable class?
a) public String getMessage()
b) public Throwable getCause()
c) public Char toString()
d) public void printStackTrace()

Answer: c
Explanation: Actually all the functions are available in throwable class. But the return type given in the option is wrong. The function toString returns string value. Hence the return type must be a String and not a char.

11. To catch the exceptions ___________________
a) An object must be created to catch the exception
b) A variable should be created to catch the exception
c) An array should be created to catch all the exceptions
d) A string have to be created to store the exception

Answer: a
Explanation: The object must be created of a specific class of which the error has occurred. If the type of error is unknown then we can use an object of class Exceptions. This object will be able to handle any kind of exception that a program might produce.
Answer: b
Explanation: The separate catch blocks for a single try block can be combined into a single catch block. All type of errors can be then handled in s single block. The type still have to be specified for the errors that might be produced.

13. Which symbol should be used to separate the type of exception handler classes in a single catch block?
a) ?
b) ,
c) –
d) |

Answer: d
Explanation: A pipe symbol can be used to separate different type of exceptions. The exceptions should always be given in proper sequence to ensure that no code remains unreachable. If not done properly the code might never be used in a program.

14. Which class is used to handle the input and output exceptions?
a) InputOutput
b) InputOutputExceptions
c) IOExceptions
d) ExceptionsIO

Answer: c
Explanation: There is a specific class to handle each type of exceptions that might be produced in a program. The input and output exceptions can be handled by an object of class IOExcceptions. This class handles all type of input and output exceptions.

15. Why do we use finally block?
a) To execute the block if exception occurred
b) To execute a code when exception is not occurred
c) To execute a code whenever required
d) To execute a code with each and every run of program

Answer: d
Explanation: Sometimes there is a need to execute a set of code every time the program runs. Even if the exception occurs and even if it doesn’t, there can be some code that must be executed at end of the program. That code is written in finally block. This block is always executed regardless of exceptions occurring.

This set of Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) focuses on “Static Data Members”.

1. Which among the following best defines static variables members?
a) Data which is allocated for each object separately
b) Data which is common to all the objects of a class
c) Data which is common to all the classes
d) Data which is common to a specific method

Answer: b
Explanation: The static data members are made common to all the object of a class. They doesn’t change from object to object. Those are property of class rather than of any individual object.

2. Which keyword should be used to declare static variables?
a) static
b) stat
c) common
d) const

Answer: a
Explanation: The keyword used to declare static variables is static. This is must be used while declaring the static variables. The compiler can make variables static if and only if they are mentioned with static keyword.

3. Any changes made to static data member from one member function _____________
a) Is reflected to only the corresponding object
b) Is reflected to all the variables in a program
c) Is reflected to all the objects of that class
d) Is constant to that function only

Answer: c
Explanation: The changes made from any function to static data member will be a common change for all the other objects also. If the change is made with respect to one object and change is printed from another object, the result will be same.

4. Which is the correct syntax for declaring static data member?
a) static mamberName dataType;
b) dataType static memberName;
c) memberName static dataType;
d) static dataType memberName;

Answer: d
Explanation: The syntax must firstly be mentioned with the keyword static. Then the data type of the member followed by the member name should be given. This is general form of declaring static data members.

5. The static data member ______________________
a) Must be defined inside the class
b) Must be defined outside the class
c) Must be defined in main function
d) Must be defined using constructor

Answer: b
Explanation: The static data members must be defined outside the class. Since these are common to all the objects and should be created only once, they must not be defined in the constructor.

6. The syntax for defining the static data members is __________
a) dataType className :: memberName = value;
b) dataType className : memberName = value;
c) dataType className . memberName = value;
d) dataType className -> memberName =value;

Answer: a
Explanation: The syntax doesn’t contain the static keyword. Since it is already been declared as static inside the class. The data type and the corresponding class name must be there to allocate the variable to a class. The value is assigned using scope resolution operator for the member name.

7. If static data members have to be used inside a class, those member functions _______________
a) Must not be static member functions
b) Must not be member functions
c) Must be static member functions
d) Must not be member function of corresponding class

Answer: c
Explanation: Only the static member functions can access the static data members. The definition of static members is made common and hence the member function should be capable of manipulating the static data members.

8. The static data member __________________________
a) Can be accessed directly
b) Can be accessed with any public class name
c) Can be accessed with dot operator
d) Can be accessed using class name if not using static member function

Answer: d
Explanation: The static data members can be accessed using the class name also. If the member functions is not used or is not to be used then we can call the static data members directly by using its corresponding class name.

9. Which among the following is the correct syntax to access static data member without using member function?
a) className -> staticDataMember;
b) className :: staticDataMember;
c) className : staticDataMember;
d) className . staticDataMember;

Answer: b
Explanation: For accessing the static data members without using the static member functions, the class name can be used. The class name followed by scope resolution, indicating that static data members is member of this class, and then the data member name.

10. Which data members among the following are static by default?
a) extern
b) integer
c) const
d) void

Answer: c
Explanation: The const data members of any class are made static by default. This is an implicit meaning given by the compiler to the member. Since const values won’t change from object to object, hence are made static instead.

11. What is the output of the following program?

 Test

		  x
	   fun
	
		 ltlt x ltlt “ ”
	

 Test   
 main

	Test x
	x.
	x.
Answer: c
Explanation: The static member is initialized with 20. Then the function is called which used pre-increment and printed value of x. The function is called twice. Hence we get 21 22 as output.

12. Whenever any static data member is declared in a class ______________________
a) Only one copy of the data is created
b) New copy for each object is created
c) New memory location is allocated with each object
d) Only one object uses the static data

Answer: a
Explanation: The static data is same for all the objects. Instead of creating the same data each time an object is created, the compiler created only one data which is accessed by all the objects of the class. This saves memory and reduces redundancy.

13. If object of class are created, then the static data members can be accessed ____________
a) Using dot operator
b) Using arrow operator
c) Using colon
d) Using dot or arrow operator

Answer: d
Explanation: The static data members can be accessed in usual way as other members are accessed using the objects. The dot operator is used generally. Arrow can be used with the pointers.

14. What will be the output of the following program?

 Test

	 Test 
	 
		  ltlt  ltlt endl  
	

 
 Result

	 Test a
	
	Result 
	 
		  ltlt  ltlt endl 
	
 
 
 main 
 
	Result b 

a) Test’s Constructor is Called
b) Result’s Constructor is Called
c) Result’s Constructor Called Test’s Constructor is Called
d) Test’s Constructor Called Result’s Constructor is Called

Answer: b
Explanation: The output is the message printed from the constructor of class Result. There is no inheritance used hence only one constructor is called. Since static members are declared once in class declaration and are not defined. The constructor of class Test will not be called.

15. Which among the following is wrong syntax related to static data members?
a) className :: staticDataMember;
b) dataType className :: memberName =value;
c) static dataType memberName;
d) className : dataType -> memberName;

Answer: d
Explanation: The syntax given in option d doesn’t belong to any particular declaration or definition. First one is to access the static members using the class name. Second is to define the static data outside the class. Third syntax id to declare a data member as static in a class.

This set of Object Oriented Programming Interview Questions and Answers for Experienced people focuses on “Passing Object to Functions”.

1. Passing object to a function _______________
a) Can be done only in one way
b) Can be done in more than one ways
c) Is not possible
d) Is not possible in OOP

Answer: b
Explanation: The objects can be passed to the functions and this requires OOP concept because objects are main part of OOP. The objects can be passed in more than one way to a function. The passing depends on how the object have to be used.

2. The object ________________
a) Can be passed by reference
b) Can be passed by value
c) Can be passed by reference or value
d) Can be passed with reference

Answer: c
Explanation: The objects can be passed by reference if required to use the same object. The values can be passed so that the main object remains same and no changes are made to it if the function makes any changes to the values being passed.

3. Which symbol should be used to pass the object by reference in C++?
a) &
b) @
c) $
d) $ or &

Answer: a
Explanation: The object to be passed by reference to the function should be preceded by & symbol in the argument list syntax of the function. This indicates the compiler not to use new object. The same object which is being passed have to be used.

4. If object is passed by value ______________
a) Copy constructor is used to copy the values into another object in the function
b) Copy constructor is used to copy the values into temporary object
c) Reference to the object is used to access the values of the object
d) Reference to the object is used to created new object in its place

Answer: a
Explanation: The copy constructor is used. This constructor is used to copy the values into a new object which will contain all the values same as that of the object being passed but any changes made to the newly created object will not affect the original object.

5. Pass by reference of an object to a function _______________
a) Affects the object in called function only
b) Affects the object in prototype only
c) Affects the object in caller function
d) Affects the object only if mentioned with & symbol with every call

Answer: c
Explanation: The original object in the caller function will get affected. The changes made in the called function will be same in the caller function object also.

6. Copy constructor definition requires __________________
a) Object to be passed by value
b) Object not to be passed to it
c) Object to be passed by reference
d) Object to be passed with each data member value

Answer: c
Explanation: The object must be passed by reference to a copy constructor. This is to avoid the out of memory error. The constructors keeps calling itself, if not passed by reference, and goes out of memory.

7. What is the type of object that should be specified in the argument list?
a) Function name
b) Object name itself
c) Caller function name
d) Class name of object

Answer: d
Explanation: The type of object is the class itself. The class name have to be specified in order to pass the objects to a function. This allows the program to create another object of same class or to use the same object that was passed.

8. If an object is passed by value, _________________
a) Temporary object is used in the function
b) Local object in the function is used
c) Only the data member values are used
d) The values are accessible from the original object

Answer: b
Explanation: When an object is called by values, copy constructor is called and object is copied to the local object of the function which is mentioned in the argument list. The values gets copied and are used from the local object. There is no need to access the original object again.

9. Can data members be passed to a function using the object?
a) Yes, it can be passed only inside class functions
b) Yes, only if the data members are public and are being passed to a function outside the class
c) No, can’t be passed outside the class
d) No, can’t be done

Answer: b
Explanation: The data members can be passed with help of object but only if the member is public. The object will obviously be used outside the class. The object must have access to the data member so that its value or reference is used outside the class which is possible only if the member is public.

10. What exactly is passed when an object is passed by reference?
a) The original object name
b) The original object class name
c) The exact address of the object in memory
d) The exact address of data members

Answer: c
Explanation: The location of the object, that is, the exact memory location is passed, when the object is passed by reference. The pass by reference is actually a reference to the object that the function uses with another name to the same memory location as the original object uses.

11. If the object is not to be passed to any function but the values of the object have to be used then?
a) The data members should be passed separately
b) The data members and member functions have to be passed separately
c) The values should be present in other variables
d) The object must be passed

Answer: a
Explanation: The data members can be passed separately. There is no need to pass whole object, instead we can use the object to pass only the required values.

12. Which among the following is true?
a) More than one object can’t be passed to a function
b) Any number of objects can be passed to a function
c) Objects can’t be passed, only data member values can be passed
d) Objects should be passed only if those are public in class

Answer: b
Explanation: There is no restriction on passing the number of objects to a function. The operating system or the compiler or environment may limit the number of arguments. But there is no limit on number of objects till that limit.

13. What will be the output if all necessary code is included (Header files and main function)?

 test Object y

    y  

 main

	Object x  null
	test x
	System.. x

a) Run time error
b) Compile time error
c) Null
d) It is a string

Answer: d
Explanation: This is because the x object is passed by reference. The changes made inside the function will be applicable to original function too.

14. In which type is new memory location will be allocated?
a) Only in pass by reference
b) Only in pass by value
c) Both in pass by reference and value
d) Depends on the code

Answer: b
Explanation: The new memory location will be allocated only if the object is passed by value. Reference uses the same memory address and is denoted by another name also. But in pass by value, another object is created and new memory space is allocated for it.

15. Pass by reference and pass by value can’t be done simultaneously in a single function argument list.
a) True
b) False

Answer: b
Explanation: There is no condition which specifies that only the reference pass or values pass is allowed. The argument list can contain one reference pass and another value pass. This helps to manipulate the objects with functions more easily.

This set of Object Oriented Programming Assessment Questions and Answers focuses on “Pointer to Objects”.

1. Which language among the following doesn’t allow pointers?
a) C++
b) Java
c) Pascal
d) C

Answer: b
Explanation: The concept of pointers is not supported in Java. The feature is not given in the language but can be used in some ways explicitly. Though this pointer is supported by java too.

2. Which is correct syntax for declaring pointer to object?
a) className* objectName;
b) className objectName;
c) *className objectName;
d) className objectName();

Answer: a
Explanation: The syntax must contain * symbol after the className as the type of object. This declares an object pointer. This can store address of any object of the specified class.

3. Which operator should be used to access the members of the class using object pointer?
a) Dot operator
b) Colon to the member
c) Scope resolution operator
d) Arrow operator

Answer: d
Explanation: The members can be accessed from the object pointer by using arrow operator. The arrow operator can be used only with the pointer of class type. If simple object is declared, it must use dot operator to access the members.

4. How does compiler decide the intended object to be used, if more than one object are used?
a) Using object name
b) Using an integer pointer
c) Using this pointer
d) Using void pointer

Answer: c
Explanation: This pointer denotes the object, in which it is being used. If member function is called with respect to one object then this pointer refers to the same object members. It can be used when members with same name are involved.

5. If pointer to an object is declared __________
a) It can store any type of address
b) It can store only void addresses
c) It can only store address of integer type
d) It can only store object address of class type specified

Answer: d
Explanation: The address of only the specified class type can get their address stored in the object pointer. The addresses doesn’t differ but they do differ for the amount and type of memory required for objects of different classes. Hence same class object pointer should be used.

6. What is the size of an object pointer?
a) Equal to size of any usual pointer
b) Equal to size of sum of all the members of object
c) Equal to size of maximum sized member of object
d) Equal to size of void

Answer: a
Explanation: The size of object pointer is same as that of any usual pointer. This is because only the address have to be stored. There are no values to be stored in the pointer.

7. A pointer _________________
a) Can point to only one object at a time
b) Can point to more than one objects at a time
c) Can point to only 2 objects at a time
d) Can point to whole class objects at a time

Answer: a
Explanation: The object pointer can point to only one object at a time. The pointer will be able to store only one address at a time. Hence only one object can be referred.

8. Pointer to a base class can be initialized with the address of derived class, because of _________
a) derived-to-base implicit conversion for pointers
b) base-to-derived implicit conversion for pointers
c) base-to-base implicit conversion for pointers
d) derived-to-derived implicit conversion for pointers

Answer: a
Explanation: It is an implicit rule defined in most of the programming languages. It permits the programmer to declare a pointer to the derived class from a base class pointer. In this way the programmer doesn’t have to declare object for derived class each time it is required.

9. Can pointers to object access the private members of the class?
a) Yes, always
b) Yes, only if it is only pointer to object
c) No, because objects can be referenced from another objects too
d) No, never

Answer: d
Explanation: The pointers to an object can never access the private members of the class outside the class. The object can indirectly use those private members using member functions which are public in the class.

10. Is name of an array of objects is also a pointer to object?
a) Yes, always
b) Yes, in few cases
c) No, because it represents more than one object
d) No, never

Answer: a
Explanation: The array name represents a pointer to the object. The name alone can represent the starting address of the array. But that also represents an array which is in turn stored in a pointer.

11. Which among the following is true?
a) The pointer to object can hold address only
b) The pointer can hold value of any type
c) The pointer can hold only void reference
d) The pointer can’t hold any value

Answer: a
Explanation: The pointer to an object can hold only the addresses. Address of any other object of same class. This allows the programmer to link more than one objects if required.
Answer: a
Explanation: The pointer should be mentioned followed by the arrow operator. Arrow operator is applicable only with the pointers. Then the function name should be mentioned that is to be called.

13. If a pointer to an object is created and the object gets deleted without using the pointer then __________
a) It becomes void pointer
b) It becomes dangling pointer
c) It becomes null pointer
d) It becomes zero pointer

Answer: b
Explanation: When the address pointed by the object pointer gets deleted, the pointer now points to an invalid address. Hence it becomes a dangling pointer. It can’t be null or void pointer since it doesn’t point to any specific location.

14. How can the address stored in the pointer be retrieved?
a) Using * symbol
b) Using $ symbol
c) Using & symbol
d) Using @ symbol

Answer: c
Explanation: The & symbol must be used. This should be done such that the object should be preceded by & symbol and then the address should be stored in another variable. This is done to get the address where the object is stored.

15. What should be done to prevent changes that may be made to the values pointed by the pointer?
a) Usual pointer can’t change the values pointed
b) Pointer should be made virtual
c) Pointer should be made anonymous
d) Pointer should be made const

Answer: d
Explanation: The pointer should be declared as a const type. This prevents the pointer to change any value that is being pointed from it. This is a feature that is made to access the values using pointer but to make sure that pointer doesn’t change those values accidently.

16. References to object are same as pointers of object.
a) True
b) False

Answer: b
Explanation: The references are made to object when the object is created and initialized with another object without calling any constructor. But the object pointer must be declared explicitly using * symbol that will be capable of storing some address. Hence both are different.

This set of Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) focuses on “Default Arguments”.

1. What are default arguments?
a) Arguments which are not mandatory to be passed
b) Arguments with default value that aren’t mandatory to be passed
c) Arguments which are not passed to functions
d) Arguments which always take same data value

Answer: b
Explanation: The arguments which are assigned with some default value. Since some value is already given, it is not mandatory to pass those arguments. They can be used directly.

2. Which is the correct condition for the default arguments?
a) Those must be declared as last arguments in argument list
b) Those must be declared first in the argument list
c) Those can be defined anywhere in the argument list
d) Those are declared inside the function definition

Answer: a
Explanation: The default arguments must be declared at last in the argument list. This is to ensure that the arguments doesn’t create ambiguity. The normal arguments should be passed first.

3. If a member function have to be made both zero argument and parameterized constructor, which among the following can be the best option?
a) Two normal and one default argument
b) At least one default argument
c) Exactly one default argument
d) Make all the arguments default

Answer: d
Explanation: All the arguments must be made default. This will make sure that none of the arguments are mandatory to be passed. Which in turn means that the function can work without any argument and can be passed with arguments too.

4. Which among the following function can be called without arguments?
a) void add(int x, int y=0)
b) void add(int=0)
c) void add(int x=0, int y=0)
d) void add(char c)

Answer: c
Explanation: For the function to be called without arguments, either it must have zero arguments or it must have all the default arguments. Here the function in option void add(int x=0, int y=0) have all the default arguments and hence can be called directly with zero argument.

5. If a function have all the default arguments but still some values are passed to the function then ______________
a) The function will use the values passed to it
b) The function will use the default values as those are local
c) The function can use any value whichever is higher
d) The function will choose the minimum values

Answer: a
Explanation: The function will use the values passed explicitly to it. The default values will be ignored. The default values are used only in case the values are not passed explicitly to the function.

6. Which among the following is correct?
a) void test(int x=0, int y, int z=0)
b) void test(int x=0, int=0)
c) void test(int x, int y=0)
d) void test(int x=’c, int y)

Answer: c
Explanation: The default arguments must be mentioned at last in the argument list. Also, the type of values assigned must match with the argument type. All the default arguments must be mentioned at last, none of the normal arguments should come in between the default arguments list.

7. What function will be called with the independent syntax “test(5,6,7);”?
a) void test(int x, int y)
b) void test(int x=0, int y, int z)
c) int test(int x=0, y=0, z=0)
d) void test(int x, int y, int z=0)

Answer: d
Explanation: There are three arguments that are getting passed to the function test(). Only the last option have all the default argument at last in the argument list. And the total number of the arguments is three. The third option is wrong because the return type is int and the syntax given is independent which means it doesn’t return any value.

8. Which among the following is a wrong call to the function void test(int x, int y=0, int z=0)?
a) test(5,6,7);
b) test(5);
c) test();
d) test(5,6);

Answer: c
Explanation: The function must be passed with at least one argument. There is two default arguments and one normal argument which must be passed with some value. Hence the third call to the function is wrong as it doesn’t pass even a single parameter to the function

9. Default arguments are _________________________
a) Only allowed in the parameter list of the function declaration
b) Only allowed in the return type of the function declaration
c) Only allowed with the class name definition
d) Only allowed with the integer type values

Answer: a
Explanation: The default arguments are only allowed in the parameter list of the function arguments. This rule was not applicable in the beginning versions of c++ but later from c++ 14th version it has been implemented. This is the only way to use default arguments.

10. Which among the following is false for default arguments?
a) Those are not allowed with a declaration of pointer to functions
b) Those are not allowed with the reference to functions
c) Those are not allowed with the typedef declarations
d) Those are allowed with pointer and reference to function declaration

Answer: d
Explanation: The statements given are true because that is a feature given to make the programming more flexible and have some security with accidental changes at same time. The last option is false because it is not a rule defined. It is an opposite statement to the rules defined for default arguments.

11. The non-template functions can be added with default arguments to already declared functions ____________________
a) If and only if the function is declared again in the same scope
b) If and only if the function is declared only once in the same scope
c) If and only if the function is declared in different scope
d) If and only if the function is declared twice in the program

Answer: a
Explanation: The non-template functions can also be added with default arguments. This can be done even if the functions were defined earlier. This is because the call to the function won’t be affected. The function can still be used in the same way as it was used earlier.
Answer: b
Explanation: The using-declaration carries over all the known default arguments. This is a common feature as the usage doesn’t gets affected even if the default arguments are added. This comes under flexible programming.

13. The names given to the default arguments are only looked up and ________________ and are bound during declaration.
a) Checked for availability
b) Checked for random access
c) Checked for accessibility
d) Checked for feasibility

Answer: c
Explanation: The names given to the default arguments are bound at time of declaration but are only checked for accessibility and to get bounded. This is mainly to bind those members during declaration.

14. The default argument get bound during declaration ________________
a) And are never executed
b) And are executed simultaneously
c) But are executed only if priority is given
d) But are executed during function call

Answer: d
Explanation: The default argument are bound at the time of declaration. That is an implicit functioning. But those are executed only when the function is called. Otherwise, those will never get executed.

15. The virtual function overrides ____________
a) Do not acquire base class declaration of default arguments
b) Do acquire base class declaration of default arguments
c) Do not link with the default arguments of base class
d) Do link with the default argument but only of derived classes

Answer: a
Explanation: The virtual function overrides do not acquire the base class declaration of default arguments. Even if a call to the virtual function is made, static type of the object decides the default arguments to be used.

This set of Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) focuses on “Upcasting”.

1. What is upcasting?
a) Casting subtype to supertype
b) Casting super type to subtype
c) Casting subtype to super type and vice versa
d) Casting anytype to any other type

Answer: a
Explanation: The upcasting concept includes only the casting of subtypes to the super types. This casting is generally done implicitly. Smaller size types can fit into larger size types implicitly.

2. Which among the following is true for upcasting in inheritance?
a) Downward to the inheritance tree
b) Upward to the inheritance tree
c) Either upward or downward
d) Doesn’t apply on inheritance

Answer: b
Explanation: The upcasting concept in inheritance is always applied upward the inheritance tree. The derived class objects can be type casted to any of its parent class type. Since is a relationship applies in general inheritance.

3. Which among the following is safe?
a) Upcasting
b) Downcasting
c) Both upcasting and downcasting
d) If upcasting is safe then downcasting is not, and vice versa

Answer: a
Explanation: The upcasting is always safe since the derived type or the smaller type is converted into the base type or the larger size. This results in allocating a smaller size data into bigger type data. No data is lost in casting, hence safe.

4. Which among the following is the best situation to use upcasting?
a) For general code dealing with only subtype
b) For general code dealing with only supertype
c) For general code dealing with both the supertype and subtype
d) For writing a rigid code with respect to subtype

Answer: b
Explanation: When a general code has to be written where we use only the supertype object or the data of bigger size, then upcasting would be the best option. Since the whole code will require only the supertype name references.

5. Which property is shown most when upcasting is used?
a) Code reusability
b) Code efficiency
c) Complex code simple syntax
d) Encapsulation

Answer: c
Explanation: The code written using upcasting mostly shows complex code in simpler syntax features. This is because the upcasting concept can be applied as polymorphism and to group the similar type of objects.

6. Upcasting and downcasting objects are the same as casting primitive types.
a) True
b) False

Answer: b
Explanation: It is a bit confusing concept since both casting concepts are different. Primitive casting depends on the type and size of data being typecast. Whereas in objects casting, the classes and inheritance order plays a big role.

7. Which casting among the following is allowed for the code given below?

 A

	  a

 B A

	 b

main

	B b A  
	A a B  

a) Casting 1
b) Casting 2
c) casting 1 and casting 2
d) casting 1 nor casting 2

Answer: b
Explanation: The casting 2 is correct. The objects casting must be done from derived class object to a parent class object. That is, the object of the superclass can be made an object of subclass only. Vice versa is not possible.

8. If multiple inheritance is implemented, which upcasting will be correct?
a) Upcast to first base class listed in inheritance
b) Upcast to send base class listed in inheritance
c) Upcast to any base class
d) Upcast is not possible

Answer: c
Explanation: The upcasting of derived class object is possible to any base class. This is because the base class object can represent any of its derived classes using upcasting.

9. If class C inherits class B and class B inherits class A ________________
a) Class C object can be upcasted to object of class B only
b) Class C object can be upcasted to object of class A only
c) Class C object can be upcasted to object of either class A or B
d) Class C object can’t be upcasted

Answer: c
Explanation: Both class A and B are parent classes of class C. Class C object hence can be upcasted to any of those class objects. It is not compulsory to upcast to nearest parent.

10. Upcasting is _____________________ without an explicit type cast.
a) Always allowed for public inheritance
b) Always allowed for protected inheritance
c) Always allowed for private inheritance
d) Not allowed

Answer: a
Explanation: The public inheritance shows the most flexible is-a relationship. Hence explicit type casting is not required. Implicit type casting is done by the compiler.
Answer: b
Explanation: Since the implicit type casting allows casting of a base class pointer to refer to its derived class object or even base class object. We need dynamic type casting so that the references can be resolved during execution of program.

12. When are the pointer types known for upcasting the objects?
a) Compile time
b) Runtime
c) Source code build time
d) Doesn’t apply to pointer types

Answer: a
Explanation: The pointer or reference types are known at compile time for the upcasting of an object. This is because the addresses must be known for casting the derived class to base class object.

13. When are the object type known for upcasting the objects?
a) Compile time
b) Runtime
c) Source code build time
d) Doesn’t apply to objects directly

Answer: b
Explanation: The upcasting with objects directly requires runtime resolving. The objects are fixed and address are allocated at compile time. But the execution of a program requires runtime knowledge of object types, for implicit type cast.

14. If two classes are defined “Parent” and “Child” then which is the correct type upcast syntax in C++?
a) Parent *p=child;
b) Parent *p=*child;
c) Parent *p=&child
d) Parent *p=Child();

Answer: c
Explanation: The syntax must contain the base class name first. So that the parent class object pointer can be declared. Then the object is assigned with the derived class object with & symbol. & symbol is added to get the address of the derived class object.

15. Which among the following is true?
a) Upcasting is possible only for single level inheritance
b) Upcasting is possible only for multilevel inheritance
c) Upcasting is possible only for multiple inheritance
d) Upcasting is possible for any type of inheritance

Answer: d
Explanation: The type of inheritance doesn’t matter with the upcasting concept. Upcasting applies to all types of inheritance. Any derived class object can be upcasted to any of its base class object.

This set of Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) focuses on “New Operator”.

1. What is the new operator?
a) Allocates memory for an object or array
b) Allocates memory for an object or array and returns a particular pointer
c) Used as return type when an object is created
d) Used to declare any new thing in a program

Answer: b
Explanation: The new keyword is used to allocate memory of an object or array. The new object or array can be of any type. Then it returns a suitable non zero pointer to the object.

2. Microsoft C++ Components extensions support new keyword to _____________
a) Modify a vtable
b) Replace a vtable slot entry
c) Add new vtable slot entries
d) Rearrange vtable slot entries

Answer: c
Explanation: The new keyword is used for adding new vtable slot entries. This is an additional feature in Microsoft C++. It can use predefined class object for this work.

3. What happens when new fails?
a) Returns zero always
b) Throws an exception always
c) Either throws an exception or returns zero
d) Terminates the program

Answer: c
Explanation: While creating new objects, the new operator may fail because of memory errors or due to permissions. At that moment the new operator returns zero or it may throw an exception. The exception can be handled as usual.

4. If new throws an error, which function can be called to write a custom exception handler?
a) _set_handler
b) _new_handler
c) _handler_setter
d) _set_new_handler

Answer: d
Explanation: If the default exception handler has to be replaced by a user defined handler, we can call _set_new_handler run-time library function with the function name as an argument. This lets the programmer to give a custom definition for handling new operator failure.

5. In C++, if new operator is used, when is the constructor called?
a) Before the allocation of memory
b) After the allocation of memory
c) Constructor is called to allocate memory
d) Depends on code

Answer: b
Explanation: The constructor function is called after the allocation of memory. In C++ the feature works in a bit different way. The memory for all the data members is allocated first and then the constructor function is called to finalize the memory allocated.

6. Which among the following is correct syntax to declare a 2D array using new operator?
a) char (*pchar)[10] = new char[][10];
b) char (pchar) = new char[][10];
c) char (*char) = new char[10][];
d) char (*char)[][10]= new char;

Answer: a
Explanation: The new operator usage to declare a 2D array requires a pointer and size of array to be declared. Data type and then the pointer with size of array. The left index can be left blank or any variable can be assigned to it.

7. For declaring data by using new operator ____________________
a) Type name can’t contain const
b) Type name can’t contain volatile
c) Type name can’t contain class declarations
d) Type name can’t contain const, volatile, class declaration or enumerations

Answer: d
Explanation: The declaration of any data where we use new operator, any of the mentioned types are not allowed. This is because the new operator allocated memory based on the type of data which can be allocated dynamically.

8. The new operator _____________
a) Can allocate reference types too
b) Doesn’t allocate reference types
c) Can allocate reference to objects
d) Doesn’t allocate any data

Answer: b
Explanation: The new operator doesn’t allocate reference types. This is because the reference types are not objects. The new operator is used to allocate memory to the direct objects.

9. Which among the following is true?
a) New operator can’t allocate functions but pointer to functions can be allocated
b) New operator can allocate functions as well as pointer to functions
c) New operator can allocate any type of functions
d) New operator is not applicable with functions allocation

Answer: a
Explanation: The new operator can’t allocate functions but can allocate pointer to the functions. This is a security feature as well as to reduce the ambiguity in code. The new keyword is not given functionality to directly allocate any function.

10. Which among the following is added in grammar of new operator?
a) Finalize
b) Arg
c) Initializer
d) Allocator

Answer: c
Explanation: The new operator grammar is added with an initializer field. This can be used to initialize an object with a user defined constructor. Hence can allocate memory as intended by the programmer.

11. Initializers __________________
a) Are used for specifying arrays
b) Are used to defined multidimensional arrays
c) Can’t be specified for arrays
d) Can’t be specified for any data

Answer: c
Explanation: The initializers can’t be specified for arrays. The initializers can create arrays of object if and only if the class has a default constructor. That is a zero argument constructor so that it can be called without any argument.
Answer: b
Explanation: It is not necessary that the objects get destroyed when they go out of scope if allocated by using new operator. This is because new operator returns a pointer to object that it had allocated. A suitable pointer with proper scope should be defined by the programmer explicitly.

13. The new operator _________________
a) Invokes function operator new
b) Doesn’t invoke function operator new
c) Invokes function operator only if required
d) Can’t invoke function operator new implicitly

Answer: a
Explanation: The new operator invokes function operator new. This is done to allocate the storage to an object. ::operator new is called for storage allocation implicitly.

14. If a new operator is defined for a class and still global new operator have to be used, which operator should be used with the keyword new?
a) Colon
b) Arrow
c) Dot
d) Scope resolution

Answer: d
Explanation: As usual, scope resolution operator is used to get the scope of parent or the global entities. Hence we can use scope resolution operator with the new operator to call the global new operator even if new operator is defined for the class explicitly.

15. How does compiler convert “::operator new” implicitly?
a) ::operator new( sizeof( type ) )
b) ::operator new( sizeof( ) )
c) new operator :: type sizeof( type )
d) new sizeof( type ) operator

Answer: a
Explanation: The compiler implicitly converts the syntax so that the instruction can be understood by the processor and proper machine code can be generated. The conversion is done implicitly and no explicit syntax is required.

This set of Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) focuses on “Automatic Variable”.

1. What are automatic variables?
a) Global variables
b) Implicit/temporary variables
c) Local variables
d) System variables

Answer: c
Explanation: The local variables are also known as automatic variables. The variables in any local scope that are created and destroyed as the program executes its scope.

2. The memory for automatic variables ___________________
a) Have to be allocated and deallocated explicitly
b) Are allocated and deallocated automatically
c) Is never actually allocated
d) Are never safe

Answer: b
Explanation: The memory is allocated and deallocated automatically for the automatic variables. As soon as the variable comes in scope, the memory is allocated. The variables are destroyed as soon as those go out of scope.

3. Scope of an automatic variable _______________
a) Is actually the whole program
b) Is actually never fixed
c) Is always equal to the whole program execution
d) Is actually function or block in which it is defined

Answer: d
Explanation: The automatic variables scope is limited only within the block or the function where those are defined. This is the property of all the automatic variables.

4. Which among the following is true for automatic variables in general?
a) Automatic variables are invisible to called function
b) Automatic variables are always visible to the called function
c) Automatic variables can’t interact with the called function
d) Automatic variables can’t be variable

Answer: a
Explanation: The automatic variables are hidden from the called function. Even if passed by reference or address, the address of the variable is used and not the actual variable of calling function. Automatic variables can be const or variable.

5. If an automatic variable is created and then a function is called then ________________
a) The automatic variable created gets destroyed
b) The automatic variable doesn’t get destroyed
c) The automatic variable may or may not get destroyed
d) The automatic variable can’t be used in this case

Answer: b
Explanation: The automatic variables are saved till the called function gets executed. This is done so as to ensure that the program can continue its execution after the called function is returned. The automatic variables gets destroyed only if those go out of scope.

6. Where are the automatic variables stored if another function is called in between the execution of the program?
a) Heap
b) Queue
c) Stack
d) Temp variable

Answer: c
Explanation: All the automatic variables are stored in a new stack entry as soon as their scope is created. If another function is called, the present data is saved in stack and new entry in stack is made for the called function. When the function returns, the automatic variables are used again from where those were left.

7. The static variables of a function ________________
a) Are also automatic variables
b) Are not automatic variables
c) Are made automatic by default
d) Can be made automatic explicitly

Answer: b
Explanation: The static members can’t be automatic. This is because the automatic variables are created and destroyed with each call to a specific function. But the static members remain throughout the execution of program once created.

8. All variables declared within a block ____________________
a) Are not always automatic
b) Can be made non-automatic
c) Are static by default
d) Are automatic by default

Answer: d
Explanation: The variables declared inside a block, are make automatic by default. This is to ensure that the variables get destroyed when not required. The variables remain live only till those are required, the life is dependent on the scope of a variable.

9. What values does uninitialized automatic variables contain?
a) Null value
b) Void value
c) Undefined/Garbage
d) Zero value

Answer: c
Explanation: The automatic variable which are not initialized, contain garbage value. If we just declare a variable and try to print its value, the result is some unknown value. The value is garbage as that was not expected value.

10. Constructor of automatic variables is called ____________________
a) When execution reaches the place of declaration of automatic variables
b) When the program is compiled
c) When the execution is just started
d) Just before the execution of the program

Answer: a
Explanation: Only when the execution reaches the place where the automatic variable was declared, the constructor is called. This is to ensure that the memory is not allocated if not needed. The memory is allocated and then destroyed as soon as it goes out of scope.

11. Does java contain auto or register keywords?
a) Yes, for declaring every type of variable
b) Yes, only to declare cache registers
c) No, because java doesn’t support automatic variables
d) No, java supports local variable concept

Answer: d
Explanation: The auto and register keywords are not supported in java. Though the same is allowed in java without specifying any of those keywords. The variables are local variables. But java makes it mandatory to initialize all of the local variables in a program.
Answer: b
Explanation: All the automatic variables in a program must be declared before their use. The compiler won’t allow any use of variable if those are not declared before their use.

13. Which error is produced if the automatic variables are used without declaration?
a) Undefined symbol
b) Memory error
c) Type mismatch
d) Statement missing

Answer: a
Explanation: If the automatic variables are used without declaration or are used before the declaration then the compiler throws an error. The error that the symbol is undefined. The compiler must know everything before that can be used.

14. In Perl, using which operator are the local variables created?
a) Dot
b) Arrow
c) Scope resolution
d) my

Answer: d
Explanation: The language perl supports local variables but the concept is bit different. And if the values are not assigned to the local variables then it contains undef value.

15. How are automatic variables different from the instance variables?
a) Automatic variables are initialized automatically but instances are not
b) Automatic variables are given zero values initially and not instances
c) Instance variables have to be initialized explicitly and automatic implicitly
d) Instance variables are initialized implicitly while automatic are not

Answer: d
Explanation: The automatic variables have to be initialized explicitly. But in case of instances, those are initialized automatically during execution of the program. The conventions are mandatory.

This set of Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) focuses on “Inbuilt Classes”.

1. What are inbuilt classes?
a) The predefined classes in a language
b) The classes that are defined by the user
c) The classes which are meant to be modified by the user
d) The classes which can’t be used by the user

Answer: a
Explanation: The classes that are already provided in a programming language for use are inbuilt classes. These classes provide some functions or objects that can be used by the programmer for easier code.

2. Inbuilt class __________________________
a) Must be included before use
b) Are not necessary to be included for use
c) Are used by the compiler only
d) Can be modified by programmer always

Answer: a
Explanation: The inbuilt classes must be included in the program. Whenever some functions are used, they must have a declaration before use. The same is the case with classes.

3. What doesn’t inbuilt classes contain?
a) Function prototype
b) Function declaration
c) Function definitions
d) Objects

Answer: c
Explanation: The classes contain the definitions of the special functions that are provided for the programmers use. Those functions can be used to make the programming easy and to reuse the already existing code.

4. Which among the following not an inbuilt class in C++?
a) System
b) Color
c) String
d) Functions

Answer: d
Explanation: There is no inbuilt class named function in java. The others are classes already provided in java. All those classes contain some special functions to be used in programming.

5. What is the InputStream class meant for?
a) To handle all input streams
b) To handle all output streams
c) To handle all input and output streams
d) To handle only input from file

Answer: a
Explanation: The InputStream is an inbuilt class which is used to handle all the tasks related to input handling. This class extends input from keyboard or file or any other possible input stream.

6. Which statement is true for the Array class?
a) Arrays can have variable length
b) The length array can be changed
c) Each class has an associated Array class
d) Arrays can contain different type of values

Answer: c
Explanation: The Array class is associated with all the other classes. This gives us the flexibility to declare an array of any type. The index goes from 0 to n, where n is some fixed size for array.

7. What is the use of Math class?
a) To use the mathematical functions with strings
b) To use the mathematical functions
c) To suppress the use of mathematical functions
d) To complex the calculations

Answer: b
Explanation: The Math class is provided with some special functions. These functions can be used to calculate and get result of some special and usual mathematical functions. We don’t have to write the code to calculate the trigonometric function results, instead we can use Math functions.

8. DataInputStream is derived from ______________________
a) StreamingInput
b) StreamedInput
c) StreameInput
d) StreamInput

Answer: d
Explanation: The DataInputStream is more specific class for operating on specific type of data inputs. This is used to read data of specific type. The same can be used to read data in a specific format.

9. Which attribute can be used to get the size of an array?
a) Size.Array
b) Array.Size
c) Array_name.length
d) length.Array_name

Answer: c
Explanation: The array name is given of which the length have to be calculated. The array length is stored in the attribute length. Hence we access it using dot operator.

10. Number class can’t manipulate ____________________
a) Integer values
b) Float values
c) Byte values
d) Character values

Answer: d
Explanation: The Number class is used to work with all the number type of values. The integers, float, double, byte etc. are all number type values. Character is not a number value.

11. Which function should be used to exit from the program that is provided by System class?
a) exit(int);
b) gc();
c) terminate();
d) halt();

Answer: a
Explanation: The exit function should be used to terminate the program. The function is passed with an argument. The argument indicated the type of error occurred.
Answer: b
Explanation: The runFinalization() Function is defined in the System class. The function is used to finalize an object which undergo destruction. The action is required to terminate the object properly.

13. What does load(String)::= function do, in System class?
a) Loads dynamic library for a path name
b) Loads all the dynamic libraries
c) Loads all the Number in string format
d) Loads the processor with calculations

Answer: a
Explanation: Only the specified path named dynamic libraries are loaded. All the dynamic libraries can’t be loaded at a time. Hence we use this function for specific libraries.

14. Which is not a System class variable?
a) err
b) out
c) in
d) put

Answer: d
Explanation: Put is not a System class variable. The most general and basic variables are err, out and in. The variables can handle most of the tasks performed in a program.

15. Which package contains the utility classes?
a) java.lang
b) java.utility
c) java.util
d) java.io

Answer: c
Explanation: The package java.util contains all the utility classes. This package also contains generic data structures, date, time etc. These can be used in any java program, you just have to include java.util package.

This set of Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) focuses on “String Class”.

1. Which is a true statement for object of String class?
a) Object are immutable
b) Object are mutable
c) Object are created only once
d) Object can’t be created

Answer: a
Explanation: The object of string class are mostly immutable. This means that the String objects are constant. These can’t be changed once created.

2. How to declare an object of class String?
a) String object_Name = value;
b) String object_name = new;
c) String object_name= new value;
d) String object_name= value new;

Answer: a
Explanation: The class name String is given. And then the object name is mentioned. There are two ways to declare and initialize the string. Either by giving direct string value or by using new keyword. But if new operator is used, constructor of String class have to be called. From the given options, the direct string value declaration is correct.

3. What does function length do in String class?
a) Returns length of string including null character
b) Returns length of string excluding null character
c) Returns length of substring
d) Returns size of string in bytes

Answer: b
Explanation: The length function returns the length of string. The length is the number of characters in the string but the last null character is not counted. The string length can be used to loop through each character in the string.

4. Which is the function to get the character present at a particular index in the string?
a) char charAt(index);
b) char charIn(StringName);
c) char charAt(StringName);
d) char charIn(index);

Answer: a
Explanation: The function can be called using dot operator with the string object. Char is the return type of the function to return the character at specified position. The index must be an integer value, less than the length of string.

5. If only one parameter is passed to substring function then __________________
a) It returns the character at the specified position
b) It returns the string of length 1 from the specified index
c) It returns the string from specified index till the end
d) It returns the string from starting of string till the specified index

Answer: c
Explanation: The substring function returns a string value. The string is the substring starting from the specified index till the end. The substring function have to be called with the object of string class.

6. If two index are given as argument to substring function then ___________________
a) String of length equal to sum of two arguments is returned
b) String starting from first index and of length equal to send argument
c) String starting from first index and of length equal to sum of two arguments
d) String starting from first index and ending at second index position

Answer: d
Explanation: A value of string type is returned from this function. The returned string is a substring that starts from the first argument position, till the second index position. The indices must be less than the length of actual string.

7. String class have a concat() function that is used to _____________________
a) Replace old string by new string
b) Add two strings
c) Append one string at end of another string
d) Remove a string from end of one string

Answer: c
Explanation: The concat function is used to append string into another string. The new string is always appended at the end of source string. The target string is appended as it is and the whole string is then ended by null character.

8. The function lastIndexOf() is used to ___________________
a) Get the index of last occurrence of specified character in argument
b) Get the index of first occurrence of specified character in argument
c) Get the index of last occurrence of first character in string
d) Get the index of last occurrence of last character of string

Answer: a
Explanation: The function is used to get the last occurrence index of a character present in a string. The return type is char. Single character is returned. The function is used with a string object and the target character is passed as its argument.

9. Function equals() is _______________ and equalIgnoreCase() is _________________
a) Case Insensitive, case insensitive
b) Case sensitive, Case insensitive
c) Case sensitive, case sensitive
d) Case insensitive, case sensitive

Answer: b
Explanation: Both the functions return Boolean value. The function equal() is case sensitive and returns false even if a single character is case different in two strings. The other function ignores the case sensitivity and only checks if the spellings are same.

10. The compareTo() function is used to ________________
a) Compare strings value to string object
b) Compare string value to string value
c) Compare string object to another string object
d) Compare string object to another string value

Answer: c
Explanation: The source and target must be objects of the string class. The compare is always case sensitive. To compare two string objects without case sensitivity then we can use compareToIgnoreCase() function.

11. String class provides function toUpper() to _____________________
a) Convert first character to uppercase
b) Convert last character to uppercase
c) Convert the whole string characters to uppercase
d) Convert uppercase to lower and lower to uppercases

Answer: c
Explanation: The function is used to convert each character of the string. If the character is already uppercase then it remains the same. But if some character is in lowercase then it will be converted to uppercase.
Answer: d
Explanation: The function is used to remove any white space from both the ends of a given string. The white space include space, tab, next line etc. It will be removed both from the starting of string and from the end of string.

13. Function replace() accepts _____________ arguments.
a) 1
b) 2
c) 3
d) 4

Answer: b
Explanation: The first argument is the target character. This target character will be replaced by another character. The new character is the second argument to the function. Only the characters can be passed as argument, not a string.

14. If two arguments are passed to the indexOf() function then ___________________
a) Second argument indicates the occurrence number of specified character from starting
b) Second argument indicates the occurrence number of specified character from end
c) Second argument indicates the index of the character in first argument
d) Second argument indicates the index of the character from the last of the string

Answer: a
Explanation: The string may have more than one occurrence of a character. We use this function to get the index at which the specified number of times a specific character has occurred in a string. For example, we can get the index of 5th occurrence of character “j” in a string.

15. The string class deals with string of only character type.
a) True
b) False

Answer: a
Explanation: The string class objects can be used for any string consisting of characters. The characters include numbers, alphabets and few special characters. String class is not necessary to be used but provides a huge set of inbuilt functions to make the string operations easier.