Top Java Interview Questions & Answers

5.00 avg. rating (99% score) - 6 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.

 

Q1. 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

 

Q2. 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.

 

Q3. Is null, delete or exit keyword in Java?

 

Ans. No.

 

Q4. 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.

 

Q5. 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.

 

Q6. When Super () keyword is used?

 

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

 

Q7. 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++.

 

Q8. What is the name of the package used for matching with regular expressions?

 

Ans. Java.util.regex package

 

Q9. What are the four principles upon which object oriented programming rest?

 

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

 

 

Q10. 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.

 

Q11. 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.

 

Q12. 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.

 

Q13. What are the different types of loops?

 

Ans. Java follows three loops:

  • Do while loop
  • For loop
  • While loop

 

Q14. 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.

 

Q15. Give an example of class variable declaration?

 

Ans. Class variables are declared with static modifiers.

Public class product

{

Public static int Barcode;

}

 

Q16. What environment variables are required to run java programs?

 

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

 

Q17. 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.

 

Q18. 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

 

Q19. What type of variables can a class consist?

 

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

 

Q20. What is synchronization?

 

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

 

Q21. What is the default value of local variable?

 

Ans. The class variables have default values. Local variables donโ€™t have any default value.

 

Q22. 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.

 

Q23. Is JVM platform independent?

 

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

 

Q24. Can a program be executed without main () method?

 

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

 

Q25. 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

 

Q26. 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.

 

Q27. 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.

 

Q28. 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.

 

Q29. 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.

 

Q30. 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.

 

Q31. What is JSON?

 

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

 

Also ReadTop Core Java Interview Questions and Answers

 

Q32. 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.

 

Q33. 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.

 

Q34. What is a Package?

 

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

 

Q35. 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

 

Q36. 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++,

 

Q37. 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.

 

 

Q38. What are wrapper classes?

 

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

 

Q39. 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.

 

Q40. 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.

 

Q41. What is the latest version of Java in 2019?

 

Ans. Java 12, released in March 2019, is the latest version of java available today. One of the important features of the version is Shenandoah garbage collector. However, Javaโ€™s new version is about to launch which will be Java Development Kit (JDK) 13 (Java 13).

 

Q42. What are the main features of Java?

 

Ans. Java has exclusive features that make the language very powerful. Some of the notable Java features are given below:

Java is an object-oriented language, and because of this, it can be extended easily.

Java is Platform independent having the ability to Write Once and Run Anywhere (WORA)

Secured programming language because it has no explicit pointer and runs inside a virtual machine sandbox

Interpreted

Robust in terms of memory management, automatic garbage collection,

Java is a high performing language when compared to other traditional interpreted programing language because it uses Just in Time compiler

Java is multi-threaded and it shares a common memory area to deal with multiple tasks in a single go.

 

Q43. Why Java is not purely an object-oriented language?

 

Ans. In order to be a purely object-oriented language, Java must satisfy all the aspects of object-oriented. But because of the primitive data types, Java is not a purely object-oriented language. Coders can work using primitive data types in Java โ€“ byte, short, int, char, Boolean, and many more.

For example, both types will return the same value:

int i = 10; – Primitive Type

integer i = 10; Object Type

Another factor in Java is that we can always directly represent the static data without instantiation. Further, in Java, one can communicate with objects without actually calling their methods.

 

Also Read>>5 Latest Web Development Tools that you must know!

 

Q44. Name frequently used Java Tools.

 

Ans. Web developers know that Java is one of the most used commercial grade languages. Thus, it’s essential for developers to use Java tools like:

JDK (Java Development Kit)

Eclipse IDE

NetBeans

JRat

Junit

Q45. What is a Class in Java?

 

Ans. When talking about Java, a class represents a defined common set of properties and methods to all objects of the same type. Generally, a class includes components like a modifier, class name, superclass, interface, and body. For real-time java applications, several types of classes are used.

Few Ways to Create a Class in Java:

New keywords

forName method

Clone () method

Q46. What do you mean by JVM JRE and JVM and what is the major difference between the three?

 

  JDK JRE JVM
