Java Developer

filter-iconFilter interviews by

200+ Java Developer Interview Questions and Answers for Freshers

Updated 28 Jan 2025

Q51. How can a Spring Boot project be created to develop an API that returns specified data?

Ans.

A Spring Boot project can be created to develop an API by setting up a new Spring Boot project, defining API endpoints, and implementing the necessary logic.

  • Create a new Spring Boot project using Spring Initializr

  • Define API endpoints using @RestController annotation

  • Implement the logic to return specified data in the API endpoints

  • Use @GetMapping, @PostMapping, @PutMapping, @DeleteMapping annotations to map HTTP methods to controller methods

Q52. What is the code to print a right-angled triangle of numbers based on a given input number?

Ans.

Use nested loops to print a right-angled triangle of numbers based on input.

  • Use two nested loops to control the rows and columns of the triangle.

  • Increment the number to be printed in each row.

  • Example: If input is 5, the output would be: 1, 12, 123, 1234, 12345

Q53. Explain Security management, how to enable security for APIs.

Ans.

Security management involves implementing measures to protect APIs from unauthorized access and ensure data integrity.

  • Implement authentication mechanisms such as OAuth or API keys to control access to APIs

  • Use encryption to secure data transmission between clients and APIs

  • Implement rate limiting to prevent abuse and protect against denial of service attacks

  • Regularly update and patch API security vulnerabilities to stay protected

  • Monitor API traffic and logs for suspicious activ...read more

Q54. Write a functional interface. Refer it to a Lamda Expression. Show it practically while explaining each step

Ans.

A functional interface is a single abstract method interface. It can be referred to using a lambda expression.

  • Functional interfaces have only one abstract method

  • Lambda expressions can be used to implement functional interfaces

  • Lambda expressions provide a concise way to write anonymous functions

  • Functional interfaces can be used as method parameters or return types

Are these interview questions helpful?

Q55. 21. Difference between array and heap?

Ans.

Arrays are fixed in size and stored in stack memory, while heap is dynamic and stored in heap memory.

  • Arrays are declared with a fixed size, while heap can grow or shrink dynamically.

  • Arrays are stored in stack memory, while heap is stored in heap memory.

  • Arrays can only store elements of the same data type, while heap can store objects of different types.

  • Examples of arrays include int[], char[], and String[], while examples of heap objects include ArrayList, HashMap, and Linked...read more

Q56. 22. Can we add duplicates to Set?

Ans.

No, Set does not allow duplicates.

  • Set is a collection that does not allow duplicates.

  • If you try to add a duplicate element to a Set, it will not be added.

  • You can use a List if you need to allow duplicates.

Share interview questions and help millions of jobseekers 🌟

man-with-laptop

Q57. 23. Can we add duplicates to Array?

Ans.

Yes, duplicates can be added to an array.

  • Arrays in Java can contain duplicate elements.

  • Duplicate elements can be added to an array using the add() method of ArrayList class.

  • For example, int[] arr = {1, 2, 3, 3, 4}; or ArrayList list = new ArrayList<>(Arrays.asList(1, 2, 3, 3, 4));

Q58. What is collection framework?

Ans.

Collection framework in Java is a set of classes and interfaces that provide a way to store and manipulate groups of objects.

  • Collection framework provides interfaces like List, Set, and Map for storing and manipulating groups of objects.

  • It includes classes like ArrayList, LinkedList, HashSet, and HashMap that implement these interfaces.

  • Collections in Java provide methods for adding, removing, and accessing elements in a collection.

  • It allows for easy iteration over elements us...read more

Java Developer Jobs

Java Developer 5-10 years
SAP India Pvt.Ltd
4.2
Bangalore / Bengaluru
Full Stack Java Developer 5-10 years
S&P Global Inc.
4.2
Hyderabad / Secunderabad
Java Developer 2-8 years
Ericsson India Global Services Pvt. Ltd.
4.1
Pune

Q59. What is HashMap? While Interating through it is it mutable?

Ans.

HashMap is a data structure in Java that stores key-value pairs. It is mutable while iterating through it.

  • HashMap is part of the Java Collections Framework.

  • It allows null values and at most one null key.

  • It provides constant-time performance for basic operations like get and put.

  • While iterating through a HashMap, it is mutable and can throw ConcurrentModificationException if modified.

  • To avoid this, use an Iterator or ConcurrentHashMap.

