Java Interview Questions

Welcome to Java Interview Questions area. In this section we will discuss about different types of questions that can be used in Java interviews , in order for the employer to test your skills in core Java in general. These questions are designed to provide a better understanding of general questions regarding Core Java programming and its functionality, collections in Java, garbage collectors, exception handling etc.

Ready to dive in? Then let's get started!

Java interview questions and answers

What is Java?

Java is a programming language and computing platform first released by Sun Microsystems in 1995 . It is an Object-Oriented, high-level secure programming language that is based on the concept of Object-Oriented Programming Systems (OOPS). Java is intended to let application developers write once, run anywhere (WORA), meaning that compiled Java code can run on all platforms that support Java without the need for recompilation.

When and by Whom was Java Developed?

Java was developed by James Gosling and first released by Sun Microsystem in 1995.

What Do JDK, JRE, and JVM Stand for?

  1. JVM stands for Java Virtual Machine, provides a runtime environment for Java Byte Codes to be executed.

  2. JRE stands for Java Runtime Environment includes sets of files required by JVM during runtime.

  3. JDK stands for Java Development Kit allows developers to create Java programs that can be executed and run by the JVM and JRE.

What Is a Package in Java?

Package in Java is a mechanism to encapsulate a group of classes , sub packages and interfaces. It can be categorized in two form, built-in package and user-defined package . There are many built-in packages such as lang, awt, javax, swing, net, io, util, sql etc.

What is WORA?

WORA or Write Once Run Anywhere was a slogan created by Sun Microsystems to illustrate the cross-platform benefits of the Java language. Java program (.java files) converts to bytecode (.class files) by JVM and this class file can run on any other platform with required JVM running on it. The term, sometimes also expressed as write once, run everywhere (WORE), was originally coined by Sun Microsystems in reference to Java.

What is bytecode?

Bytecodes are the machine language of the JVM (Java virtual machine) understand, and which makes Java platform independent. When ever you compile a Java code it will be converted to highly optimised byte code(.class file) and when you run it will be converted to corresponding machine code. To be more precise a Java bytecode is the machine code in the form of a .class file.

Define JIT compiler in Java?

The Just-In-Time (JIT) compiler is a component of the Java Runtime Environment that improves the performance of Java applications at run time. JIT is much more sophisticated than the javac compiler , and it runs complex optimizations to generate high-quality machine code. It is enabled by default.

What are primitive data types?

A primitive is the smallest unit of processing available to a programmer and they are like individual bricks which programmers use to build functions and methods. Primitive data types in Java are the major constituents of data manipulation. These are the most basic data types that are available in Java. Primitive data types includes byte, short, int, long, float, double, boolean and char.

Is there any default value for local variables?

The local variables do not have any default values in Java. This means that they can be declared and assigned a value before the variables are used for the first time , otherwise, the compiler throws an error.

Are Arrays of Primitive Data Types in Java?

No. In Java, Arrays are objects . We can create arrays by using new operator and we know that every object is created using new operator. Hence we can say that array is also an object .

Can we overload main() method in Java?

overload main() method in Java

Yes, you can overload main method in Java by using method overloading . But the program doesn't execute the overloaded main method when you run your program, you have to call the overloaded main method from the actual main() method . This means that the main method acts as an entry point for the java interpreter to start the execute of the application. where as a loaded main need to be called from main.

What Is the void Type and When Do We Use It?

Void is not a type , it is not also a return type. If we want the method to return no value , we can use the void keyword. Therefore, the only value we can assign to a Void variable is null .

What is enumeration?

It is an interface you can use to access original data structure from which the enumeration is obtained. Each enumeration constant is public, static and final by default.

Why there are no global variables in Java?

Global variables are usually a design flaw . Java was designed with object-oriented principles in mind and as such, every variable in Java is either local or a member of a class. Java does not support globally accessible variables due to following reasons:

  1. The global variables breaks the referential transparency.
  2. Global variables creates collisions in namespace.

If you really want to do that, make it a public static variable . However, you'd be advised to try not to - it makes for less elegant, harder to maintain, harder to test code.

What is meant by looping?

It is the way to execute a set of instructions any number of times . They are of three types- For Loops, While Loops and Do While Loops.

Define Object in Java?

An entity that has state and behaviour is known as an Object . Every object in Java has both state and behaviour .

  1. State: represents the data (value) of an object.
  2. Behavior: represents the behaviour (functionality) of an object.

What will be an initial value for an object reference which is defined as an instance variable?

Any variable that you define in Java which you do not assign a value, whether it is an instance variable or a local variable, or a static variable, will have a default value of null . However in order to do anything useful with these references, you must set them to a valid object, else you will get NullPointerExceptions .

Can we override static method in Java?

No, we can't override the static method in Java because they are not dispatched on the object instance at runtime. The compiler decides which method gets called. In practice it means that the compiler will decide which method to execute at the compile time, and not at the runtime , as it does with overridden instance methods.

Core Java Interview Questions and answers

Which class is the superclass for all the classes in Java?

java.lang.Object class is a superclass for all the classes in Java. All non-primitive types (including arrays) inherit either directly or indirectly from this class.

What is the super keyword in Java?

The super keyword refers to superclass (parent) objects. It's mainly used when we want to access a variable, method, or constructor in the base class , from the derived class.

How to handle

You can effectively handle it by define a connection timeout and later handle it by using a try-catch block. From the client side you can use the following method and set the timeout parameter:

From the server side you can use the following method and set the timeout parameter:

Also, if you are a programmer, you can surround the socket connection part of your code in a try/catch/finally and handle the error in the catch. You might try connecting a second time, or try connecting to another possible socket, or simply exit the program cleanly.

What are the difference between an Inner Class and a Sub-Class?

  1. An Inner class is a class that defined inside a class whereas a Sub-class is a class which is inherited to another class.

  2. Inner classes are in the same file, whereas subclasses can be in another file, maybe in another package.

  3. Inner classes are used when you have a class that you need to use only in particular class whereas Sub classes uses when you have inheritance logic.

  4. Inner classes have the methods they want, whereas subclasses have the methods of their parent. Subclasses can define additional methods, but they'll always have those of their parent class.

What is the default size of the load factor in the hashing-based collection?

Default size = 0.75

For example, an instance of HashMap has two parameters that affect its performance: initial capacity and load factor.

Initial capacity of the HashMap takes is 16 and load factor is 0.75f.

This represents that after storing the 12th key – value pair into the HashMap , its capacity becomes 32.

Possible ways to avoid java.lang.OutOfMemoryError?

  1. Use local variables wherever possible.
  2. Release those objects which you think shall not be needed further.
  3. Make sure you select the correct object (ex: Selection between String, StringBuffer and StringBuilder).
  4. Avoid creation of objects in your loop each time.
  5. Use a good code system for your program(ex: Using static variables Vs. non static variables).
  6. Try to use caches.
  7. Try to move with Multy Threading.
Next: Java Interview Questions- (Part 2) (C) 2021    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About