Casting and Instanceof

Quiz Guidelines

 

Multiple Choice Question

QID: 
400

Consider the following code:

interface Flyer{ }
class Bird implements Flyer { }
class Eagle extends Bird { }
class Bat { }
public class TestClass {
public static void main(String[] args) {
Flyer f = new Eagle();
Eagle e = new Eagle();
Bat b = new Bat();
if(f instanceof Bird) System.out.println("f is a Bird");
if(e instanceof Flyer) System.out.println("e is a Flyer");
if(b instanceof Flyer) System.out.println("f is a Bird");
}
}

What will be printed when the above code is compiled and run?

Select 1 option

A. It will not compile.
B. It will throw an exception when run.
C. f is a Bird
e is a Flyer
D. f is a Bird
E. e is a Flyer

Multiple Choice Question

QID: 
399

Consider the following code:

interface Flyer{ }
class Bird implements Flyer { }
class Eagle extends Bird { }
class Bat { }
public class TestClass {
public static void main(String[] args) {
Flyer f = new Eagle();
Eagle e = new Eagle();
Bat b = new Bat();
if(f instanceof Flyer) System.out.println("f is a Flyer");
if(e instanceof Bird) System.out.println("e is a Bird");
if(b instanceof Bird) System.out.println("f is a Bird");
}
}

What will be printed when the above code is compiled and run?

Select 1 option

A. It will not compile.
B. It will throw an exception when run.
C. f is a Flyer
    e is a Bird
D. f is a Flyer
E. e is a Bird

Multiple Choice Question

QID: 
398

Which of the given statements are correct about the following code?

//Filename: TestClass.java

class TestClass{
public static void main(String[] args){
A a = new A();
B b = new B();
};
}
class A implements T1, T2{}
class B extends A implements T1{}
interface T1 { }
interface T2 { }

Select 4 options

A. (a instanceof T1) will return true.
B. (a instanceof T2) will return true.
C. (b instanceof T1) will return true.
D. (b instanceof T2) will return true.
E. (b instanceof A) will return false.

Multiple Choice Question

QID: 
397

Consider :
class A {}
class B extends A {}
class C extends B {}

Which of these boolean expressions correctly identifies when an object 'o' actually refers to an object of class B and not of C?

Select 2 options

A. (o instanceof B) && (!(o instanceof A))
B. !((o instanceof A) || (o instanceof B))
C. (o instanceof B) && (!(o instanceof C) 
D. ! ( !(o instanceof B) || (o instanceof C))
E. (o instanceof B) && !((o instanceof A) || (o instanceof C))

True or False

QID: 
396

If a.equals(b) returns true, b instanceof ClassOfA must always be true.
(Assume that ClassOfA is the name of the class of the variable a.)

Select 1 option

A. True
B. False

Pages