Q60. If you perer this language to why?

Ans.

I prefer Java because of its versatility, strong community support, and extensive libraries.

  • Java is a versatile language that can be used for a wide range of applications.

  • It has a strong community support with a large number of developers and resources available.

  • Java has extensive libraries that provide ready-made solutions for various tasks.

  • It offers platform independence, allowing the code to run on different operating systems.

  • Java's object-oriented nature promotes code reu...read more

Q61. What are the various annotations used in SpringBoot (explain)

Ans.

Various annotations used in SpringBoot

  • 1. @SpringBootApplication: Marks the main class of a Spring Boot application

  • 2. @RestController: Marks a class as a RESTful controller

  • 3. @RequestMapping: Maps HTTP requests to handler methods

  • 4. @Autowired: Injects dependencies automatically

  • 5. @Component: Marks a class as a Spring component

  • 6. @Service: Marks a class as a service component

  • 7. @Repository: Marks a class as a repository component

  • 8. @Configuration: Marks a class as a source of b...read more

Q62. Write a program to find the numbers divisble by 3 in a string - and reverse its indexes eg : - input : String s = "123456789" output String s = "129456783"

Ans.

Program to find numbers divisible by 3 in a string and reverse their indexes.

  • Iterate through the string and check if each character is a number divisible by 3.

  • Store the divisible numbers in an array and reverse their indexes.

  • Join the array back into a string and return the result.

Q63. 15. What is protected ?

Ans.

protected is a Java access modifier that allows access within the same package and subclasses.

  • Protected members can be accessed within the same package and subclasses.

  • It is used to provide encapsulation and prevent direct access from outside the class.

  • Protected members can be accessed using the dot operator.

  • Example: protected int num; can be accessed in a subclass using obj.num;

Q64. 19. What are collections?

Ans.

Collections are objects that group multiple elements into a single unit.

  • Collections are used to store, retrieve, manipulate and communicate aggregate data.

  • Java provides a set of interfaces and classes that implement various types of collections.

  • Examples of collections include List, Set, Map, Queue, etc.

  • Collections can be used to improve code readability, reusability, and performance.

Q65. Why you want to Change from mechanical to IT? Distinguish between JRE JVM JDK?

Ans.

I transitioned from mechanical to IT because of my passion for technology and problem-solving.

  • Passion for technology and problem-solving led me to pursue a career in IT

  • Enjoyment of coding and creating software applications

  • Opportunities for growth and learning in the IT field

  • Mechanical background provides a unique perspective and problem-solving skills in IT

  • Example: I found coding more fulfilling and challenging than mechanical engineering tasks

Q66. What is threads. Different types of threads? What is interitance . types of inheritance?

Ans.

Threads are lightweight processes that enable multitasking. Inheritance is a mechanism of creating a new class from an existing class.

  • Threads allow multiple tasks to run concurrently within a single program

  • Types of threads include user threads, daemon threads, and system threads

  • Inheritance allows a new class to inherit properties and methods from an existing class

  • Types of inheritance include single inheritance, multiple inheritance, and hierarchical inheritance

Q67. What is compile time polymorphism and runtime polymorphism?

Ans.

Compile time polymorphism is achieved through method overloading, while runtime polymorphism is achieved through method overriding.

  • Compile time polymorphism is also known as static polymorphism.

  • It is achieved by having multiple methods in the same class with the same name but different parameters.

  • Example: method overloading in Java.

  • Runtime polymorphism is also known as dynamic polymorphism.

  • It is achieved by having a method in a superclass and a method with the same signature ...read more

Q68. Why we need Java ? What is Polymorphism Can we use return type in constructer

Ans.

Java is a widely used programming language known for its platform independence and extensive libraries.

  • Java is platform independent, meaning it can run on any operating system

  • Java has a large standard library with pre-built classes and methods for common tasks

  • Java is object-oriented, allowing for modular and reusable code

  • Java supports multithreading, enabling concurrent execution of tasks

  • Java has automatic memory management through garbage collection

  • Java is widely used in ent...read more

Q69. Difference between error and exception ?

Ans.

