Blog Java Top 20 Core Java Interview Questions and Answers
Core Java Interview Questions

Top 20 Core Java Interview Questions and Answers

You may have gone through the previous article on Top 100 Java Interview Questions and Answers where we have covered 20+ Core Java interview questions. Here we bring a new set of top 20 Core Java interview questions that are frequently asked in the Java interview. This article will help you prepare and crack the Java interview, let’s know something about Java first.

As technology has evolved through the time and many programming has come and gone. But Java has been the most consistent, prominent, and the most efficient programming language through decades. It has been one of the most popular programming languages around the globe and is designed to work in a proficient manner.

Check now: Oracle Java Certification Training Courses

Java was created by the Sun Microsystems in 1995 as the open-source and platform-independent programming language. Later, Oracle acquired Java from the Sun Microsystems and since then it has gained the prized possession. Oracle worked rigorously on the Java security features and made it more versatile and portable, so it can run on any operating system. Java quickly took over the software industry and became the game-changing technology for various sectors and devices.

Java is used by 90% of the fortune 500 companies. It is used by various sectors such as government, healthcare, education, defense, insurance, manufacturing, tech and many more. Java is used for providing the backend architecture for the companies and its scalability and speed over the various other tools is recommendable. Java is considered to be the omnipresent programming language as it has run on over 3 billion machines around the world which is more than any other language.

Core Java Interview Questions with Detailed Answers

If you are a fresher and looking for a job in the IT sector than you should prepare with core Java interview questions properly because in an interview you will face a barrage of core Java questions. For the interview, you need to be familiar with all the Java-based technologies for doing well in the interview. In this article, we have compiled a list of core Java interview questions with the questions that are likely to be asked in the interview. Latest core java interview question plays a vital role in any IT sector job interview. Core Java maintained the top spot for any interviewer and interviewee and it plays a crucial role in the interview and is always the deciding factor.

This article covers core java interview questions which are prepared in a way that gives a clear and easy understanding of basic and advanced concepts.

Most Popular Core Java Interview Questions for Freshers

Let’s begin with the Core Java interview questions for freshers. As a fresher in Java, you may not have much knowledge about Advanced Java so the interviewer will test your basic knowledge on the core Java concepts. Here we bring the latest Core Java interview questions for freshers to you start a career in Java. You may also be asked a few Java programming interview questions so we’ve included them too.

1. What is JAVA?

Answer: Java is a robust, secure and high-level programming language which was designed by James Gosling in 1995. Java is concurrent, class-based, and the collection of objects. It works on the principle of write once and use anywhere that means once it compiles, it can be run on any platform without the requirement of recompilation. Java is also used for developing many applications, websites, and games.

2. What is Java Virtual Machine and is it platform-independent?

Answer: Java Virtual Machine is the heart and soul of the Java programming language. JVM is a virtual machine which acts as a real runtime engine which invokes the main method present in the java program. It is responsible for converting bytecode into native code which is machine readable.

JVM is not platform independent as you need different JVM for different operating systems. Its interface does not depend on the underlying operating system because of its virtual nature.

3. What is the key difference between JDK, JVM, and JRE?

Answer: The key difference between JDK, JVM, and JRE can be given as –

JDK

JDK stands for Java Development Kit. For the development of any applications, we have to go through two phases:

In the first phase, we write code and in the second phase, we compile and run the code. For all this to happen, we need an environment which is provided by the JDK.

JRE

JRE stands for Java Runtime Environment. It is a set of tools which are used for developing the applications and it is used to provide a runtime environment. JRE is an implementation of JDK and it contains different libraries and other files which are used by the JDK at runtime.

JVM

JVM stands for Java Virtual Machine and it is responsible for running Java program in JRE. It is an interpreter as it runs code line by line. It is also responsible for converting the bytecode into native code which is machine readable.

4. Why Java is not purely Object Oriented language?

Answer: Java is not purely Object Oriented because it has different primitive data types such as int, long, short, byte etc. Obviously, primitive data types bring simplicity to the Java program and we can also have wrapper objects for the primitive data types but just for representation sake; it will not provide any benefit.

Java Banner

5. Why in Java, the main method is static?

Answer: The main method is static because if it is not there, there will be a lot of confusion about which constructor to call. Let’s take an example of it:

public class DemoJava 
   protected DemoJava (int x)   {  

   }   public void main (Stings   [  

   ]     args)   {  

   }
}

