15 Most Asked Java Programming Interview Questions You Should Know

Bikash Daga (Jain)
Dare To Be Better
Published in
6 min readJun 13, 2022

--

Overview

Java is a high-level, object-oriented, secure, and robust platform-independent language with high performance and it is a multithreaded and portable programming language. It is extensively used in industries.

Some Important Interview Questions:

Q1. What are Wrapper Classes in Java?

Java Primitives are converted into the reference types (objects) with the help of wrapper classes. It has a dedicated class for each primitive data type. Data structures in the collection framework, store only objects and not primitive types, and to convert them to reference types we need wrapper classes.

They “wrap” the primitive data type into an object of that particular class that’s why it is called wrapper classes. It is needed as in multithreading an object is needed to support synchronization.

Q2. What is Golden Ratio in the Fibonacci Series in Java?

Ans: Golden Ratio is approximately around 1.618034 and this is the ratio of two successive Fibonacci numbers, the ratio is close to the Golden Number of the Fibonacci series.

5/3 = 1.66
13/8 = 1.625
144/89 = 1.617

It also works when we use any two random whole numbers to start the sequence. For example, if we start the Fibonacci series with 7 and 19 the series will be

7, 19, 26, 45, 71, 116, 187, 303, 490

Golden Ratio is

116/ 71 = 1.63
187/116 = 1.612
490/303 = 1.617

Q3. What are Identifiers in Java?

In Java, Identifiers are the names of variables, classes, packages, methods, and interfaces.

Identifiers are case-sensitive and must not contain white spaces. Identifiers should be unique and can only have alphanumeric characters and underscore in them, the first character needs to be either an alphabet or an underscore. Reserved keywords can not be used as identifiers.

An example of the identifier is shown below:

public class MainClass {
public static void main(String[] arguments) {
int variable1 = 50;
double variable2 = 10.0;
System.out.println(" Hello World! ");
}
}

Q4. What is the Static Keyword in Java and What is a Static Variable in Java?

In Java static is a keyword. It is used to manage the memory to make the efficient use of memory. Static means class level in java and it is a non-access modifier. We can apply the Static keyword to methods, variables, and nested classes only and define static blocks also.

Any variable declared with the static keyword is known as the static variable. It is a class-level variable. When we refer to the common property of all the objects, there we use a static variable.

For example here the name strings are a static variable as the name of the institute is common to all the students of that institute.

public class Demo
{
static String name = " IIT Delhi ";// This is a static variable
.....
.....
.....
}

Q5. What is the Life Cycle of a Thread in Java?

A thread has various stages in its lifecycle like new, runnable, waiting, time waiting, and terminated (dead).

New- A new thread gets started in the new state. It remains in the same state till the time program starts. It is also known as the born thread.

Runnable- when a newborn thread is made, it is runnable. In this state, a thread is considered to be executing a task.

Timed Waiting- A runnable thread can get into it for some specified interval of time and after that specified time period it goes back to a runnable state.

Waiting- When another thread is performing the task a thread has to wait. This is the waiting stage of a thread in java. When the other thread completes the task the waiting list performs its task.

Terminated(Dead)- When a thread completes its task or terminates otherwise it enters the dead stage.

Q6. How to Convert String to int in Java?

A string can be converted into an integer using the “Integer.parseInt()” method. The parseInt() is the static method of integer class in java.

Syntax of parseInt() method is

public static int parseInt(String s)

A simple example is to convert a string to an int in java.

int j=Integer.parseInt("50");

Q7. What is the Pool String in Java?

A string pool is a storage area in the Java heap where the string literals are stored. It is also referred to as String Constant pool or String Intern pool. It is similar to object allocation. It is empty by default and when we a string is created, it occupies some memory in the java heap privately maintained by the Java script class. In JVM the string class keeps a pool of strings to decrease the number of string objects.

Q8. What are the Differences Between String Buffer and String Builder?

Differences Between String Buffer and String Builder

Example of StringBuffer

public class BufferTest{  
public static void main(String[] args){
StringBuffer buffer=new StringBuffer(" hola ");
buffer.append(" java language ");
System.out.println(buffer);
}
}

Read more about StringBuffer on Scaler Topics.

Example of StringBuilder

public class BuilderTest{  
public static void main(String[] args){
StringBuilder builder=new StringBuilder(" hola ");
builder.append(" java language ");
System.out.println(builder);
}
}

Understand StringBuilder by Scaler Topics.

Q9. Describe Break Statement in Java

In loops when required to terminate the loop at a given condition, we use a break statement in Java. The break statement immediately terminates the loop and control of the program move to the next statement following the loop. Read in-depth here.

Q10. Explain JDK in Java

JDK stands for Java development kit and To compile, document, and package Java programs JDK is used. It includes JRE+ development tools.

JRE stands for Java Runtime Environment and it refers to a runtime environment in which java bytecode can be executed. It is an implementation of JVM.

JVM stands for Java Virtual machine and It is an abstract machine. To provides a run time environment for java bytecode is provided by JVM.

Q11. Why Java is not a Pure Object-Oriented Language?

Java uses primitive data types like char, boolean, int, byte, double, float, long, and short which are not objects. Hence Java is not 100% object-oriented.

Q12. Why is Java a Platform-Independent Language?

The developers designed it in such a way that it does not depend on any hardware or software as when the compiler compiles the code, it converts it to a platform-independent byte code, which can be run on multiple systems. The standard condition to run a code in any machine is to have a runtime environment (JRE) installed in it.

Q13. Why Pointers are not Used in Java?

Pointers are not so simple and are a bit unsafe to be used by beginner programmers. Java is made to be simple and pointers can complicate it. It can also cause potential errors. There are security issues if pointers are used as the user can directly access memory with help of pointers. Also, the use of pointers may hinder the performance of the java language.

Q14. What is a ClassLoader?

The very first thing that is loaded in an executable file is ClassLoader. A class Loader in java is a subsystem of JVM (java virtual machine), which focuses particularly on loading class files at the time of execution.

Q15. Why are Strings Immutable in Java?

References are shared in different functions and the String pool can be changed using the same shared references from anywhere.

Strings are shared in different areas like database connections, networking connections, and file systems, sharing strings in multiple areas makes it easy to alter the strings so making them immutable ensures their security.

Happy Learning!

--

--

Bikash Daga (Jain)
Dare To Be Better

Learn, Implement and Grow this is the mantra I follow to level up myself. I am a passionate technical writer.