Java OCA OCP Practice Question 1812

Question

Given the following classes and declarations, which statements are true?.

// Classes//w  ww.  j  a  v a  2  s .c o m
class MyBaseClass {
  private int i;
  public void f() { /* ... */ }
  public void g() { /* ... */ }
}

class Main extends MyBaseClass {
  public int j;
  public void g() { /* ... */ }
}

// Declarations:
  MyBaseClass a = new MyBaseClass();
  Main b = new Main();

Select the three correct answers.

  • (a) The Main class is a subclass of MyBaseClass.
  • (b) The statement b.f(); is legal.
  • (c) The statement a.j = 5; is legal.
  • (d) The statement a.g(); is legal.
  • (e) The statement b.i = 3; is legal.


(a), (b), and (d)

Note

Main is a subclass of MyBaseClass that overrides the method g().

The statement a.j = 5 is not legal, since the member j in the class Main cannot be accessed through a MyBaseClass reference.

The statement b.i = 3 is not legal either, since the private member i cannot be accessed from outside of the class MyBaseClass.




PreviousNext

Related