Knowee
Questions
Features
Study Tools

Predict the outputclass Car implements Insurance{        public int calcPremium(int i)    {        return i = i * i;    }}interface Insurance{    int calcPremium(int i);}public class MainClass {    public static void main(String[] args)    {        Insurance b = new Car();        System.out.println(b.calcPremium(2));    }}Select one:a.The output will be 4b.Compile time error because you must create interface before implementing it.c.Compile time error because you cannot create an object of type interface Insuranced.Run time Error

Question

Predict the outputclass Car implements Insurance{        public int calcPremium(int i)    {        return i = i * i;    }}interface Insurance{    int calcPremium(int i);}public class MainClass {    public static void main(String[] args)    {        Insurance b = new Car();        System.out.println(b.calcPremium(2));    }}Select one:a.The output will be 4b.Compile time error because you must create interface before implementing it.c.Compile time error because you cannot create an object of type interface Insuranced.Run time Error

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

Solution

The correct answer is:

a. The output will be 4

Explanation:

In the given code, the class Car is implementing the interface Insurance. The method calcPremium(int i) is overridden in the Car class and it returns the square of the input integer. In the main method, an object of type Insurance is created which is actually an instance of the Car class. When the method calcPremium(2) is called on this object, it returns 2*2 which is 4. Hence, the output will be 4.

This problem has been solved

Similar Questions

Predict the outputabstract class Vehicle{    abstract void calcPremium(Number N);}interface Insurance{    abstract void calcPremium (Object O);}class Car extends Vehicle implements Insurance{                 public void calcPremium (Object O)                    {                        System.out.println("Object");                    }    void calcPremium (Number N)     {        System.out.println("Number");    } }public class Test{    public static void main(String[] args)    {        Vehicle a = new Car();         a. calcPremium (new Integer(121));          Insurance b = new Car();           b. calcPremium (new Integer(121));           Car c = new Car();            c. calcPremium (new Integer(121));    }}Select one:a.NumberObjectNumberb.NumberNumberObjectc.Run time errord.Compile time 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(); }}Options20001000Compiler errorNone of these

Predict the output:interface Employee {       int a=90;       }class PermanentEmployee implements Employee {                    public void f1()       {                a=10;        }}Select one:a.error, since interfaces Employee is not publicb.error, since variable a is defaultc.error, since variable a is assigned a valued.no error

Predict the output of the following program:abstract class Demo{    public int a;    Demo()    {        a = 10;    }    abstract public void set();    abstract final public void get();}class Test extends Demo{    public void set(int a)    {        this.a = a;    }   final public void get()    {        System.out.println("a = " + a);    }    public static void main(String[] args)    {        Test obj = new Test();        obj.set(20);        obj.get();    }}Select one:a.a = 10b.a = 20c.Compilation error

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

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.