Top 50 Java Interview Questions & Answers

14-Nov-2022

This post is created for Java interviews for both fresher’s and experienced applicants. The Java interview questions and answers furnished below will assist you in grasping the fundamental ideas of Java programming in preparation for interviews. For your ease of learning, all the key JAVA principles are included in this list of the top Java interview questions and answers. The questions cover fundamental terminology, Access Specifies, OOP ideas, Collections, Exceptions, Serialization, and other JAVA subjects with examples to help you have a thorough preparation for any JAVA interview. 

Top JAVA Interview Questions and Answers

1. What is JAVA?

Java is a high-level, platform-independent programming language. Java is made up of various objects. Sun Microsystems created it. Many programs, websites, and video games are created using the Java programming language.

2. What are the characteristics of JAVA?

Java  has the following features:

OOP Concepts

  • Object-oriented
  • Inheritance
  • Encapsulation
  • Polymorphism
  • Abstraction

Platform Independent: A single program runs unchanged across various platforms.

High Performance: Java's JIT (Just In Time) compiler makes it possible for high performance. After the bytecode has been translated into machine language by JIT, the JVM launches the execution.

Multi-threaded: A thread is a stream of execution. The main thread is a thread that the JVM creates. By extending the thread class or by implementing the Runnable interface, the user can create many threads.

3. How does JAVA facilitate high performance?

The Just In Time compiler is used by Java to enable great performance. The instructions are changed into bytecodes using it.

4. What are the IDEs of Java?

The IDE's of Java are Eclipse and NetBeans

5. What is Constructor?

The constructor may be thoroughly explained using the following point:

  • Whenever a new object is generated in a program, the appropriate function Object for the class is called.
  • A method with the same name as the class is the function Object
  • A default function Object will be built if the user doesn't explicitly build one.
  • There is a possibility of the constructor getting overloaded
  • If a function constructor with a parameter was established by the user, he or she should expressly create another function Object without a parameter.

6. Define Local and Instance variables?

Local Variable: The definition of local variables and their range is contained within the method itself.

Instance variables are defined both inside and outside of the method, and their scope is shared by the entire class.

7. Describe a class?

A class defines all Java codes. It has options and processes. Variables are properties that specify how a class is in a given state. The precise business logic must be performed in methods. It includes a series of declarations (or instructions) that fulfill the specific criterion.

For Instance:

public class Addition{ //Class name declaration

int a = 5; //Variable declaration

int b= 5;

public void add(){ //Method declaration

int c = a+b;

}

}

8. Define Object

An instance of a class may be referred to as an Object. Its state and behavior are both available. Every time the JVM encounters the "new()" keyword, an instance of that class will be created.

9. Can you list out the concepts in OOPs?

The OOPs concepts include:

  • Inheritance
  • Encapsulation
  • Polymorphism
  • Abstraction
  • Interface

10. Define Inheritance?

Inheritance refers to the ability of one class to extend to another class. so that the codes can be transferred between classes. The subclass is also known as derived classes while the superclass is the original one. 

Superclass:

public class Manipulation(){

}

Subclass:

public class Addition extends Manipulation(){

}

Inheritance can be applied to the public and only to protected members. Private members are not applicable for Inheritance. 

11. What is the task of encapsulation?

Encapsulation performs the task of

  • Protecting code from others
  • Code Maintainability

12. Describe polymorphism?

Polymorphism means multiple forms. It is a single object's caliber to refer to any of the classes whether a superclass or subclass as per the reference type. 

We can use the Manipulation reference type to invoke the "add()" function of the Addition class. Polymorphism is the name given to this talent. In contrast to overloading, polymorphism is appropriate for overriding.

We can use the Manipulation reference type to invoke the "add()" function of the Addition class. Polymorphism is the name given to this talent. In contrast to overloading, polymorphism is appropriate for overriding.

13. What does Method Overriding entail?

The following conditions must be met by the sub-class method and the super-class method in order for method overriding to occur:

  • The same method name should be used.
  • The argument must be the same.
  • The return type must be the same
  • The main advantage of overriding is that the subclass can supply more particular information than the superclass about that subclass type.

14. Explain Overloading in Java?

The correct answer is that method overloading can occur between classes or even inside the same class. For method overloading, the sub-class method together with the super-class method (or) methods from the same class itself should meet the following requirements:

  • Same approach
  • Various forms of arguments
  • There could be various return kinds.

15. What does "interface" mean?

