Java Developer
200+ Java Developer Interview Questions and Answers for Freshers
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
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.
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
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.
Q3. Do you have any Java certficate? If no then please leave you are rejected.
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.
Q4. How to change a div background colour?
To change a div background color in Java, use the setBackground() method and pass the desired color as a parameter.
Create a reference to the div element using its id or class
Use the setBackground() method to set the background color
Pass the desired color as a parameter to the setBackground() method
Q5. 13. Other than inheritance what you will use to connect two classes?
We can use composition, aggregation, or association to connect two classes.
Composition is a strong form of aggregation where the lifetime of the contained object is controlled by the container object.
Aggregation is a weaker form of composition where the contained object has an independent lifecycle.
Association is a relationship between two classes where one class uses the functionality of another class.
Examples of association are dependency, inheritance, and realization.
Examp...read more
Q6. 9. When is memory allocated, when class is created or instance is created or when it is?
Memory is allocated when an instance of a class is created.
Memory allocation happens when an object is created using the 'new' keyword.
Static variables are allocated memory when the class is loaded.
Memory is released when the object is no longer referenced or when the program terminates.
Share interview questions and help millions of jobseekers 🌟
Q7. What is the difference between Local Variable, static variable, and instance variable?
Local, static, and instance variables differ in their scope and lifetime.
Local variables are declared inside a method and have a limited scope.
Static variables belong to the class and are shared among all instances.
Instance variables are unique to each object and can be accessed using the object reference.
Q8. How do we call stored procedures in java ?
Stored procedures can be called in Java using JDBC API.
Create a CallableStatement object using Connection.prepareCall() method.
Set the input parameters using setXXX() methods.
Execute the stored procedure using execute() or executeUpdate() method.
Retrieve the output parameters using getXXX() methods.
Close the CallableStatement object using close() method.
Java Developer Jobs
Q9. What is session in java?
Session in Java is a way to store information about a user across multiple requests.
Session is used to maintain stateful information about a user.
It is created when a user first accesses a web application and remains active until the user logs out or the session times out.
Session data is stored on the server and can be accessed by multiple requests from the same user.
It is commonly used for user authentication, shopping carts, and personalization.
The HttpSession interface in ...read more
Q10. what are the implicit objects in Jsp servlets ?
Implicit objects in JSP servlets are pre-defined objects that can be accessed without any declaration.
Implicit objects are created by the container and are available to the JSP page.
Some of the implicit objects are request, response, session, application, out, pageContext, config, exception, etc.
These objects can be used to perform various operations like accessing request parameters, setting attributes, forwarding requests, etc.
Q11. What is java? What is oops? Difference between hashmap and hashset
Java is a high-level programming language known for its portability and object-oriented programming support. OOPs stands for Object-Oriented Programming. HashMap and HashSet are both data structures in Java, but HashMap is used to store key-value pairs while HashSet is used to store unique elements.
Java is a high-level programming language used for developing applications and software.
OOPs (Object-Oriented Programming) is a programming paradigm based on the concept of objects...read more
Q12. 1.Difference between HashMap and Hashtable ?
HashMap is not synchronized and allows null values, while Hashtable is synchronized and does not allow null values.
HashMap is faster than Hashtable due to lack of synchronization.
Hashtable is thread-safe while HashMap is not.
HashMap allows null values for both key and value, while Hashtable does not.
HashMap is part of the Java Collections Framework while Hashtable is a legacy class.
HashMap uses Iterator while Hashtable uses Enumeration.
Example: HashMap
map = new HashMap<>(); ...read more
Q13. In Tr what is exception handling.oops, difference between abstraction and encapsulation about project.
The question is about exception handling, OOPs, and the difference between abstraction and encapsulation in a project.
Exception handling is a mechanism to handle runtime errors and prevent program termination.
Abstraction is the process of hiding implementation details and showing only necessary information to the user.
Encapsulation is the process of binding data and methods together in a single unit.
In a project, abstraction can be achieved by using interfaces and abstract cl...read more
Q14. What is an object in java ?
An object in Java is an instance of a class that encapsulates data and behavior.
Objects have state and behavior
They are created from classes
They can be used to represent real-world entities or concepts
Objects can interact with each other through method calls
Q15. What is the differnce between Interface and Class?
Interface defines a contract for behavior while class is an implementation of behavior.
Interface only contains method signatures while class contains method implementations.
A class can only extend one class but can implement multiple interfaces.
Interfaces are used for achieving abstraction and loose coupling.
Classes are used for encapsulation and code reusability.
Example: Comparable interface defines a contract for objects that can be compared while String class implements th...read more
Q16. Can we use try block without catch block?
Yes, we can use try block without catch block to handle exceptions using finally block.
Try block can be used without catch block if there is a finally block to handle exceptions.
Finally block is executed whether an exception is thrown or not.
Example: try { // code that may throw exception } finally { // code to be executed regardless of exception }
Q17. 14. What are different types of Access Modifiers in Java?
Access modifiers in Java are used to set the accessibility of classes, methods, and variables.
There are four types of access modifiers in Java: public, private, protected, and default.
Public: accessible from anywhere in the program.
Private: accessible only within the same class.
Protected: accessible within the same class, same package, and subclasses.
Default: accessible within the same package only.
Q18. Creating multiple methods in a class with a same name and different arguments. The argument must be different type and length
Method overloading allows creating multiple methods with the same name but different arguments.
Method signature must differ in number, type, or order of parameters
Return type can be different but not the only difference
Example: public void print(int num), public void print(String str), public void print(int[] arr)
Overloading improves code readability and reusability
Q19. 3. What is difference between Abstract class and Interface.
Abstract class can have implementation while interface cannot. A class can implement multiple interfaces but can only extend one abstract class.
Abstract class can have constructors while interface cannot.
Abstract class can have instance variables while interface cannot.
Abstract class can have non-abstract methods while interface can only have abstract methods.
A class can implement multiple interfaces but can only extend one abstract class.
Abstract classes are used when there ...read more
Q20. 5. What is difference between composition and inheritance
Composition is a way to combine objects to create complex structures, while inheritance is a way to create new classes based on existing ones.
Composition is a 'has-a' relationship, where an object contains other objects as its parts.
Inheritance is an 'is-a' relationship, where a subclass inherits properties and behaviors from its superclass.
Composition allows for greater flexibility and modularity in design, as objects can be easily swapped out or added.
Inheritance can lead t...read more
Q21. 20. What are different types of collections?
Collections are used to store and manipulate groups of objects in Java. There are three main types of collections.
The three main types of collections are List, Set, and Map.
List is an ordered collection that allows duplicates. Example: ArrayList.
Set is an unordered collection that does not allow duplicates. Example: HashSet.
Map is a collection that maps keys to values and does not allow duplicate keys. Example: HashMap.
Q22. What is super keyword ? What is inheritance ? What is abstract and why it is used? And many more questions was asked......?
Answers to common Java interview questions on super keyword, inheritance, and abstract classes.
The super keyword is used to call a method or constructor in the parent class.
Inheritance is a mechanism in which one class acquires the properties and methods of another class.
Abstract classes are classes that cannot be instantiated and are used as a base for other classes to inherit from.
Abstract methods are methods that do not have a body and must be implemented by the subclass.
P...read more
Q23. What is the difference between HashMap and HashSet?
HashMap is a key-value pair collection while HashSet is a set of unique elements.
HashMap allows duplicate values but not duplicate keys
HashSet does not allow duplicate values
HashMap implements Map interface while HashSet implements Set interface
HashMap uses put() method to add elements while HashSet uses add() method
Example: HashMap
map = new HashMap<>(); HashSet set = new HashSet<>();
Q24. 17. Can subclass inherit the properties of Parent class if we not load parent class
No, subclass cannot inherit properties of parent class if parent class is not loaded.
In order for a subclass to inherit properties of a parent class, the parent class must be loaded.
If the parent class is not loaded, the subclass will not have access to its properties.
This is because inheritance is a compile-time concept and requires the parent class to be present during compilation.
Attempting to access properties of an unloaded parent class will result in a compilation error...read more
Q25. 7. What is difference between throw and throws
throw is used to explicitly throw an exception while throws is used to declare an exception that a method may throw.
throw is used within a method to throw an exception when a certain condition is met
throws is used in the method signature to declare the exceptions that the method may throw
throw is followed by an instance of an exception class while throws is followed by the name of the exception class
throw is used to handle exceptions within a method while throws is used to pr...read more
Q26. 2. How to define Thread in java?
Thread in Java is a lightweight sub-process that executes a set of instructions independently.
Thread can be defined by extending the Thread class or implementing the Runnable interface.
Thread class provides various methods to control the execution of threads like start(), sleep(), join(), etc.
Runnable interface has only one method run() which needs to be implemented.
Threads can be created and started using the start() method.
Example: public class MyThread extends Thread { pub...read more
Q27. Please tell me What is HTML?
HTML stands for HyperText Markup Language. It is the standard markup language for creating web pages.
HTML is used to structure content on the web.
It uses tags to define elements such as headings, paragraphs, links, images, etc.
HTML documents are interpreted by web browsers to display the content.
Example:
This is a heading
Q28. What is hibernate and how we can use it with springboot?
Hibernate is an ORM framework that simplifies database operations in Java applications. It can be integrated with Spring Boot for data persistence.
Hibernate is an Object-Relational Mapping (ORM) framework that maps Java objects to database tables and vice versa.
It simplifies database operations by providing a higher-level abstraction for data persistence.
Spring Boot can be integrated with Hibernate to easily manage database transactions and entities in a Spring application.
By...read more
Q29. What is Java Class Library?
Java Class Library is a collection of pre-compiled classes and methods that provide ready-to-use functionality for Java developers.
Java Class Library contains classes for common tasks like input/output, networking, database access, etc.
Developers can use these classes to save time and effort by reusing existing code.
Examples include java.lang, java.util, java.io, java.net, etc.
Q30. 16. Can subclass access private methods parent class?
No, subclass cannot access private methods of parent class.
Private methods are only accessible within the same class where they are defined.
Subclasses can access protected and public methods of parent class.
Private methods can be accessed indirectly through public or protected methods of parent class.
Q31. 10. What is stack in programming?
A stack is a data structure that follows the Last In First Out (LIFO) principle.
Elements are added and removed from the top of the stack.
Push() adds an element to the top of the stack.
Pop() removes the top element from the stack.
Peek() returns the top element without removing it.
Used in programming for function calls, expression evaluation, and memory management.
Q32. 11. What is queue in programming?
A queue is a data structure that follows the FIFO (First In First Out) principle.
Elements are added to the back of the queue and removed from the front.
Common operations include enqueue (add to back) and dequeue (remove from front).
Examples include a line of people waiting for a movie or a printer queue.
Java provides the Queue interface and its implementations like LinkedList and PriorityQueue.
Q33. What is linked lists,what is multiple inheritance,what is compiler and interpreter and where they both are used
Linked lists are data structures where each element points to the next element. Multiple inheritance is when a class inherits from more than one parent class. A compiler translates source code into machine code, while an interpreter executes code line by line.
Linked lists are used to store data in a linear sequence, with each element pointing to the next one.
Multiple inheritance allows a class to inherit attributes and methods from more than one parent class.
A compiler transl...read more
Q34. 5. How do you connect database with Java ?
Java provides JDBC API to connect with databases.
Load the JDBC driver class
Create a connection object
Create a statement object
Execute the query
Process the result set
Q35. 2. Give me Real life example of Polymorphism
Polymorphism is when an object can take on multiple forms and behave differently based on the context.
A car can be a vehicle, but it can also be a sports car or a family car, each with different behaviors.
A shape can be a square, rectangle, or triangle, each with different methods for calculating area and perimeter.
A media player can play different types of media, such as audio or video, each with different playback controls.
Q36. 6. What is Checked and Unchecked exception?
Checked exceptions are checked at compile-time while unchecked exceptions are not checked at compile-time.
Checked exceptions are those which are checked at compile-time and the programmer is forced to handle them using try-catch or throws keyword.
Unchecked exceptions are those which are not checked at compile-time and the programmer is not forced to handle them.
Examples of checked exceptions are IOException, ClassNotFoundException, SQLException, etc.
Examples of unchecked exce...read more
Q37. What is waperclass in java ?
There is no such thing as waperclass in Java.
The term 'waperclass' is not a valid Java keyword or class name.
It is possible that the interviewer made a mistake or was testing the candidate's ability to handle unexpected questions.
As a Java developer, it is important to be familiar with the language's syntax and keywords.
Q38. 1. What is collection ?
A collection is a framework that provides an architecture to store and manipulate a group of objects.
Collections are used to store, retrieve, manipulate, and communicate data between objects.
They provide various data structures like lists, sets, and maps.
Collections offer methods to add, remove, and search for elements in the collection.
Examples of collections in Java include ArrayList, HashSet, and HashMap.
Q39. What is Type casting in java ?
Type casting in Java is the process of converting one data type into another.
Type casting can be done implicitly or explicitly.
Implicit type casting is done automatically by the compiler when there is no loss of data.
Explicit type casting is done manually by the programmer when there is a possibility of data loss.
Type casting is useful when we want to use a variable of one data type in an expression or assignment of another data type.
Example: int num = 10; double decimalNum =...read more
Q40. write a program for christmas tree with 8 and 4 stars?
Program to print Christmas tree with 8 and 4 stars
Use loops to print the tree structure
For 8 stars tree, use 4 rows and for 4 stars tree, use 3 rows
Print spaces before and after the stars to align them properly
Q41. given two integer array. find consecutive sub arrays and return sub array which has sum >6
Find consecutive subarrays in two integer arrays with sum > 6.
Iterate through both arrays to find consecutive subarrays.
Calculate the sum of each subarray and check if it is greater than 6.
Return the subarray with sum > 6.
Q42. What are the different types of status code ?
HTTP status codes indicate the outcome of a HTTP request. They are categorized into 5 classes.
1xx: Informational - Request received, continuing process
2xx: Success - Request successfully received, understood, and accepted
3xx: Redirection - Further action needed to complete the request
4xx: Client Error - Request contains bad syntax or cannot be fulfilled
5xx: Server Error - Server failed to fulfill a valid request
Q43. 12. Difference between error and exception
Errors are unrecoverable, while exceptions are recoverable.
Errors are caused by the environment in which the application is running, while exceptions are caused by the application itself.
Errors cannot be handled by the application, while exceptions can be handled using try-catch blocks.
Examples of errors include OutOfMemoryError and StackOverflowError, while examples of exceptions include NullPointerException and ArrayIndexOutOfBoundsException.
Q44. 4. Can we have multi level inheritance in Java
Yes, Java supports multi level inheritance.
Multi level inheritance is the process of inheriting a class from another derived class.
It helps in creating a hierarchy of classes with each level inheriting properties from the previous level.
For example, class C can inherit from class B, which in turn inherits from class A.
Java does not support multiple inheritance, where a class can inherit from multiple classes at the same level.
Q45. How many teams involved in spiral model?
The number of teams involved in spiral model varies based on the project requirements.
Spiral model is a flexible model that allows for multiple teams to work on different phases simultaneously.
The number of teams involved can range from one to many depending on the size and complexity of the project.
Each team is responsible for a specific phase of the project, such as planning, design, implementation, and testing.
For example, a large software development project may involve m...read more
Q46. Types of datatypes in Java
Java has several types of datatypes including primitive and reference types.
Primitive datatypes include int, double, boolean, char, etc.
Reference datatypes include classes, interfaces, arrays, etc.
Examples: int age = 25; String name = "John"; int[] numbers = {1, 2, 3};
Wrapper classes like Integer, Double, Boolean, etc. are used to wrap primitive types.
Q47. Write a program with Java 8 to filter the employees based on salary greater than 10,000 from a list of Employee objects.
Program to filter employees with salary > 10,000 using Java 8.
Use Java 8 Stream API to filter employees based on salary.
Create a Predicate to check if salary is greater than 10,000.
Use filter() method to apply the Predicate on the list of Employee objects.
Q48. Write a program to find the pairs of the number which give sum=8
Program to find pairs of numbers that sum up to 8
Use a nested loop to iterate through the array and find pairs
Check if the sum of the pair is equal to 8
Store and display the pairs that satisfy the condition
Q49. 1. What are four pillars of OOPS ?
The four pillars of OOPS are Abstraction, Encapsulation, Inheritance, and Polymorphism.
Abstraction: Hiding implementation details and showing only necessary information.
Encapsulation: Binding data and methods together to protect data from outside interference.
Inheritance: Creating new classes from existing ones, inheriting properties and methods.
Polymorphism: Ability of objects to take on multiple forms or behaviors.
Q50. Explain Security management, how to enable security for APIs.
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
Interview Questions of Similar Designations
Top Interview Questions for Java Developer Related Skills
Interview experiences of popular companies
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
Reviews
Interviews
Salaries
Users/Month