Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

Can You Identify The Errors In The Below Code? Interface I { Class C Implements I { Public Void Method(int I) { System.out.println(i); } } Void Method(int I); }

Answer»

No ERRORS.

No errors.

2.

What Will Be The Output Of The Following Program? Class A { } Class B Extends A { } Class C Extends B { } Interface Abc { Void Method(a A); } Interface Pqr { Void Method(b B); } Class M Implements Abc, Pqr { Public Void Method(a A) { System.out.println(2); } Public Void Method(b B) { System.out.println(3); } } Public Class Mainclass { Public Static Void Main(string[] Args) { M M = New M(); M.method(new A()); M.method(new B()); M.method(new C()); } }

Answer»

2

3

3

2

3

3

3.

Is The Below Program Written Correctly? If Yes, What Will Be The Output? Interface I { Class C { Int I; Public C(int I) { This.i = ++i; } Int Methodc() { Return ++i; } } } Public Class Mainclass { Public Static Void Main(string[] Args) { I.c C = New I.c(000); System.out.println(c.method()); } }

Answer»

YES, PROGRAM is WRITTEN CORRECTLY. OUTPUT will be,

2

Yes, program is written correctly. Output will be,

2

4.

What Will Be The Output Of The Following Program? Abstract Class A { Abstract Void Mymethod(number N); } Interface B { Abstract Void Mymethod(object O); } Class C Extends A Implements B { Void Mymethod(number N) { System.out.println("number"); } Public Void Mymethod(object O) { System.out.println("object"); } } Public Class Mainclass { Public Static Void Main(string[] Args) { A A = New C(); A.mymethod(new Integer(121)); B B = New C(); B.mymethod(new Integer(121)); C C = New C(); C.mymethod(new Integer(121)); } }

Answer»

NUMBER

OBJECT

Number

Number

Object

Number

5.

Can We Define Interface As Generic?

Answer»

YES, we can DEFINE GENERIC INTERFACE

Yes, we can define generic interface

6.

Can You Identify The Error In The Below Code? Interface X { Void Methodx(); } Interface Y Extends X { Void Methody(); } Class Z Implements Y { Public Void Methody() { System.out.println("method Y"); } }

Answer»

CLASS Z MUST IMPLEMENT methodX() ALSO.

Class Z must implement methodX() also.

7.

Interfaces Are Abstract And Public By Default. True Or False?

Answer»

FALSE. INTERFACES are ABSTRACT by DEFAULT but not PUBLIC.

False. Interfaces are abstract by default but not public.

8.

Can You Identify The Error In The Below Code? Class A Implements A.b { Static Interface B { Void Methodb(); } }

Answer»

Cycle DETECTED. Any CLASS can not extend itself or it’s MEMBER types.

Cycle detected. Any class can not extend itself or it’s member types.

9.

Is The Below Program Written Correctly? If Yes, What Will Be The Output? Interface X { Void Methodx(); Interface Y { Void Method(); } } Class Z Implements X, X.y { { Method(); System.out.println(1); } Public Void Methodx() { Methody(); System.out.println(2); } Public Void Methody() { System.out.println(3); } } Public Class Mainclass { Public Static Void Main(string[] Args) { Z Z = New Z(); Z.methodx(); Z.methody(); X X = Z; X.methodx(); } }

Answer»

Yes, program is CORRECT. Output will be,

3

2

1

3

2

3

3

Yes, program is correct. Output will be,

3

2

1

3

2

3

3

10.

What Will Be The Output Of The Following Program? Interface A { String A = "aaa"; String Methoda(); } Interface B { String B = "bbb"; String Methodb(); } Class C Implements A, B { Public String Methoda() { Return A+b; } Public String Methodb() { Return B+a; } } Class D Extends C Implements A, B { String D = "ddd"; Public String Methoda() { Return D+methodb(); } } Public Class Mainclass { Public Static Void Main(string[] Args) { C C = New C(); System.out.println(c.methoda()); System.out.println(c.methodb()); C = New D(); System.out.println(c.methoda()); System.out.println(c.methodb()); } }

Answer»

AAABBB

BBBAAA

DDDBBBAAA

BBBAAA

AAABBB

BBBAAA

DDDBBBAAA

BBBAAA

11.

All Members Of Interface Are Public By Default. True Or False?

Answer»

True.

True.

12.

