Which three are valid? (Choose three.)

Given:

class ClassA {}
class ClassB extends ClassA {}
class ClassC extends ClassA {}
and:
ClassA p0 = new ClassA();
ClassB p1 = new ClassB();
ClassC p2 = new ClassC();
ClassA p3 = new ClassB();
ClassA p4 = new ClassC();

Which three are valid? (Choose three.)

Given:

class ClassA {}
class ClassB extends ClassA {}
class ClassC extends ClassA {}
and:
ClassA p0 = new ClassA();
ClassB p1 = new ClassB();
ClassC p2 = new ClassC();
ClassA p3 = new ClassB();
ClassA p4 = new ClassC();

Which three are valid? (Choose three.)

A.
p0 = p1;

B.
p1 = p2;

C.
p2 = p4;

D.
p2 = (ClassC)p1;

E.
p1 = (ClassB)p3;

F.
p2 = (ClassC)p4;

Explanation:
A:
compiled successfully

B:
incompatible types
found : ClassC
required: ClassB
p1 = p2;
^

C:
incompatible types
found : ClassA
required: ClassC
p2 = p4;
^

D:
inconvertible types
found : ClassB
required: ClassC
p2 = (ClassC)p1;
^

E:
compiled successfully

F:
compiled successfully



Leave a Reply 0

Your email address will not be published. Required fields are marked *