Core Java Interview Questions- Part 5

Core Java Interview Questions- Part 5
During programming interviews, employers want to know how proficient you are in Core Java concepts like object-oriented principles, memory management, exception handling, multithreading, etc. So, to help you out, we have created this Java interview questions page.
The page has about 200 questions, covering all the Core Java topics so that when you go to interviews, you are well-prepared for the questions, which are commonly asked by the interviewers.
You can go through the essential concepts by reading the questions and answers. This interview questions guide is useful for gaining knowledge, identifying weak areas, and improving your responses. Let’s start practicing and move one step closer to your dream Java job.

Answer:

A deadlock occurs when multiple threads are blocking each other. It is in regards to the threads trying to acquire the shared resources. A race condition occurs when multiple threads interact negatively depending on the exact order that their execution takes place.

Answer:

Java does not support multiple inheritance using class because whenever a sub or child class wants to access the property of two or more parent or super class that have same method, java compiler can’t decide which class method it should be inherit.

Answer:

When we have more than one method in a class with the same name but different arguments, it is called overloading. Overriding occurs when we have two methods with the same signature from which one in the child class & the other is in the parent class.

Answer:

No, while defining two or more classes in a single Java file, we need to ensure that only one class amongst them is public. If we have multiple public classes in a single file, it generates a compile-time error.

Answer:

Java package is a mechanism that organizes classes by grouping them. The grouping logic can be based on modules or functionality. A java class fully classified name contains package and class name. Java compiler imports java.lang package by default & it doesn’t need to be imported explicitly.

Answer:

Finally block is used to execute clean-up codes such as closing files, closing connections, or freeing up threads since it excites irrespective of an exception. Whereas, Finalize is a method in the Object class that can be overridden in our classes. It is generally overridden to release system resources whenever the object is garbage collected.

Answer:

An interface can extend many interfaces. Still, it cannot implement another interface because if an interface is implemented, its methods need to be defined & the interface never has a definition of any method.

Answer:

Annotation is metadata about the program embedded in itself. It can be parsed by the compiler or annotation parsing tool. One can specify annotation availability till runtime or to compile-time only.

Answer:

An anonymous inner class refers to a local inner class with no name. It is defined & instantiated into a single statement. Anonymous inner class always implements an interface or extends a class. It is only accessible at the point where it is defined.

Answer:

As object references are passed by value, so Java also pass by value, not by reference.

Answer:

Yes, the final blank variable can be initialized in the constructor if it is not static. In case it is a static blank final variable, then it can be initialized in the static block only.

Answer:

‘this’ keyword refers to the current object in a method or constructor. It is most commonly used to eliminate the confusion between class attributes & parameters with the same name.

Answer:

There is nothing wrong with using HashMap; it all depends on how you use it. For instance, if you initialize the HashMap using one thread & all the other threads are just reading from it, it does not create any problem. However, the main issue arises when at least one thread is updating HashMap, i.e., changing, removing, or adding any key-value pair.

Answer:

No, it is not essential to have all properties final. In immutable objects, you must not allow the users to modify the class’s variables. To do that, you can make variables private and not provide setter methods to change them.

Answer:

When we call a stored procedure, it executes in the database server, so if an exception occurs, that can be handled in the EXCEPTION block in the stored procedure. If the stored procedure fails, it throws an SQLException, which can be handled by a try or catch block.

Answer:

It is particularly overridden when you want to check equality based on business logic instead of object equality. It is also essential to override both methods if you want to use them as a key in HashMap.

Answer:

If you want to use any object as a key in HashMap, then it must implement the hashcode & equals method.

Answer:

A wrapper class converts the primitive data types into objects or reference types. Each primitive data type has a class dedicated to it, known as wrapper class, as it wraps the primitive data type into that class object.

Answer:

Enumeration means a list of named constant that defines a class type in Java. An Enumeration can have methods, constructors, & instance variables. It is created using the enum keyword. Each Enumeration constant is static, final, & public by default. Enumeration variables are used & declared in the same way as primitive variables.