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 web technology
  1. Implements java.io.Serializable

  2. Override equals() method

  3. Override hashCode() method

  4. Both a, b and c

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

Component classes require implementing Serializable for persistence/caching, and must override equals() and hashCode() for proper behavior in collections and as cache keys. All three requirements are mandatory for component classes in frameworks like Hibernate.

Multiple choice technology web technology
  1. Transient, persistent, destroyed

  2. Transient, stored, detached

  3. Transient, persistent, detached

  4. open, close, detached

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

Hibernate defines three states for persistent classes: transient (not associated with any session, no database row), persistent (associated with a session and has a database identity), and detached (formerly persistent but no longer associated with a session, still has database identity).

Multiple choice technology web technology
  1. Override equals() method

  2. Override hashCode() method

  3. Both a and b

  4. Neither a nor b

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

Java's Set implementations rely on both equals() and hashCode() to determine uniqueness and prevent duplicates. When storing persistent objects in a Set, you must override both methods to ensure correct behavior during add/contains operations.

Multiple choice technology web technology
  1. In classes that implement IEnumerable, the collection is not serialized

  2. You cannot serialize an object's public fields.

  3. You cannot serialize XmlNode object

  4. It does not serialize type information.

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

XML serialization in .NET cannot serialize type information, which means polymorphic behavior is lost during serialization. When an object is serialized and then deserialized, it may not retain its original runtime type. This is a key limitation compared to binary serialization.

Multiple choice technology web technology
  1. Delegation

  2. Composition

  3. Polymorphism

  4. Inheritance

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

The code demonstrates Composition - a 'has-a' relationship between classes. Car contains an Engine object (myEngine), and Engine contains an array of Piston objects (myPistons). This is object composition, not inheritance (is-a relationship), delegation (forwarding calls), or polymorphism (same interface different implementations).

Multiple choice technology programming languages
  1. to get to access hardware that Java does not know about

  2. to define a new data type such as an unsigned integer

  3. to write optimised code for performance in a language such as C/C++

  4. to overcome the limitation of the private scope of the method

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

Native methods are defined to access hardware features that Java doesn't support directly (A), and to write performance-critical code in lower-level languages like C/C++ (C). They do not define new data types (B is wrong) and don't overcome private scope limitations (D is wrong).

Multiple choice technology programming languages
  1. Nested class is a way of logically grouping classes that are only used in one place.

  2. A static nested class can refer directly to instance variables or methods defined in its enclosing class.

  3. Nested classes can lead to more readable and maintainable code.

  4. A static nested class interacts with the instance members of its outer class.

Reveal answer Fill a bubble to check yourself
A,C,D Correct answer
Multiple choice technology programming languages
  1. True

  2. False

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

Type erasure is a fundamental process in Java generics where the compiler removes all generic type information at compile time. Generic types like List are converted to their raw forms (List), and type parameters are replaced with their bounds or Object. This ensures backward compatibility with pre-generic Java code while maintaining type safety during compilation.

Multiple choice technology programming languages
  1. implements

  2. use

  3. extends

  4. extend

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

Java uses the 'extends' keyword to declare bounded type parameters in generics, regardless of whether you're bounding to a class or interface. This creates an upper bound - the type parameter must be the specified type or a subclass. 'implements' is used for class declarations, not type parameters. 'extend' is a common misspelling - the keyword is 'extends'.

Multiple choice technology programming languages
  1. Error

  2. Hello 1

  3. Hi 3

  4. Hello 3

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

The line A = B reassigns the class name A to point to class B. Therefore, A() creates an instance of class B, and calling a.foo() executes B's foo method, which prints 'Hi 3'.

Multiple choice technology programming languages
  1. True

  2. False

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

Final variables must be initialized exactly once. Static final variables get their value from static initializers (or the declaration), while instance final variables can be initialized in constructors (or instance initializers). This is a fundamental Java rule.