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

  2. tcs

  3. null

  4. Exception

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

The output is null, not 'tcs' or 'UX'. The testPoly() method returns a TestPolymorphism2 object, but the return value is discarded - it's not assigned to TSuper. TSuper.name remains null because the superclass's name field was never initialized (the subclass's local name is different).

Multiple choice technology programming languages
  1. TestSup = 100 ,TestPoly = 400 ,TP = 500 ,TS = 500 ,TSP = 500

  2. TestSup = 500 ,TestPoly = 500 ,TP = 500 ,TS = 500 ,TSP = 500

  3. TestSup = 100 ,TestPoly = 400 ,TP = 400 ,TS = 100 ,TSP = 500

  4. TestSup = 500 ,TestPoly = 400 ,TP = 500 ,TS = 100 ,TSP = 400

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

Since the variable p is declared as static in the superclass TestSup, a single shared copy of p exists for both classes and all instances. Modifying p through any reference (TP, TS, TSP) updates this single value. The last assignment sets p to 500, so all print statements output 500.

Multiple choice technology programming languages
  1. p = 200.0, p1 = 30.0

  2. p = 200.0, p1 = 200.0

  3. p = 30.0, p1 = 30.0

  4. Compiler Error

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

Compiler error occurs because p1 is not declared - 'p1 = TSU.TestPoly(11, 9.0, 10);' lacks a type declaration. Should be 'double p1 = ...'. The code has nothing to do with polymorphism behavior - it's a simple syntax error.

Multiple choice technology programming languages
  1. TestSupe = 300 ,TestPoly = 400 ,TP = 400 ,TS = 300 ,TSP = 500

  2. TestSupe = 500 ,TestPoly = 400 ,TP = 400 ,TS = 500 ,TSP = 500

  3. TestSupe = 500 ,TestPoly = 400 ,TP = 400 ,TS = 300 ,TSP = 500

  4. TestSupe = 300 ,TestPoly = 400 ,TP = 500 ,TS = 500 ,TSP = 500

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

In Java, static variables are NOT polymorphic - they are bound to the reference type, not the actual object type. When we assign TSP.p = 500, TSP is declared as TestSupe, so it sets TestSupe.p to 500. Similarly, TP.p = 400 sets TestPoly1.p to 400. When printing static variables using the class name (TestSupe.p or TestPoly1.p), we access the variable for that specific class. This is static variable hiding, not overriding.

Multiple choice technology web technology
  1. Object attributes are not of any specific data type

  2. The constructor of the class is not correctly defined

  3. You cannot put an equal when recieving a parameter in the constructor

  4. The word function is missing before the class constructor

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

In PHP 5, class constructors must be declared with the 'function' keyword before __construct() or the class-name method. The constructor is a special method that's automatically called when an object is instantiated, but it still requires standard function declaration syntax.

Multiple choice technology programming languages
  1. The same program logic can be used with objects of several related types.

  2. Variables can be re-used in order to save memory.

  3. Constructing new objects from old objects of a similar type saves time.

  4. Polymorphism is a dangerous aspect of inheritance and should be avoided.

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

Polymorphism allows the same code to work with different types of objects that share a common interface or base class. This enables writing more flexible and reusable code. Options B and C describe benefits unrelated to polymorphism. Option D is incorrect - polymorphism is a core OOP benefit, not something to avoid.

Multiple choice technology programming languages
  1. One per defined class.

  2. One per constructor definition.

  3. As many as the program needs.

  4. One per program

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

A class is a template - you can create as many objects (instances) of a class as your program needs, limited only by available memory. There's no built-in limit of one object per class, constructor, or program. Objects are created using the 'new' keyword and can be instantiated repeatedly.

Multiple choice technology programming languages
  1. True

  2. False

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

This is False due to constant ambiguity. When a class implements two interfaces that both define the same constant (PI), Java cannot determine which interface's constant to reference. This causes a compilation error. The constant must be explicitly qualified with the interface name (e.g., MyInterface1.PI) to resolve the ambiguity.

Multiple choice technology programming languages
  1. A pattern (design) whereby collaborators are passed into an object from the outside - the object does not allocate them for itself.

  2. It is a pattern in which the child class inherit the parent class properties

  3. It is a type of operator overloading in java

  4. It represents the compile time and run time polymorphism in java

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

Dependency injection is a design pattern where objects receive their dependencies from external sources rather than creating them internally. This promotes loose coupling, easier testing, and better code maintainability by allowing dependencies to be swapped or mocked.

Multiple choice technology programming languages
  1. A Java class encapsulating one or more advices.

  2. A child class inheriting properties of parent class

  3. A class that implements an interface

  4. Transaction handling capability of spring framework

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

In Aspect-Oriented Programming (AOP), particularly in Spring AOP, an aspect is a modularization of a concern that cuts across multiple objects, typically implemented as a regular Java class annotated or configured to encapsulate advices and pointcuts. The other options describe inheritance, interface implementation, or specific framework capabilities.

Multiple choice technology programming languages
  1. The Adapter pattern implements an interface known to its clients and provides an instance of a class not known to its clients.

  2. The Bridge pattern implements an interface known to its clients and provides an instance of a class not known to its clients.

  3. The Adapter pattern creates a separation between abstractions and classes that implement those abstractions.

  4. The Bridge pattern creates a separation between abstractions and classes that implement those abstractions.

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

Adapter wraps an incompatible class to match an expected interface, making unrelated classes work together. Bridge separates abstraction from implementation so both can vary independently. Adapter is about making incompatible interfaces compatible after-the-fact, while Bridge prevents permanent binding between abstraction and implementation.

Multiple choice technology programming languages
  1. Abstract factory

  2. Builder

  3. Prototype

  4. Singleton

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

The Prototype pattern allows creating new objects by copying an existing instance (prototype) without knowing their concrete class or how to construct them. This matches the requirements of a custom class loader loading classes dynamically. Builder, Abstract Factory, and Singleton require explicit creation logic or type knowledge.

Multiple choice technology programming languages
  1. Strategy

  2. Adapter

  3. Template Method

  4. Interpreter

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

Template Method defines algorithm skeleton in base class, letting subclasses override specific steps without changing structure. This lets vendors customize spell-checking logic while keeping core algorithm unchanged. Strategy encapsulates entire algorithms, while Template only varies steps within a fixed algorithm structure.