Java Questions & Answers – Interfaces – 1

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: The interface keyword (all lowercase) is used in Java to define an interface. Interfaces in Java specify a set of methods that a class can implement. The other options are invalid as Java is case-sensitive, and such keywords do not exist.

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: The concept of fully abstracting a class from its implementation is commonly achieved using interfaces.

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 an 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.
advertisement

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

Answer: c
Explanation: interface is inherited by a class using implements.

5. Which of the following is the 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: implements keyword is used to inherit an interface in a class.
Syntax:

Free 30-Day C Certification Bootcamp is Live. Join Now!
class <ClassName> implements <InterfaceName>
{           // body		}

6. Which of the following is an 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 public
View Answer

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

7. What will be the output of the following Java program?

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

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

Answer: c
Explanation: The cal() method is overridden in the Display class, which inherits from Calculate. This method sets the instance variable x to the square of the passed value, i.e., 2 * 2 = 4. Therefore, arr.x is 4.
Output:

advertisement
$ javac interfaces.java
$ java interfaces
4

8. What will be the output of the following Java program?

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

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

9. What will be the output of the following Java program?

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

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

Answer: c
Explanation: According to the overridden cal() method in the Display class, the method sets the instance variable x to VAR if the argument passed is less than 2; otherwise, it sets x to the square of the argument passed.
output:

$ javac interfaces.java
$ java interfaces 
0 0 4

Sanfoundry Global Education & Learning Series – Java Programming Language.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can find me on LinkedIn, watch my free Youtube Masterclasses, or join my Telegram discussions.

I also mentor professionals - especially those in their 40s to 60s, who are navigating career shifts, midlife transitions, or simply looking to rediscover purpose in their work and life. Learn more here.