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

Multiple choice general knowledge science & technology
  1. using super keyword

  2. defining the required class members as private

  3. define in members protected and using super keyword in subclass

  4. we cant access

Reveal answer Fill a bubble to check yourself
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.

Multiple choice general knowledge
  1. True

  2. False

Reveal answer Fill a bubble to check yourself
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.

Multiple choice general knowledge
  1. automatically invoked

  2. initialize object after construction

  3. has access specifiers

  4. has atleast void as return type

Reveal answer Fill a bubble to check yourself
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.

Multiple choice general knowledge
  1. collection of classes,i/face and subclss

  2. contains keywords 1.import and 2.package

  3. has 2 types 1.build in and 2.user defined

  4. all the above

Reveal answer Fill a bubble to check yourself
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.

Multiple choice general knowledge
  1. The code compiles successfully and one bytecode file is generated: Book.class.

  2. The code compiles successfully and two bytecode files are generated: Book.class and BookReader.class.

  3. The code compiles successfully and two bytecode files are generated: Book.class and Book$BookReader.class.
  4. A compiler error occurs on line 4.

Reveal answer Fill a bubble to check yourself
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.

Multiple choice general knowledge
  1. Beverage

  2. Coffee

  3. Compiler error on line 2

  4. Compiler error on line 13

  5. Compiler error on line 15

Reveal answer Fill a bubble to check yourself
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.

Multiple choice general knowledge
  1. The subclass and the superclass has an "is-a" relationship.

  2. Inheritance cannot change the behaviors of the existing class

  3. The original class is the parent class or the base class or the superclass.

  4. The new class is called a child class or a subclass or a derived class of the parent.

  5. 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.

Reveal answer Fill a bubble to check yourself
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.

Multiple choice general knowledge
  1. True

  2. False

Reveal answer Fill a bubble to check yourself
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.

Multiple choice general knowledge
  1. The Object class does not provide any implementation for the hashCode method; every class must override it

  2. As far as it may be practically possible, the hashCode method defined by the Object class does return distinct integers for distinct objects

  3. For two object references referring to the same object, the hashCode method returns the same integer

  4. It returns a fixed number that internally represents the Object class for the JVM

  5. Only choice D is correct

Reveal answer Fill a bubble to check yourself
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.

Multiple choice general knowledge
  1. Compiler Error

  2. Exception

  3. Prints 10

  4. Prints 0

Reveal answer Fill a bubble to check yourself
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.