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
-
using super keyword
-
defining the required class members as private
-
define in members protected and using super keyword in subclass
-
we cant access
C
Correct answer
Explanation
To access superclass members from a subclass, the members must be declared with appropriate access (protected or public) and then accessed using the 'super' keyword. Option A is incomplete because it doesn't specify the access modifier requirement. Option B is wrong because private members are not accessible outside the class. Option D is incorrect because superclass members can indeed be accessed if properly declared.
A
Correct answer
Explanation
Interfaces in Java can contain inner classes, including nested interfaces, classes, and enums. These are implicitly static and public when declared inside an interface.
B
Correct answer
Explanation
Interface variables in Java are implicitly public, static, and final (constants). They cannot use private or protected access modifiers - these modifiers are not allowed for interface fields.
-
automatically invoked
-
initialize object after construction
-
has access specifiers
-
has atleast void as return type
D
Correct answer
Explanation
Constructors are automatically invoked when an object is created and they initialize the object. They can have access specifiers (public, private, protected). However, constructors have NO return type - not even void - which is what makes option D false.
-
collection of classes,i/face and subclss
-
contains keywords 1.import and 2.package
-
has 2 types 1.build in and 2.user defined
-
all the above
D
Correct answer
Explanation
Java packages are collections of classes, interfaces, and subclasses. The 'package' keyword defines a package, and 'import' is used to include packages. Java has both built-in packages (like java.util) and user-defined packages. Option A has poor grammar but all three concepts (A, B, C) are valid.
-
public, protected
-
private, static, final
-
abstract
-
all the above
D
Correct answer
Explanation
Inner classes are class members and can use all modifiers: access modifiers (public, protected, private) and non-access modifiers (static, final, abstract). Therefore all options A, B, and C are valid.
-
The code compiles successfully and one bytecode file is generated: Book.class.
-
The code compiles successfully and two bytecode files are generated: Book.class and BookReader.class.
-
The code compiles successfully and two bytecode files are generated: Book.class and Book$BookReader.class.
-
A compiler error occurs on line 4.
C
Correct answer
Explanation
In Java, inner class bytecode files use the $ notation. A top-level class Book generates Book.class, while its inner class BookReader generates Book$BookReader.class. The file BookReader.class is never created for inner classes.
-
Beverage
-
Coffee
-
Compiler error on line 2
-
Compiler error on line 13
-
Compiler error on line 15
A
Correct answer
Explanation
The code demonstrates Java method binding rules. Despite b being a Coffee object at runtime, the private method drink() in Beverage is called because private methods are not virtual and cannot be overridden. The reference type Beverage determines which method is called.
B
Correct answer
Explanation
In Java, the 'static' keyword makes a class member belong to the class rather than instances. To prevent inheritance, you use the 'final' keyword, not 'static'. A final class cannot be extended.
-
The subclass and the superclass has an "is-a" relationship.
-
Inheritance cannot change the behaviors of the existing class
-
The original class is the parent class or the base class or the superclass.
-
The new class is called a child class or a subclass or a derived class of the parent.
-
In a subclass you can add new methods and new fields as well as override existing methods in the parent class to change their behaviors.
B
Correct answer
Explanation
Inheritance in Java explicitly allows changing behaviors through method overriding - a subclass can override methods from the parent class to provide different implementations. This is a core feature of inheritance, making option B the correct choice as it is NOT a feature of inheritance.
A
Correct answer
Explanation
Static members (methods and variables) belong to the class itself, not to instances of the class. They can be called using the class name directly without creating any object. This is why static methods are often used for utility functions that don't require object state.
-
Ctrl + T
-
Ctrl + E
-
Ctrl + O
-
Ctrl + C
A
Correct answer
Explanation
Ctrl+T in Eclipse displays the Type Hierarchy, showing the inheritance structure of the current class - its superclass and subclasses. This helps visualize object-oriented relationships.
-
The Object class does not provide any implementation for the hashCode method; every class must override it
-
As far as it may be practically possible, the hashCode method defined by the Object class does return distinct integers for distinct objects
-
For two object references referring to the same object, the hashCode method returns the same integer
-
It returns a fixed number that internally represents the Object class for the JVM
-
Only choice D is correct
B,C
Correct answer
Explanation
The Object class does provide a default hashCode() implementation (not overridable as A claims). It typically converts the internal memory address to an integer, so distinct objects usually get distinct hashCodes (B is true). For references to the same object, hashCode() returns the same integer (C is true). D is false because it's not a fixed number representing the Object class itself.
-
Compiler Error
-
Exception
-
Prints 10
-
Prints 0
C
Correct answer
Explanation
The code uses Java generics with type inference. Gen is created, and when i1.get() returns an Integer, Java's autounboxing converts it to int automatically. This compiles and runs without error, printing 10. The generic type parameter T is inferred as Integer, making the assignment int i = i1.get() valid through autounboxing.