MCQs
None.
output:
$ javac interfaces.java
$ java interfaces
0 0 4
What is the output of this program?
interface calculate {
void cal(int item);
}
class displayA implements calculate {
int x;
public void cal(int item) {
x = item * item;
}
}
class displayB implements calculate {
int x;
public void cal(int item) {
x = item / item;
}
}
class interfaces {
public static void main(String args[]) {
displayA arr1 = new displayA;
displayB arr2 = new displayB;
arr1.x = 0;
arr2.x = 0;
arr1.cal(2);
arr2.cal(2);
System.out.print(arr1.x + " " + arr2.x);
}
}
class displayA implements the interface calculate by doubling the value of item, where as
class displayB implements the interface by dividing item by item, therefore variable x of
class displayA stores 4 and variable x of class displayB stores 1.
Output:
$ javac interfaces.java
$ java interfaces
4 1
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);
}
}
None.
Output:
$ javac interfaces.java
$ java interfaces
4
None.
All methods and variables are implicitly public if interface is declared public.
None.
None.
Access specifier of interface is either public or no specifier. When no access specifier is used
then default access specifier is used due to which interface is available only to other members
of the package in which it is declared, when declared public it can be used by any code.
None.
interface is inherited by a class using implements.