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. The keyword extends is used to specify that an interface inherits from another interface.

  2. The keyword extends is used to specify that a class inherits from an interface.

  3. The keyword implements is used to specify that an interface inherits from another interface.

  4. The keyword implements is used to specify that a class inherits from an interface.

  5. The keyword implements is used to specify that a class inherits from another class.

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

In Java, 'extends' is used for interface inheritance (one interface extending another) and class inheritance (subclass extending superclass). 'implements' is used when a class implements an interface. Options B and E are incorrect because classes don't inherit from interfaces using 'extends', and classes don't inherit from other classes using 'implements'. Option C is wrong because interfaces don't use 'implements'.

Multiple choice technology programming languages
  1. from base to the derived class

  2. from derived to base

  3. only the derived class

  4. only the base class

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

In Java, constructor execution flows from base class to derived class. The base class constructor completes first to initialize inherited fields, then derived class constructors run. B is backwards, C and D are incomplete.

Multiple choice technology programming languages
  1. A subclass must define all the methods from the superclass

  2. It is possible for a subclass to define a method with the same name and parameters as a method defined by the superclass.

  3. It is possible for a subclass to define a field with the same name as a field defined by the superclass.

  4. It is possible for two classes to be the superclass of each other.

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

B describes method overriding, which is legal in Java. C describes field hiding, also legal. A is false - abstract methods need implementation, but concrete methods can be inherited. D is false - circular inheritance creates a paradox and Java prohibits it.

Multiple choice technology programming languages
  1. The class will fail to compile, since the class Other has not yet been declared when referenced in class AClass.

  2. The class will fail to compile, since import statements must never be at the very top of a file.

  3. The class will fail to compile, since the package declaration can never occur after an import statement.

  4. The class will fail to compile, since the class Other must be defined in a file called Other.java.

  5. The class will fail to compile, since the class Other must be declared public.

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

Java enforces strict file structure: package declaration (if present) must be first line, followed by imports, then class/interface declarations. Option C correctly identifies this ordering violation. A is wrong - forward references are allowed within a compilation unit. B is wrong - imports do come before classes but after package. D and E are wrong - a class can be defined in the same file without being public or matching the filename.

Multiple choice technology programming languages
  1. Under all circumstances

  2. Unless A is abstract

  3. A class that implements an interface cannot be abstract

  4. none of the above

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

A concrete class implementing an interface must provide implementations for all interface methods. However, if the implementing class is abstract, it can defer implementation to its subclasses. Option A is too absolute, C is false (abstract classes can implement interfaces), and D is incorrect because B is valid.

Multiple choice technology programming languages
  1. True

  2. False

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

Abstract classes can contain both abstract and concrete (non-abstract) methods. This is a key distinction from interfaces. Abstract classes provide partial implementation with abstract methods defining what subclasses must implement, and concrete methods providing shared behavior.

Multiple choice technology programming languages
  1. True

  2. False

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

An abstract class in Java cannot be declared as final because abstract classes are designed to be extended by subclasses, whereas final classes explicitly prevent inheritance. These two modifiers are mutually exclusive.

Multiple choice technology programming languages
  1. keyword

  2. method

  3. both

  4. none of the above

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

'super' is a reserved keyword in Java, not a method. It is used to access superclass members (super.method() or super.field) or to call a superclass constructor (super() as the first statement in a constructor). It cannot be used as a variable name or for any other purpose.

Multiple choice technology programming languages
  1. True

  2. False

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

While Java permits accessing static members through an object reference (obj.staticMethod()), this is misleading and discouraged. Static members belong to the class, not instances. The correct practice is ClassName.staticMethod(). The compiler actually uses the declared type of the reference, not the runtime object.

Multiple choice technology programming languages
  1. True

  2. False

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

Java only provides a default no-argument constructor if no other constructor is explicitly defined. Once you define any constructor (with or without arguments), the default constructor is not automatically generated. If you need both parameterized and no-arg constructors, you must explicitly define both.

Multiple choice technology programming languages
  1. True

  2. False

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

Private member variables in a superclass are not accessible to subclasses - this is a fundamental principle of encapsulation in object-oriented programming. Only the superclass itself can access its private members. Subclasses can only access public, protected, and package-private (if in same package) members.

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().