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

  2. Abstract

  3. super

  4. classtype

  5. final

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

All five options are valid class keywords in Cache ObjectScript. Clientdatatype defines a custom data type class, Abstract prevents instantiation, super references the superclass, classtype defines the class category, and final prevents further inheritance or overriding. These keywords control class behavior and inheritance patterns.

Multiple choice technology programming languages
  1. Data type classes cannot be instantiated

  2. Data type classes can contain properties

  3. Data type classes automatically includes methods for validation and translation

  4. Data type classes cannot be inherited

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

Data type classes in Cache ObjectScript cannot be directly instantiated (they're abstract by nature) and automatically include methods for validation and translation when used. However, they CAN contain properties to define the data structure, and they CAN be inherited to create specialized data types. The claimed answer A and C are correct.

Multiple choice technology programming languages
  1. keywords are not inherted

  2. keywords from first superclass are inherited

  3. keywords from last superclass are inherited

  4. keywords from all superclasses are inherited

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

In Cache ObjectScript multiple inheritance, class keywords are inherited only from the first superclass in the inheritance list. Keywords from subsequent superclasses are not inherited. This prevents conflicts when multiple superclasses define conflicting keywords. The inheritance is determined by the order of superclasses in the class definition.

Multiple choice technology web technology
  1. BD

  2. DB

  3. BDC

  4. DBC

  5. Compilation fails

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

When a superclass lacks a no-argument constructor, subclasses must explicitly call a superclass constructor with matching arguments using super(...). Bottom2 fails to do this, causing compilation to fail because the implicit super() call cannot find a valid constructor in Top. The Top class only has Top(String s), so Bottom2's constructor must explicitly call super(s) or super(someString).

Multiple choice technology web technology
  1. Cohesion is the OO principle most closely associated with hiding implementation details

  2. Cohesion is the OO principle most closely associated with making sure that classes know about other classes only through their APIs

  3. Cohesion is the OO principle most closely associated with making sure that a class is designed with a single, well-focused purpose

  4. Cohesion is the OO principle most closely associated with allowing a single object to be seen as having many types

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

Cohesion measures how focused and single-purpose a class is. High cohesion means a class performs one logical task. Hiding implementation details is encapsulation, interface-only interaction is loose coupling, and treating objects as multiple types is polymorphism.

Multiple choice technology web technology
  1. furry bray

  2. stripes bray

  3. furry generic noise

  4. stripes generic noise

  5. Compilation fails

  6. An exception is thrown at runtime

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

Fields in Java are not polymorphic - they bind to the declared type (Mammal), so m.name returns 'furry '. Methods ARE polymorphic and use the actual object type (Zebra), so m.makeNoise() returns 'bray'. This is a key distinction between field access and method invocation in Java's inheritance model. Even though the object is a Zebra, field access is determined at compile time based on the reference type.

Multiple choice technology web technology
  1. ClassD has low cohesion

  2. ClassA has weak encapsulation

  3. ClassB has weak encapsulation

  4. ClassB has strong encapsulation

  5. ClassC is tightly coupled to ClassA

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

Direct access to another class's public variables violates encapsulation principles. ClassB should protect its data with private fields and provide access through getter/setter methods, making ClassB responsible for its own data management. When ClassA directly uses ClassB's public variables, ClassB has weak encapsulation because it exposes its internal implementation details.

Multiple choice technology web technology
  1. "X extends Y" is correct if and only if X is a class and Y is an interface

  2. "X extends Y" is correct if and only if X is an interface and Y is a class

  3. "X extends Y" is correct if X and Y are either both classes or both interfaces

  4. "X extends Y" is correct for all combinations of X and Y being classes and/or interfaces

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

In Java, 'extends' is valid only when both X and Y are classes, or when both X and Y are interfaces. A class cannot extend an interface (it implements it), and an interface cannot extend a class. This rule maintains the type system's consistency - inheritance relationships must be between the same kind of type.

Multiple choice technology web technology
  1. -124

  2. -134

  3. -424

  4. -434

  5. -444

  6. Compilation fails

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

The code tests Java's method overload resolution with varargs. sifter(aa) calls sifter(A[]... a2) matching -4. sifter(ba) matches sifter(B[] b1) NOT sifter(B[]... b1) because Java prefers non-varargs over varargs when both match - giving -3. sifter(7) matches sifter(Object o) as 7 autoboxes to Integer then widens to Object - giving -4. Final result: -434.

Multiple choice technology web technology
  1. Protected

  2. Private

  3. Public

  4. Public & private

  5. Protected & Public

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

Java's access modifiers follow this order: private (most restrictive) → default (package) → protected → public (least restrictive). When overriding a method, the subclass cannot make it more restrictive than the parent. A protected method can be overridden as protected or public, but NOT private (too restrictive) or default (package-private).

Multiple choice technology web technology
  1. Non Static field

  2. Base class fields

  3. Transient Fields

  4. none

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

During Java serialization, transient fields are explicitly ignored and not included in the serialized output. The transient keyword is used specifically to mark fields that should not be serialized. Static fields are also not serialized, but the question specifically asks about which fields are ignored, and transient is the key mechanism for this purpose.

Multiple choice technology web technology
  1. must have empty constructor

  2. should have no public fields

  3. Both a and b

  4. None of a and b

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

Java Beans must have a no-argument constructor (empty constructor) to enable instantiation by containers and frameworks. They should follow encapsulation principles by having no public fields - all data access should be through getter and setter methods. Both requirements are part of the Java Bean specification, making option C correct.

Multiple choice technology web technology
  1. 500

  2. 100

  3. 0

  4. Null Pointer Exception

Reveal answer Fill a bubble to check yourself
A Correct answer