In Java, multiple inheritances are not possible. The Interface concept is presented as a solution to this issue. An interface is a template that contains method declarations- not the actual implementation of the methods. 

The following are some features:

  • Internally each interface method is a public abstract void. 
  • All of the interface's variables are internal public static final constants.
  • Classes may not extend; they may only implement the interface.
  • All of the methods declared in the interface should have an implementation in the class that implements it.

16. What does "Abstract class" mean?

The "Abstract" keyword placed before the class name allows us to build the Abstract class. Both "Abstract" and "Non-abstract" methods that are of a concrete class can be found in an abstract class.

Abstract approach: The abstract method is one that just has a declaration and no implementation, and it has the keyword "abstract" in its name. A semicolon indicates a declaration's end. A non-abstract method may also be present in an abstract class. The implementation of abstract methods should be provided by the concrete subclass that extends the Abstract class.

17. Define String, String Builder, and String Buffer

String: A "constant string pool" is where string variables are kept. The old value from the "constant string pool" cannot be removed once the string reference has changed it.

String buffer: In this case, string values are kept in a stack. If the values are altered, the new value will take the place of the previous value. Because it is synchronized, the string buffer is thread-safe. The performance is comparatively slower than that of a string builder. 

String Builder: With the exception of the string builder, which cannot be threaded safely and is not synchronized, this is identical to the string buffer. It follows that the performance is quick. 

18. Differentiate between HashMap and HashTable?

We can differentiate between the two in the following points:

  • HashMap's methods are not synchronized while Hashtable's key methods are synchronized. 
  • While HashTable offers thread safety, HashMap is not.
  • For iterating the values Iterator is used in HashMaps but in HashTable Enumerator is used. 
  • HashMap allows one null key as well as several null values. On the contrary, HashTable does not allow anything null.
  • HashMap has a higher performance than HashTable. 

19. How will you differentiate the Hash set and Tree set?

The differences between Hash Set and Tree Set are as follows:

  • In Hash Set, elements inserted are kept in random order. While maintenance of the elements in TreeSet is done in a sorted manner
  • HashSet can store Null objects but Tree Set does not store Null Objects
  • Hash Set has faster performance in comparison to Tree Set. 

20. In what way do Interface and Abstract classes differ from one another?

The following are the differences between an abstract class and an interface:

Abstract Class: Every time a concrete subclass is instantiated, the default function Object() { [native code] } of abstract classes is called. It includes both Non-Abstract and Abstract techniques. Only the Abstract methods must be implemented in the concrete sub-class; the class that extends the Abstract class shouldn't require the implementation of all the methods. Instance variables are present in abstract classes.

Interface: It lacks a function Object() { [native code] } and is not instantiable. It is necessary to declare only the abstract method. All of the methods' implementations should be provided by classes that implement the interface. There are just constants in the interface.

21. What does "Collections" in Java mean?

In order to change the design and store the objects, a framework called a collection is used. The following operations are carried out using collections:

  • Searching
  • Sorting
  • Manipulation
  • Insertion
  • Deletion

Collections are a grouping of objects. The Java util package contains all of the classes and interfaces for collecting.

22. Name the Classes and Interfaces available in the Collections?

The classes and interfaces available in the Collections can be listed below:

Interfaces:

Collection

  • List
  • Set
  • Map
  • Sorted Set
  • Sorted Map
  • Queue

Classes:

  • Lists
  • Array List
  • Vector
  • Linked List

Sets:

  • Hash set
  • Linked Hash Set
  • Tree Set

Maps:

  • Hash Map
  • Hash Table
  • TreeMap
  • Linked Hashed Map

Queue:

Priority Queue

23. What do "Ordered" and "Sorted" in collections mean?

Ordered: This describes a collection's storage of values as being based on the values that have been added to the collection. Consequently, we are able to iterate the collection's values in a particular order.

Sorted: Sorting techniques can be used internally or externally to organize a group of objects into a collection according to their characteristics.

24. Describe the numerous lists that are available in the collection?

The list is organized by index position, and values are added based on index position. Duplications are permitted. List types include:- Array List, Vector, and Linked List. 

Array List:

  • Facilitates Fast Random Access and Quick Iteration. 
  • It is not sorted but rather ordered (by index). 
  • Implements Random Access Interface 

 Vector: It is similar to the Array list.

  • Synchronized methods
  • Thread Safety 
  • Implements Random Access 
  • Thread Safety normally prompts a performance hit.

 Linked List:

  • Elements are linked to one another doubly
  • Slower performance in comparison to the Array list. 
  • Good choice for ' Insert' and 'Delete' functions
  • Supports queue methods like peek( ), Pool ( ), Offer ( ) etc. in Java 5.0

