Java Developer

1500+ Java Developer Interview Questions and Answers

Updated 16 Dec 2024

Popular Companies

search-icon
Q1. Sort 0 1

You have been given an integer array/list(ARR) of size N that contains only integers, 0 and 1. Write a function to sort this array/list. Think of a solution which scans the array/list only once and don'...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

You are given an array β€˜ARR’ of 'N' integers. Your task is to find the longest harmonious subsequence of this array.

A sequence is Harmonious if the difference between the maximum ...read more

Q4. Convert Bst To The Greater Sum Tree

You have been given a Binary Search Tree of integers. You are supposed to convert it to a greater sum tree such that the value of every node in the given BST is replaced with ...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. Calculate the frequency of each word in given string

You are given a string S of words. Your task is to count the occurrence of each word present in the string S. A word is a sequence of one or more non-space ch...read more

Q6. Remove duplicates from a sorted array

You are given a sorted integer array' ARR' of size 'N'. You need to remove the duplicates from the array such that each element appears only once. Return the length of this ...read more

Share interview questions and help millions of jobseekers 🌟

man-with-laptop
Q7. Find the maximum element in the array after update operations.

You have been given an array/list β€˜A’ consisting of β€˜N’ integers all of which are β€˜0’ initially. You are given an array/list β€˜ARR’ consisting of β€˜M’...read more

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

Java Developer Jobs

Java Developer - 3-7 years β€’ 3-7 years
SAP India Pvt.Ltd
β€’
4.2
Bangalore / Bengaluru
Java Developer β€’ 3-5 years
IBM India Pvt. Limited
β€’
4.1
New Delhi
Java Developer β€’ 4-9 years
Verizon Data Services India Pvt.Ltd
β€’
4.2
Hyderabad / Secunderabad

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

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

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

Q12. OOPS Question

Which among String or String Buffer should be preferred when there are lot of updates required to be done in the data?

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

Suppose you have a 4 liter jug and a 9 liter bucket . The buckets have no measurement lines on them either. How could you measure exactly 6 liter using only those buckets and you have as much extra water ...read more

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

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

Q18. Java Question

Which among String or String Buffer should be preferred when there are lot of updates required to be done in the
data?

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

Q20. Spring Boot Question

What is the @Controller annotation used for? How can you create a controller without an annotation?

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

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

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

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

Q25. Swap two numbers without using the third variable

Take two numbers as input and swap them and print the swapped values.

Input Format:
The first line of input contains a single integer 't', representing the total...read more

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

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

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

Q29. Java 8 Question

What are the new features in Java8?

Q30. Middle of Linked List

Given the head node of the singly linked list, return a pointer pointing to the middle of the linked list.

If there are an odd number of elements, return the middle element if there are eve...read more

Q31. SpringBoot Question

What is the starter dependency of the Spring boot module?

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

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

Q34. Java Question

How would you differentiate between a String, StringBuffer, and a StringBuilder?

Q35. Java Question

What are the advantages of Packages in Java?

Q36. Spring Boot Question

What Are the Basic Annotations that Spring Boot Offers?

Q37. Java Question

What is static variable and static method?

Q38. OOPS Question

How would you differentiate between a String, StringBuffer, and a StringBuilder?

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

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

Q41. Java Question

Why Java is platform independent and JVM platform dependent?

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

Q43. Spring Boot Question

How to enable debugging log in the spring boot application?

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

Q45. SpringBoot Question

What does the @SpringBootApplication annotation do internally?

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

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

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

Q49. MVC Question

What is the root application context in Spring MVC? How is it loaded?

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

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
Β β€’Β 10k Interviews
3.9
Β β€’Β 7.8k Interviews
3.7
Β β€’Β 7.3k Interviews
3.8
Β β€’Β 5.4k Interviews
3.7
Β β€’Β 5.2k Interviews
3.8
Β β€’Β 4.6k Interviews
3.6
Β β€’Β 3.7k Interviews
3.6
Β β€’Β 3.6k Interviews
3.6
Β β€’Β 2.3k Interviews
4.1
Β β€’Β 2.3k 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
Get AmbitionBox app

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