Knowee
Questions
Features
Study Tools

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

Question

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

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

Solution

The correct answer is 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 provides the implementation for the cal(int item) method. In this method, the passed integer is squared (item * item) and the result is assigned to the instance variable x.
  3. In the main method of the interfaces class, an instance of display class is created and the x variable is initially set to 0.
  4. The cal method is called on the arr object with 2 as the argument. This results in x being set to 2 * 2, which is 4.
  5. Finally, arr.x is printed to the console, which will output 4.

This problem has been solved

Similar Questions

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

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

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

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

Select the correct answerPredict the output of following Java program.?class CT {      public static void main(String args[]) {               int a;               System.out.println(a);     }   } Optionscompiler errorgarbage value0runtime error

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.