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. a, b

  2. b, c

  3. a, d

  4. a, e

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

To solve this question, the user needs to know about abstract classes and abstract methods in Java.

An abstract class is a class that cannot be instantiated on its own and is often used as a template for other classes. Abstract methods are methods that are declared but have no implementation in the abstract class.

Option a declares an abstract class Digit with an abstract method print(). This is a valid declaration of an abstract class.

Option b declares a class Digit with an abstract method print(). This is not a valid declaration because a non-abstract class cannot have an abstract method.

Option c declares an abstract class Digit with an abstract method print() that has an empty implementation. This is not valid because an abstract method cannot have an implementation in the abstract class.

Option d declares an abstract class Digit with a non-abstract method print(). This is not valid because an abstract class must have at least one abstract method.

Option e declares a non-abstract class Digit with a non-abstract method print(). This is a valid declaration of a non-abstract class.

Therefore, the correct answer is:

The Answer is: D

Multiple choice technology programming languages
  1. a, b

  2. a, c

  3. b, c

  4. c, d

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

To fix the compilation error in the given code, we need to understand the concept of abstract classes and methods in Java.

An abstract class is a class that cannot be instantiated, and it can have abstract methods, which are declared without an implementation. Any subclass of an abstract class must either implement all the abstract methods of its parent class or be declared as abstract itself.

In the given code, we have an abstract class AirPlane and its subclass AirJet. AirPlane has one abstract method fly() and one non-abstract method land(), while AirJet overrides the fly() method and declares another abstract method land().

The compilation error in the code is due to the fact that AirJet is not implementing all the abstract methods of its parent class AirPlane. To fix this error, we have two options:

a) Remove abstract from line 20 and add body to method land()

This option is correct. Since AirJet is a concrete class, it must implement all the abstract methods of its parent class AirPlane. Therefore, we can remove the abstract keyword from the declaration of land() method and provide an implementation for it.

b) Declare class AirJet as abstract to at line 10

This option is incorrect. Declaring AirJet as abstract will not solve the compilation error. In fact, it will introduce another error because AirJet will now have two abstract methods, but it is not declaring itself as abstract.

c) Remove super() call at line 13

This option is incorrect. Removing the super() call will not solve the compilation error. In fact, it will introduce another error because AirPlane does not have a default constructor, and super() is trying to call it.

d) Remove abstract at line 1 and line 2

This option is incorrect. Removing the abstract keyword from line 1 and line 2 will not solve the compilation error. In fact, it will introduce another error because now AirPlane will have a non-implemented method.

Therefore, the correct answer is:

The Answer is: A. a, b

Multiple choice technology programming languages
  1. a, b, c

  2. a, d, e

  3. a, c, f

  4. a, d, f

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

Option a) compiles: concrete class implements both methods publicly. Option c) compiles: abstract class can implement some methods and leave others abstract. Option f) compiles: interfaces can extend other interfaces. Options b, d, e fail: b's missing public modifiers, d's incomplete implementation, e's unreachable code and missing method. Answer C is correct.

Multiple choice technology programming languages
  1. c

  2. d

  3. e

  4. f

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

All variables declared inside a Java interface are implicitly public, static, and final. The transient modifier is illegal for interface fields and causes a compilation error because interface fields are static constants, and transient applies only to instance variables during serialization.

Multiple choice technology programming languages
  1. test1, test2 and test4

  2. test2, test4 and test5

  3. test1, test4 and test5

  4. test1, test2 and test3

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

test1 fails: abstract methods can't have bodies in abstract classes. test4 fails: abstract methods can't be static. test5 fails: abstract methods can't be final. test2 and test3 are valid: abstract classes can have final and static methods with implementations. Option C correctly identifies the three invalid declarations.

Multiple choice technology programming languages
  1. Yes, this is a correct and free of error declaration

  2. No, compilation error at line 1

  3. No, compilation error at line 3

  4. No, compilation error at line 5

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

Interface methods without default or static modifiers are implicitly abstract and must not contain a body. Line 5 attempts to define void method1() {}; with an empty implementation block {}, causing a compilation error. Interfaces can be declared abstract (line 1) and variables can be final (line 3).

Multiple choice technology programming languages
  1. A

  2. B

  3. Compiler error on line 7

  4. Compiler error on line 13

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

