What is the Output of following Java Program?abstract class Demo{ public int a; Demo() { a = 10; } abstract public void set(); }class Test extends Demo{ final public void get() { System.out.println("a = " + a); } public static void main(String[] args) { Test obj = new Test(); obj.get(); }}Select one:a.Compile Time Errorb.a=10c.Runtime Exception
Question
What is the Output of following Java Program?abstract class Demo{ public int a; Demo() { a = 10; } abstract public void set(); }class Test extends Demo{ final public void get() { System.out.println("a = " + a); } public static void main(String[] args) { Test obj = new Test(); obj.get(); }}Select one:a.Compile Time Errorb.a=10c.Runtime Exception
Solution
The output of the given Java program will be a Compile Time Error.
Here's why:
In the given Java program, the class 'Demo' is declared as abstract and it contains an abstract method 'set()'. However, when 'Demo' is extended by the class 'Test', the 'set()' method is not implemented or overridden.
In Java, when a regular (i.e., non-abstract) class extends an abstract class, it must provide implementations (i.e., bodies) for all of the abstract methods in the superclass. If it doesn't, the subclass itself must also be declared abstract.
Since 'Test' does not implement the abstract method 'set()' from 'Demo' and 'Test' is not declared as abstract, the Java compiler will produce a compile time error.
So, the correct answer is a. Compile Time Error.
Similar Questions
What will be the output of the following code snippet?public class MyClass { static int value = 10; public void printValue() { System.out.println(value); } public static void main(String[] args) { MyClass obj = new MyClass(); obj.printValue(); }}Question 4Answera.0b.10c.Compilation errord.Runtime exception
What is the output of the following Java program?class Main { public static void main(String args[]) { final int i; i = 20; i = 30; System.out.println(i); }}OptionsCompiler Error30Garbage value0
What will be the output of the following Java program?class exception_handling { public static void main(String args[]) { try { System.out.print("Hello" + " " + 1 / 0); } catch(ArithmeticException e) { System.out.print("World"); } } }a) Hellob) Worldc) HelloWorldd) Hello World
What is the output of the following Java program?class Base extends Exception {}class Derived extends Base {} public class Main { public static void main(String args[]) { // some other stuff try { // Some monitored code throw new Derived(); } catch(Base b) { System.out.println("Caught base class exception"); } catch(Derived d) { System.out.println("Caught derived class exception"); } }}
the correct answerWhat will be the output of the given Java program? abstract class java { int x; abstract void display(); } class method extends java { int y; void display() { System.out.println(y); } } class Abstract_demo { public static void main(String args[]) { method obj = new method(); obj.y = 1; obj.display(); } }Options0Runtime Error1Compilation Error
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.