Casting and Instanceof

Quiz Guidelines

 

True or False

QID: 
395

Expression (s instanceof java.util.Date) will return false if 's' was declared as a variable of class java.lang.String.

Select 1 option

A. True
B. False

True or False

QID: 
394

Given the following class definitions, the expression

(obj instanceof A) && ! (obj instanceof C) && ! (obj instanceof D)

correctly identifies whether the object referred to by obj was created by instantiating class B rather than classes A, C and D?

class A {}
class B extends A {}
class C extends B {}
class D extends C {}

Select 1 option

A. True
B. False

Multiple Choice Question

QID: 
393

Which of the following statements are true?

Select 1 option

A. For any non-null reference o1, the expression (o1 instanceof o1) will always yield true.
B. For any non-null reference o1, the expression (o1 instanceof Object) will always yield true.
C. For any non-null reference o1, the expression (o1 instanceof o1) will always yield false.
D. For any non-null reference o1, the expression (o1 instanceof Object) may yield false.
E. None of the above

True or False

QID: 
392

Given the following class definitions and declaration:
  class A {}
  class B extends A {}
  class C extends B {}
  class D extends C {}
  D d = new D();
the expression (d instanceof A) will return true.

Select 1 option

A. True
B. False

Multiple Choice Question

QID: 
391

Which letters will be printed when the following program is run?

public class TestClass{
public static void main(String args[]){
B b = new C();
A a = b;
if (a instanceof A) System.out.println("A");
if (a instanceof B) System.out.println("B");
if (a instanceof C) System.out.println("C");
if (a instanceof D) System.out.println("D");
}
}class A { }
class B extends A { }
class C extends B { }
class D extends C { }

Select 3 options

A. A will be printed.
B. B will be printed.
C. C will be printed.
D. D will be printed.

Pages