Welcome to Java daily quiz 3, test yourself with this Java quiz. The answer and explanation are given at the end for your reference.
Check out all the Java daily quiz questions at https://www.javaguides.net/p/java-daily-quiz.html.
Check out 200+ video tutorials on my YouTube channel at https://www.youtube.com/c/javaguides.
Quiz 3 - What will be the output of the following Java program?
class Base {
public void test() {
}
}
class Base1 extends Base {
public void test() {
System.out.println("Base1");
}
}
class Base2 extends Base {
public void test() {
System.out.println("Base2");
}
}
class Test {
public static void main(String[] args) {
Base obj = new Base1();
((Base2) obj).test(); // CAST
}
}
a) The program will print the following: Base1.
b) The program will print the following: Base2.
c) The compiler will report an error in the line marked with comment CAST.
d) The program will result in an exception (ClassCastException).
Answer
d) The program will result in an exception (ClassCastException).
Check out all the Java daily quiz questions at https://www.javaguides.net/p/java-daily-quiz.html.
Check out 200+ video tutorials on my YouTube channel at https://www.youtube.com/c/javaguides
Comments
Post a Comment
Leave Comment