In Java, private methods cannot be overridden. The doStuff() method in A is private, so it is invisible to subclasses. When a.doStuff() is called, the compiler resolves it directly to the private method in class A because the reference type is A. Thus, it prints 'A' without dynamic dispatch to B.doStuff(), which is a completely independent method.

Multiple choice technology architecture
  1. Methods cannot be overridden to be more private

  2. static methods cannot be overloaded

  3. private methods cannot be overloaded

  4. An overloaded method cannot throw exceptions not checked in the base class

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

Overriding methods cannot narrow access visibility (private < default < protected < public), so they can't become more private. Static methods can be overloaded, private methods can be overloaded, and overloaded methods have no exception restrictions since they're not overrides.

Multiple choice technology architecture
  1. Compile and run without error

  2. Compile time Exception

  3. Runtime Exception

  4. None of the above

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

Code compiles successfully since Base to Sub cast is syntactically valid. At runtime, b is a Base object, not a Sub. Downcasting requires the actual object type to match, so this throws ClassCastException.

Multiple choice technology architecture
  1. Set

  2. List

  3. Map

  4. Vector

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

To store unique object elements without sorting, the most suitable interface would be a Set.

A Set is a collection that contains no duplicate elements. It provides methods to add, remove, and check if an element is present. Additionally, it does not guarantee the order of the elements.

A List is an ordered collection that allows duplicates. A Map is a collection that maps key-value pairs, and a Vector is a synchronized version of the List interface. None of these interfaces guarantee uniqueness of elements without additional implementation.

Therefore, the option that best suits the given requirement is:

The Answer is: A - Set

Multiple choice technology architecture
  1. At the root of the collection hierarchy is a class called Collection

  2. The collection interface contains a method called enumerator

  3. The interator method returns an instance of the Vector class

  4. The Set interface is designed for unique elements

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

Set interface specifically prohibits duplicate elements. Collection is an interface, not a class. There's no 'enumerator' method in Collection, and iterator() returns Iterator, not Vector.

Multiple choice technology architecture
  1. Static void doCalc(int x, int… args) {}

  2. Static void doCalc(int… args, int x) {}

  3. Static void doCalc(int[] args)

  4. Static void doCalc(int args…)

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

Java varargs syntax requires the varargs parameter to be the LAST parameter. Option A follows this: int x, int... args (x is fixed, args is varargs at end). Option B is invalid: int... args, int x (varargs cannot be followed by other parameters). Option C is missing the varargs syntax (three dots). Option D has incorrect placement and syntax.

Multiple choice technology architecture
  1. Methods cannot be overridden to be more private

  2. static methods cannot be overloaded

  3. private methods cannot be overloaded

  4. An overloaded method cannot throw exceptions not checked in the base class

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

Java enforces that overriding methods cannot be more restrictive than the method they override. This preserves the Liskov Substitution Principle - a subclass must be usable wherever its superclass is expected. Therefore, you cannot override a public method as private or protected. Static methods CAN be overloaded (B is false). Private methods CAN be overloaded within the same class (C is false). Overloaded methods (not overriding) can throw any exceptions regardless of the base class (D is false - it describes overriding, not overloading).

Multiple choice technology architecture
  1. Set

  2. List

  3. Map

  4. Vector

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

The Set interface is designed specifically to store unique elements - it explicitly prohibits duplicate values. This matches the requirement of storing unique objects without sorting. List allows duplicates and maintains insertion order. Map stores key-value pairs, not just elements. Vector is a legacy List implementation that allows duplicates. Set implementations like HashSet provide uniqueness without sorting (unlike TreeSet which sorts).

Multiple choice technology architecture
  1. At the root of the collection hierarchy is a class called Collection

  2. The collection interface contains a method called enumerator

  3. The interator method returns an instance of the Vector class

  4. The Set interface is designed for unique elements

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

The Set interface is indeed designed to store unique elements - no duplicate elements are allowed. This is its defining characteristic. Option A is false because Collection is an interface, not a class. Option B is false because the Collection interface doesn't have an 'enumerator' method - it has an 'iterator' method. Option C is false because the iterator() method returns an Iterator, not a Vector instance - Vector is a concrete class that implements List.