Computer Knowledge

Object-Oriented Programming

2,239 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. Interfaces can implement multiple interfaces

  2. Interfaces can extend any number of other interfaces.

  3. Members of an interface are never static.

  4. class can extend multiple classes

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

Interfaces in Java can extend any number of other interfaces, allowing multiple inheritance of type. This is different from classes which can only extend one class. Interfaces do not 'implement' other interfaces - they extend them. Interface members are implicitly public and can be static (default methods are instance methods). A class can extend only one class due to single inheritance.

Multiple choice technology programming languages
  1. Private methods cannot be overridden in subclasses

  2. A subclass can override any method in a superclass.

  3. An overriding method can declare that it throws more exceptions than the method it is overriding.

  4. The parameter list of an overriding method must be a subset of the parameter list of the method that it is overriding.

  5. The overriding method can have a different return type than the overridden method.

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

Private methods are not visible to subclasses and therefore cannot be overridden. A subclass can only override public and protected methods. An overriding method cannot declare more checked exceptions than the overridden method. The parameter list must match exactly (same number and types). The return type must be covariant (same type or subtype).

Multiple choice technology programming languages
  1. foreach(x) System.out.println(z);

  2. for(int z : x) System.out.println(z);

  3. while( x.hasNext()) System.out.println( x.next());

  4. for( int i=0; i< x.length; i++ ) System.out.println(x[i]);

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

Varargs parameters in Java are treated as arrays. Iterating over x can be done using either an enhanced for-loop for(int z : x) or a standard index-based loop. foreach is not a Java keyword, and arrays do not have iterator methods like hasNext() or next().

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.