Top Java Interview Questions & Answers

5.00 avg. rating (99% score) - 5 votes
Java Interview Questions

 

Java is everywhere! Over 10 million developers across the world use Java to develop applications for 15 billion Java-supporting devices. Developers have been using it to create applications having a myriad of applications, starting from Big Data to DTH boxes. Usability and scalability makes Java Certification a high in-demand certification in programming domain.

We have compiled a list of some of the most popular Java interview questions and answers that will help you prepare for your next interview.

 

1) What are the main features of Java?

 

Ans. Java features are simple and easy to understand. Some of them are given below:

  • Object-oriented
  • Secured
  • Interpreted
  • Robust
  • High performance
  • Multithreaded

 

2) What do you mean by JVM?

 

Ans. JVM stands for Java virtual machine. It enables Java bytecode to be executed as operating system on any system irrespective of the operating system.

 

3) Is null, delete or exit keyword in Java?

 

Ans. No.

 

4) What is the difference between object based programming language and object oriented programming language?

 

Ans. Object based programming language doesn’t support the features of OOPs except inheritance. Object oriented languages are java, C and C++.

5) What are primitive data types?

 

Ans. There are 8 data types (Boolean, byte, char, long, float, double, short and int) that serve as the building blocks of data manipulation in Java.

 

6) When Super () keyword is used?

 

Ans. This method is used to call the constructor of parent class.

 

7) What is Data Encapsulation?

 

Ans. One of the four fundamental object oriented programming concepts, Data Encapsulation is a process of wrapping code and data into a single unit.

 

8) What is the name of the package used for matching with regular expressions?

 

Ans. Java.util.regex package

 

9) What are the four principles upon which object oriented programming rest?

 

Ans. Easily known as A-PIE: Abstraction, Polymorphism, Inheritance and Encapsulation

 

10) What do you mean by method overriding?

 

Ans. If child class has the same method as declared in the parent class, this is called method overriding.

 

11) When throws keyword can be used?

 

Ans. Providing an information to the programmer that there may happen an exception so that the normal flow can be maintained, Throws keyword is used to declare an exception.

 

12) What is abstract method?

 

Ans. It is basically a function that is declared but no implementation.  It lies in the heart of inheritance when you define certain functions which are dealt within the child classes differently.

 

13) What are the different types of loops?

 

Ans. Java follows three loops:

  • Do while loop
  • For loop
  • While loop

 

14) What is constructor?

 

Ans. A constructor is a block of code that enables you to create an object of class. It is called automatically when a new instance of an object is created.

 

15) Give an example of class variable declaration?

 

Ans. Class variables are declared with static modifiers.

Public class product

{

Public static int Barcode;

}

 

16) What environment variables are required to run java programs?

 

Ans. PATH and CLASSPATH are needed to run a simple java application.

 

17) What does JAXB stand for?

 

Ans. JAXB means Java API for XML binding. It is a fast and suitable way to bind Java representation to incorporate XML data in Java applications.

 

18) What are the new features for Java 8?

 

Ans. Java 8 is full of really interesting features at both the language level and the JVM level. Some of the features that are an absolute must to know about are:-

  • Parallel operations
  • Concurrent accumulators
  • Lambda Expressions
  • Generic type changes and improvements
  • Functional interfaces

 

 

19) What type of variables can a class consist?

 

Ans. A class comprises of instance variable, class variable and local variable.

 

20) What is synchronization?

 

Ans. Synchronization is applied to static/non-static methods. The synchronized keyword also prevents thread interference and memory consistency errors.

 

21) What is the default value of local variable?

 

Ans. The class variables have default values. Local variables don’t have any default value.

 

22) What is static keyword?

 

Ans. It is used for memory management to refer the common property of all objects. If we apply java static keyword with any method is called as static method. Moreover, static can be applied to class nested with another class, initialization block, method and variable.

 

23) Is JVM platform independent?

 

Ans. No, JVM is not platform independent as it is not written in Java.

 

24) Can a program be executed without main () method?

 

Ans. One can execute a program without main method by using static block.

 

25) What are the various access modifiers for Java sessions?

 

Ans. Java offers an array of access modifiers to aid you set the amount of access you want for methods and fields in your classes. The types of access modifiers are:

  • Private
  • Default
  • Public
  • Protected

 

26) How do you avoid deadlock in Java?

 

Ans. We can achieve this by breaking circular wait condition. For this, we need to arrange the code such that it imposes the ordering on acquisition and release of locks.

 

27) How can you implement to use an Object as key in HashMap?

 

Ans. To use any object as a Key in HashMap or Hashtable, it must implement equals and hashcode method in Java.

 

28) How do you convert bytes to character in Java?

 

Ans. This is one of the most commonly asked Java interview questions, here is how you can frame the answer.

We can convert bytes to character or text data using character encoding. Incorrect choice of character encoding may change the meaning of message as it interprets it differently.

 

29) Can you override static method in Java?

 

Ans. No, static method in Java cannot be override because they are resolved in compile time and not runtime.

 

30) How do you define Destructors in Java?

 

Ans. Generally, a destructor is a method that removes an object from computer’s memory. Java lacks a destructor element, and instead they make use of garbage collector for resource deallocation.

 

31) What is JSON?

 

Ans. JSON is an acronym for JavaScript Object Notation. Its format is text only and uses JavaScript syntax.

 

32) What is an Anonymous Class?

 

Ans. As the name suggests, an Anonymous Class is defined without a name in a single line of code using a new keyword.

 

33) What is the difference between Stack and Queue?

 

Ans. Stack is based on the Last in First out (LIFO) principle
Queue is based on FIFO (First In, First Out) principle.

 

34) What is a Package?

 

Ans. A package is a namespace of related classes and interfaces. They are similar to different folders on your computer.

 

35) How to access package from another package?

 

Ans. We can access the package from outside the package via three ways –

import package.*;

import package.classname;

fully qualified name

 

36) What is a pointer? Does Java support pointer?

 

Ans. A pointer helps to directly access the memory location using the address. Java does not have the concept of a pointer because improper handling of pointers results in memory leaks and other related problems. This makes Java a powerful language than C or C++,

 

37) What is an immutable object?

 

Ans. Immutable object cannot be modified once created. Software developers rely on immutable objects for creating simple and reliable codes.

 

38) What are wrapper classes?

 

Ans. Wrapper classes are used to convert or wrap Java primitives into the reference objects.

 

39) What are local variables?

 

Ans. A variable declared within the body of a method is local variable. These variables need to be initialized before use.

 

40) What are instance variables?

 

Ans. A variable declared inside a class, but outside a method is called an instance variable. These variables don’t need to be initialized before use, as they automatically initialize to their default values.

These were the top 40 Java interview questions and answers. Java is a vast topic and needs a thorough understanding and study of all the aspects. It is important to go through every Java interview question diligently and ensure that you leave no stone unturned to succeed in your interview. Just stay positive and go well-prepared.

[course category=’Software development’ total-course=”1″ ]

About the Author

Twinkle kapoor

Though from a techie background, her interest in writing on plethora of topics has made her today an experienced writer. She has written articles, blogs and web page content for oodles of websites.