Error is a serious issue that cannot be handled at runtime, while exception is a runtime issue that can be handled.

  • Errors are caused by the environment in which the application is running, while exceptions are caused by the application code.

  • Errors cannot be caught or handled, while exceptions can be caught and handled using try-catch blocks.

  • Examples of errors include OutOfMemoryError and StackOverflowError, while examples of exceptions include NullPointerException and ArrayIn...read more

Q70. 8. Can we have multiple try block

Ans.

Yes, we can have multiple try blocks in Java.

  • Multiple try blocks can be used to handle different exceptions separately.

  • Each try block must have at least one catch or finally block.

  • Nested try blocks can also be used.

  • Example: try { //code } catch (Exception e) { //code } try { //code } catch (IOException e) { //code }

  • Example of nested try blocks: try { try { //code } catch (Exception e) { //code } } catch (Exception e) { //code }

Q71. What is method overloading?

Ans.

Method overloading is when multiple methods have the same name but different parameters.

  • Method overloading allows for more flexibility in method calls.

  • The methods must have different parameters, such as different data types or different numbers of parameters.

  • Example: public void print(int num) and public void print(String str)

  • Overloading constructors is also common in Java.

Frequently asked in,

Q72. What programming language do you learn.

Ans.

I have learned Java programming language.

  • Java is a widely used programming language for developing various applications.

  • It is an object-oriented language known for its platform independence.

  • Java is used for developing web, desktop, mobile, and enterprise applications.

  • It has a large standard library and supports multithreading, networking, and database connectivity.

  • Some examples of Java frameworks are Spring, Hibernate, and JavaFX.

Q73. Explain Sonarqube, different type of code smells, and how its implemented in CI CD

Ans.

Sonarqube is a static code analysis tool used to detect code smells and bugs in code. It is integrated into CI/CD pipelines for continuous code quality checks.

  • Sonarqube is a static code analysis tool that identifies code smells, bugs, and security vulnerabilities in code.

  • Code smells are common programming issues that may indicate a deeper problem in the code. Examples include duplicated code, long methods, and complex conditionals.

  • Sonarqube can be integrated into CI/CD pipeli...read more

Q74. How many ways to create the object in java

Ans.

There are multiple ways to create objects in Java.

  • Using the 'new' keyword

  • Using object cloning

  • Using object deserialization

  • Using reflection

  • Using factory methods

  • Using dependency injection frameworks

Q75. How do you do authentication and authorisation in your java spring boot app

Ans.

Authentication and authorization in a Java Spring Boot app involves using Spring Security for managing user access.

  • Use Spring Security to handle authentication and authorization

  • Configure security settings in the application.properties file

  • Implement custom authentication providers if needed

  • Use annotations like @PreAuthorize and @Secured for method-level security

Q76. What is the difference between Call By value and call by reference

Ans.

Call by value passes a copy of the value while call by reference passes the address of the value.

  • Call by value passes a copy of the value while call by reference passes the address of the value.

  • In call by value, any changes made to the parameter inside the function do not affect the original value outside the function.

  • In call by reference, any changes made to the parameter inside the function affect the original value outside the function.

  • Java uses call by value for primitive...read more

Q77. 6. What is exception handing

Ans.

Exception handling is the process of handling errors and exceptions that occur during program execution.

  • Exceptions are objects that represent errors or exceptional situations that occur during program execution.

  • Exception handling involves catching and handling these exceptions to prevent program crashes.

  • Java provides try-catch-finally blocks for handling exceptions.

  • Examples of exceptions include NullPointerException, ArrayIndexOutOfBoundsException, and FileNotFoundException.

Q78. 7. Why we use throw and throws ?

Ans.

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

  • throw is used to handle exceptional situations in the code

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

  • throw is followed by an instance of an exception class

  • throws is followed by the list of exceptions that a method may throw

  • throw is used inside a method

  • throws is used in the method signature

Q79. What is the use of final key word in java

Ans.

The final keyword in Java is used to restrict the user from modifying the value of a variable, the definition of a method, or the inheritance of a class.

  • When applied to a variable, the final keyword makes it a constant that cannot be changed.

  • When applied to a method, the final keyword prevents it from being overridden by subclasses.

  • When applied to a class, the final keyword prevents it from being extended by other classes.

  • Final variables must be initialized and cannot be reas...read more

Q80. How to rollback a transaction in spring boot

Ans.

To rollback a transaction in Spring Boot, use @Transactional annotation and throw an exception

  • Use @Transactional annotation on the method where the transaction needs to be rolled back

  • Throw an exception within the method to trigger the rollback process

  • Spring will automatically rollback the transaction when an exception is thrown

Q81. How to write a test case for a private method

Ans.

Use reflection to access and test private methods

  • Use reflection to access the private method in the test case

  • Set the method accessible using setAccessible(true)

  • Invoke the method with the necessary parameters and assert the expected result

Q82. What are checked and unchecked exceptions

Ans.

Checked exceptions are checked at compile time, while unchecked exceptions are not.

  • Checked exceptions must be either caught or declared in the method signature using 'throws' keyword

  • Unchecked exceptions do not need to be caught or declared

  • Examples of checked exceptions: IOException, SQLException

  • Examples of unchecked exceptions: NullPointerException, ArrayIndexOutOfBoundsException

Q83. What is inheritance in java?

Ans.

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

  • Inheritance is achieved using the 'extends' keyword.

  • The class that is being inherited from is called the superclass or parent class.

  • The class that inherits from the superclass is called the subclass or child class.

  • Subclasses can access the public and protected members of the superclass.

  • Inheritance promotes code reusability and allows for the creation of hierarchical relationships.

Q84. Find the duplicate element in a given array using Java 8 features.

Ans.

Find duplicate element in array using Java 8 features.

  • Convert array to stream using Arrays.stream()

  • Use Collectors.groupingBy() to group elements by their occurrence

  • Filter the grouped elements to find duplicates

Q85. Why use in java to Javascript

Ans.

Java and JavaScript are two different programming languages with different use cases.

  • Java is used for building server-side applications, Android apps, and enterprise software.

  • JavaScript is used for building interactive web applications and front-end development.

  • Java is a compiled language while JavaScript is an interpreted language.

  • Java is statically typed while JavaScript is dynamically typed.

  • Java code runs on the Java Virtual Machine (JVM) while JavaScript code runs on web ...read more

Q86. What are the difference between array and list in java

Ans.

Arrays are fixed in size, while lists can grow dynamically. Lists can hold objects of different types.

  • Arrays are of fixed size, while lists can grow dynamically.

  • Arrays can hold only elements of the same type, while lists can hold objects of different types.

  • Arrays are faster for accessing elements, while lists are faster for adding or removing elements.

  • Arrays are implemented as a simple object, while lists are implemented as a linked list.

  • Examples: int[] arr = new int[5]; List...read more

Q87. What are the different bean scopes in Spring

Ans.

The different bean scopes in Spring are singleton, prototype, request, session, and application.

  • Singleton scope creates a single instance of a bean per Spring IoC container.

  • Prototype scope creates a new instance of a bean every time it is requested.

  • Request scope creates a new instance of a bean for each HTTP request.

  • Session scope creates a new instance of a bean for each HTTP session.

  • Application scope creates a single instance of a bean per ServletContext.

Q88. Create package and call tha class in anotherclassby using object on oops

Ans.

Create a package in Java and call a class from that package in another class using object-oriented programming principles.

  • Create a package by using the 'package' keyword at the beginning of the Java file.

  • Define a class within the package and create an object of that class in another class.

  • Import the package in the class where you want to use the class from the package.

  • Access the class from the package using the object created in the other class.

Q89. explain opps concept, what is spring boot

Ans.

OOPs concept is a programming paradigm based on the concept of objects, Spring Boot is a framework for building Java applications.

  • OOPs concept stands for Object-Oriented Programming, which focuses on creating objects that interact with each other to solve problems.

  • It involves concepts like inheritance, encapsulation, polymorphism, and abstraction.

  • Spring Boot is a framework that simplifies the development of Java applications by providing pre-configured settings and tools.

  • It a...read more

Q90. what are all the difficulties do have experianced

Ans.

Difficulties faced as a Java Developer

  • Debugging complex code

  • Handling legacy code

  • Working with outdated technologies

  • Managing project timelines and expectations

  • Collaborating with non-technical stakeholders

Q91. which design pattern to use for complex design?

Ans.

Use the Strategy design pattern for complex design.

  • Strategy pattern allows you to define a family of algorithms, encapsulate each one, and make them interchangeable.

  • It helps in separating the behavior of a class from the class itself, making it easier to switch between different algorithms at runtime.

  • Examples of using Strategy pattern include implementing different sorting algorithms in a class or defining various payment methods in an e-commerce application.

Q92. Number Plate Using Array MultiDimension Array

Ans.

Creating a number plate using a multidimensional array of strings.

  • Create a 2D array of strings to represent the number plate

  • Use loops to populate the array with the required characters

  • Print the array to display the number plate

Q93. 3. What is Servlets and JSP?

Ans.

Servlets and JSP are Java technologies used for web development.

  • Servlets are Java classes that handle HTTP requests and responses.

  • JSP (JavaServer Pages) are HTML pages with embedded Java code.

  • Servlets and JSP work together to create dynamic web pages.

  • Servlets are faster than JSP as they directly generate HTML.

  • JSP is easier to use for web designers as it allows them to focus on HTML and CSS.

Q94. Coding: Sort list of student details using mark.

Ans.

Sort list of student details by mark.

  • Create a custom class Student with attributes like name, mark.

  • Implement Comparator interface to sort by mark.

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

Q95. The internal implementation of HashMap.

Ans.

HashMap is a data structure that stores key-value pairs and uses hashing to retrieve values.

  • HashMap uses an array of linked lists to store key-value pairs

  • The hash code of the key is used to determine the index of the array

  • If multiple keys have the same hash code, they are stored in the same linked list

  • HashMap uses load factor to determine when to resize the array

  • The default load factor is 0.75

  • HashMap allows null keys and values

Q96. Explain encapsulation and it's uses

Ans.

Encapsulation is the concept of bundling data and methods that operate on the data into a single unit, known as a class.

  • Encapsulation helps in hiding the internal state of an object and restricting access to it.

  • It allows for better control over the data by preventing direct access and manipulation.

  • Encapsulation also helps in achieving data abstraction, where the internal details of an object are hidden and only the necessary information is exposed.

  • By using access modifiers li...read more

Q97. Difference between extend and implement?

Ans.

Extend is used to inherit properties and methods from a class, while implement is used to implement interfaces.

  • Extend is used to create a subclass that inherits properties and methods from a superclass.

  • Implement is used to define a class that implements the methods declared in an interface.

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

  • Extend is used for inheritance, while implement is used for interface implementation.

Q98. What is transient keyword in Java?

Ans.

The transient keyword in Java is used to indicate that a variable should not be serialized.

  • Variables marked as transient are not included in the serialization process

  • Transient variables are not saved when an object is serialized and are set to their default values when the object is deserialized

  • Useful for excluding sensitive or unnecessary data from being serialized

Q99. 18. Can class be private?

Ans.

Yes, a class can be private in Java.

  • A private class can only be accessed within the same outer class.

  • It cannot be accessed from outside the class or even from subclasses.

  • Private classes are often used for implementation details that should not be exposed to other classes.

  • Example: private class Node in a LinkedList implementation.

Q100. 4. What is Polymorphism?

Ans.

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

  • Polymorphism allows objects of different classes to be treated as if they are of the same class.

  • It can be achieved through method overloading and method overriding.

  • Example: A parent class Animal can have child classes like Dog, Cat, and Cow. All these child classes can have their own implementation of the method 'makeSound', but they can be called using the same method name from the parent class.

  • Polymorphism helps...read more

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

Interview experiences of popular companies

3.7
 • 10.4k Interviews
3.8
 • 8.2k Interviews
3.6
 • 7.6k Interviews
3.7
 • 5.6k Interviews
3.7
 • 5.6k Interviews
3.7
 • 4.8k Interviews
3.5
 • 3.8k Interviews
3.5
 • 3.8k Interviews
3.8
 • 2.9k Interviews
4.0
 • 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

Recently Viewed
DESIGNATION
SALARIES
GlobalLogic
INTERVIEWS
Infosys
No Interviews
DESIGNATION
DESIGNATION
INTERVIEWS
Infosys
No Interviews
INTERVIEWS
Infosys
No Interviews
DESIGNATION
INTERVIEWS
Infosys
No Interviews
SALARIES
Deloitte
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