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.

Which of the following is an incorrect statement regarding the use of generics and parameterized types in Java?(a) Generics provide type safety by shifting more type checking responsibilities to the compiler(b) Generics and parameterized types eliminate the need for down casts when using Java Collections(c) When designing your own collections class (say, a linked list), generics and parameterized types allow you to achieve type safety with just a single class definition as opposed to defining multiple classes(d) All of the mentionedI had been asked this question in an online quiz.I would like to ask this question from Type Interface topic in division Interfaces & Packages of Java

Answer»

Right choice is (c) When designing your own collections class (say, a linked list), GENERICS and parameterized types ALLOW you to achieve type SAFETY with just a single class definition as opposed to defining multiple classes

The BEST explanation: None.

2.

Which of these is an correct way of defining generic class?(a) class name(T1, T2, …, Tn) { /* … */ }(b) class name { /* … */ }(c) class name[T1, T2, …, Tn] { /* … */ }(d) class name{T1, T2, …, Tn} { /* … */ }This question was posed to me during an interview for a job.Asked question is from Type Interface in section Interfaces & Packages of Java

Answer»

The correct choice is (b) class name { /* … */ }

The BEST I can EXPLAIN: The type parameter SECTION, delimited by angle brackets (<>), FOLLOWS the class name. It specifies the type parameters (also called type variables) T1, T2, …, and Tn.

3.

Which of these type parameters is used for a generic class to return and accept a number?(a) K(b) N(c) T(d) VI have been asked this question in a job interview.My query is from Type Interface topic in division Interfaces & Packages of Java

Answer» CORRECT option is (B) N

Easy explanation: N is USED for Number.
4.

Which of these type parameters is used for a generic class to return and accept any type of object?(a) K(b) N(c) T(d) VThis question was posed to me during a job interview.This is a very interesting question from Type Interface in section Interfaces & Packages of Java

Answer» CORRECT choice is (c) T

To explain I would say: T is USED for type, A type variable can be any non-primitive type you specify: any class type, any interface type, any ARRAY type, or even another type variable.
5.

Why are generics used?(a) Generics make code more fast(b) Generics make code more optimised and readable(c) Generics add stability to your code by making more of your bugs detectable at compile time(d) Generics add stability to your code by making more of your bugs detectable at runtimeI have been asked this question in my homework.Question is from Type Interface in division Interfaces & Packages of Java

Answer»

Correct choice is (c) Generics add STABILITY to your code by making more of your BUGS DETECTABLE at compile TIME

For EXPLANATION: Generics add stability to your code by making more of your bugs detectable at compile time.

6.

Which of these class allows us to get real time data about private and protected member of a class?(a) java.io(b) GetInformation(c) ReflectPermission(d) MembersPermissionThis question was posed to me in a national level competition.My question is from Core Java API Packages in division Interfaces & Packages of Java

Answer»

Correct CHOICE is (C) ReflectPermission

To elaborate: The ReflectPermission class allows reflection of private or PROTECTED members of a class. This was added after JAVA 2.0 .

7.

Which of this package is used for handling security related issues in a program?(a) java.security(b) java.lang.security(c) java.awt.image(d) java.io.securityThis question was addressed to me by my college professor while I was bunking the class.I'd like to ask this question from Core Java API Packages topic in section Interfaces & Packages of Java

Answer»

The correct CHOICE is (a) java.SECURITY

To elaborate: java.security handles certificates, keys, digests, signatures, and other security functions.

8.

Which of this package is used for analyzing code during run-time?(a) java.applet(b) java.awt(c) java.io(d) java.lang.reflectThis question was addressed to me in semester exam.This intriguing question comes from Core Java API Packages in portion Interfaces & Packages of Java

Answer» RIGHT option is (d) java.lang.reflect

To EXPLAIN: REFLECTION is the ABILITY of a SOFTWARE to analyze itself. This is provided by java.lang.reflect package.
9.

Which of these package is used for graphical user interface?(a) java.applet(b) java.awt(c) java.awt.image(d) java.ioThis question was posed to me in exam.I want to ask this question from Core Java API Packages topic in portion Interfaces & Packages of Java

Answer» RIGHT OPTION is (B) java.awt

To elaborate: java.awtprovides capabilities for graphical USER INTERFACE.
10.

Can “abstract” keyword be used with constructor, Initialization Block, Instance Initialization and Static Initialization Block.(a) True(b) FalseThe question was asked in homework.My query is from Interfaces in division Interfaces & Packages of Java

Answer»

Correct choice is (B) False

Explanation: No, Constructor, STATIC INITIALIZATION Block, INSTANCE Initialization Block and VARIABLES cannot be abstract.

11.

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 exceptionI have been asked this question in examination.This question is from Interfaces in portion Interfaces & Packages of Java

Answer» CORRECT choice is (a) COMPILATION failure

The EXPLANATION: CONSTRUCTOR is not provided by interface as OBJECTS cannot be instantiated.
12.

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 definedThe question was asked in an internship interview.Enquiry is from Interfaces in section Interfaces & Packages of Java

Answer»

Right OPTION is (d) The interfaceName.variableName needs to be defined

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

13.

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 thrownI had been asked this question during a job interview.This interesting question is from Interfaces in portion Interfaces & Packages of Java

Answer»

Right answer is (c) COMPILATION failure

The best explanation: The METHODS of interfaces are ALWAYS abstract. They PROVIDE only METHOD definition.

14.

What type of methods an interface contain by default?(a) abstract(b) static(c) final(d) privateI had been asked this question in exam.I would like to ask this question from Interfaces in division Interfaces & Packages of Java

Answer»

Right choice is (a) abstract

Explanation: By default, interface CONTAINS abstract METHODS. The abstract methods need to be implemented by concrete CLASSES.