Full-Form Java Development Kit Java Run-Time Environment Java Virtual Machine
Comprises JRE + Development tools (debugger + compiler + JavaDoc Set of Libraries used by JVM at runtime JVM
Task Itโ€™s a full-featured SDK software development kit It is basically an implementation of JVM and it is essential to run any Java code It is used for Code loading, Verifying, Execution, and offers run time environment
Existence Exists Physically Exists Physically Doesnโ€™t exists physically

 

Q47. Please introduce Java frameworks Spring.

 

Ans. Frameworks, a large body of pre-existing codes, is fast, efficient, and light-weighted. It is used to solve the coderโ€™s specific problems very quickly. Java being fast-paced programming languages has widely used frameworks like JSF, Struts, and play!.

Among this, Spring framework is one of the most powerful and light-weighted frameworks used for Enterprise Java (JEE). Spring is widely used for developing Java-based applications.

Features of Java Framework โ€“ Spring

Extensible XML configuration

IoC container containing assembler code handling configuration management

Spring MVC framework

Support various dynamic languages

Supports recognized system modules

Compatible with various versions of Java

Simple to use, testable,

Loose coupling because of concepts like AOP

Note: Various hiring companies give special preference to candidates taking Java Certification courses. Enroll yourself now to fetch a handsome job as a Java Developer.

 

Q48. Define the Spring annotations that you use in projects.

 

Ans. Widely used Spring Annotations:

@Controller

@RequestMapping

@Qualifier

@Scope

Q49. What is Polymorphism in Java?

 

Ans. Polymorphism, an ability to perform single tasks in multiple ways, in Java is divided into two parts โ€“ Compile-time and Runtime polymorphism.

Compile-time also called a static method is a process wherein the call during the overloading method is resolved at the same time of compilation.

Runtime polymorphism also known as dynamic dispatch has a provision to support the overriding of methods. This is done during the runtime.

 

Q50. What is abstract class in Java? Is it similar or different when compared to C++?

 

Ans. The abstract class generally contains one or more than one abstract methods and these abstract methods are declared but they do not contain implementation. In Java, in order to make a class abstract, a separate keyword is required โ€˜abstractโ€™. But in C++, if any class comprises of at least one pure vital function, then the class automatically becomes abstract.

 

Also ReadPython Vs Java โ€“ Which is better to learn

 

Q51. What is Java copy constructor?

 

Ans. Java has a special type of constructor called the Copy Constructor in Java that is used for delivering a copy of specified objects. This is mostly used when a coder wants to copy something that is heavy to instantiate. Also, it is recommended that in order to detach both objects, use the deep copy. Further, it also offers full control over object creation.

 

Q52. Define the advantage of Copy constructor over Object.clone().

 

Ans. Java experts pick copy constructor over the Object.clone() because copy constructor does not push to implement any specific interface but one can implement it as and when required. Similarly, it also allows modification in final fields.

 

Q53. Why Java doesn’t support multiple inheritances yet multiple interface implementation is possible?

 

Ans. Multiple inheritances refer to the process by which one inherits the properties and behavior of multiple classes into one single class like in C++. But Java being a simple, robust, and secure language, it omits multiple inheritances usually called the Diamond Problem.

In Java 8, it supports default methods and thatโ€™s how a class can go ahead with the implementation of two or more interfaces.

 

Q54. What is a Java servlet?

 

Ans. Java developers when need to outspread the capabilities of a server, use Servlets. Basically, servlets are used in order to extend the applications that are hosted by a web server and it is deployed to design a dynamic web page as well by using Java. These servlets run on JVM and resist attacks. Unlike CGI (Common Gateway Interface) the servlets are portable.

 

Q55. Define the steps of creating a servlet in Java.

 

Ans. Servlet follows a lifecycle having four stages:

Loading

Initializing

Request Handling

Servlet destroying

So in order to go ahead with the process, first and foremost things is to create a servlet. Below is the process:

Develop a structured directory

Create the servlet

Compile

Add mappings into the web.xml file

Deploy the project by initiating server

Access it

Q56. What is a JSP in Java and are they better than servlets?

 

Ans. Java Server Pages (JSP) is a server-side technology that is used to create applications, dynamic web content, and independent web pages.

Yes, JSP is a better technology then servlets as they are very easy to maintain. Also, it doesn’t require any sort of recompilation or redeployment. JSP, being an extension to servlets, majorly covers most of its features.

 

Also ReadJava Certification Questions and Answers

 

Q57. A new version of Eclipse IDE for Java has arrived. Define something about it.

 

Ans. Recently in 2019, the latest version of Eclipse was launched โ€“ Eclipse IDE 2019-06. Some of the updated features are:

New themes and styling

Includes Support from Java 12 and Java editor benefiting Java developers

New Plug-in, API tools are included

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.