How Do You Print The Value Of Field ‘i’ Of Interface ‘onetwothree’ In The Below Example And What Will Be The It’s Value? Interface One { Int I = 222; Interface Onetwo { Int I = One.i+one.i; Interface Onetwothree { Int I = Onetwo.i + Onetwo.i; } } }

Answer»

PRINTING ‘i’ VALUE —> System.out.println(One.OneTwo.OneTwoThree.i)

Value of One.OneTwo.OneTwoThree.i will be 888.

Printing ‘i’ value —> System.out.println(One.OneTwo.OneTwoThree.i)

Value of One.OneTwo.OneTwoThree.i will be 888.

13.

What Will Be The Output Of The Following Program? Interface A { Int Methoda(); } Interface B { Int Methodb(); } Interface C { Int Methodc(); } Class D Implements A, B, C { Int I = 999+111; Public Int Methoda() { I =+ I / I; Return I; } Public Int Methodb() { I =- I * I; Return I; } Public Int Methodc() { I = ++i - --i; Return I; } } Public Class Mainclass { Public Static Void Main(string[] Args) { D D = New D(); System.out.println(d.i); System.out.println(d.methoda()); System.out.println(d.methodb()); System.out.println(d.methodc()); } }

Answer»

1110

1

-1

1

1110

1

-1

1

14.

Can Interfaces Have Methods Other Than Abstract?

Answer»

YES, from Java 8, INTERFACES can have STATIC methods and DEFAULT methods other than abstract methods.

Yes, from Java 8, interfaces can have static methods and default methods other than abstract methods.

15.

What Will Be The Output Of The Below Program? Interface X { Void Method(); } Class Y { Public Void Method() { System.out.println("class Y"); } } Class Z Extends Y Implements X { } Public Class Mainclass { Public Static Void Main(string[] Args) { X X = New Z(); X.method(); } }

Answer»

CLASS Y

CLASS Y

16.

What Will Be The Output Of The Following Program? Interface One { String S = "final"; String Methodone(); } Interface Two { String Methodone(); } Abstract Class Three { String S = "not Final"; Public Abstract String Methodone(); } Class Four Extends Three Implements One, Two { Public String Methodone() { String S = Super.s + One.s; Return S; } } Public Class Mainclass { Public Static Void Main(string[] Args) { Four Four = New Four(); System.out.println(four.methodone()); One One = Four; System.out.println(one.s); } }

Answer»

NOT FINALFINAL

FINAL

NOT FINALFINAL

FINAL

17.

Can We Declare An Interface As ‘abstract’?

Answer»

YES, interfaces can be declared as ‘ABSTRACT’. But, there is no need to DECLARE LIKE that because interfaces are ‘abstract’ by default.

Yes, interfaces can be declared as ‘abstract’. But, there is no need to declare like that because interfaces are ‘abstract’ by default.

18.

Can You Identify The Error In The Below Code? Interface A { Void Methoda(); } Class B Implements A { Public Void Methoda() { Interface C { Int I = 123; } } }

Answer»

INTERFACES can’t be LOCAL MEMBERS of a METHOD.

Interfaces can’t be local members of a method.

19.

What Will Be The Output Of The Following Program? Interface X { Char C = 'a'; Char Methodx(); } Class Y Implements X { { System.out.println(c); } Public Char Methodx() { Char C = This.c; Return ++c; } } Public Class Mainclass { Public Static Void Main(string[] Args) { Y Y = New Y(); System.out.println(y.methodx()); System.out.println(y.c); System.out.println(x.c); } }

Answer»

A

B

A

A

A

B

A

A

20.

Is The Following Program Written Correctly? If Yes, What Will Be The Output? Interface Abc { Void Methodone(); } Interface Pqr Extends Abc { Void Methodtwo(); } Abstract Class Xyz Implements Pqr { Public Void Methodone() { Methodtwo(); } } Class Mno Extends Xyz { Public Void Methodtwo() { Methodone(); } } Public Class Mainclass { Public Static Void Main(string[] Args) { Abc Abc = New Mno(); Abc.methodone(); } }

Answer»

Yes, program is WRITTEN is correctly. But, it will throw StackOverflowError at RUN time. Because, METHODONE() and methodTwo() are cyclicly CALLED.

Yes, program is written is correctly. But, it will throw StackOverflowError at run time. Because, methodOne() and methodTwo() are cyclicly called.

21.

Can Interfaces Have Static Methods?

Answer»

YES, from JAVA 8, interfaces can have static METHODS

