Select the correct answerWhat will be the output of the following Java code? class main { public int x; protected int y; } class derived extends main { int y; void display() { super.y = 5; System.out.println(x + " " + y); } } class Output { public static void main(String args[]) { derived obj = new derived(); obj.x=2; obj.y=3; obj.display(); } }Options3 22 32 55 2
Question
Select the correct answerWhat will be the output of the following Java code? class main { public int x; protected int y; } class derived extends main { int y; void display() { super.y = 5; System.out.println(x + " " + y); } } class Output { public static void main(String args[]) { derived obj = new derived(); obj.x=2; obj.y=3; obj.display(); } }Options3 22 32 55 2
Solution
The correct answer is "2 3".
Here's the step by step explanation:
- An object of the derived class is created in the main method of the Output class.
- The variables x and y of the derived class object are set to 2 and 3 respectively.
- The display method of the derived class is called. In this method, the value of y in the superclass (main class) is set to 5. However, this does not affect the value of y in the derived class.
- The values of x and y in the derived class are printed, which are 2 and 3 respectively.
Similar Questions
Select the correct answerWhat will be the output of the following Java program? final class base { int a; } class derived extends base { int b; System.out.println(b + " " + a); } class inheritance { public static void main(String args[]) { derived obj = new derived(); obj.display(); } }OptionsRuntime Error3 32 2Compilation Error
What will be the output of the following code?This question is required.*class Base {public void display() {System.out.println("Base");}}class Derived extends Base {public void display() {System.out.println("Derived");}}public class Main {public static void main(String[] args) {Base b = new Derived();b.display();}}
Select the correct answerWhat will be the output of the following Java program?class A { int i; void display() { System.out.println(i); }}class B extends A { int j; void display() { System.out.println(j); }}class inheritance_demo { public static void main(String args[]) { B obj = new B(); obj.i = 1; obj.j = 2; obj.display(); }}Options102Compilation Error
ect the correct answerWhat will be the output of the following Java code? class Codetantra { final public int output(int x, int y) { return 1; } } class Program extends Codetantra { public int output(int x, int y) { return 2; } } public class result { public static void main(String args[]) { Program object = new Program(); System.out.print("y is " + y.output(0, 1)); } }
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
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.