Select the correct answerAssuming all the necessary imports are done, predict the output of the following program. abstract class demo { public int marks; demo() { marks = 20; } abstract public void set(); abstract final public void get(); } class Test extends demo { public void set(int marks) { this.marks = marks; } final public void get() { System.out.println("marks = " + marks); } public static void main(String[] args) { Test obj = new Test(); obj.set(10); obj.get(); } }Optionsmarks = 20marks = 10Compilation error
Question
Select the correct answerAssuming all the necessary imports are done, predict the output of the following program. abstract class demo { public int marks; demo() { marks = 20; } abstract public void set(); abstract final public void get(); } class Test extends demo { public void set(int marks) { this.marks = marks; } final public void get() { System.out.println("marks = " + marks); } public static void main(String[] args) { Test obj = new Test(); obj.set(10); obj.get(); } }Optionsmarks = 20marks = 10Compilation error
Solution
The correct answer is "Compilation error".
The reason is that in the abstract class 'demo', the method 'set()' does not take any parameters, but in the subclass 'Test', the method 'set(int marks)' does take a parameter. This is not a valid method overriding.
Also, the method 'get()' in the abstract class 'demo' is declared as both abstract and final, which is not allowed in Java. A method cannot be both abstract (meant to be overridden in a subclass) and final (not allowed to be overridden).
Therefore, the program will not compile.
Similar Questions
Predict the output of the following program:abstract class Demo{ public int a; Demo() { a = 10; } abstract public void set(); abstract final public void get();}class Test extends Demo{ public void set(int a) { this.a = a; } final public void get() { System.out.println("a = " + a); } public static void main(String[] args) { Test obj = new Test(); obj.set(20); obj.get(); }}Select one:a.a = 10b.a = 20c.Compilation error
Select the correct answerAssuming that the required imports are done, what will be the output of the following Java program?class string_class { public static void main(String args[]) { String obj = "I LIKE CODETANTRA"; System.out.println(obj.length()); }}Options1116917
Select the correct answerAssuming that all the necessary imports are done, what will be the output of the following Java code? class codetantra { public int a; public int b; codetantra() { a = 5; b = 6; } } class coding extends codetantra { int i; coding() { super(); } } class inheritance { public static void main(String args[]) { coding obj = new coding(); System.out.println(obj.a + " " + obj.b); } }Options6 55 6Runtime ErrorCompilation Error
Select 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(); } }OptionsRuntime Error10Compilation Error
Select the correct answerAssuming that the required imports are done, what will be the output of the following Java code?class rectangle { int width; int length; int area; void area() { area = width*length; System.out.println(area); }} class Output { public static void main(String args[]) { rectangle obj = new rectangle(); obj.width=5; obj.length=6; obj.area(); }}Options1301136
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.