Core Java Interview Questions with Answers

Like this post? Rate it:
1631

In this Java Interview Questions post, I am going to list down some of the important and mix of basic and advanced Java Interview questions which will increase your chances to pursue your career in Java programming.

25 Core Java Interview Questions and Answers

1. What do you know about JVM? Why Java is considered as platform independent language?

Java Virtual Machine or JVM is a process that executes Java bytecode. Each of the Java source file is basically compiled into a bytecode file, which then executed by JVM. Java has the ability to allow a program to be built and run anywhere without having to write again or recompile for a separate platform. This is only possible through JVM because it is aware of the specific instructions and other specifications underlying the particular hardware platform.

2. List down Data Types supported by Java?

Primitive data types which are supported by Java are as follows:

  • byte
  • short
  • int
  • long
  • float
  • double
  • boolean
  • char

3. What do you know about JDK and JRE? What is the difference?

Java Runtime Environment or JRE is basically a Java Virtual Machine (JVM) where actual java programs get executed. Whereas the Java Development Kit or JDK is the package which featured software development kit for Java. JDK includes JRE, compilers and other tools which are essential in order to develop, compile, and execute Java programs.

4. What can you tell about function overriding and overloading in Java?

When there are two or more methods in the same class having exact name but different parameters, then function overloading occurs. Whereas, when a child class redefines the same method as that of parents class then we call this case as function overriding.

5. What do static keywords mean?

This means that a method or a member variable can be accessed without having or requiring proper instantiation of the class where it belongs.

6. Can you override private or static method in Java?

No. Because method overriding is based on dynamic binding at runtime only, while static methods bound at compile time.

7. Do multiple inheritances are supported in Java?

Not at all. In Java, each class can extend only one class, while it can implement more than one interface.

8. How Abstract class and Interface differs in Java?

In Java, we are able to create both abstract classes and interfaces and they share some common characteristics as well, but they differ in the following ways:

  • In a class, we can implement a number of interfaces but can only extend one abstract class.
  • In an interface, member is by default public. Whereas abstract class can be public, private, or protected.
  • In order to implement an interface, a class must implement all the declared methods. While in abstract class, a class may not implement all declared methods.

9. What is the difference between processes and threads?

A process in Java, is an execution of a program, while Threads, on the other hand, is a single execution sequence within the process. A process can contain multiple threads.

Learn more about Multithreading in Java.

10. What are the different ways of creating Threads? Which way will you prefer and what is the reason?

There are two major ways in Java to create a thread:

  • A class may extend a Thread class.
  • Implement Runnable interface in a class.

The Runnable interface is the preferred way of creating the thread as it does not require an object to inherit the Thread class. For complex applications, if there is a requirement for multiple inheritance then the only way is to pick Runnable interface.

Learn more about creating threads in Java.

11. Explain and list down thread states in an order?

New: This is the initiation phase when thread is created. Thread will reside in this state until the run method is not called off. It is also known as born thread.

Runnable: After the thread is created and once it starts using start () method then we call that state runnable with the usage of run () method. Thread in this state is considered to be executing its specific task.

Waiting: Sometimes a thread transition to the waiting state while it waits for another thread to perform a task. A waiting thread transitions back to the runnable state only when another thread notifies it to continue executing.

Timed Waiting: When a thread is in the runnable state it can enter in the timed waiting state for a specific time interval. A thread in this state goes back to the runnable state when the time interval expires or when the event for which it is waiting to occur.

Terminated: A running method ends it life when it completes its task. It happens by default.

To know more about states in threads, click here .

12. Do you know about basic interfaces of Java Collections Framework?

  • Collection: it provides the framework to represents group of objects
  • Set: a collection which does not contain duplicate elements
  • List: collection in an ordered form and contain duplicate elements
  • Map: an object that maps keys to the values and cannot contain duplicates keys

13. Explain the difference between Array and ArrayList?

  • ArrayList contains only objects. Array contains primitive or objects.
  • ArrayList is dynamic, while Arrays have fixed size.
  • ArrayList provides methods and features such as: addAll, removeAll, iterator etc.

Learn more about Array and ArrayList in Java.

14. What is the difference between Enumeration and Iterator?

Both are interfaces in Java and perform same function i.e. traversing the Collection object. Enumeration is considered as fast and uses very less memory, whereas Iterator is safe as compared to Enumeration due to fact that thread do not able to modify the collection object which is currently traversed by Iterators.

15. What do you tell us about garbage collection? What is the purpose?

In Java, Garbage Collection identifies and follows by discarding those objects which are no longer needed within the application, so that resources can be reused.

16. What is the purpose of finalize ()? When it’s called?

This method is called by garbage collector before release the memory of the object. It is a good practice to release resources which is held by the object inside finalize ().

17. Define two types of Exceptions in Java and explain the differences?

There are two kinds of exceptions: Checked and Unchecked.

Checked exceptions are checked at compile time. If some piece of code or module in the application throws such exception then method must handle the exception or specify it by using throws keyword. While unchecked exceptions are not checked at compile time. Compiler will not complain if such exceptions occur. It is a good practice to specify or catch that particular exception.

18. What is the difference between Exception and Error?

Both exception and error are subclasses belong to Throwable class. In case of Error, a class defines exception that is not expected to be caught by the program, whereas Exception class is used for exceptional conditions that are to be caught by the program.

19. What is an Applet? Explain its lifecycle.

Applets are used to create interactive, user friendly and interactive web application. These are the Java programs that can be included in HTML code. Following is the lifecycle of an Applet:

  • Init: Applet is initialized each time it is loaded
  • Start: Applet begins the execution
  • Stop: Applets stops the execution
  • Destroy: Cleanup before unloading

20. What’s the difference between Applet and Java Application?

Java Application is a standalone program that is executed outside of a browser while Applets need to be executed within the Java-enabled browser. In order to start their execution, Java Application requires the main method with a specific signature, while Applets don’t need such methods. However, both need the existence of JVM.

21. What do you understand by deadlock in Java?

Deadlock is a condition where two processes are waiting for each other to get completed before proceeding further. This results in the process to wait endlessly.

22. What is the difference between HashMap and HashTable?

Both of them implements Map interface and have many characteristics in common, but they differs in the following ways:

  • HashMap allows existence of null keys and values, while HashTable doesn’t allow any null keys and values.
  • HashMap is not synchronized, but HashTable is.
  • HashMap is preferred for single-threaded in environment and HashTable for multi-threaded environment.
  • HashTable is considered to be a legacy class.

23. What is Comparable and Comparator Interface?

For more on this topic, click here.

24. Does main () return anything in Java?

No. It does not return anything. It is always declared with a void return type.

25. Why Java is not 100% Object Oriented language?

It is because Java uses eight primitive data types such as boolean, byte, char, int, float, double, long, short which are not objects.

No comments yet. Be the first to add a comment!

Write a comment

Loading...