Question Core Java
Question Core Java
4.1.4. What is the result of compiling and running the following code?
what is the result of compiling and running the following code?
interface Colorable { }
interface Bouncable extends Colorable { }
class Super implements Bouncable { }
class Sub extends Super implements Bouncable { }
class Individual { }
public class Tester {
public static void main(String[] args) {
System.out.print(new Sub() instanceof Super);
4.1.5. Will the following code compile and if yes , what is the output?
Will the following code compile and if yes , what is the output?
interface Colorable {}
class Vehicle {}
class Car extends Vehicle implements Colorable {}
public class Tester {
public static void main(String[] args) {
Vehicle a = new Car();
Colorable i = (Colorable) a;
ClassCastException: Vehicle cannot be cast to Colorable
Vehicle b = new Vehicle();
at Tester.main(Tester.java:14)<<<this for second casting
Colorable j = (Colorable) b;
}
}
Please choose only one answer:
The code compiles fine and produces no output
The code compiles fine but throws a RuntimeException because of the second casting
There is a compilation error because of the second casting
The code compiles fine but throws a RuntimeException because of the first casting
4.1.6. Would the following code compile and if yes , what is the output?
Would the following code compile and if yes , what is the output?
interface Colorable {}
class Vehicle {}
class Car extends Vehicle implements Colorable {}
public class Tester {
public static void main(String[] args) {
Vehicle a = new Car();
Colorable i = (Colorable) a;
Vehicle b = new Vehicle();
Colorable j = (Colorable) b;
}
}
Please choose only one answer:
The code compiles fine and produces no output
The code compiles fine but throws a RuntimeException because of the second casting
There is a compilation error because of the second casting
4.1.7. What is the output of compiling and running the following program?
What is the output of compiling and running the following program?
class Category {
Category() {
System.out.print("Category_");
}
}
class SubCategory extends Category {
SubCategory() {
System.out.print("SubCategory_");
}
}
class SubSubCategory extends SubCategory {
SubSubCategory() {
System.out.print("SubSubCategory_");
}
}
public class Tester {
public static void main(String[] args) {
new SubSubCategory();
}
}
Please choose only one answer:
SubSubCategory_SubCategory_Category_
SubSubCategory_
Category_SubCategory_SubSubCategory_
4.1.8. What is the result of compiling and running the following code?
What is the result of compiling and running the following code?
interface Chewable {}
interface Eatable extends Chewable{}
class Food implements Chewable { }
class Meat extends Food {}
class Gum implements Chewable{}
public class Tester {
public static void main(String[] args) {
Food food = new Food();
Meat meat = new Meat();
Gum gum = new Gum();
System.out.print(food instanceof Eatable);
System.out.print(meat instanceof Food);
System.out.print(gum instanceof Food);
System.out.print(gum instanceof Chewable);
}
}
Please choose only one answer:
True true true true
True true false true
incompatible types: Gum cannot be converted to Food
False true false true
System.out.print(gum instanceof Food);
Compilation error
4.1.9. What is the result of compiling and running the following code?
What is the result of compiling and running the following code?
interface Chewable {}
interface Eatable extends Chewable{}
class Food implements Eatable { }
class Meat extends Food {}
class Gum implements Chewable{}
public class Tester {
public static void main(String[] args) {
Food food = new Food();
Meat meat = new Meat();
Gum gum = new Gum();
System.out.print(food instanceof Chewable);
System.out.print(meat instanceof Eatable);
System.out.print(gum instanceof Eatable);
System.out.print(gum instanceof Chewable);
}
}
Please choose only one answer:
False true false true
True true false true
True true true true
Compilation error
4.1.11. What is the result of compiling and running the following code?
What is the result of compiling and running the following code?
class Base {
public void Base() {System.out.print("Base");}
}
public class Derived extends Base {
public Derived() {System.out.print("Derived");}
public static void main(String[] args) {
new Derived();
}
}
Please choose only one answer:
BaseDerived
DerivedBase
Derived
Base
Compilation error
4.1.12. What is the result of compiling and running the following code?
What is the result of compiling and running the following code?
Please choose only one answer:
constructor Base in class Base cannot be applied to given types;
Base1Derived
{System.out.print("Derived");}
Base2Derived
required: String
Derived
found: no arguments
Compilation error
reason: actual and formal argument lists differ in length
class Base {
public Base(String n) {System.out.print("Base1");}
public void Base(String n) {System.out.print("Base2");}
}
public class Derived extends Base {
public Derived() {System.out.print("Derived");}
public static void main(String[] args) {
new Derived();
}
}
4.1.13. What is the result of compiling and running the following code?
What is the result of compiling and running the following code?
class Base {
public Base() {System.out.print("Base");}
}
public class Derived extends Base {
public Derived() {this("JavaChamp"); System.out.print("Derived");}
public Derived(String s) { System.out.print(s);}
public static void main(String[] args) {
new Derived();
}
}
Please choose only one answer:
JavaChampDerived
JavaChampBaseDerived
BaseJavaChampDerived
JavaChampDerivedBase
Compilation error
}
}
class Falcon extends Bird {
String getName() {
return "Falcon";
}
}
public class Tester {
public static Bird getIt(Creature c) {
System.out.println(c.getName());
return (Bird) c;
}
public static void main(String[] args) {
// insert code here
}
}
Please choose all the answers that apply:
getIt(new Creature());Creature
getIt(new Bird());
Exception in thread "main" java.lang.ClassCastException: Creature cannot be cast to
getIt(new Falcon()); Bird
getIt(new Object()); Object cannot be converted to Creature getIt(new Object());
Check the answer of this question online at QuizOver.com:
4.1.15. The following program fails to compile, where could possibly be the...
The following program fails to compile, where could possibly be the compilation error(s)?
class Creature {}
C:\Users\sksho\Desktop\New folder (2)>java Tester
class Bird extends Creature {}
Exception in thread "main" java.lang.ClassCastException: Creature cannot
class Falcon extends Bird {}
be cast to Bird
public class Tester {
at Tester.main(Tester.java:17)
public static void main(String[] args) {
Creature c1 = new Creature();
C:\Users\sksho\Desktop\New folder (2)>javac Tester.java
Creature c2 = new Bird();
C:\Users\sksho\Desktop\New folder (2)>java Tester
Bird b1 = (Bird) c1; // Line 1
Exception in thread "main" java.lang.ClassCastException: Creature cannot
Bird b2 = (Falcon) c2; // Line 2
be cast to Bird
Bird b3 = c2; // Line 3
at Tester.main(Tester.java:17)
Bird b4 = new Falcon(); // Line 4
Bird b5 = (Bird) new Creature(); // Line 5
C:\Users\sksho\Desktop\New folder (2)>javac Tester.java
Falcon f1 = b4; // Line 6
C:\Users\sksho\Desktop\New folder (2)>java Tester
}
Exception in thread "main" java.lang.ClassCastException: Bird cannot be
}
cast to Falcon
Please choose all the answers that apply:
at Tester.main(Tester.java:18)
Line 1
Line 2
C:\Users\sksho\Desktop\New folder (2)>javac Tester.java
Line 3
C:\Users\sksho\Desktop\New folder (2)>java Tester
Line 4
Line 5
C:\Users\sksho\Desktop\New folder (2)>
class Creature {
void grow() {
}
}
class Bird extends Creature {
void fly() {
}
}
class Falcon extends Bird {
void hunt() {
}
}
public class Tester {
public static void main(String[] args) {
Creature c1 = new Bird();
Falcon c2 = new Falcon();
// insert code here
}
}
4.1.17. What inserted, independently at // insert code here , will compile ...
What inserted, independently at // insert code here , will compile and run with no errors and
exceptions?
class Creature {
void grow() {
}
}
class Bird extends Creature {
void fly() {
}
}
class Falcon extends Bird {
void hunt() {
}
}
public class Tester {
public static void main(String[] args) {
Creature c1 = new Bird();
Falcon c2 = new Falcon();
// insert code here
}
}
Please choose all the answers that apply:
c1.grow();
c1.fly();
((Bird)c1).fly();
((Falcon)c1).hunt();