25. What is a priority queue?

Priority Queue: The queue interface has been added to the linked list class. A linked list can be used to manage queues. A queue's motto is "Priority-in, Priority-out."

As a result, items are arranged either naturally or by the comparator. The order of the elements indicates their relative importance.

26. What does the term "exception" mean?

An issue that can arise in the course of normal execution is known as an exception. When something fails during execution, a procedure may throw an exception. The exception is stopped before it completes the task in the event of an inability of handling the exception. If the exception was handled, the process returns to normal. 

Exceptions are subtype of java.lang.Exception.

27. What kinds of exceptions are there?

There are two different kinds of exceptions. They are thoroughly explained as follows:

  1. a) Checked Exception: The compiler examines these exceptions as it is compiling. Checked Exceptions are classes that extend Throwable but do not extend Runtime exception or Error.

Checked exceptions must either be declared with the throws keyword or appropriately encircled by try/catch.

ClassNotFound Exception, for instance.

b)The compiler does not examine these exceptions at compile time. These exceptions are not required to be handled by the compiler. It contains:

  • Arithmetic Exception
  • ArrayIndexOutOfBounds Exception

28. Describe the set and the varieties of sets in a collection?

Set values originality. It prohibits duplications. In this case, determining if two objects are identical or not is done using the "equals ()" method.

Hash Set 

Unsorted and out of order.

Inserts the data using the object's hash code.

Use this when "no duplicates and don't care about the order" is the requirement.

Linked Hash Set

The Linked Hash Set is an ordered variation of the hash set.

Maintains a list of all the elements that are doubly linked.

When an iteration order is necessary, use this.

Tree Set 

One of the two sorted groupings, really

Ensures that the items are arranged in ascending order by using the "Read-Black" tree structure.

With the function Object() { [native code] }, we may create a tree set by utilizing a comparable (or) comparator.

29. What are the benefits of Exception Handling?

The merits of Exception Handling are:

There will be no termination of the normal flow if an exception is handled.

The problem can be identified by utilizing the Catch declaration. 

30. What are the keywords for managing Exception in Java?

There are particularly two keywords for handling Exception, namely:

Try: when a try block encloses code that poses a risk. 

A catch block is used to handle an exception that occurs in the try block. 

Try may be followed by catch, finally, both, or neither. 

However, any one of the blocks must be used.

Catch: The next thing is a try block. 

Here, exceptions are noted.

Finally: Try block or catch block may come after this. 

The execution of this block is unaffected by any exceptions. Clean-up codes are therefore generally offered here.

31. List the distinction between Object-based and Object-oriented languages?

The following are the main distinctions between object-oriented and object-based languages.

  • While object-based languages do not adhere to all of the OOPs principles, such as polymorphism and inheritance, object-oriented languages do.
  • Object-oriented languages do not have built-in objects, whereas object-based languages do. For instance, JavaScript contains a window object.

32. What will an object reference that is specified as an instance variable have as its initial value?

Every object reference in java is initialized to null.

33. What are the variants of constructors that Java support?

There are two variants of constructors in Java, depending on the inputs supplied to them.

  • Default Constructor: The default function Object() { [native code] } is the one that does not take any arguments. Instance variables are typically initialized with default values using the default function Object() { [native code] }. It can also be utilized to carry out various beneficial tasks related to object generation. If no function Object() { [native code] } is defined in the class, the compiler implicitly calls the default function Object() { [native code] }.
  • Parameterized Constructor: This function Object() { [native code] } can initialize instance variables with specified values. In other words, we can say that parameterized constructors are constructors that can receive arguments.

34. What function does a default function Object() { [native code] } serve?

The default value is given to the objects by the default function Object() { [native code] }. If the class doesn't contain a function Object() { [native code] }, the Java compiler implicitly constructs one.

35. What value does the function Object() { [native code] } return?

Yes; you cannot use a function Object() { [native code] } with an explicit return type. The function Object() { [native code] } implicitly returns the current instance of the class. More information

36. Does the function Object() { [native code] } inherit?

It is not inherited, hence the function Object() { [native code] } is not.

37. Can a function Object() { [native code] } final be made?

