Object Orientation

12 important questions on Object Orientation

What is the result? A. BD
B. DB
C. BDC
D. DBC
E. Compilation fails

E is correct. The implied super() call in Bottom2’s constructor cannot be satisfied because there is no no-arg constructor in Top. A default, no-arg constructor is generated by the compiler only if the class has no constructor defined explicitly

What is the result? A. Clidlet
B. Clidder
C. Clidder Clidlet
D. Clidlet Clidder
E. Compilation fails

A is correct. Although a final method cannot be overridden, in this case, the method is private, and therefore hidden. The effect is that a new, accessible, method flipper is created. Therefore, no polymorphism occurs in this example, the method invoked is simply that of the child class, and no error occurs.

What is the result? A. pre b1 b2 r3 r2 hawk
B. pre b2 b1 r2 r3 hawk
C. pre b2 b1 r2 r3 hawk r1 r4
D. r1 r4 pre b1 b2 r3 r2 hawk
E. r1 r4 pre b2 b1 r2 r3 hawk
F. pre r1 r4 b1 b2 r3 r2 hawk
G. pre r1 r4 b2 b1 r2 r3 hawk
H. The order of output cannot be predicted
I. Compilation fails

D is correct. Static init blocks are executed at class loading time; instance init blocks run right after the call to super() in a constructor. When multiple init blocks of a single type occur in a class, they run in order, from the top down.
  • Higher grades + faster learning
  • Never study anything twice
  • 100% sure, 100% understanding
Discover Study Smart

Which of the following, inserted at line 9, will compile? (Choose all that apply.) A. x2.do2();
B. (Y)x2.do2();
C. ((Y)x2).do2();
D. None of the above statements will compile

C is correct. Before you can invoke Y’s do2 method, you have to cast x2 to be of type Y

What is the result? (Choose all that apply.) A. 2 will be included in the output
B. 3 will be included in the output
C. hi will be included in the output
D. Compilation fails
E. An exception is thrown at runtime

A is correct. It’s legal to overload main(). Since no instances of Locomotive are created, the constructor does not run and the overloaded version of main() does not run.

What is the result? (Choose all that apply.) A. howl howl sniff
B. howl woof sniff
C. howl howl followed by an exception
D. howl woof followed by an exception
E. Compilation fails with an error at line 14
F. Compilation fails with an error at line 15

F is correct. Class Dog doesn’t have a sniff method.

What is the result? (Choose all that apply.) A. An exception is thrown at runtime
B. The code compiles and runs with no output
C. Compilation fails with an error at line 8
D. Compilation fails with an error at line 9
E. Compilation fails with an error at line 12
F. Compilation fails with an error at line 13

A is correct. A ClassCastException will be thrown when the code attempts to downcast a Tree to a Redwood.

What is the result? A. fa fa
B. fa la
C. la la
D. Compilation fails
E. An exception is thrown at runtime

B is correct. The code is correct, but polymorphism doesn’t apply to static methods.

What is the result?
A. subsub
B. sub subsub
C. alpha subsub
D. alpha sub subsub
E. Compilation fails
F. An exception is thrown at runtime

C is correct. Watch out, because SubSubAlpha extends Alpha! Since the code doesn’t attempt to make a SubAlpha, the private constructor in SubAlpha is okay.

What is the result? A. h hn x
B. hn x h
C. b h hn x
D. b hn x h
E. bn x h hn x
F. b bn x h hn x
G. bn x b h hn x
H. Compilation fails

C is correct. Remember that constructors call their superclass constructors, which execute first, and that constructors can be overloaded.

What is the result? A. furry bray
B. stripes bray
C. furry generic noise
D. stripes generic noise
E. Compilation fails
F. An exception is thrown at runtime

A is correct. Polymorphism is only for instance methods, not instance variables.

You’re designing a new online board game in which Floozels are a type of Jammers, Jammers can have Quizels, Quizels are a type of Klakker, and Floozels can have several Floozets. Which of the following fragments represent this design? (Choose all that apply.)

A and C are correct. The phrase “type of” indicates an IS-A relationship (extends or implements), and the word “have” of course indicates a HAS-A relationship (usually instance variables).

The question on the page originate from the summary of the following study material:

  • A unique study and practice tool
  • Never study anything twice again
  • Get the grades you hope for
  • 100% sure, 100% understanding
Remember faster, study better. Scientifically proven.
Trustpilot Logo