Log In Register

Units


Interfaces - Questions and Answers

 

Practice these Interfaces under Java Programming walk-in interview Questions/ examination questions with best tricks and short cuts with solution. Student (candidate) who want to crack the walk in interview, competitive exams and want to find short cuts and tricks to solve questions on Interfaces for following purpose.


– Anyone who is wishing to increase their perfect knowledge of Interfaces of Java Programming.
– Anyone who is preparing for aptitude test and increase his in depth aptitude knowledge Java Programming specifically Interfaces.
– Anyone who is preparing for interviews (competitive examinations, govt examinations and bank examinations, off-campus or on campus interviews, walk-in interview and company interviews) having questions on Interfaces under Java Programming.

– Anyone who is preparing for aptitude test or competitive exam or entrance exam.
– Anyone who is preparing for entrance examinations and other competitive examinations.
– All Experienced, Freshers and Students.

Here is list of Questions and Answers covering all key area of  Interfaces topic of Java Programming:

 

1. Which of these method is used to begin the execution of a thread?

Answer: Option B

Explanation:

2. Determine output of the following code. interface A { } class C { } class D extends C { } class B extends D implements A { } public class Test extends Thread{ public static void main(String [] args){ B b = new B(); if (b instanceof A) System.out.pri ntln("b is an instance of A"); if (b instanceof C) System.out.pri ntln("b is an instance of C"); } }

Answer: Option D

Explanation:

3. Which of these keywords is used to define interfaces in Java?

Answer: Option A

Explanation:

4. Which of these keywords is used by a class to use an interface defined previously?

Answer: Option C

Explanation:
interface is inherited by a class using implements.

5. Which of these access specifiers can be used for an interface?

Answer: Option 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

6. What is the output for the below code ? interface A{ public void printValue(); } 1. public class Test{ 2. public static void main (String[] args){ 3. A a1 = new A(){ 4. public void printValue(){ 5. Syst em.out.println("A"); 6. } 7. }; 8. a1.printValue(); 9. } 10. }

Answer: Option B

Explanation:
The A a1 reference variable refers not to an instance of interface A, but to an instance of an anonymous (unnamed) class. So there is no compilation error.

7. Which of the following is correct way of implementing an interface salary by class manager?

Answer: Option B

Explanation:

8. What will happen after compiling this program code? abstract class MyClass{ //line 1 private int a, b; public void call(int a, int b){ this.a = a; this.b = b; System.out.print(a+b); } } public class Test{ public static void main(String a rgs[]){ MyClass m = new MyClass(); //line 2 m.call(12,25); } }

Answer: Option C

Explanation:
Abstract class is not concrete class which means object cannot be created for abstract class, its requires extending it and then create the object of extended class.

9. Which two of the following are legal declarations for abstract classes and interfaces? 1. final abstract class Test {} 2. public static interface Test {} 3. final public class Test {} 4. protected abstract class Test {} 5. protected interface Test {} 6. abstract public class Test {}

Answer: Option D

Explanation:
(3), (6). Both are legal class declarations. (1) is wrong because a class cannot be abstract and final "there would be no way to use such a class". (2) is wrong because interfaces and classes cannot be marked as static. (4) and (5) are wrong because classes and interfaces cannot be marked as protected.

10.  Which of these method waits for the thread to treminate?

Answer: Option C

Explanation:

11. What will be the output when the following program is compiled and executed? abstract class TestAbstract{ String my_name; String myName(){ my_name = "TestMirror"; return my_name; } abstract void display(); } public class Test extends TestAbstract{ void display(){ String n = myName(); System.out.print("My name is "+ n); } public static void main(String a rgs[]){ Test t = new Test(); t.display(); } }

Answer: Option A

Explanation:
The options B, C and D are incorrect options as in Java we can declare an abstract class comprising of abstract and non-abstract methods that will not lead to any compilation error. Therefore, A is the correct answer implying that the 't' instance of Test class invokes the display method, which is implemented in the Test class. The display method invokes myName() method declared int the TestAbstract class and prints the name.

12.  Which of the following is incorrect statement about packages?

Answer: Option D

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

13. What will be the output? interface A{ public void method(); } class One{ public void method(){ System.out.println("Cl ass One method"); } } class Two extends One implements A{ public void method(){ System.out.println("Cl ass Two method"); } } public class Test extends Two{ public static void main(String [] args){ A a = new Two(); a.method(); } }

Answer: Option B

Explanation:

14.  Which of these method is used to implement Runnable interface?

Answer: Option B

Explanation:
To implement Runnable interface, a class needs only to implement a single method called run().

15. What will be the output? 1. public interface InfA{ 2. protected String getName(); 3. } public class Test implements InfA{ public String getName(){ return "test-name"; } public static void main (String [] args){ Test t = new Test(); System.out.println(t.getName()); } }

Answer: Option B

Explanation:
Illegal modifier for the interface method InfA.getName(); only public and abstracts are permitted.