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 programming languages
  1. Classes in class diagrams may be grouped into packages in order to illustrate the overall organization of a model

  2. In object diagrams, names of instances are in italics or all-caps

  3. If package B depends on package A, then any change in A will require a change in B

  4. Object diagrams and class diagrams are completely interchangeable

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

In UML class diagrams, classes can be grouped into packages to organize the model. Object diagram names are underlined (not italicized), package dependencies do not always force changes, and class/object diagrams are not fully interchangeable.

Multiple choice technology programming languages
  1. methods in the associated classes

  2. subclasses needed to accomplish the same functionality

  3. case statements and conditionals

  4. coupling between classes in the system

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

Polymorphism's key advantage is replacing conditional logic (case/switch statements and if-else chains) with dynamic dispatch through virtual functions. Instead of checking type and executing corresponding code, polymorphism lets each subclass implement its own behavior, invoked through a common interface. This reduces conditional complexity.

Multiple choice technology programming languages
  1. Multiple Inheritance and Overloading

  2. Operator Overloading and Overriding

  3. Multiple Inheritance and Operator Overloading

  4. Pointers and Single Inheritance

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

Java does not support multiple inheritance of classes (to avoid the diamond problem) nor does it support user-defined operator overloading. Therefore, this pair is not supported in Java.

Multiple choice technology programming languages
  1. any name

  2. x

  3. exforsys

  4. None of the Above

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

In Java and similar object-oriented languages, a constructor must have the exact same name as its class. If the class is named 'exforsys', the constructor must also be named 'exforsys'. The constructor name cannot be arbitrary (option A), cannot be the same as a field name (option B), and option D is incorrect because there is a specific answer.

Multiple choice technology programming languages
  1. Does not compile

  2. prints 0

  3. prints 10

  4. prints 7

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

The code fails to compile due to two errors: (1) 'class B extends class A' - should be 'class B extends A' without the word 'class' before A, and (2) class A has a private constructor, so it cannot be instantiated in main(). The code has multiple compilation errors preventing it from running.

Multiple choice technology programming languages
  1. public void amethod(int i) throws FileNotFoundException{ }

  2. public void amethod(int i) throws IOException, RuntimeException{ }

  3. public void amethod(int i) throws RuntimeException{ }

  4. public void amethod(int i) throws Exception{ }

  5. public void amethod(int i) { }

  6. public void amethod(int i) throws Throwable{ }

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

When overriding a method that throws IOException, you can: throw no exceptions (E), throw fewer or same checked exceptions (A - FileNotFoundException is subclass of IOException), add unchecked exceptions freely (B, C - RuntimeException is unchecked), but you cannot throw broader checked exceptions (D - Exception, F - Throwable). Options A, B, C, E all follow these rules.

Multiple choice technology programming languages
  1. private

  2. protected

  3. transient

  4. public

  5. final

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

A top-level Java class can only be declared with 'public', 'abstract', or 'final' access modifiers, or default (package-private) access. 'private', 'protected', and 'transient' cannot be applied to top-level classes.

Multiple choice technology programming languages
  1. A Bar is a Baz

  2. A Foo has a Bar

  3. A Baz is a Foo

  4. A Foo is a Baz

  5. A Baz has a Bar

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

Baz extends Foo, so every Baz object is also a Foo through inheritance (is-a relationship). Baz contains a Bar field named 'thing', so a Baz has-a Bar. Option C correctly states the inheritance relationship, and E correctly states the composition relationship.

Multiple choice technology programming languages
  1. It exhibits high cohesion

  2. It exhibits low cohesion

  3. It exhibits tight coupling

  4. It exhibits loose coupling

  5. None of the Above

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

The Waiter class methods all work closely together to perform related tasks regarding order handling and serving. This strong functional relatedness within a single class represents high cohesion.

Multiple choice technology programming languages
  1. Does not compile

  2. prints "ABC"

  3. prints "ACB"

  4. prints "CAB"

  5. run time exception

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

In Java, either this() or super() can be the first statement in a constructor, but never both. The no-arg B() constructor attempts to call this(4) followed by super(), which violates Java's constructor chaining rules. Since this(4) must be first and it will implicitly call super(), the explicit super() call causes compilation failure.