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
Question
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
Solution
The correct answer is:
c. error, since variable a is assigned a value
Explanation:
In Java, the fields in an interface are implicitly public, static, and final. This means they cannot be modified. They are constants. So, when you try to assign a new value to 'a' in the PermanentEmployee class, it will result in a compile-time error. Therefore, you cannot change the value of 'a' as it is declared in an interface.
Similar Questions
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
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 DoStuff2 { float getRange(int low, int high);}interface DoMore{ float getAvg(int a, int b, int c); } abstract class DoAbstract implements DoStuff2, DoMore { } class DoStuff implements DoStuff2 { public float getRange(int x, int y) { return 3.14f; }} interface DoAll extends DoMore { float getAvg(int a, int b, int c, int d);} Select one:a.Compile time Errorb.The file will compile without error.c.Runtime ErrorClear my choice
t answerWhat type of variable can be defined in an interface?
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.