15.

All methods must be implemented of an interface.(a) True(b) FalseThis question was addressed to me in an international level competition.Asked question is from Interfaces topic in portion Interfaces & Packages of Java

Answer»

The correct choice is (a) True

For explanation I WOULD say: CONCRETE classes must IMPLEMENT all methods in an interface. Through interface multiple inheritance is possible.

16.

What does an interface contain?(a) Method definition(b) Method declaration(c) Method declaration and definition(d) Method nameThis question was posed to me in examination.This question is from Interfaces in division Interfaces & Packages of Java

Answer» RIGHT answer is (b) Method DECLARATION

Easy explanation: Interface CONTAINS the only declaration of the method.
17.

What type of variable can be defined in an interface?(a) public static(b) private final(c) public final(d) static finalThis question was addressed to me during an interview for a job.Asked question is from Interfaces in section Interfaces & Packages of Java

Answer»

Correct OPTION is (d) STATIC final

To explain: variable DEFINED in an interface is implicitly final and static. They are usually written in capital LETTERS.

18.

Which of the following is the correct way of implementing an interface A by class B?(a) class B extends A{}(b) class B implements A{}(c) class B imports A{}(d) None of the mentionedThe question was posed to me by my school principal while I was bunking the class.The doubt is from Interfaces topic in portion Interfaces & Packages of Java

Answer»

The correct answer is (B) class B IMPLEMENTS A{}

Easiest EXPLANATION: Concrete class implements an interface. They can be instantiated.

19.

Which of the following access specifiers can be used for an interface?(a) Protected(b) Private(c) Public(d) Public, protected, privateThe question was asked in exam.Question is from Interfaces in section Interfaces & Packages of Java

Answer»

The correct option is (a) Protected

To explain: Interface can have EITHER public ACCESS SPECIFIER or no specifier. The REASON is they NEED to be implemented by other classes.

20.

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 mentionedThe question was asked in a national level competition.My doubt is from Interfaces in chapter Interfaces & Packages of Java

Answer»

The CORRECT choice is (B) class manager IMPLEMENTS SALARY {}

To EXPLAIN I would say: None.

21.

Which of these keywords is used by a class to use an interface defined previously?(a) import(b) Import(c) implements(d) ImplementsThe question was asked in final exam.My question is taken from Interfaces in portion Interfaces & Packages of Java

Answer»

Right choice is (C) implements

To explain I would say: interface is inherited by a CLASS using implements.

22.

Which of these access specifiers can be used for an interface?(a) Public(b) Protected(c) private(d) All of the mentionedI have been asked this question in exam.This intriguing question comes from Interfaces topic in section Interfaces & Packages of Java

Answer»

The CORRECT answer is (a) Public

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.

23.

Which of these can be used to fully abstract a class from its implementation?(a) Objects(b) Packages(c) Interfaces(d) None of the MentionedI have been asked this question in an interview for job.My doubt stems from Interfaces topic in section Interfaces & Packages of Java

Answer» RIGHT choice is (C) Interfaces

For EXPLANATION I would SAY: NONE.
24.

Which of these keywords is used to define interfaces in Java?(a) interface(b) Interface(c) intf(d) IntfThis question was posed to me during an online interview.The origin of the question is Interfaces in division Interfaces & Packages of Java

Answer» RIGHT OPTION is (a) interface

The BEST I can EXPLAIN: NONE.
25.

Which of these access specifiers can be used for a class so that its members can be accessed by a different class in the different package?(a) Public(b) Protected(c) Private(d) No ModifierThe question was asked in final exam.The doubt is from Packages in section Interfaces & Packages of Java

Answer»

The CORRECT CHOICE is (a) Public

The BEST EXPLANATION: NONE.

26.

Which of the following is an incorrect statement about packages?(a) Package defines a namespace in which classes are stored(b) A package can contain other package within it(c) Java uses file system directories to store packages(d) A package can be renamed without renaming the directory in which the classes are storedI have been asked this question in an internship interview.My question is from Packages in chapter Interfaces & Packages of Java

Answer» CORRECT CHOICE is (d) A package can be renamed WITHOUT renaming the directory in which the classes are STORED

To explain: A package can be renamed only after renaming the directory in which the classes are stored.
27.

Which of the following is the correct way of importing an entire package ‘pkg’?(a) import pkg.(b) Import pkg.(c) import pkg.*(d) Import pkg.*The question was asked in exam.This is a very interesting question from Packages topic in chapter Interfaces & Packages of Java

Answer»

Right choice is (c) IMPORT PKG.*

For explanation I would say: Operator * is USED to import the ENTIRE PACKAGE.

28.

Which of this access specifies can be used for a class so that its members can be accessed by a different class in the same package?(a) Public(b) Protected(c) No Modifier(d) All of the mentionedI got this question during a job interview.The question is from Packages in chapter Interfaces & Packages of Java

Answer» CORRECT option is (d) All of the mentioned

For explanation: EITHER we can use PUBLIC, protected or we can NAME the CLASS without any specifier.
29.

Which of these keywords is used to define packages in Java?(a) pkg(b) Pkg(c) package(d) PackageI got this question at a job interview.My question is based upon Packages in chapter Interfaces & Packages of Java

Answer» RIGHT CHOICE is (C) package

To ELABORATE: NONE.
30.

Which of these is a mechanism for naming and visibility control of a class and its content?(a) Object(b) Packages(c) Interfaces(d) None of the Mentioned.This question was addressed to me in an online quiz.I need to ask this question from Packages in section Interfaces & Packages of Java

Answer»

Right option is (b) Packages

Easy explanation: Packages are both naming and visibility control mechanism. We can define a CLASS INSIDE a package which is not accessible by code OUTSIDE the package.