Knowee
Questions
Features
Study Tools

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);}}

🧐 Not the exact question you are looking for?Go ask a question

Solution

The output of this program is 4.

Here's the step-by-step explanation:

  1. An interface named calculate is declared with a method cal(int item).

  2. A class display implements the calculate interface and defines the cal(int item) method. This method takes an integer as an argument and assigns the square of this integer to the variable x.

  3. In the main method of the interfaces class, an instance of the display class is created named arr.

  4. The x variable of the arr object is initially set to 0.

  5. The cal method of the arr object is called with 2 as an argument. This sets x to the square of 2, which is 4.

  6. Finally, arr.x is printed to the console, so the output of the program is 4.

This problem has been solved

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

1/3

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.