Java Developer

1500+ Java Developer Interview Questions and Answers

Updated 11 Feb 2025
search-icon

Q1. Sort 0 and 1 Problem Statement

Given an integer array ARR of size N containing only integers 0 and 1, implement a function to sort this array. The solution should scan the array only once without using any addi...read more

Ans.

The function sorts an integer array containing only 0s and 1s in linear time complexity.

  • Use two pointers, one starting from the beginning and the other from the end of the array.

  • Swap the elements at the pointers if the element at the beginning pointer is 1 and the element at the end pointer is 0.

  • Continue moving the pointers towards each other until they meet in the middle of the array.

Q2. Parent class has run() and walk() . Parent run() - calls walk() Child Class overrides Parent class Inside run() - calls super.run() Walk() - calls super.walk In main class Parent p = new child() p.run() What wi...

read more
Ans.

The order of execution will be: parent's run() -> child's run() -> parent's walk()

  • When p.run() is called, it will first execute the run() method of the parent class

  • Inside the parent's run() method, it will call the walk() method of the parent class

  • Since the child class overrides the parent class, the child's run() method will be executed next

  • Inside the child's run() method, it will call the run() method of the parent class using super.run()

  • Finally, the walk() method of the pa...read more

Java Developer Interview Questions and Answers for Freshers

illustration image

Q3. Longest Harmonious Subsequence Problem Statement

Determine the longest harmonious subsequence within a given array of integers, where the difference between the maximum and minimum elements of the subsequence i...read more

Q4. Convert BST to Greater Sum Tree

Given a Binary Search Tree (BST) of integers, your task is to convert it into a greater sum tree. In the greater sum tree, each node's value should be replaced with the sum of al...read more

Ans.

The task is to convert a Binary Search Tree into a Greater Sum Tree.

  • Traverse the BST in reverse inorder (right, root, left) to visit nodes in descending order.

  • Keep track of the sum of all greater nodes encountered so far.

  • Update the value of each node by adding the sum of greater nodes and update the sum.

  • Continue this process until all nodes have been visited.

  • Return the modified BST.

Are these interview questions helpful?
Q5. Which should be preferred between String and StringBuffer when there are many updates required to the data?

Q6. 2. What will happen if hashcode only returns a constant? How will it affect the internal working of the HashMap?

Ans.

If hashcode returns a constant, all elements will be stored in the same bucket, resulting in poor performance.

  • All elements will be stored in the same bucket of the HashMap.

  • This will lead to poor performance as the HashMap will essentially become a linked list.

  • Retrieving elements will take longer as the HashMap will have to iterate through the linked list to find the desired element.

Share interview questions and help millions of jobseekers 🌟

man-with-laptop

Q7. Word Frequency Counter

Given a string 'S' consisting of words, your task is to calculate how many times each word appears in the string. A word is a series of one or more non-space characters. In 'S', words can...read more

Q8. Remove Duplicates from a Sorted Array

Given a sorted integer array ARR of size N, you need to remove duplicates such that each element appears only once and return the length of this new array.

Input:

The first...read more

Java Developer Jobs

Java Developer 6-8 years
SAP Labs India Pvt. Ltd.
4.2
Bangalore / Bengaluru
Java Developer/Architect 9-14 years
Ericsson India Global Services Pvt. Ltd.
4.1
Pune
Java Developer 1-6 years
Robert Bosch Engineering and Business Solutions Private Limited
4.2
Coimbatore

Q9. Max Element After Update Operations

Given an array A of size N, initialized with all zeros, and another array ARR of M pairs of integers representing operations. Each operation consists of a range where each el...read more

Q10. What array list and linkedlist difference,how hashmap internally working,what is synchronised and it's type,what is difference between abstract and interface,class loading mechanism in Java, you hat is single t...

read more
Ans.

