Knowee
Questions
Features
Study Tools

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

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

Solution

The output of the Java program will be 4.

Here's the step-by-step explanation:

  1. The interface calculate is defined with a method cal(int item).
  2. The 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 object arr of class display is created.
  4. The variable x of the object arr is initialized to 0.
  5. The cal method of the object arr is called with 2 as an argument. This changes the value of x in the object arr to 2*2 = 4.
  6. The value of x in the object `arr

This problem has been solved

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

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.