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 technology programming languages
  1. Alpha a = x;

  2. Foo f= (Delta)x;

  3. Foo f= (Alpha)x;

  4. Beta b = (Beta)(Alpha)x;

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Option B causes ClassCastException because x is a Beta object, and you cannot cast a Beta to Delta (Delta is a subclass of Beta, not the other way around). At runtime, when the JVM tries to cast the Beta object to Delta, it fails because the actual object is not a Delta. Options A, C, and D are valid casts that don't throw exceptions - Beta to Alpha is upcasting, Beta to Foo is valid (Beta implements Foo through Alpha), and the double cast in D is just a redundant upcast then downcast back to Beta.

Multiple choice technology programming languages
  1. The class is fully encapsulated.

  2. The code demonstrates polymorphism.

  3. The ownerName variable breaks encapsulation

  4. The cardlD and limit variables break polymorphism.

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

Encapsulation requires hiding variables using the private modifier and exposing them via public methods. Declaring ownerName as public exposes it directly, breaking encapsulation.

Multiple choice technology programming languages
  1. import sun.scjp.Color.*;

  2. import static sun.scjp.Color.*;

  3. import sun.scjp.Color; import static sun.scjp.Color.*;

  4. import sun.scjp.; import static sun.scjp.Color.;

  5. import sun.scjp.Color; import static sun.scjp.Color.GREEN;

Reveal answer Fill a bubble to check yourself
C,E Correct answer
Explanation

Line 4 declares a Color variable, requiring the Color type to be imported. Line 6 uses GREEN directly, requiring a static import. Option C imports both the type and all static members. Option E imports the type and just the GREEN constant. Option B only static imports, missing the type import needed for line 4 where Color is used as a type.

Multiple choice technology programming languages
  1. s.writeInt(x);

  2. s.serialize(x);

  3. s.writeObject(x);

  4. s.defaultWriteObject();

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

To solve this question, the user needs to understand the concept of serialization in Java. Serialization is the process of converting a Java object into a stream of bytes so that it can be stored in a file or transmitted over a network. Deserialization is the reverse process of converting a stream of bytes back into a Java object.

The code given above defines a class Foo that implements the Serializable interface. This means that instances of Foo can be serialized and deserialized. The writeObject() method is a callback method that is called during serialization and allows the developer to customize the serialization process.

Option A: s.writeInt(x); This option will write the integer value of x to the ObjectOutputStream. However, this is not enough to fully serialize the Foo object, as there are other instance variables besides x that also need to be serialized.

Option B: s.serialize(x); This option is not valid, as there is no such method as serialize() in the ObjectOutputStream class.

Option C: s.writeObject(x); This option will write the object x to the ObjectOutputStream. However, this will not fully serialize the Foo object, as x is just one instance variable and there are others that also need to be serialized.

Option D: s.defaultWriteObject(); This option is the correct answer. The defaultWriteObject() method serializes all non-transient, non-static instance variables in the object. This includes the instance variable x, as well as any other instance variables that Foo may have. By calling this method, we ensure that the entire Foo object is serialized and can be correctly deserialized later.

Therefore, the answer is: D

Multiple choice technology programming languages
  1. An encapsulated, public class promotes re-use.

  2. Classes that share the same interface are always tightly

  3. An encapsulated class allows subclasses to overload methods, but

  4. An encapsulated class allows a programmer to change an

Reveal answer Fill a bubble to check yourself
A,D Correct answer
Explanation

Encapsulation promotes reusability by hiding implementation details and exposing only necessary interfaces. A well-encapsulated class allows programmers to modify internal implementation without breaking dependent code. Option B is incorrect because shared interfaces actually promote loose coupling, not tight coupling. Option C is incorrect because encapsulation doesn't restrict method overloading in subclasses.

Multiple choice technology programming languages
  1. abstract double area() { }

  2. abstract double area()

  3. abstract double area();

  4. abstract double area(); { }

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

