Java Interview Questions and Answers for freshers 2024

Core Java Interview Questions and Answers for fresher and final year college students. If you are a final-year student or going to attend a Java interview.

Below are some important Core Java Interview questions and answers that will help you to answer all the basic questions of java.

Core Java Interview Questions and Answers for freshers 2024

Set up a Java environment in your machine to write a Java program by yourself and think about all the things that you are seeing in the source code file. below is the list of some interview questions and answers from your first Java Program and Core Java.

  • What is Java why do we call it Core Java?
  • How to set an environment for developing a java program?
  • What is JDK and how to install JDK?
  • How to set the path for javac and java.
  • How to write the first program in Java.
  • What is the main method?
  • How to generate .class file in Java.

What is Java and why do we call it Core Java?

Java is an Object-Oriented programming language. Java is the most demanding programming language for the last many years and millions of Java applications run on devices. Java has a rich history and a lot more things to know about Java. Click on the Java Introduction tutorial to know more details about the core java.

Why do we call Java Core Java?

There are many frameworks that are using Java as a core programming language. Frameworks make a developer’s life easy and efficient way but still, we need to perform some logical and business operations that will be done in java Even these frameworks also depend on Java. So we called it core Java.

What are the basic requirements to write a java program?

To write a Java program we need JDK in the machine and an IDE like a notepad, eclipse, or Netbean to write the core. Check How to setup the Java environment and Write the first Java program

  1. JDK (Java Development Kit).
  2. An IDE or editor to write the code.

What is a Java development tool (JDK)?

JDK is a Kit of programs that helps to compile and run our Java code in the computer system. It comes according to the operating system. We can download the latest version of JDK from “Download JDK “.

How to install JDK in windows?

Download JDK from the official website of Oracle and install it on your machine. to check the installation steps please follow the below video to get a clear picture of JDK installation and verification.

Where I can write Java code?

An editor like notepad, notepad++, etc. and you can use any Ide. There are lots of IDE tools available in the market like Eclipse, Netbeans, STS tools, etc.

Why do we need to set a path for Javac Globally?

To compile our java code we need access to a file “Javac” Which is a part of JDK and is available in the Installation directory of JDK. by default it’s “C:\Program Files\Java\jdk1.8.0_211\bin”. So to compile the java code we have two ways.

  1. Navigate to the installation directory of JDK and access the Javac.
  2. Set the path of Javac globally in your machine and access it from anywhere.

So, for good practice, we always follow the setup path globally and run a java program from any directory. See how to run a Java program.

How to set a path globally in Java

Find the Java installation directory “C:\Program Files\Java\jdk1.8.0_211\bin” Open the system variable in your machine and add the path as a new global variable. check the below video tutorial to set the path in Java.

How to write the first program in Java

Write your first Java program and observe the source code and think about why all these keywords are used in this program also can we replace the location of these words? let’s see some core Java interview questions and answers around your first Java program.

public class Main {
  
  public static void main(String[] args) {
    System.out.println("Hello Java");
  }

}

Java Programming interview questions and answers

While you writing a java program there are many important terms that are important to understand and you might get many questions from your first java program, Below I am highlighting some of them.

Can we keep a java class name as “main” or the same as the main method name?

No, Java is case sensitive so we can keep the class name as “Main” but it will not be as “main”.

Should the main class name be the same as the file name?

Yes, the file name should be the same as the main class name. If a class is public then it’s necessary to save it with the same name otherwise there will be a compile-time error.

If the class is not public and you save the file with another name then it compiles successfully but it will give you a runtime error “Error: Could not find or load main class Main”

Can we change the positions of the public static void main?

Yes, there is some different way to write the main method. There are some valid and some invalid things that I have covered in the below video tutorial with real-time practical examples for details please watch the video.

Why the main method is static?

The main method is static because according to the static properties. “we can call a method directly by name without object creation” so when we compile and run the Java program the main method is directly called by JVM. because the main method is the entry point for the compiler for your entire Java program.

Can we write multiple public classes in a single file?

No. we can’t write because In a single file we can write only one public class in a file. If we write multiple public classes in a single file then there will a compile-time error.

What is System.out.println in java?

The system is a class. Out is a  static variable in this system class and print() is a method. The System class belongs to java.lang package. println() method is used to print any value as output.

class System {
  public static final PrintStream out;
  //...
}

The Prinstream class belongs to java.io package

class PrintStream{
public void println();
//...
}

What is String[] args in the main method of Java?

It is an array of type String and its name is args. name is optional we can change its name to anything. Example String[] xyz

What are static blocks and static initializers in Java?

Static blocks or static initializers are used to initialize static fields in java. we declare static blocks when we want to initialize static fields in our class. Static blocks get executed exactly once when the class is loaded. Static blocks are executed even before the constructors are executed.

How to call one constructor from the other constructor?

Within the same class if we want to call one constructor from another we use this() method. Based on the
number of parameters we pass appropriate this() method is called.
Restrictions for using this method :
1) this must be the first statement in the constructor
2) we cannot use two this() methods in the constructor

Core Java Interview Questions and Answers