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
  1. .h

  2. .java

  3. >java

  4. .class

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

When Java source code is compiled, each class is converted to bytecode and stored in a separate file with the .class extension. This bytecode file contains instructions that the JVM can execute. The filename matches the class name (case-sensitive).

Multiple choice technology
  1. Dependent

  2. Neutral

  3. Independent

  4. None of the above

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

Java is Architectural Neutral because the compiled bytecode can run on any processor architecture. The JVM abstracts the underlying hardware, allowing the same bytecode to execute on different platforms without recompilation. This is a key feature that enables Java's 'write once, run anywhere' capability.

Multiple choice technology architecture
  1. Prototype

  2. Factory method

  3. Abstract factory

  4. Proxy

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

The Prototype pattern specifies object creation by cloning a prototypical instance instead of creating new instances from scratch. It's useful when creating a new object is expensive or complex, and you want to create new objects by copying this prototype and then modifying as needed.

Multiple choice technology architecture
  1. Decorator

  2. Observer

  3. State

  4. Strategy

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

The State pattern allows an object to change its behavior when its internal state changes, making it appear as if the object has changed its class. This is exactly what the question describes. Decorator adds responsibilities dynamically, Observer notifies dependents of state changes, and Strategy encapsulates interchangeable algorithms.

Multiple choice technology architecture
  1. Decorator

  2. Observer

  3. State

  4. Strategy

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

The State pattern allows an object to change its behavior when its internal state changes, making it appear as if the object has changed its class. This is exactly what the question describes. Decorator adds responsibilities dynamically, Observer notifies dependents of state changes, and Strategy encapsulates interchangeable algorithms.

Multiple choice technology architecture
  1. Meditator

  2. Template method

  3. Adaptor

  4. Bridge

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

The Adapter pattern converts the interface of a class into another interface clients expect. Option A should be 'Mediator' not 'Meditator' (typo). Template method defines skeleton of algorithm, Bridge separates abstraction from implementation. Adaptor (Adapter) is correct.

Multiple choice technology architecture
  1. Mediator

  2. Memento

  3. Strategy

  4. Decorator

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

The Decorator pattern attaches additional responsibilities to an object dynamically, providing a flexible alternative to subclassing for extending functionality. Mediator defines how objects interact, Memento captures/restores state, and Strategy encapsulates interchangeable algorithms.

Multiple choice technology architecture
  1. Prototype

  2. Builder

  3. Template method

  4. Momento

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

The Builder pattern separates the construction of a complex object from its representation, allowing the same construction process to create different representations. Prototype clones objects, Template method defines algorithm skeleton, and Momento (Memento) captures/restores state.

Multiple choice technology architecture
  1. Visitor

  2. Momento

  3. Mediator

  4. Bridge

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

The Mediator pattern defines an object that encapsulates how a set of objects interact, promoting loose coupling by keeping objects from referring to each other explicitly and letting you vary their interaction independently. Visitor separates operations from object structure, Momento captures state, and Bridge separates abstraction from implementation.

Multiple choice technology programming languages
  1. a) By using the mutable="false" attribute in the class mapping.

  2. b) By using the ismutable="false" attribute in the class mapping.

  3. c) By using the ismutable="no" attribute in the class mapping.

  4. d) None

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

In Hibernate ORM mapping, the mutable="false" attribute tells Hibernate that the entity's state never changes after initialization. This allows Hibernate to optimize performance by skipping dirty checking during flush operations for this entity. The ismutable attribute name doesn't exist in Hibernate.

Multiple choice technology databases
  1. a) Only local or packaged sub programs can be overloaded.

  2. b) Overloading allows different functions with the same name that differ only in their return types.

  3. c) Overloading allows different subprograms with the same number, type and order of the parameter.

  4. d) Overloading allows different subprograms with the same name and same number or type of the parameters.

  5. e) Overloading allows different subprograms with the same name but different in either number or type or order of parameter.

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

Package overloading allows multiple subprograms with the same name if they differ in the number, type, or order of parameters (option E). Only local (private) subprograms within the package body or packaged subprograms in the specification can be overloaded (option A). Option B is false because subprograms cannot differ only in return type - the parameter signature must be different. Option C is false because identical parameters would create a duplicate, not an overload. Option D is false because the parameters must differ in some way.

Multiple choice technology databases
  1. a) Packages can be nested.

  2. b) You can pass parameters to packages.

  3. c) A package is loaded into memory each time it is invoked.

  4. d) The contents of packages can be shared by many applications.

  5. e) You can achieve information hiding by making package constructs private.

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

Packages are designed to be shared - their contents (specification and body) can be used by many applications and users simultaneously (option D). Information hiding is achieved by declaring constructs in the package body only, making them private and inaccessible outside the package (option E). Packages cannot be nested (option A). Parameters are passed to procedures/functions within packages, not to packages themselves (option B). Packages are loaded once per session and cached, not reloaded each invocation (option C).