JVM will get confused, should it call the new DemoJava (int)? What value for X has to pass it for? If not then should it go for the DemoJava class without running any of the constructor methods? Sometimes there will be the case in which initialization is not done and JVM have to check it in every method. For making it all sense for JVM the instantiation of a class should be at the entry point and that’s why main is static.

6. What is overloading and overriding in Java?

Answer: The concept of overloading and overriding can be explained as below:

  • When a Java program has more than one method with the same name in a single class but the arguments are different, then it is called method overloading.
  • Overriding in Java is done when inheritance comes up in the picture. When two methods have the same name and the same signature, one is parent class and another is child class. In this type of cases where the method of child class is overridden to make sure if any changes are done in parent class then child class also get changed according to it.

7. Why is multiple inheritance not supported by Java?

Answer: Because of “Diamond Problem” multiple inheritance is not supported by the Java. It is an ambiguity that arises as a consequence of multiple inheritance. However, multiple inheritance  is supported by Java through the use of interface where methods are declared but the implementation is present in the implementing class.

8. What is “this” keyword in Java?

Answer: this keyword is present in the instance method or a constructor and used to reference the current object. The most important work is to make sure that object variables are being used, not the local variables with the same name.

//constructor

public Demo(int x, int y)

{

this.x = x;

this.y = y;

}

With this keyword, we can also invoke a constructor from other constructors

public Car() 
   this(0,
   0,
   0,
   0   );
}public Car(int width,
int height){  
   this(0,
   0,
   width,
   height)
}public Car(int x,
int y,
int width,
int height){  
   this.x = x; 

this.y = y; 

this.x = width; 

this.y = height;

9. Can we overload the main method?

Answer: Yes, we can have multiple methods with the name “main” in a single class. But when we execute the program, Java Runtime Environment searches the program with the main method that has the following syntax:

public static void main(String[] args)

10. What is the Java Package and which is the default Java package?

Answer: Java Package is the process of managing the Java classes by grouping them. This grouping is done on the basis of functionality or modules. A Java class contains different fully classified name contains packages and class names. For Example java.lang.The object is the fully classified name of an object class which is part of the java.lang package.

Java.lang package is the default package which is imported without any call and we can use any class from this package explicitly.

Top Core Java Interview Questions and Answers for Experienced

In this part, we are going to talk about the most popular Core Java interview questions and answers for experienced, though freshers should also go through these questions. If you are looking for some most popular Core Java Interview questions for doing well in the interview, these questions will definitely provide you with a better platform for strengthening your preparation.

11. What is an abstract class?

Answer: Abstract classes are the classes in Java which are used to create a class with some default method implementation for the subclasses. An abstract class can have different methods but without containing the body and it can have methods with implementations also.

The abstract keyword is used to create the abstract class. Instantiation of an abstract class cannot be done and is used only to provide a base for sub-classes to extend and implement the abstract methods and override or use the implemented methods in the abstract class. Abstract classes use final methods which cannot be overridden.

Follow these top 20 Java Influencers and experience the difference in your knowledge.

12. What is “super” keyword and what does it do?

Answer: The super keyword in java is a reference variable which is used to reference parent class objects. The keyword super only comes into the light with the concept of Inheritance. When an instance of a sub-class is created then the parent class instance is also created implicitly which is referred by the super reference variable.

// Base class fruit 

class Fruit 
   Int price = 120;
}// sub class mango for extending fruit 

class Fruit extends Mango{  
   int price = 120; 

void display()   {  
      System.out.println(“Maximum Price”);
   }
}class Demo{  
   Public static void main(String   [  

   ]   args)   {  
      mango safari = new Mango(); 

safari.display();
   }
}

13. What is Deadlock? How will you check and avoid deadlock situation?

Answer: Deadlock is a programming phenomenon where two or more threads are blocked forever. This situation arises with at least two threads and two or more resources.

To check a deadlock situation, we focus on the Java thread dump of the application where we search for the threads which are in blocked state and its resources are waiting to lock, with the unique ID every thread have we can find the thread which is holding the object.

The best way to avoid is deadlock by never using nested locks, used locks only when it required and not using waiting indefinitely are some of the common ways to avoid the deadlock situation.

14. What are the benefits of multi-threaded programming?

