Inheritance - Instanceof Flashcards

1
Q

What will the following expression return?

(s instance of java.util.Date)

Assume ‘s’ is a variable of class java.lang.String

A

Nothing, it won’t even compile.

The compiler knows ‘s’ can NEVER refer to java.util.Date, so it won’t accept this code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Is the following code valid?

Short k = new Short(4)

A

No, there is no constructor for Short that takes an int.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Can the left operand of the instanceof operator be a primitive?

i.e.:
short s = 4;
System.out.println(s instance of Short);

A

No!

It must be a reference variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the left-hand operand of an instanceof statement?

A

A reference variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the right-hand operand of instanceof?

A

A reference type name (class, interface, or enum)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What can be compared with the == operator?

A

Two primitives. Also, a numeric primitive and a primitive wrapper.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What cannot be compared with the == operator?

A

Two wrappers of different types. For example, you cannot compare an Integer and a Short

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Will the following code compile?

Short k = 9; Integer i = 9; System.out.println(k == i);

A

No, they must have an IS-A relationship among themselves.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Will the instanceof operator return true if the right-hand side is a super class?

i.e. for class reference foo
(foo instanceof Object)
A

Yes.

The right hand side will return true even if it is a super class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

If a.equals(b) returns true, b instanceof ClassOfA must always be true.

Right?

A

Not necessarily.

equals ( ) can be overridden.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly