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 is better in terms of performance for iterating an array?(a) for(int i=0; i=0; i–)(c) for(int i=100; i0; i++)I had been asked this question in a job interview.Origin of the question is Coding best practices topic in section Autoboxing & Miscellaneous of Java

Answer»

Correct option is (b) for(INT i=99; i>=0; i–)

The BEST I can EXPLAIN: reverse traversal of array take HALF number cycles as COMPARED to forward traversal. The other for loops will go in infinite loop.

2.

Which of the below is false about java coding?(a) variable names should be short(b) variable names should be such that they avoid ambiguity(c) test case method names should be created as english sentences without spaces(d) class constants should be used when we want to share data between class methodsI have been asked this question during an internship interview.I'm obligated to ask this question of Coding best practices topic in portion Autoboxing & Miscellaneous of Java

Answer»

Right answer is (a) variable names should be short

To explain I would say: variable names LIKE i, a, abc, etc should be avoided. They should be real world names which AVOID ambiguity. TEST case name should explain its significance.

3.

What data structure should be used when number of elements is fixed?(a) Array(b) Array list(c) Vector(d) SetThis question was posed to me during an online interview.This interesting question is from Coding best practices topic in portion Autoboxing & Miscellaneous of Java

Answer»

Right CHOICE is (a) Array

For EXPLANATION I would say: Array list has VARIABLE size. Array is stored in contiguous memory. Hence, reading is faster. ALSO, array is memory efficient.

4.

Which of the below is true about java class structure?(a) The class name should start with lowercase(b) The class should have thousands of lines of code(c) The class should only contain those attribute and functionalitywhich it should; hence keeping it short(d) The class attributes and methods should be publicI have been asked this question during an interview for a job.Enquiry is from Coding best practices in division Autoboxing & Miscellaneous of Java

Answer»

Right OPTION is (c) The class should only contain those attribute and functionalitywhich it should; hence KEEPING it short

For explanation I WOULD say: Class name should always start with upper case and contain those attribute and functionality which it should (Single Responsibility Principle); hence keeping it short. The attributes should be USUALLY private with get and set methods.

5.

Which of the following is a best practice to measure time taken by a process for execution?(a) System.currentTimeMillis()(b) System.nanoTime()(c) System.getCurrentTime()(d) System.getProcessingTime()I got this question in an internship interview.This is a very interesting question from Coding best practices in portion Autoboxing & Miscellaneous of Java

Answer»

Right ANSWER is (B) System.nanoTime()

The best explanation: System.nanoTime takes AROUND 1/100000 TH of a second WHEREAS System.currentTimeMillis takes around 1/1000th of a second.

6.

Which one of the following causes memory leak?(a) Release database connection when querying is complete(b) Use Finally block as much as possible(c) Release instances stored in static tables(d) Not using Finally block oftenThis question was posed to me in an interview for internship.My query is from Coding best practices topic in portion Autoboxing & Miscellaneous of Java

Answer» CORRECT option is (d) Not USING Finally block often

To EXPLAIN I would say: Finally block is called in successful as well exception SCENARIOS. HENCE, all the connections are closed properly which avoids memory leak.
7.

What should the return type of method where there is no return value?(a) Null(b) Empty collection(c) Singleton collection(d) Empty StringI have been asked this question in quiz.I'm obligated to ask this question of Coding best practices topic in chapter Autoboxing & Miscellaneous of Java

Answer»

Right OPTION is (b) EMPTY COLLECTION

Explanation: Returning Empty collection is a GOOD practice. It eliminates chances of UNHANDLED null pointer exceptions.

8.

Which of the following is not an advantage of using Hibernate Query Language?(a) Database independent(b) Easy to write query(c) No need to learn SQL(d) Difficult to implementI had been asked this question by my school teacher while I was bunking the class.Origin of the question is Hibernate topic in portion Autoboxing & Miscellaneous of Java

Answer» CORRECT answer is (d) Difficult to implement

To explain I WOULD SAY: HQL is easy to implement. Also, to implement it HQL it is not DEPENDENT on a database platform.
9.

In which file database table configuration is stored?(a) .dbm(b) .hbm(c) .ora(d) .sqlThis question was addressed to me in an interview.Query is from Hibernate topic in division Autoboxing & Miscellaneous of Java

Answer»

The correct option is (b) .HBM

The best I can explain: Database table CONFIGURATION is stored in .hbm FILE.

10.