Java Developer interview questions covering array list, linkedlist, hashmap, synchronization, abstract vs interface, singleton class, Spring framework, database configuration, and Java 8 features.

  • ArrayList and LinkedList differ in terms of implementation, performance, and usage

  • HashMap uses hashing to store and retrieve key-value pairs

  • Synchronized is used to ensure thread safety and prevent race conditions

  • Abstract classes cannot be instantiated and can have both abstract and n...read more

Q11. what are the difference between abstract class and interface, and throw and throws, and why we use throws?? Why String is Immutable?

Ans.

Explaining differences between abstract class and interface, throw and throws, and why we use throws. Also, why String is immutable.

  • Abstract class can have both abstract and non-abstract methods, while interface can only have abstract methods.

  • A class can implement multiple interfaces, but can only extend one abstract class.

  • Throw is used to explicitly throw an exception, while throws is used to declare the exceptions that a method may throw.

  • We use throws to inform the caller o...read more

Q12. How to sort a list of students on the basis of their First name?

Ans.

To sort a list of students on the basis of their First name, use the Collections.sort() method with a custom Comparator.

  • Create a custom Comparator that compares the first names of the students.

  • Implement the compare() method in the Comparator to compare the first names.

  • Use the Collections.sort() method to sort the list of students using the custom Comparator.

Q13. 1. Abstraction vs Inheritance 2. What is Garbage collector? 3. What is class loader? 4. Spring security 5. Scopes of bean 6. Wait , notify and notify all 7. Class level vs object level local 8. Arraylist vs Lin...

read more
Ans.

Java interview questions covering topics like abstraction, inheritance, garbage collector, class loader, Spring security, bean scopes, wait-notify, class vs object level local, ArrayList vs LinkedList, Singleton class, and RestController vs Controller.

  • Abstraction focuses on hiding implementation details while inheritance allows a class to inherit properties and methods from another class.

  • Garbage collector is a program that automatically frees up memory by deleting objects tha...read more

Q14. Explain OOPS Concept? What is Polymorphism and Types of polymorphism? Write Code for compile time and Run time Polymorphism? What are singleton class and factory method? What is Exception and Exception Hierarch...

read more
Ans.

Java interview questions on OOPS concepts, polymorphism, exceptions, data structures, and threading.

  • OOPS concepts include encapsulation, inheritance, and polymorphism.

  • Polymorphism refers to the ability of an object to take on multiple forms.

  • Singleton class is a class that can only have one instance, while factory method is a method that creates objects.

  • Exception is an error that occurs during program execution, with a hierarchy of exceptions.

  • Volatile keyword is used to indica...read more

Q15. write a code to filter out loans with incomplete status using java 8 features.

Ans.

Code to filter out loans with incomplete status using Java 8 features.

  • Use stream() method to convert the list of loans into a stream

  • Use filter() method to filter out loans with incomplete status

  • Use collect() method to collect the filtered loans into a new list

Q16. Write a program to return the length of the longest word from a string whose length is even?

Ans.

This program returns the length of the longest word from a string whose length is even.

  • Split the string into an array of words using a space as the delimiter.

  • Iterate through the array and check if the length of each word is even.

  • Keep track of the length of the longest even word encountered.

  • Return the length of the longest even word.

Q17. 1.What is Singleton in java and create your own singleton class countering all breakable conditions? 2. What is Auto Configuration? 3. @Primary vs @Qualifier 4. What is idempotent? 5. What is class loader? Type...

read more
Ans.

The interview questions cover various topics related to Java development, including Singleton pattern, memory management, exception handling, Spring framework, and design patterns.

  • Singleton pattern ensures a class has only one instance and provides a global point of access to it.

  • Auto Configuration in Spring Boot automatically configures the Spring application based on dependencies present in the classpath.

  • @Primary annotation is used to give higher preference to a bean when mu...read more

Q18. 5. If we have a mutable object inside immutable class then how will you handle it?

Ans.

