What is the output of this 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);}}
Question
What is the output of this 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);}}
Solution
The output of this program is 4.
Here's the step-by-step explanation:
-
An interface named
calculateis declared with a methodcal(int item). -
A 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 instance of thedisplayclass is created namedarr. -
The
xvariable of thearrobject is initially set to 0. -
The
calmethod of thearrobject is called with 2 as an argument. This setsxto the square of 2, which is 4. -
Finally,
arr.xis printed to the console, so the output of the program is 4.
Similar Questions
Select the correct answerWhat 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); }}Options204Compiler Error
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 output of the following code:public class Test{ public static void main(String[] args){ int[] x = {120, 200, 016 }; for(int i = 0; i < x.length; i++) System.out.print(x[i] + " "); }
What is the output of the below program?public class Main{ public static void main(String[] args) { int a = 25; if(a > 5) System.out.print("Hi "); if(a < 20) System.out.print("Hello "); else System.out.print("Know Program "); }}
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
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.