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

  2. False

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

Constructors cannot be abstract (they must be implemented), static (they operate on instances), or native/synchronized (these modifiers don't apply to constructors). Constructors can be final in some languages to prevent subclassing, but the statement claims all modifiers are valid which is incorrect.

Multiple choice technology programming languages
  1. True

  2. False

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

Constructors can indeed have public, protected, or private access modifiers. Public constructors allow instantiation from anywhere, protected from subclasses and same package, and private restricts instantiation (used in singleton pattern). The default constructor's access matches the class's access modifier.

Multiple choice technology programming languages
  1. True

  2. False

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

Constructors can use this() to call another constructor in the same class (constructor chaining). This allows you to avoid code duplication by having one constructor do the common work and others call it with appropriate default parameters. The called constructor must have a different parameter list.

Multiple choice technology programming languages
  1. True

  2. False

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

When a constructor uses this() to call another constructor, it MUST be the first statement in the constructor body. This ensures initialization happens in the correct order - you can't do other work before delegating to another constructor. If it's not first, the compiler will generate an error.

Multiple choice technology programming languages
  1. True

  2. False

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

The default constructor's access modifier IS the same as the class modifier. If the class is public, the default constructor is public. If the class has package-private access, the default constructor is also package-private. This ensures the constructor is accessible wherever the class itself is accessible.

Multiple choice technology programming languages
  1. True

  2. False

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

When a constructor uses super() to call the parent class constructor, it MUST be the first statement. This ensures the parent class is properly initialized before the child class does its own initialization. The compiler enforces this rule and will complain if super() is not first.

Multiple choice technology programming languages
  1. "X extends Y" is correct if and only if X is a class and Y is an interface

  2. "X extends Y" is correct if and only if X and Y are either both classes or both interfaces

  3. "X extends Y" is correct if and only if X is an interface and Y is a class

  4. None of these

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

In Java, the extends keyword is used for both class inheritance and interface inheritance. A class can extend another class, and an interface can extend another interface, but they cannot be mixed - a class implements an interface using the implements keyword, not extends.

Multiple choice technology programming languages
  1. Interface can extend one or more interfaces

  2. Interface can extend one or more class

  3. Interface cannot implement a class or interface

  4. All of the above

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

In Java and similar OO languages, an interface can extend one or more interfaces (multiple inheritance is allowed for interfaces), but cannot extend classes. Classes implement interfaces. Option C is incomplete, and 'All of the above' is incorrect because interfaces don't extend classes.