Yes, from Java 8, interfaces can have static methods

22.

Does Below Program Compile Successfully? Interface Abc { Public Void Methodone(); Public Void Methodtwo(); } Interface Pqr Extends Abc { Public Void Methodone(); Public Void Methodtwo(); }

Answer»

YES, PROGRAM COMPILES SUCCESSFULLY.

Yes, program compiles successfully.

23.

Like Classes In Java, Interfaces Also Extend Java.lang.object Class By Default. True Or False?

Answer»

FALSE. INTERFACES don’t EXTEND OBJECT CLASS.

False. Interfaces don’t extend Object class.

24.

How Do You Access Interface Field ‘i’ In The Below Code? Class P { Interface Q { Int I = 111; } }

Answer»

P.Q.i

P.Q.i

25.

Can You Find Out The Errors In The Following Code? Interface A { { System.out.println("interface A"); } Static { System.out.println("interface A"); } }

Answer»

INTERFACES can’t have INITIALIZERS.

Interfaces can’t have initializers.

26.

Is The Below Program Written Correctly? If Yes, What Will Be The Output? Class A Implements B { Public Int Methodb(int I) { Return I =+ I * I; } } Interface B { Int Methodb(int I); } Public Class Mainclass { Public Static Void Main(string[] Args) { B B = New A(); System.out.println(b.methodb(2)); } }

Answer»

YES, PROGRAM is WRITTEN CORRECTLY. OUTPUT will be,

4

Yes, program is written correctly. Output will be,

4

27.

Can Interfaces Have Constructor?

Answer»

No. INTERFACES can’t have CONSTRUCTORS.

No. Interfaces can’t have constructors.

28.

What Will Be The Output Of The Following Program? Interface P { String P = "pppp"; String Methodp(); } Interface Q Extends P { String Q = "qqqq"; String Methodq(); } Class R Implements P, Q { Public String Methodp() { Return Q+p; } Public String Methodq() { Return P+q; } } Public Class Mainclass { Public Static Void Main(string[] Args) { R R = New R(); System.out.println(r.methodp()); System.out.println(r.methodq()); } }

Answer»

QQQQPPPP

PPPPQQQQ

QQQQPPPP

PPPPQQQQ

29.

Is The Following Code Written Correctly? Class A { //class A } Interface B Extends A { //interface B Extending Class A }

Answer»

No. An INTERFACE can EXTEND ANOTHER interface not the CLASS.

No. An interface can extend another interface not the class.

30.

Does Below Code Compile Successfully? If Not, Why? Interface A { Int I = 111; } Class B Implements A { Void Methodb() { I = 222; } }

Answer»

No, because interface fields are static and FINAL by default and you can’t change their VALUE once they are initialized. In the above code, METHODB() is changing value of interface field A.i. It shows COMPILE time error.

No, because interface fields are static and final by default and you can’t change their value once they are initialized. In the above code, methodB() is changing value of interface field A.i. It shows compile time error.

31.

Why The Below Code Is Showing Compile Time Error? Interface X { Void Method X(); } Class Y Implements X { Void Methodx() { System.out.println("method X"); } }

Answer»

Interface methods MUST be implemented as public. Because, interface methods are public by DEFAULT and you should not REDUCE the VISIBILITY of any methods while overriding.

Interface methods must be implemented as public. Because, interface methods are public by default and you should not reduce the visibility of any methods while overriding.

32.

Can A Class Implement More Than One Interfaces?

Answer»

YES, a CLASS can IMPLEMENT more than ONE INTERFACES.

Yes, a class can implement more than one interfaces.

33.

What Will Be The Output Of The Following Program? Interface A { Void Mymethod(); } Class B { Public Void My Method() { System.out.println("my Method"); } } Class C Extends B Implements A { } Class Main Class { Public Static Void Main(string[] Args) { A A = New C(); A.mymethod(); } }

Answer»

My METHOD

My Method

34.

Can You Identify The Error In The Below Code? Interface A { Private Int I; }

Answer»

ILLEGAL MODIFIER for field i. Only public, STATIC and FINAL are ALLOWED.

Illegal modifier for field i. Only public, static and final are allowed.

35.

For Every Interface Written In A Java File, .class File Will Be Generated After Compilation? True Or False?

Answer»

TRUE. For EVERY interface WRITTEN in a java file, .class file will be generated after compilation.

True. For every interface written in a java file, .class file will be generated after compilation.