What does Liskov substitution principle specify?(a) parent class can be substituted by child class(b) child class can be substituted by parent class(c) parent class cannot be substituted by child class(d) No classes can be replaced by each otherI had been asked this question in class test.My question is from Liskov’s Principle in section Autoboxing & Miscellaneous of Java

Answer»

The CORRECT answer is (a) parent class can be substituted by child class

Explanation: Liskov substitution principle states that Objects in a program should be REPLACEABLE with instances of their SUB types without ALTERING the correctness of that program.

11.

Which of the following is not an inheritance mapping strategies?(a) Table per hierarchy(b) Table per concrete class(c) Table per subclass(d) Table per classThis question was posed to me in examination.This interesting question is from Hibernate topic in chapter Autoboxing & Miscellaneous of Java

Answer»

The CORRECT OPTION is (d) Table PER CLASS

For explanation: Table per class is not an inheritance mapping strategies.

12.

Which of the following method is used inside session only?(a) merge()(b) update()(c) end()(d) kill()The question was asked during an interview for a job.This question is from Hibernate in section Autoboxing & Miscellaneous of Java

Answer» RIGHT choice is (B) update()

Explanation: update() METHOD can only be used INSIDE SESSION. update() should be used if session does not contain persistent object.
13.

Which of the following is not a state of object in Hibernate?(a) Attached()(b) Detached()(c) Persistent()(d) Transient()I have been asked this question in exam.Origin of the question is Hibernate topic in portion Autoboxing & Miscellaneous of Java

Answer» CORRECT option is (a) ATTACHED()

The best I can EXPLAIN: Attached() is not a state of object in Hibernate. Detached(), PERSISTENT() and Transient() are the only states in Hibernate.
14.

Which of the following methods hits database always?(a) load()(b) loadDatabase()(c) getDatabase()(d) get()The question was posed to me during an internship interview.I'd like to ask this question from Hibernate in chapter Autoboxing & Miscellaneous of Java

Answer»

Correct choice is (d) GET()

The explanation is: get() method hits DATABASE ALWAYS. Also, get() method does not return proxy OBJECT.

15.

Which of the following methods returns proxy object?(a) loadDatabase()(b) getDatabase()(c) load()(d) get()I got this question in an internship interview.I'd like to ask this question from Hibernate in section Autoboxing & Miscellaneous of Java

Answer» RIGHT choice is (C) LOAD()

EASIEST explanation: load() method returns proxy OBJECT. load() method should be used if it is sure that instance exists.
16.

SessionFactory is a thread-safe object.(a) True(b) FalseThe question was asked in final exam.The doubt is from Hibernate in section Autoboxing & Miscellaneous of Java

Answer» RIGHT ANSWER is (a) True

The explanation is: SessionFactory is a thread-safe object. MULTIPLE threads can ACCESS it SIMULTANEOUSLY.
17.

How can we filter lines based on content?(a) lines.filter()(b) filter(lines)(c) lines.contains(filter)(d) lines.select()I had been asked this question in quiz.This key question is from File and Directory in section Autoboxing & Miscellaneous of Java

Answer» RIGHT CHOICE is (a) lines.FILTER()

EASY explanation: lines.filter(line -> line.contains(“===—> Loaded package”)) can be used to filter out.
18.

How can we create a symbolic link to file?(a) createLink()(b) createSymLink()(c) createSymbolicLink()(d) createTempLink()The question was posed to me in homework.Asked question is from File and Directory topic in division Autoboxing & Miscellaneous of Java

Answer»

The CORRECT option is (C) createSymbolicLink()

To explain: createSymbolicLink() creates a SYMBOLIC link to a TARGET.

19.

Which of the following is not a core interface of Hibernate?(a) Configuration(b) Criteria(c) SessionManagement(d) SessionThe question was posed to me in examination.I want to ask this question from Hibernate topic in division Autoboxing & Miscellaneous of Java

Answer» RIGHT choice is (c) SESSIONMANAGEMENT

To elaborate: SessionManagement is not a core INTERFACE of Hibernate. Configuration, Criteria, SessionFactory, Session, Query and TRANSACTION are the core INTERFACES of Hibernate.
20.

How to read entire file in one line using java 8?(a) Files.readAllLines()(b) Files.read()(c) Files.readFile()(d) Files.lines()This question was addressed to me by my school teacher while I was bunking the class.This intriguing question originated from File and Directory in chapter Autoboxing & Miscellaneous of Java

Answer»

Correct ANSWER is (a) Files.readAllLines()

The best explanation: Java 8 provides Files.readAllLines() which allows us to READ entire file in ONE task. We do not NEED to worry about READERS and writers.

21.

Which jar provides FileUtils which contains methods for file operations?(a) file(b) apache commons(c) file commons(d) dirThe question was asked in an interview.This question is from File and Directory topic in portion Autoboxing & Miscellaneous of Java

Answer»

Right option is (b) apache commons

For explanation I would SAY: FileUtils is a part of apache commons which PROVIDES various methods for file OPERATIONS LIKE writeStringToFile.

22.

How can we get the size of specified file?(a) capacity(path)(b) size(path)(c) length(path)(d) Path.size()I got this question during an online exam.Question is taken from File and Directory in chapter Autoboxing & Miscellaneous of Java

Answer»

Right answer is (b) SIZE(PATH)

Easiest explanation: size(Path) RETURNS the size of the specified FILE in BYTES.

23.

How can we delete all files in a directory?(a) Files.delete(path)(b) Files.deleteDir()(c) Directory.delete()(d) Directory.delete(path)I have been asked this question in a job interview.My question is from File and Directory topic in division Autoboxing & Miscellaneous of Java

Answer»

The correct OPTION is (a) Files.delete(PATH)

Easy explanation: The delete(Path) method deletes the file or THROWS an EXCEPTION if the deletion FAILS. If file does not exist a NoSuchFileException is thrown.

24.

How to copy the file from one location to other?(a) Files.copy(source, target)(b) Path.copy(source, target)(c) source.copy(target)(d) Files.createCopy(target)This question was addressed to me in an interview for internship.The above asked question is from File and Directory topic in section Autoboxing & Miscellaneous of Java

Answer»

Right answer is (a) Files.COPY(source, target)

The explanation is: Files.copy(source, target) is used to copy a file from one LOCATION to another. There are VARIOUS options available like REPLACE_EXISTING, COPY_ATTRIBUTES and NOFOLLOW_LINKS.

25.

Which method can be used to check fileAccessiblity?(a) isReadable(path)(b) isWritable(path)(c) isExecutable(path)(d) isReadable(path), isWritable(path), and isExecutable(path)I have been asked this question in an international level competition.I'm obligated to ask this question of File and Directory topic in division Autoboxing & Miscellaneous of Java

Answer»

The CORRECT OPTION is (d) isReadable(PATH), isWritable(path), and isExecutable(path)

To ELABORATE: FILE accessibilty can be checked usingisReadable(Path), isWritable(Path), and isExecutable(Path).

26.

Which method is used to create a directory with fileattributes?(a) Path.create()(b) Path.createDirectory()(c) Files.createDirectory(path, fileAttributes)(d) Files.create(fileAttributes)I have been asked this question during an interview.Enquiry is from File and Directory in portion Autoboxing & Miscellaneous of Java

Answer»

The CORRECT ANSWER is (c) Files.createDirectory(path, FILEATTRIBUTES)

EASIEST explanation: New directory can be created USING Files.createDirectory(path, fileAttribute).

27.

What does Files.lines(Path path) do?(a) It reads all the files at the path specified as a String(b) It reads all the lines from a file as a Stream(c) It reads the filenames at the path specified(d) It counts the number of lines for files at the path specifiedThe question was asked in class test.This question is from Java 8 Features in section Autoboxing & Miscellaneous of Java

Answer» CORRECT choice is (b) It reads all the lines from a FILE as a Stream

Explanation: Files.lines(Path path) that reads all lines from a file as a Stream.
28.

What is Optional object used for?(a) Optional is used for optional runtime argument(b) Optional is used for optional spring profile(c) Optional is used to represent null with absent value(d) Optional means it’s not mandatory for method to return objectThe question was asked in final exam.My doubt is from Java 8 Features topic in chapter Autoboxing & Miscellaneous of Java

Answer» CORRECT answer is (c) Optional is used to represent null with ABSENT value

Explanation: Optional object is used to represent null with absent value. This class has various utility methods to facilitate code to HANDLE values as ‘available’ or ‘not available’ instead of CHECKING null values.
29.

What is the substitute of Rhino javascript engine in Java 8?(a) Nashorn(b) V8(c) Inscript(d) NarcissusI have been asked this question in quiz.My query is from Java 8 Features in chapter Autoboxing & Miscellaneous of Java

Answer»

Correct choice is (a) Nashorn

Best explanation: Nashorn provides 2 to 10 TIMES FASTER in terms of performance, as it directly COMPILES the code in memory and passes the bytecode to JVM. Nashorn USES invoke dynamic feature.

30.

Which feature of java 8 enables us to create a work stealing thread pool using all available processors at its target?(a) workPool(b) newWorkStealingPool(c) threadPool(d) workThreadPoolI got this question in a job interview.I would like to ask this question from Java 8 Features in section Autoboxing & Miscellaneous of Java

Answer»

The correct option is (B) NEWWORKSTEALINGPOOL

For explanation: Executors newWorkStealingPool() method to create a work-stealing THREAD pool using all available processors as its target PARALLELISM level.

31.

What are the two types of Streams offered by java 8?(a) sequential and parallel(b) sequential and random(c) parallel and random(d) random and synchronizedI had been asked this question in an interview.The above asked question is from Java 8 Features topic in division Autoboxing & Miscellaneous of Java

Answer»

The CORRECT CHOICE is (a) sequential and parallel

The best explanation: Sequential stream and parallel stream are two types of stream provided by JAVA.

32.

Which is the new method introduced in java 8 to iterate over a collection?(a) for (String i : StringList)(b) foreach (String i : StringList)(c) StringList.forEach()(d) List.for()The question was posed to me in final exam.This interesting question is from Java 8 Features topic in chapter Autoboxing & Miscellaneous of Java

Answer»

Correct ANSWER is (C) StringList.forEach()

For EXPLANATION: TRAVERSING through forEach method of Iterable with anonymous CLASS.

33.

What is the return type of lambda expression?(a) String(b) Object(c) void(d) FunctionThis question was posed to me in examination.This question is from Java 8 Features in division Autoboxing & Miscellaneous of Java

Answer»

Correct answer is (d) Function

To explain: LAMBDA expression ENABLES us to pass FUNCTIONALITY as an argument to another method, such as what action should be taken when someone CLICKS a button.

34.

What is the purpose of BooleanSupplier function interface?(a) represents supplier of Boolean-valued results(b) returns Boolean-valued result(c) There is no such function interface(d) returns null if Boolean is passed as argumentI got this question in an online quiz.My question comes from Java 8 Features in portion Autoboxing & Miscellaneous of Java

Answer» RIGHT CHOICE is (a) REPRESENTS supplier of Boolean-valued results

The best I can EXPLAIN: BooleanSupplier function interface represents supplier of Boolean-valued results.
35.

Which of the following is not introduced with Java 8?(a) Stream API(b) Serialization(c) Spliterator(d) Lambda ExpressionThis question was addressed to me at a job interview.My question is taken from Java 8 Features topic in section Autoboxing & Miscellaneous of Java

Answer» RIGHT option is (b) SERIALIZATION

The EXPLANATION: Serialization is not introduced with Java 8. It was introduced with an EARLIER version of Java.
36.

Which of these is not a mocking framework?(a) EasyMock(b) Mockito(c) PowerMock(d) MockJavaI had been asked this question in a national level competition.My question is from JUnits topic in portion Autoboxing & Miscellaneous of Java

Answer»

The correct option is (d) MockJava

Explanation: EASYMOCK, jMock, Mockito, Unitils MOCK, PowerMock and JMockit are a VARIOUS MOCKING FRAMEWORK.

37.

Which of the below is an incorrect annotation with respect to JUnits?(a) @Test(b) @BeforeClass(c) @Junit(d) @AfterEachThe question was asked in quiz.My question is from JUnits topic in chapter Autoboxing & Miscellaneous of Java

Answer»

The correct OPTION is (c) @Junit

Best explanation: @Test is USED to ANNOTATE method under test, @BeforeEach and @AfterEach are CALLED before and after each method respectively. @BeforeClass and @AfterClass are called only once for each CLASS.

38.

Which of the below statement about JUnit is false?(a) It is an open source framework(b) It provides an annotation to identify test methods(c) It provides test runners for running test(d) They cannot be run automaticallyI have been asked this question in quiz.My question is taken from JUnits in division Autoboxing & Miscellaneous of Java

Answer» RIGHT option is (d) They cannot be RUN automatically

Easy explanation: JUnits test can be run automatically and they CHECK their own RESULTS and provide immediate feedback.
39.

JUnits are used for which type of testing?(a) Unit Testing(b) Integration Testing(c) System Testing(d) Blackbox TestingThe question was posed to me in unit test.Question is from JUnits in chapter Autoboxing & Miscellaneous of Java

Answer»

The correct option is (a) UNIT Testing

For explanation I would say: JUNIT is a testing FRAMEWORK for unit testing. It uses java as a PROGRAMMING platform. It is managed by junit.org community.