Java Questions & Answers – Interfaces

This section of our 1000+ Java MCQs focuses on interfaces of Java Programming Language.

1. Which of these keywords is used to define interfaces in Java?
a) interface
b) Interface
c) intf
d) Intf
View Answer

Answer: a
Explanation: None.

2. 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

Answer: c
Explanation: None.

3. Which of these access specifiers can be used for an interface?
a) Public
b) Protected
c) private
d) All of the mentioned
View Answer

Answer: 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 these keywords is used by a class to use an interface defined previously?
a) import
b) Import
c) implememnts
d) Implememnts
View Answer

Answer: c
Explanation: None.

5. Which of the following is correct way of implementing an interface salary by class manager?
a) class manager extends salary {}
b) class manager implements salary {}
c) class manager imports salary {}
d) None of the mentioned.
View Answer

Answer: b
Explanation: None.

6. Which of the following is incorrect statement about packages?
a) Interfaces specifies what class must do but not how it does.
b) Interfaces are specified public if they are to be accessed by any code in the program.
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

Answer: d
Explanation: All methods and variables are implicitly public if interface is declared public.

7. Which of the following package stores all the standard java classes?
a) lang
b) java
c) util
d) java.packages
View Answer

Answer: b
Explanation: None.

8. What is the output of this program?

  1.     interface calculate {
  2.         void cal(int item);
  3.     }
  4.     class display implements calculate {
  5.         int x;
  6.         void cal(int item) {
  7.             x = item * item;            
  8.         }
  9.     }
  10.     class interfaces {
  11.         public static void main(String args[]) {
  12.             display arr = new display;
  13.             arr.x = 0;      
  14.             arr.cal(2);
  15.             System.out.print(arr.x);
  16.         }
  17.     }

a) 0
b) 2
c) 4
d) None of the mentioned
View Answer

Answer: c
Explanation: None.
Output:
$ javac interfaces.java
$ java interfaces
4

9. What is the output of this program?

  1.     interface calculate {
  2.         void cal(int item);
  3.     }
  4.     class displayA implements calculate {
  5.         int x;
  6.         void cal(int item) {
  7.             x = item * item;            
  8.         }
  9.     }
  10.     class displayB implements calculate {
  11.         int x;
  12.         void cal(int item) {
  13.             x = item / item;            
  14.         }
  15.     }
  16.     class interfaces {
  17.         public static void main(String args[]) {
  18.             displayA arr1 = new displayA;
  19.             displayB arr2 = new displayB;
  20.             arr1.x = 0;
  21.             arr2.x = 0;      
  22.             arr1.cal(2);
  23.             arr2.cal(2);
  24.             System.out.print(arr1.x + " " + arr2.x);
  25.         }
  26.     }

a) 0 0
b) 2 2
c) 4 1
d) 1 4
View Answer

Answer: c
Explanation: 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

10. What is the output of this program?

  1.     interface calculate {
  2.         int VAR = 0;
  3.         void cal(int item);
  4.     }
  5.     class display implements calculate {
  6.         int x;
  7.         void cal(int item) {
  8.             if (item < 2)
  9.                 x = VAR;
  10.             else
  11.                 x = item * item;            
  12.         }
  13.     }
  14.     class interfaces {
  15.         public static void main(String args[]) {
  16.             display arr[] = new display[3];
  17.             arr[0].cal(0);    
  18.             arr[1].cal(1);
  19.             arr[2].cal(2);
  20.             System.out.print(arr[0].x + " " + arr[1].x + " " + arr[2].x);
  21.         }
  22.     }

a) 0 1 2
b) 0 2 4
c) 0 0 4
d) 0 1 4
View Answer

Answer: c
Explanation: None.
output:
$ javac interfaces.java
$ java interfaces
0 0 4

Sanfoundry Global Education & Learning Series – Java Programming Language.

About

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.


Sanfoundry Training Center of Excellence (CoE)

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.







Sanfoundry Internships
Subscribe Newsletter & Posts
Follow Manish & Sanfoundry