Java Interview Questions
Top 30 Java Interview Questions in 2021

Top 30 Java Interview Questions in 2021

This post was most recently updated on May 12th, 2021

In this article, we’ll be discussing the top Java interview questions frequently employed by several companies. The questions start with simple level questions or the basics, and then we’ll move on to the advanced java interview questions. So, let’s begin the interview questions on the topic JAVA.

What’s Java in your point of view?

Java may be a simple language that is straightforward to find, and it follows an object-oriented paradigm. Java is portable, which supports the read-once-write-anywhere approach. It is platform-independent, which is secured and a robust programming language.

What are the different types of access specifiers in Java?

In java, access specifiers are the keywords that are wont to define the access scope of the tactic, Class, or variable. It’s four access specifiers which are

PUBLIC – the classes or methods or variables which are defined as public are often accessed by any class or method.

PROTECTED – it is often accessed by the category of an  

Equivalent package or by the sub-class of this Class or within the same Class.

DEFAULT – it’s accessible within the package only. By default, all the classes, methods, and variables are of default scope.

PRIVATE – this private Class, methods, or variables defined as private is often accessed within the category only. 

What’s the difference between an object-oriented programming language and an object-based programming language?

There are subsequent basic differences between object-oriented programming language and object-based language.

  • Object-oriented languages always follow all the concepts of OOPs whereas, the other object-based language doesn’t follow all the concepts of OOPs.
  • OOPs like inheritance and polymorphism. Well, the  Object-oriented languages do not have any inbuilt objects whereas Object-based languages always have inbuilt objects, for instance, Javascript has window objects.
  • The examples of OOPs language are java, C#, Smalltalk, and etc, whereas the other samples of object-based languages are javascript, VBScript, etc.

What’s an inline function?

An Inline function may be a technique employed by the compilers and instructs to insert the complete body of the function wherever that function is employed within the program ASCII text file.

What’s an interface?

An interface may be a collection of abstract methods. If the category implements an inheritance, then it inherits all the abstract methods of an interface.

What’s exception handling?

The exception is an occasion that happens during the execution of the program. Exceptions are often of any type- runtime exception, error exceptions. Those kinds of exceptions are handled properly through the exception handling mechanisms like try, catch and throw keywords.

What’s a JVM and JIT compiler?

JVM (Java virtual machine) may be a virtual machine that permits the pc to run the java program. JVM acts sort of a run time engine that calls the most methods present in java code. 

JIT (Just-in-time) is employed to enhance the performance. JIT compiles parts of the bytecode that have similar functionality at an equivalent time and hence reduces the quantity of your time needed for the compilations.

What’s a copy constructor?

This is a special constructor for creating a replacement object as a replica of an existing object. There’ll always be only one copy constructor, which will be either defined by the user of the system.

What are constants? How can we create constants in java?

Constants are fixed values whose values can’t be changed during the execution of the program. We create constants in java using the final keyword.

Ex: final int number = 10;

     Final String str = “java-interview-questions”

  1. Explain Java coding standards for interfaces?
  2. The interface should start with uppercase letters.
  3. Interfaces names should be adjectives

                       Ex: runnable, serialization, marker, cloneable.

What does null mean in java?

When a reference variable doesn’t point to any kind of value, then it is as assigned null.

Ex: Employee employee;

If we look at the above example, the employee object is not instantiated so it  is pointed nowhere.

What are packages in java?

Package may be a mechanism to group related classes, interfaces, and enums into one module. The package is often declared by using the subsequent statement: 

Syntax: package <package-name>

Package names should be declared in small letters. The package statement defines the namespace. The main use of the package is

  • To resolve naming conflicts
  • We can define classes and interfaces that aren’t accessible outside the category class for visibility control.

Can we try a block without a catch block?

Each of the try blocks will require at least one catch block or maybe one final block. A try block without catch or finally will end in a compile error. We will skip either catch or finally block but not both.

Is writing a code after a throw statement possible?

After the throw statement, JVM stops execution, and subsequent statements aren’t executed. If we attempt to write any statement after the throw, we do get a compile-time error saying unreachable code.

What are user-defined exceptions?

To create the customized error messages, we always use user-defined exceptions. We will create user-defined exceptions as checked for unchecked exceptions. We will create user-defined exceptions that reach the exception class or subclasses of checked exceptions so, in order to understand, exceptions become checked.

Can we try nested statements in java?

Yes, try statements are often nested. We will declare try statements inside the block of another try statement.

What’s a thread in java? What is the difference between process and thread?

Thread is a separate path of execution in a program. They’re lightweight, share an equivalent address space, creating a thread is straightforward in comparison to process. 

The process is like a program in execution, and whereas thread is a separate path of execution in a program where one or more threads is named a process.

List Java API that supports threads.

java.lang.Thread – this is often one among the thanks to creating a thread. By extending the Thread class and overriding run() we will create a thread in java.

java.lang.Runnable – runnable is an interface in java. With the implementation of a runnable interface and overriding run(), we can create a thread in java.

java.lang.Object – Object class is the super class for all the classes in java. In object class we’ve thread methods wait(), notify(), notifyAll() that supports threads.

java.util.concurrent: this package has classes and therefore the interfaces that support concurrent programming.

In what ways will we synchronize in java?

There are two ways to do synchronization in java- synchronization methods and synchronization blocks. To do the synchronization, we will use a synchronized keyword.

When can we use synchronized methods in java?

If multiple methods try to access a way where a method can manipulate the state of the object, in such a scenario we will declare a way as synchronized.

What are all the methods used to prevent thread execution?

The three methods in thread class always prevent the execution of the thread.

  • yield()
  • join()
  • sleep()
  • What are nested classes and why do we use nested classes in java?

A class declared within another class is defined as a nested class. There are two types – Static nested class and Non-Static nested class.

We use them to group classes, extend encapsulation, code readable and maintainable, and hide the implementation.

How can we perform encapsulation in java?

Make instance variables private and then define the getter and setter methods to access instance variables.

Can we have a method name that is the same as a class name in java?

True, we can have the method name the same as the class name, where it won’t throw any kind of compilation error but it shows a warning message that the method name is the same as the class name.

Can we override constructors in java?

Only methods are often overridden in java. Constructors can’t be inherited in java. So there’s no point in overriding constructors in java.

What is the difference between object and reference?

Reference and objects are entirely different. Objects are instances of sophistication that reside in heap memory. Objects don’t have any names, so to access objects, we use references. There’s no alternative way to access objects except through the references.

Objects can’t be assigned to other objects, and objects can’t be passed as arguments to a method. A reference can often be assigned to another reference passed to a method.

Explain the scope or lifetime of class variables or static variables?

Static variables don’t belong to instances of the category class where it can access static fields even before instantiating the class and the variable will remain in memory till the lifetime of the application.            

What do you mean by array and ArrayList?

Arrays are useful to store primitives or objects of the same type or variables that are subclasses of the same type.

ArrayList is an ordered collection that grows dynamically.

  • List the implementations of list interface. 

They are arraylist, vector and linkedlist.

  • What are the implementations of Set interface?

The implementations of the set interface are HashSet, linkedhashset, and treeset.

This brings us to the end of the top Java interview questions that you must know! We hope that you are now better acquainted with the top java interview questions and will ace your interview process. Good luck and happy learning!



Share via
Copy link
Powered by Social Snap