Answer: In a multi-threaded programming environment, multiple threads are executed concurrently which improves the performance because CPU is not idle even for a second and no thread is waiting for its execution. Multiple threads share the heap memory, so it’s good to create multiple threads to execute some set of tasks rather than going for the multiple processes. The most common example of faster performance is Servlet as it is uses multi-threading programming but CGI doesn’t.

15. What is the Exception in Java and how these are handled?

Answer: An exception is an error event which happens during the execution of a program and causes a disturbance in the normal flow. An exception arises due to a different kind of circumstances which are a hardware failure, wrong input by the user, network failure etc.

Whenever an error causes a disturbance in the normal flow of the execution of the Java statement, an exception object is created and JRE tries to find the exception handler so it can handle the exception. If no exception handler is found related to the exception then the exception is thrown by the application to JRE which terminates the program.

There are four keywords that are used for handling the exception in Java.

  • throw: when we want to create an exception object explicitly then we throw it and halt the normal processing of the program. This keyword is used to throw an exception in the runtime action to handle it.
  • throws: the throws keyword is used for declaring any exception. It gives the programmer some information about the exception which may occur during the program execution so they can use a proper exception handler to maintain the normal flow of the program.
  • try-catch: try-catch keyword in Java is used for handling the exception in the program. try block is written at the starting of the code and catch block is written in the end to handle the exception.
  • finally: finally block is executed always whether an exception occurs or not. This is an optional one and only used with a try-catch block. When a process is a halt during exception some resources which are open will not get closed, so for this, we use finally block.

16. What is an immutable object? How can we write immutable object?

Answer: Immutable classes are part of the Java classes whose object is once created, it cannot be modified. If any modification is done in the immutable object then it creates a new object. In Java, the string is immutable. Usually Immutable are final in Java but by the use of the overriding method, immutability can be compromised.

Also Read: Top 100 Java Interview Questions

17. What is serialization and deserialization in Java?

Answer: The process of converting Java object into a stream is called serialization. Once the object is converted into a stream then it is saved as a file or can be sent over the network or used in socket connections. The serializable interface should be implemented by the object and we can use java.io.ObjectOutputStream for writing the object to file or to any OutputStream.

The process converting back a stream of data into the object is known as the deserialization.

18. How does HashMap work in Java?

Answer: HashMap is used to store key-value pair in Map. In this entry, statically nested class implementation is done. HashMap works on the hashing algorithm where hashcode() and equals() methods input and get methods. When put method is called by passing key-value pair then HashMap uses hashcode with hashing to find out the index to store the key-value pair. The LinkedList is used to store the entry value and if there is already some value stored then equals() method used to check it with the existing entry. If it matches with existing entry then it overwrites it and creates a new entry.

When the get method is called by passing key then again it uses hashcode() to find the index in the array and use equals() method to find the correct entry and return the value.

19. What is object cloning?

Answer: Object cloning is the process of creating an exact copy of the original object. A class supports cloning when its implement Java.lang package. Cloneable interface and override clone() method from object class is used. Clone() method syntax is as following:

Protected Object clone() throws CloneNotSupportedException

If Cloneable interface is not supported by the Object class then it throws exception ‘CloneNotSupportedException’.

20. What is Big-O notation?

Answer: The Big-O notation tells the performance of an algorithm in terms of the number of elements in a data structure.  It is a language which generally tells about how long it takes to run the algorithm. In this, we compare the efficiency of different approaches to a problem.

Generally, it’s like math; not the boring one but awesome one where you can focus on the things which are really happening.

Final Words

Whether you are a fresher or have gained some experience, preparing with these Core Java interview questions will really help you in your preparation. In this article, we have tried to cover all the latest Core Java interview questions which will definitely help you in your job interview. you may also come across a number of Java programming interview questions in the interview, so get prepared with those too. With this set of top Core Java interview questions, you will be ready to crack the Java interview.

Certifications also play an important role to make you stand out from the crowd in the interview. So, it is recommended to get a recognition of your knowledge with any Java certification. Whizlabs helps you prepare for the Oracle Java certifications with online courses and practice tests that you can check.

Become an Oracle Certified Java Professional and get ready for the interview with these top Core Java Interview Questions, and all done. Join us now to get your dream job.

About Amit Verma

Amit is an impassioned technology writer. He always inspires technologists with his innovative thinking and practical approach. A go-to personality for every Technical problem, no doubt, the chief problem-solver!
Spread the love

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here