What will be the output of the following Java program?interface calculate { void cal(int item);}class display implements calculate { int x; public void cal(int item) { x = item * item; }}class interfaces { public static void main(String args[]) { display arr = new display(); arr.x = 0; arr.cal(2); System.out.print(arr.x); }}Options04Compiler Error2
Question
What will be the output of the following Java program?interface calculate { void cal(int item);}class display implements calculate { int x; public void cal(int item) { x = item * item; }}class interfaces { public static void main(String args[]) { display arr = new display(); arr.x = 0; arr.cal(2); System.out.print(arr.x); }}Options04Compiler Error2
Solution
The output of the Java program will be 4.
Here's the step-by-step explanation:
- The interface
calculateis defined with a methodcal(int item). - The class
displayimplements thecalculateinterface and defines thecal(int item)method. This method takes an integer as an argument and assigns the square of this integer to the variablex. - In the
mainmethod of theinterfacesclass, an objectarrof classdisplayis created. - The variable
xof the objectarris initialized to 0. - The
calmethod of the objectarris called with 2 as an argument. This changes the value ofxin the objectarrto 2*2 = 4. - The value of
xin the object `arr
Similar Questions
What is the output of the following code?public class Solution { public void display() { int x = 10; } public static void main(String args[]) { System.out.println(x); }}Options: Pick one correct answer from below100Compile time errorRuntime error
What is the output of the below Java program with an Interface?interface Car { int basePrice = 1000;}public class InterfaceTest2 implements Car { void changePrice() { basePrice = 2000; System.out.print(basePrice); } public static void main(String[] args) { new InterfaceTest2().changePrice(); }}Options1000Compiler errorNone of these2000
What 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(); }}Options02Compilation Error1
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
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
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.