Computer Knowledge
Object-Oriented Programming
2,686 Questions
Object-oriented programming questions test core computer science concepts like classes, inheritance, polymorphism, and encapsulation. The focus includes Java program structures, method overloading, and memory allocation for objects. This topic is essential for technical sections in various recruitment tests.
Java class definitionsMethod overriding rulesPolymorphism conceptsGeneric type parametersMemory allocation in objects
Object-Oriented Programming Questions
-
Private and protected data members of the class can be accessed by defining the friend function.
-
Only protected data members of the class can be accessed by defining the friend function.
-
Only private data members of the class can be accessed by defining the friend function.
-
Only public and protected data members of the class can be accessed by defining the friend function.
-
None of these
A
Correct answer
Explanation
We can access private and protected members both in the class by making the friend function of that class.
-
A class is user-defined data type in C++, whereas a structure is not.
-
A class has private access specifier by default, whereas a structure has protected access specifier in C++.
-
A class has private access specifier by default, whereas a structure has public access specifier in C++.
-
Both (1) and (3)
-
Both (2) and (3)
C
Correct answer
Explanation
This statement is true about class and the structure in C++ and this is the only difference between them.
-
We can use this variable in the member variable of the class.
-
This keyword is used to modify the const objects of the mutable data members.
-
This keyword is used to make the objects of data members as constant.
-
Both (1) and (2)
-
Both (1) and (3)
D
Correct answer
Explanation
Both the options are true about mutable keyword in C++.
-
C++ is an object-oriented language.
-
Encapsulation determines the binding of data variables and functions into a single unit.
-
Data abstraction is a design approach with implementation in C++.
-
Polymorphism can be achieved in C++ by virtual function.
-
None of these
C
Correct answer
Explanation
Abstraction determines only the interface not implementation. It can separate the interface with implementation.
-
A()
-
A::A()
-
~A()
-
Both (1) and (2)
-
All of the above
D
Correct answer
Explanation
Yes, both the options determine the true concept.
-
We cannot create virtual constructor in C++.
-
We cannot create virtual destructors in C++.
-
We can define pure virtual destructor also in our program.
-
Both (2) and (3)
-
Both (1) and (2)
B
Correct answer
Explanation
This is not a true statement. There is a necessary condition to make the destructor as virtual as our program can not manage the memory allocated by downward subclasses and it may be leaked.
-
There is no multiple inheritance in C++.
-
C++ supports multilevel inheritance.
-
Hybrid inheritance is possible in C++.
-
Both (1) and (3)
-
None of these
A
Correct answer
Explanation
This is a false statement as C++ determines multilevel inheritance and in that case, a single derived class can inherit from two or more base classes.
-
There are two types of overloading, i.e. function overloading and operator overloading in C++.
-
It is not necessary to initialise the static variables.
-
We can have a static virtual function in C++.
-
Both (2) and (3)
-
None of these
D
Correct answer
Explanation
Yes, both options are the false statements, so this option is correct according to the question.
-
A new color will be created.
-
There will be a runtime exception.
-
Parameters are not enough.
-
There will be an error
Compile time error : cannot find method color.
-
Compile time error : Color is abstract, cannot create an object.
B
Correct answer
Explanation
This is the correct answer because color constructor creates a new colour but it takes parameters in the range 0-255. But here the value passed is 284(in first parameter).So a runtime exception named “IllegalArgumentException” will be thrown by the JVM.So this is the correct answer.
B
Correct answer
Explanation
This is correct choice because the constructor of the Font class takes Three parameters :-First :- first parameter is of String type which specifies the name of the Font.Second :- second parameter is of type int which specifies the style of the font like Font.BOLD.Third :- third parameter is of type int which specifies the size of the font.ExampleFont f1 = new Font(“Serif”, Font.BOLD, 32);So this option is true.
C
Correct answer
Explanation
This is correct choice because there are 8 primitive data types in java. These are :-ByteShortIntLongDoubleCharFloatBooleanSo this is correct answer.
-
Multilevel, multiple and hierarchical inheritance’s instances are not involved.
-
No such concept like virtual base class exists only functions can be made virtual.
-
It creates only one instance of the super class.
-
It helps in creating redundant data, when we want to achieve data security.
-
Both 2 and 3
C
Correct answer
Explanation
Virtual base class creates one instance of the super class.
-
Structure
-
String
-
Long
-
Class
-
Byte
B
Correct answer
Explanation
It is a derived data type.
-
Encapsulation means hiding of data from the inside view.
-
Encapsulation can be achieved by proper use of modifiers.
-
Abstraction is a process like generalization.
-
Abstraction Is closely associated with encapsulation.
-
All of the above are true statements.
E
Correct answer
Explanation
Yes, this is the right option.
-
Overloading occurs in the same class.
-
In method overriding, the return type of the method is different.
-
Final methods can not be overridden.
-
All of the above
-
Only (1) and (3) are true statements.
E
Correct answer
Explanation
Yes, both of these are true.