An abstract method in Java has no method body - just the signature followed by a semicolon. Option B shows the correct syntax for an abstract method declaration. Option A incorrectly includes a method body with curly braces, which would make it a concrete method. Option C uses semicolon incorrectly with braces.

Multiple choice technology programming languages
  1. A finalizer may NOT be invoked explicitly.

  2. The finalize method declared in class Object takes no action.

  3. super.finalize() is called implicitly by any overriding finalize method.

  4. The finalize method for a given object will be called no more than

  5. The order in which finalize will be called on two objects is based on

Reveal answer Fill a bubble to check yourself
B,D Correct answer
Explanation

B is true: Object.finalize() does nothing by default (empty implementation). D is true: The JVM guarantees finalize() is called at most once per object. A is false: finalize() can be called explicitly. C is false: super.finalize() must be called explicitly. E is false: finalization order is unpredictable.

Multiple choice technology programming languages
  1. The return type for a method can be any Java type, including void

  2. An important principal of object oriented programming is implementation hiding

  3. When you perform mathematical calculations on the unlike data type, java will perform a implicit conversion to unify the types

  4. All the Above

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

Option A is true because Java methods can return any type including primitives, objects, void, and custom types. Option B is true because encapsulation (implementation hiding) is a core OOP principle that hides internal details. Option C is true because Java performs implicit type widening/conversion when operands of different types are used in arithmetic operations. Since A, B, and C are all true, D 'All the Above' is correct.

Multiple choice technology programming languages
  1. Encapsulation

  2. Class

  3. Inheritance

  4. Polymorphism

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

Encapsulation is the object-oriented programming concept of wrapping data and code into a single unit and restricting access to class members using modifiers like private, protected, and public. Distractors like inheritance, class, or polymorphism do not directly restrict access to members.

Multiple choice technology programming languages
  1. It must have a package statement

  2. It must be named Test.java

  3. It must import java.lang

  4. It must declare a public class named Test

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Java requires that public class names match the source file name exactly. If a source file contains a public class named Test, the file must be named Test.java. Package statements and imports are optional - java.lang is imported automatically by default.

Multiple choice technology programming languages
  1. The type List is assignable to List<A>.

  2. The type List<Object> is assignable to List<?>.

  3. The type List<D> is assignable to List<? extends B>.

  4. The type List<? extends B> is assignable to List<? extends A>.

  5. The type List<Object> is assignable to any List reference.

Reveal answer Fill a bubble to check yourself
B,C,D Correct answer
Explanation

In Java generics, List can be assigned to List> because the unbounded wildcard accepts any type. List can be assigned to List extends B> because D extends B. List extends B> can be assigned to List extends A> because B extends A. Option A is false because generics are invariant - List is not assignable to List. Option E is false because List cannot be assigned to List due to invariance.

Multiple choice technology programming languages
  1. Direction d = NORTH;

  2. Nav.Direction d = NORTH;

  3. Direction d = Direction.NORTH;

  4. Nav.Direction d = Nav.Direction.NORTH;

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

The Direction enum is nested inside the Nav class, so it must be accessed as Nav.Direction. To use the enum constant NORTH, the full qualified name Nav.Direction.NORTH is required. Options A, B, and C miss either the outer class reference or the enum type.

Multiple choice technology programming languages
  1. Cant do

  2. use this ()

  3. new keyword

  4. using objectname

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

To call one constructor from another in the same class, you use the this() keyword as the first statement. For example, this(5) calls a constructor taking an int parameter. Option A is incorrect because constructor chaining is supported. Option C would create a new object, not call another constructor.

Multiple choice technology web technology
  1. No the reference cannot be change

  2. Using object class

  3. Using interface

  4. Using thread

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

A final object reference cannot be changed to point to a different object once initialized. This is what final means for reference types. However, the object's internal state CAN be modified if it's mutable. The question asks about changing the reference itself, not the object's contents.