Java Programming Multiple Choice Questions - Interfaces
This section focuses on the "Interfaces" in Java programming. These Multiple Choice Questions (MCQ) should be practiced to improve the Java programming skills required for various interviews (campus interviews, walk-in interviews, company interviews), placements and other competitive examinations.
1. Which of these keywords is used to define interfaces in Java?
A. interface
B. Interface
C. intf
D. Intf
View Answer
Ans : A
Explanation: interface keywords is used to define interfaces in Java.
2. A java interface can contain _______.
A. Abstract methods(unimplemented) and implemented methods both
B. public Abstract methods
C. public static Final Variables only
D. public static Final Variables and abstract methods both
View Answer
Ans : D
Explanation: An interface can have both final variables and abstract methods.
3. Which of these access specifiers can be used for an interface?
A. Public
B. private
C. Protected
D. All of the mentioned
View Answer
Ans : A
Explanation: 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.
4. Which of the following is a correct interface?
A. abstract interface A { abstract void print(); { }}
B. abstract interface A { print(); }
C. interface A { void print() { } }
D. interface A { void print(); }
View Answer
Ans : D
Explanation: No explanation.
5. Which of the following classes directly implement Set interface?
A. Vector
B. HashSet
C. HashTable
D. LinkedList
View Answer
Ans : B
Explanation: HashSet classes directly implement Set interface
6. Which of these keywords is used by a class to use an interface defined previously?
A. Import
B. import
C. implements
D. Implements
View Answer
Ans : C
Explanation: interface is inherited by a class using implements.
7. Which is the correct way to inherit and implement the interface?
A. class Cat implements IAnimal{}
B. class Cat import IAnimal{}
C. class Cat extends IAnimal{}
D. None is correct
View Answer
Ans : A
Explanation: Classes always implements an interface. An interface can extends another interface or multiple interfaces. Hence, answer would be A.
8. which of the following is true about methods in an interface in java?
A. An interface can contain only abstract method.
B. We can define a method in an interface
C. Private and protected access modifiers can also be used to declare methods in interface
D. None of the above
View Answer
Ans : A
Explanation: In java, an interface contains only abstract method that can be public and it does not have any method implementation.
9. Which of the following is the correct way of implementing an interface salary by class manager?
A. class manager imports salary {}
B. class manager implements salary {}
C. class manager extends salary {}
D. none of the mentioned
View Answer
Ans : B
Explanation: No Explanation.
10. Which of the following is an incorrect statement about packages?
A. Interfaces are specified public if they are to be accessed by any code in the program
B. Interfaces specifies what class must do but not how it does
C. All variables in interface are implicitly final and static
D. All variables are static and methods are public if interface is defined pubic
View Answer
Ans : D
Explanation: All methods and variables are implicitly public if interface is declared public.
11. Which of these can be used to fully abstract a class from its implementation?
A. Objects
B. Packages
C. Interfaces
D. None of the Mentioned
View Answer
Ans : C
Explanation: None.
12. 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 Main
{
public static void main(String args[])
{
display arr = new display();
arr.x = 0;
arr.cal(2);
System.out.print(arr.x);
}
}
A. 0
B. 2
C. 4
D. None of the mentioned
View Answer
Ans : C
Explanation: None.
Output:
$ javac interfaces.java
$ java interfaces
4
13. All methods must be implemented of an interface.
A. TRUE
B. FALSE
C. Can be true or false
D. can not say
View Answer
Ans : A
Explanation: Concrete classes must implement all methods in an interface. Through interface multiple inheritance is possible.
14. What type of variable can be defined in an interface?
A. public static
B. private final
C. public final
D. static final
View Answer
Ans : D
Explanation: variable defined in an interface is implicitly final and static. They are usually written in capital letters.
15. What does an interface contain?
A. Method definition
B. Method declaration
C. Method declaration and definition
D. Method name
View Answer
Ans : B
Explanation: Interface contains the only declaration of the method.
16. What type of methods an interface contain by default?
A. abstract
B. static
C. final
D. private
View Answer
Ans : A
Explanation: By default, interface contains abstract methods. The abstract methods need to be implemented by concrete classes.
17. What will happen if we provide concrete implementation of method in interface?
A. The concrete class implementing that method need not provide implementation of that method
B. Runtime exception is thrown
C. Compilation failure
D. Method not found exception is thrown
View Answer
Ans : C
Explanation: The methods of interfaces are always abstract. They provide only method definition.
Output:
$ javac interfaces.java
$ java interfaces
4
18. What happens when a constructor is defined for an interface?
A. Compilation failure
B. Runtime Exception
C. The interface compiles successfully
D. The implementing class will throw exception
View Answer
Ans : A
Explanation: Constructor is not provided by interface as objects cannot be instantiated.
19. What happens when we access the same variable defined in two interfaces implemented by the same class?
A. Compilation failure
B. Runtime Exception
C. The JVM is not able to identify the correct variable
D. The interfaceName.variableName needs to be defined
View Answer
Ans : D
Explanation: The JVM needs to distinctly know which value of variable it needs to use. To avoid confusion to the JVM interfaceName.variableName is mandatory.
20. Can "abstract" keyword be used with constructor, Initialization Block, Instance Initialization and Static Initialization Block.
A. TRUE
B. FALSE
C. Can be true or false
D. can not say
View Answer
Ans : B
Explanation: No, Constructor, Static Initialization Block, Instance Initialization Block and variables cannot be abstract.
Also check :
Discussion
THANUJA ROY M ROY
Thank you