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. 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 web technology
  1. Interface Injection

  2. Setter Injection

  3. Constructoir Injection

  4. Constructor Arguments

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

Inversion of Control/Dependency Injection in Spring can be implemented through three main types: Interface Injection (where dependencies are injected through interfaces), Setter Injection (through setter methods), and Constructor Injection (through constructor parameters).

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.

Multiple choice technology programming languages
  1. Parent

  2. Child

  3. Both

  4. None of these

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

Static methods are bound at compile time based on reference type, not actual object type. Parent p = new Child(); p.staticMethod() calls Parent's version. This is because static methods belong to the class, not instances.

Multiple choice technology programming languages
  1. return type should be different

  2. access speciifer should be different

  3. arguments should be different

  4. the overloaded methods should throw a different excpetion

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

Method overloading requires different parameter lists (type, count, or order). Return type, access specifier, and exception throws alone don't distinguish overloaded methods - the compiler would see them as duplicate methods.

Multiple choice technology programming languages
  1. Overriding method can not throw more generic exception than base method

  2. Overriding method can throw new or broader checked exceptions

  3. None of the above

  4. Both a and b

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

In Java, an overriding method in a subclass cannot throw broader or more generic checked exceptions than the overridden method in the superclass. Doing so violates the Liskov Substitution Principle and causes a compilation error.