To handle a mutable object inside an immutable class, make the object private and provide only read-only access methods.

  • Make the mutable object private to encapsulate it within the immutable class.

  • Provide read-only access methods to retrieve the state of the mutable object.

  • Ensure that the mutable object cannot be modified from outside the class.

  • If necessary, create defensive copies of the mutable object to prevent unintended modifications.

Q19. 1. Can we keep an object of a Class as key in HashMap?

Ans.

Yes, we can keep an object of a Class as a key in HashMap.

  • The key in a HashMap can be any non-null object.

  • The key's class must override the hashCode() and equals() methods for proper functioning.

  • If two objects have the same hashCode(), they will be stored in the same bucket and compared using equals().

  • If the key is mutable, its state should not be modified after it is used as a key in the HashMap.

Q20. What are the main OOPS concepts in java and explain one by one?

Ans.

Java OOPS concepts include inheritance, polymorphism, encapsulation, and abstraction.

  • Inheritance allows a class to inherit properties and methods from another class.

  • Polymorphism allows objects to take on multiple forms and behave differently based on their context.

  • Encapsulation hides the implementation details of a class and only exposes necessary information.

  • Abstraction allows for the creation of abstract classes and interfaces that can be implemented by other classes.

  • Exampl...read more

Q21. write program fibonacci series, write program using boolean, write class cast exceptn, singleton design pattern(like you have to jvm one have jdk 1.7 and other have jdk 1.8 how many instance created if one then...

read more
Ans.

This interview question covers various topics including programming, design patterns, data structures, and exception handling in Java.

  • Write a program to generate the Fibonacci series

  • Write a program using boolean variables

  • Explain the ClassCastException and how to handle it

  • Discuss the Singleton design pattern and its implementation with different JDK versions

  • Explain abstract classes and interfaces in your current project

  • Explain hashing in HashMap and the number of buckets

  • Differ...read more

Q22. Swap Two Numbers Without Using a Third Variable

Your task is to swap two given numbers without utilizing an additional variable and print the swapped values.

Input:

The first line contains an integer 't', indic...read more

Q23. Write a query to find name of authors who have written more than 10 books. Table - Bookauthhor Column - Book Column - Author

Ans.

Query to find authors who have written more than 10 books

  • Use the GROUP BY clause to group the records by author

  • Use the HAVING clause to filter the groups with a count greater than 10

  • Join the Bookauthor table with the Author table to get the author names

Q24. Is Java platform-independent, if yes why?

Ans.

Yes, Java is platform-independent because of its 'write once, run anywhere' principle.

  • Java programs are compiled into bytecode, which can be executed on any platform with a Java Virtual Machine (JVM).

  • The JVM acts as an interpreter, translating the bytecode into machine code specific to the underlying platform.

  • This allows Java programs to run on different operating systems and hardware architectures without modification.

  • For example, a Java program developed on a Windows machin...read more

Q25. 1. What is JDK, JVM, JRE.

Ans.

JDK is a development kit, JRE is a runtime environment, and JVM is a virtual machine for executing Java code.

  • JDK includes JRE and development tools like javac and java

  • JRE includes JVM and necessary libraries to run Java applications

  • JVM is responsible for interpreting Java bytecode and executing it

  • Example: JDK 8 includes JRE 8 and development tools like javac and java

  • Example: JRE 8 includes JVM 8 and necessary libraries to run Java applications

Q26. What are the new features in Java 8?

Q27. Middle of a Linked List

You are given the head node of a singly linked list. Your task is to return a pointer pointing to the middle of the linked list.

If there is an odd number of elements, return the middle ...read more

Q28. What is the use of private variables even though they are accessible via getters and setters from some other class?

Ans.

Private variables provide encapsulation and data hiding.

  • Private variables can only be accessed within the class they are declared in, providing encapsulation and preventing direct modification from outside the class.

  • Getters and setters provide controlled access to private variables, allowing for validation and manipulation of the data before it is accessed or modified.

  • For example, a class may have a private variable for a person's age, but the getter can return a calculated a...read more

Q29. Q1. What are the technologies you have worked on ? Q2. What are the corporate training you have attended ? Q3. How do you handle non-productive team members ? Q4. Tell us some of your extraordinary skills which...

read more
Ans.

Answers to interview questions for a Java Developer position

  • Q1. Mention the technologies you have worked on, such as Java, Spring, Hibernate, etc.

  • Q2. List any corporate training you have attended, like Java certification courses or workshops.

  • Q3. Explain how you handle non-productive team members, emphasizing communication and motivation strategies.

  • Q4. Highlight your extraordinary skills that set you apart from other technical associates, such as problem-solving abilities or l...read more

Q30. What are the advantages of using Packages in Java?
Q31. What are the basic annotations that Spring Boot offers?
Q32. What is a static variable and a static method in Java?
Q33. What is the starter dependency of the Spring Boot module?

Q34. 3. What if we do not override Equals method ? What is we do not override Hashcode method?

Ans.

Not overriding equals method can lead to incorrect comparison of objects. Not overriding hashCode method can lead to incorrect behavior in hash-based data structures.

  • If equals method is not overridden, the default implementation from Object class will be used which compares object references.

  • This can lead to incorrect comparison of objects where two different objects with same content are considered unequal.

  • If hashCode method is not overridden, the default implementation from...read more

Q35. 2.differance between throw and throws, and difference between final,finally,finalize and difference between arraylist and linked list

Ans.

throw is used to explicitly throw an exception, while throws is used to declare exceptions that a method may throw.

  • throw is used within a method to throw an exception explicitly

  • throws is used in the method signature to declare the exceptions that the method may throw

  • final is a keyword used to make a variable, method, or class unchangeable

  • finally is a block that is used to execute code regardless of whether an exception is thrown or not

  • finalize is a method that is called by th...read more

Q36. Why is Java considered platform independent, while the Java Virtual Machine (JVM) is platform dependent?

Q37. 5. What are scope of beans? 6. Functional interfaces and examples 7. Can we make a class as private? 8. SQL query to find employees of particular department table. 9. Cascading 10. MapBy in hibernate 11. What i...

read more
Ans.

Java Developer interview questions on beans, functional interfaces, SQL queries, hibernate, and more.

  • Scope of beans refers to the visibility of a bean within an application context.

  • Functional interfaces have only one abstract method and can be used as lambda expressions.

  • Classes cannot be private, but their constructors can be.

  • SQL query to find employees of a particular department table: SELECT * FROM employees WHERE department = 'department_name'

  • Cascading in Hibernate refers ...read more

Q38. How do you enable debugging logs in a Spring Boot application?

Q39. What are the principles of object oriented programming, such as OOPs concepts in Java

Ans.

Object-oriented programming (OOP) principles in Java include encapsulation, inheritance, polymorphism, and abstraction.

  • Encapsulation: Bundling data and methods together into a single unit (class) to hide implementation details.

  • Inheritance: Creating new classes (subclasses) from existing classes (superclasses) to inherit properties and behaviors.

  • Polymorphism: The ability of an object to take on many forms, allowing objects of different classes to be treated as objects of a com...read more

Q40. What does the @SpringBootApplication annotation do internally?

Q41. If we have @Service, @Controller, @Configuration, and @Repository why do we need @Component? Can we replace them with @Component?

Ans.

The @Component annotation is a generic stereotype annotation that can be used in place of other annotations.

  • The @Component annotation is a general-purpose annotation that can be used in place of @Service, @Controller, @Configuration, and @Repository annotations.

  • However, using more specific annotations can help in better understanding the role of the annotated class.

  • For example, using @Service annotation on a class that provides a service makes it easier to understand the purp...read more

Q42. Tell me some keywords to make a thread safe program in java ?

Ans.

Keywords for thread safe program in Java

  • Synchronization using synchronized keyword

  • Using volatile keyword for shared variables

  • Using atomic classes for thread safe operations

  • Using thread safe collections like ConcurrentHashMap

  • Using locks and semaphores for synchronization

  • Avoiding shared mutable state

  • Using immutable objects

  • Using thread local variables

Q43. what is interface, inheritance doesn't supported by java, where to use interface, what are the advantage of using program to reverse each word in a string (eg. input -> pace wisdom output ->ecap modsiw)

Ans.

Answering questions related to Java interfaces and string manipulation

  • Interface is a collection of abstract methods that can be implemented by a class

  • Inheritance is supported by Java, but only single inheritance is allowed

  • Interfaces are used to achieve abstraction and provide a contract for implementing classes

  • Advantages of program to reverse each word in a string include improved readability and searchability

  • To reverse each word in a string, split the string into words, reve...read more

Q44. Using Java 8 find the sum of squares of all the odd numbers in the arraylist.

Ans.

Using Java 8, find the sum of squares of all the odd numbers in the arraylist.

  • Use the stream() method to convert the ArrayList to a stream

  • Use the filter() method to filter out the even numbers

  • Use the mapToInt() method to square each odd number

  • Use the sum() method to find the sum of the squared odd numbers

Q45. What is meant by an interface in Object-Oriented Programming?
Q46. What is the difference between a User thread and a Daemon thread in Java?

Q47. 6. Advantages and Disadvantages of immutable class?

Ans.

Immutable classes have advantages like thread safety, security, and simplicity, but also have disadvantages like memory usage and performance overhead.

  • Advantages of immutable classes:

  • - Thread safety: Immutable objects can be safely shared among multiple threads without the need for synchronization.

  • - Security: Immutable objects cannot be modified, which can prevent unauthorized changes to sensitive data.

  • - Simplicity: Immutable objects are easier to reason about and debug since...read more

Q48. Do you have any Java certficate? If no then please leave you are rejected.

Ans.

Java certification is not mandatory for a Java Developer role.

  • Certification is not a measure of practical skills and experience.

  • Experience and skills are more important than certification.

  • Certification can be helpful in some cases, but not mandatory.

  • Certification can be expensive and time-consuming.

  • Employers may value certification differently.

  • Examples of Java certifications: Oracle Certified Professional Java SE 11 Developer, Java SE 8 Programmer, etc.

Q49. What are the primitive data types in Java?

Q50. 1.why we go with oops concept? 2.what is polymorphism? 3.what is JVM? 4.what is the Database connection coding in JAVA? 5.what is JOIN(QUERY)? 6.what is TRIGGER? 7.what is PROCEDURE? 8.what is Hashmap? 9.what i...

read more
Ans.

Basic Java and Query questions for Java Developer position.

  • OOPs concept helps in creating modular, reusable and maintainable code.

  • Polymorphism is the ability of an object to take on many forms.

  • JVM stands for Java Virtual Machine, which executes Java bytecode.

  • Database connection coding in Java involves creating a connection object, setting connection properties, and executing SQL queries.

  • JOIN is used to combine rows from two or more tables based on a related column between the...read more

1
2
3
4
5
6
7
Next
Interview Tips & Stories
Ace your next interview with expert advice and inspiring stories

Interview experiences of popular companies

3.7
 • 10.5k Interviews
3.8
 • 8.2k Interviews
3.6
 • 7.6k Interviews
3.7
 • 5.6k Interviews
3.8
 • 5.6k Interviews
3.7
 • 4.8k Interviews
3.5
 • 3.8k Interviews
3.5
 • 3.8k Interviews
3.8
 • 3k Interviews
4.0
 • 2.4k Interviews
View all

Calculate your in-hand salary

Confused about how your in-hand salary is calculated? Enter your annual salary (CTC) and get your in-hand salary

Java Developer Interview Questions
Share an Interview
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter