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. Late binding

  2. Early binding

  3. Java developers are very introspective people

  4. Reflection is possible in all languages

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

Reflection is possible in Java because of late binding - method and field references are resolved at runtime rather than compile time. This allows programs to inspect and modify their own structure and behavior dynamically.

Multiple choice technology programming languages
  1. The Java language has loose type checking

  2. The Java language has tight type checking

  3. To keep tabs on garbage collection

  4. You never know how many you need, so you should always cover the most obvious cases

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

Because Java is a strongly (or tightly) typed language, methods cannot accept arbitrary types without explicit definitions. To allow assertions on different data types (like integers, booleans, floats, and objects), the Assert class must provide multiple overloaded implementations of its assertion methods.

Multiple choice technology programming languages
  1. True

  2. False

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

In Java, Throwable is the ultimate superclass of all errors and exceptions. While Exception is the superclass of all checked and unchecked exceptions, it inherits from Throwable. Therefore, saying Exception is the absolute superclass of all exceptional events is technically false because Throwable sits at the top of the hierarchy.

Multiple choice technology programming languages
  1. True

  2. False

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

The code is illegal because ExceptionA is a checked exception thrown by thrower(). The try block only catches ExceptionB, which is a subclass of ExceptionA. Since the superclass exception ExceptionA is neither caught nor declared in the main method's signature, the code fails to compile.

Multiple choice technology programming languages
  1. In Java, to create an applet, you can't call on functions without defining a class to call them.

  2. To appear on the web, Java must use an applet, and without first defining a class, you won't be painting onto anything.

  3. The drawString function is not defined without the "import" statements at the top.

  4. All of the above.

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

In Java applets, painting requires a proper class structure extending Applet or JApplet to provide the drawing surface. Import statements are needed for graphics classes like Graphics. The drawString method must be called within a method like paint() that belongs to a properly defined applet class. All three reasons explain why a single painting line is insufficient.

Multiple choice technology programming languages
  1. When a method in a class overrides a method in a superclass

  2. When a method in an interface overrides a method in a superinterface

  3. For overridden constructors

  4. When a method in a class implements a method in an interface

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

Javadoc automatically inherits documentation when a method overrides a superclass method, overrides a superinterface method, or implements an interface method. This prevents redundant documentation. However, constructors cannot be overridden in Java, meaning they cannot inherit comments, making that option incorrect.

Multiple choice technology architecture
  1. They can be applied to both data & methods

  2. They must precede a class's data variables or methods

  3. They can follow a class's data variables or methods

  4. They can appear in any order

  5. They must be applied to data variables first and then to methods

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

Access modifiers (public, private, protected) can be applied to both data fields and methods. They precede the member declaration and can appear in any order relative to other modifiers like static or final. The statement that they can follow data variables is incorrect.

Multiple choice technology architecture
  1. They are not associated with any instance of an outer class

  2. They may access final initialized variables and parameters that are in the scope of the statement block in which the class is declared

  3. They may be declared public, protected or private

  4. They may not implement an interface

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

Local inner classes are declared inside method blocks and are not associated with any outer class instance. They can only access final or effectively final local variables and parameters from their enclosing scope. They cannot have access modifiers (public, protected, private) and can implement interfaces.

Multiple choice technology programming languages
  1. Inheritance represents an is-a relationship

  2. . Inheritance represents a has-a relationship

  3. Interfaces must be used when creating a has-a relationship

  4. Instance variables can be used when creating a has-a relationship

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

Inheritance models an is‑a relationship (a subclass is‑a type of its superclass). A has‑a relationship is expressed by including instance variables that reference other objects. Therefore the true statements are the inheritance one and the instance‑variable one; the others mischaracterize the relationships.

Multiple choice technology
  1. Inherits from @baseclass

  2. Inherits from Work-

  3. Contains most of your business logic

  4. Is usually the same as a “bottom level” class

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

In Pega's class hierarchy, @baseclass is the ultimate root class from which all classes inherit. Top-level classes directly inherit from @baseclass, while Work- and other pattern classes are specialized subclasses. Business logic resides in lower-level implementation classes, not top-level structural ones.

Multiple choice technology programming languages
  1. Java.lang

  2. Java.nio

  3. Java.rmi

  4. Java.basic

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

The java.lang package provides classes fundamental to the Java programming language itself, including Object, String, wrapper classes, and basic math operations. This package is automatically imported into all Java programs. Java.nio handles I/O, Java.rmi handles remote method invocation, and Java.basic is not a standard package.

Multiple choice technology programming languages
  1. pc

  2. pcc

  3. pcp

  4. pcpc

  5. Compilation fails

  6. An exception is thrown at runtime

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

During serialization, the parent class constructor is called when creating c1, printing 'p', then child constructor prints 'c'. During deserialization, constructors are not called for the object itself, but the parent class constructor runs again, printing another 'p'. The output is 'pcp'. Option C is correct.

Multiple choice technology programming languages
  1. Protected

  2. Private

  3. Public

  4. 2&3

  5. 1&3

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

When overriding a protected method in Java, the subclass method must have the same or wider access modifier. Protected is the baseline, so valid options are Protected (same) or Public (wider). Private is narrower and thus invalid. Option E correctly identifies Protected and Public as valid.

Multiple choice technology programming languages
  1. Non Static field

  2. Base class fields

  3. Transient Fields.

  4. Static Field

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

During Java serialization, fields marked with the 'transient' keyword are explicitly ignored and not written to the serialization stream. This allows developers to exclude sensitive or non-serializable data. Static fields are also not serialized, but 'transient' is the specific mechanism for ignoring instance fields.