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
Solution
The correct answer is 4.
Here's the step by step explanation:
- The interface
calculateis defined with a methodcal(int item). - The class
displayimplements thecalculateinterface and provides the implementation for thecal(int item)method. In this method, the passed integer is squared (item * item) and the result is assigned to the instance variablex. - In the
mainmethod of theinterfacesclass, an instance ofdisplayclass is created and thexvariable is initially set to 0. - The
calmethod is called on thearrobject with 2 as the argument. This results inxbeing set to2 * 2, which is 4. - Finally,
arr.xis printed to the console, which will output 4.
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
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.