Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

,15%) which of the following 9 statements (see ** r1, r2 , r9 ) cause compilatio

ID: 3730092 • Letter: #

Question

,15%) which of the following 9 statements (see ** r1, r2 , r9 ) cause compilation or runtime errors. class X class Y extends X class Z extends X the following statements creating references rl, r2,, r9 Y r2 new XO: class Test public statie void main(String argsl)t Nm = new XO; Yn = new Yo; Zrz = new ZO; object ro = new Z0; X16 = ry; Zr7 = (Z) rl; Object r8 -ry; Object = r1; 1 Ill end main Il end class Test Answer :The following statements cause compilation or runtime errors: (Note: Just need to list reference names) 3. [1596] Answer questions (i) and (ii) below: class A void 170 t/ statements static void f80 // data/fields public int b; private int e protected int d; static private int e; statie public int f final int g=5; * (0): list all fields/methods in class A that can be directly accessed here/ I/ end class A class B !/ in same package as class A publie static void main(String argy) l methods void f10 t statements A objA new A0 j* (iü): : list all fields/methods in class A that can be accessed using objA/ public void 120statements/ protected void 130 statements) private void f40 { /* statements */ } static publie void 1s01/ statements/ final void f60 statements / 1 Il end class B Answer:

Explanation / Answer

2)

statements causing Compilation errors

Reference names (r2,r3)

1) Y r2=new X();

Here incompatible type error will occur where X cannot be converted to Y

2) Z r3=rx;

Here incompatible type error will occur where X cannot be converted to Z

Runtime error

Reference name-r5

Y r5=(Y) ro;

Here ClassCastException will occur where Z can't be casted(converted) to Y

3)

(i) static methods can only access static members and static methods declared inside the class

so the fields and methods accessible inside

static void f8() are

fields- e and f (static private int e;
static public int f;)

method- f5() (static public void f5() {/* statements */}

(ii)

The important points to be noticed are

1) default variables- accessible inside the class and in the same package but not in the different package

2) private variables- accessible only inside the class but not in same package or different package

3) public variables- accessible inside the class and in the same package and also in different package

4) protected variables- accessible inside the class and in the same package but also in different package which derives particular class

Therefore the fields accesible using object are

a,b,d,f and g

methods accessible using object are

f1(), f2(),f3(),f5(),f6(),f7(),f8()

Note :the variables c and e are private variables so they are inaccessible outside the class

the method f4() is private method so they are inaccessible outside the class