The function Object() { [native code] } cannot be final.

38. Can the constructors be overloaded?

Yes, there is a possibility for constructors to get overloaded by altering the parameters' data types or the number of arguments the function Object() { [native code] } will accept.

39. What does the Java copy function Object() { [native code] } mean?

Java doesn't have a copy function Object() { [native code] }. However, similar to the copy function Object() { [native code] } in C++, we can copy values from one object to another.

In Java, there are numerous approaches to copying values from one object to another. As follows:

  • By a builder
  • The process of transferring values from one object to another through the Object class's clone() function

40. What does static variable mean?

Employer's names for employees, college names for students, etc. are examples of common properties that apply to all objects but are not unique to each object and are referred to by the static variable. When a class is loaded, a static variable only ever receives one memory allocation in the class area. Your software will use less memory if you use static variables (it saves memory). Static variables are the property of the class, not the object.

41. What exactly is a static method?

A static method is an attribute of the class, not the object.

To invoke the static methods, an object does not need to be created.

The static variable's value can be accessed and modified using a static method.

42. What limitations are placed on Java's static methods?

The static approaches are subject to two fundamental limitations.

The static method cannot explicitly call the non-static method or use non-static data members.

Since they are not static, it and super cannot be utilized in a static context.

43. Give reasons for the main method being static?

Since calling the static method does not require the object. Making the main method non-static will force the JVM to construct its object before calling main(), which will need additional memory allocation. More information

44. Is it possible to override static methods?

No, there is no possibility of static methods being overridden.

45. What exactly is a static block?

The static data member is initialized using the static block. During classloading, it is done before the main procedure.

46. Can constructors be made static?

As is well known, the class, not the object, owns the static context (method, block, or variable). Making the constructors static makes no sense because they are only called when the object is created. However, the compiler will display the compiler error if you attempt to do so.

47. Is it possible to make Java's abstract methods static?

Making abstract methods static in Java makes them part of the class and allows for direct calling, which is not essential. Using an undefined method is never a good idea.

Can static variables and methods be declared in an abstract class?

Yes, static variables and methods can be declared in abstract methods. We may access the static context stated inside the abstract class by using the name of the abstract class because we are aware that the object does not need to be made in order to access the static context.

48. What does this Java keyword mean?

This is a reference variable that points to the currently selected item. This keyword can be used in a number of different ways in Java. It can be used to refer to current class attributes like constructors, variables, and instance methods. It may also be provided to the constructors or methods as an argument. It can also be given back.

49. What are the main applications for this term?

The following are some applications for this term.

  • Using this, one may identify the current class instance variable.
  • With "this", you can call the current class method (implicitly)
  • The current class function Object() { [native code] } can be called using the function this().
  • In the call method, 'this' represents the argument. 
  • The function Object() { [native code] } call can accept this as input.
  • this can be used to have the method return the current instance of the class.

50. Can static members be referred to using this keyword?

Since this is merely a reference variable related to the current class object, it is permissible to employ this keyword to refer to static members. However, since it is redundant to access static variables via objects, using this to refer to static members is not the best practice.

That brings to the end of the  top java interview questions & answers.

Read More

Top 80 Python Interview Questions & Answers

Top 50 React Interview Questions and Answers in 2022

Top 50 Blockchain Interview Questions and Answers

Investment Banking Interview Questions and Answers

Top 50 Project Management (PMP) Interview Questions & Answers

Top 50 Agile Interview Questions And Answers

Top 30 Data Engineer Interview Questions & Answers

Top 50 Network Security Interview Questions and Answers

Top 80 Data Science Interview Questions & Answers

Cyber Security Architect Interview Questions and Answers

Top 120 Cyber Security Interview Questions & Answers in 2022

Top Project Manager Interview Questions and Answers

Top 50 Angular Interview Questions & Answers

Top 50 Tableau Interview Questions and Answers

Top 50 Artificial Intelligence Interview Questions and Answers

Top 50 R Interview Questions & Answers

Top 50 AWS Architect Interview Questions

Top 30 Machine Learning Interview Questions & Answers

Top 50 Hadoop Interview Questions & Answers

Top 80 Azure Interview Questions & Answers

Top 50 Power Bi Interview Questions & Answers

Top 80 Digital Marketing Questions & Answers

Top 50 Big Data Interview Questions & Answers

Top 80 SQL Interview Questions & Answers

Top 50 Scrum Master Interview Questions Answers

Post a Comment

Submit
Top