Top 250 Java Interview Questions and Answers

Updated 11 Mar 2025

Q201. What is precondition

Ans.

A precondition is a condition that must be true before a function or method can be executed.

  • Preconditions are used to ensure that the inputs to a function or method are valid.

  • They help prevent errors and unexpected behavior by checking if certain conditions are met.

  • If a precondition is not satisfied, an exception or error may be thrown.

  • For example, a precondition for a function that calculates the square root of a number could be that the number must be positive.

View 1 answer
right arrow
Frequently asked in

Q202. Why dagger is required?

Ans.

Dagger is required for dependency injection in Android development.

  • Dagger helps in managing dependencies and reduces boilerplate code.

  • It provides compile-time safety and improves code readability.

  • Dagger also helps in testing and modularizing the codebase.

  • It is widely used in Android development for building scalable and maintainable apps.

Add your answer
right arrow

Q203. How to define user defined exceptions ?

Ans.

User defined exceptions can be defined by creating a new class that extends the Exception class.

  • Create a new class that extends the Exception class

  • Add constructors to the class to initialize the exception

  • Throw the exception using the 'throw' keyword

Add your answer
right arrow
Frequently asked in

Q204. Can you write code for printing a star in java

Ans.

Yes, I can write code for printing a star in Java.

  • Create an array of strings to represent the star

  • Use a loop to iterate through each row of the array

  • Use another loop to iterate through each column of the array

  • Print the value at each row and column position

  • Example: String[] star = {" * ", " *** ", "*****", " *** ", " * "};

  • Example: for (String row : star) { System.out.println(row); }

Add your answer
right arrow
Are these interview questions helpful?

Q205. what are the uses of final in java

Ans.

The 'final' keyword in Java is used to declare constants, prevent method overriding, and ensure thread safety.

  • Final variables cannot be reassigned once initialized

  • Final methods cannot be overridden by subclasses

  • Final classes cannot be extended by other classes

  • Final parameters ensure that they cannot be modified within a method

  • Final fields can be used to achieve thread safety

Add your answer
right arrow

Q206. Difference between checked and unchecked exceptions.

Ans.

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

  • Checked exceptions are checked at compile-time and must be handled or declared in the method signature.

  • Unchecked exceptions are not checked at compile-time and do not need to be handled or declared.

  • Checked exceptions are usually used for recoverable errors while unchecked exceptions are used for unrecoverable errors.

  • Examples of checked exceptions include IOException and SQLException while exampl...read more

Add your answer
right arrow
Share interview questions and help millions of jobseekers 🌟

Q207. Difference between wait and sleep

Ans.

wait is used for synchronization between threads while sleep is used to pause the execution of a thread

  • wait is used to wait for a specific condition to occur while sleep is used to pause the execution for a specific amount of time

  • wait releases the lock on the object while sleep does not

  • wait is called on an object while sleep is called on a thread

  • wait can be interrupted by another thread while sleep cannot

Add your answer
right arrow

Q208. What is super class?

Ans.

A super class is a class that is inherited by other classes, providing them with common attributes and behaviors.

  • Super class is also known as a base class or parent class.

  • It serves as a blueprint for creating derived classes.

  • Derived classes inherit the properties and methods of the super class.

  • Super class can be extended or overridden by the derived classes.

  • Super class promotes code reusability and modularity.

  • Example: In a vehicle hierarchy, 'Vehicle' can be a super class wit...read more

View 2 more answers
right arrow
Frequently asked in

Java Jobs

Software Quality Automation Engineer 5-10 years
Intel Technology India Pvt Ltd
4.2
Bangalore / Bengaluru
Manager Embedded Software development 20-25 years
Intel Technology India Pvt Ltd
4.2
Bangalore / Bengaluru
System Software Dev Engineer 8-13 years
Intel Technology India Pvt Ltd
4.2
Bangalore / Bengaluru

Q209. What is @primary annotation and when would you use it

Ans.

The @primary annotation is used to mark a primary key in a database table.

  • It is used in database design to indicate the primary key of a table

  • It is often used in conjunction with other annotations such as @Entity and @Id

  • It can be used to specify the name of the primary key column

  • Example: @Entity @Table(name = "users") public class User { @Id @GeneratedValue @Column(name = "user_id") private Long id; }

Add your answer
right arrow
Frequently asked in

Q210. Do you know about IT skills like java etc.

Ans.

Yes, I have knowledge of IT skills like Java, Python, C++, etc.

  • I have experience in recruiting IT professionals with expertise in Java, Python, C++, etc.

  • I am familiar with the technical requirements and job responsibilities of IT roles that require these skills.

  • I keep myself updated with the latest trends and advancements in the IT industry.

  • I have successfully placed candidates with proficiency in these skills in the past.

  • I am comfortable discussing technical aspects of the j...read more

View 1 answer
right arrow

Q211. What are extends?

Ans.

Extends is a keyword in object-oriented programming that allows a class to inherit properties and methods from another class.

  • Extends is used in inheritance to create a subclass that is a modified version of the parent class.

  • The subclass inherits all the properties and methods of the parent class and can also have its own unique properties and methods.

  • For example, a class Animal can be extended by a subclass Dog, which inherits all the properties and methods of Animal and can ...read more

Add your answer
right arrow
Frequently asked in

Q212. Compare them with java?

Ans.

Python is a dynamically typed language with simpler syntax and better readability than Java.

  • Python has a simpler syntax than Java

  • Python is dynamically typed while Java is statically typed

  • Python has better readability than Java

  • Python has a larger standard library than Java

  • Java is faster than Python in terms of execution speed

Add your answer
right arrow

Q213. When java program is save at that time Which file is create.

Ans.

When a Java program is saved, a file with the same name as the class containing the main method is created.

  • The file extension for Java source code is .java

  • When the Java program is compiled, a bytecode file with the .class extension is created

  • The bytecode file can be executed by the Java Virtual Machine (JVM)

View 1 answer
right arrow

Q214. Who is java Father?

Ans.

James Gosling is considered the father of Java programming language.

  • James Gosling is a Canadian computer scientist who created the Java programming language.

  • He developed Java while working at Sun Microsystems in the mid-1990s.

  • Java was initially designed for interactive television, but it quickly gained popularity for its platform independence and object-oriented features.

  • Gosling's team also developed the original Java compiler and virtual machine.

  • Java has since become one of ...read more

View 1 answer
right arrow
Frequently asked in

Q215. What is Java core

Ans.

Java core refers to the fundamental components of the Java programming language.

  • Includes basic syntax, data types, control structures, and object-oriented programming concepts

  • Provides the foundation for building Java applications

  • Examples include classes like String, Integer, and Boolean

Add your answer
right arrow

Q216. What is Java language and where we used ?

Ans.

Java is a high-level programming language used for developing applications and software.

  • Java is object-oriented and platform-independent.

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

  • Java is widely used in enterprise applications, such as banking and finance.

  • It is also used in developing Android applications.

  • Java is known for its security features and robustness.

Add your answer
right arrow
Frequently asked in

Q217. how in JAVA

Ans.

Can you explain how to implement inheritance in Java?

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

  • Use the 'extends' keyword to create a subclass that inherits from a superclass

  • The subclass can override methods from the superclass or add new methods

  • Access modifiers can be used to control the visibility of inherited members

Add your answer
right arrow
Frequently asked in

Q218. when will get bean not found exception

Ans.

Bean not found exception occurs when a requested bean is not present in the container.

  • Occurs during runtime when a bean is not defined in the application context

  • Can be caused by typos in bean names or incorrect configuration

  • Can be resolved by defining the missing bean or correcting the configuration

Add your answer
right arrow

Q219. what static block

Ans.

Static block is a block of code that is executed when a class is loaded into memory.

  • Static block is declared using the 'static' keyword.

  • It is executed only once when the class is loaded.

  • It is used to initialize static variables or perform any other static initialization.

  • It can throw exceptions which need to be handled.

  • Multiple static blocks can be defined in a class and they are executed in the order they are defined.

Add your answer
right arrow

Q220. Rate yourself in Java in the scale of 10

Ans.

I would rate myself 8 out of 10 in Java.

  • I have a strong understanding of core Java concepts.

  • I am proficient in object-oriented programming and can write clean and efficient code.

  • I have experience in using Java frameworks and libraries like Spring and Hibernate.

  • I am familiar with Java development tools and IDEs like Eclipse and IntelliJ.

  • I have worked on various Java projects and have a good understanding of Java best practices.

Add your answer
right arrow
Frequently asked in

Q221. Why Java? ----answered

Ans.

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

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

  • It has a large and active community, providing support and resources.

  • Java offers extensive libraries and frameworks for various purposes, making development faster and easier.

  • It is widely used in enterprise applications, Android development, and big data processing.

  • Java's object-oriented n...read more

View 1 answer
right arrow
Frequently asked in

Q222. What is the difference between stuff() and substring()

Ans.

stuff() replaces a specified number of characters with another string, while substring() extracts a portion of a string.

  • stuff() is used to replace a specified number of characters in a string with another string.

  • substring() is used to extract a portion of a string based on the starting and ending index.

  • stuff() can be used to mask sensitive data in a string, such as credit card numbers.

  • substring() can be used to extract a specific part of a string, such as the first name in a ...read more

Add your answer
right arrow
Frequently asked in

Q223. What is Java Program?

Ans.

Java Program is a set of instructions written in the Java programming language that can be executed by a computer.

  • Java Program is a collection of classes and methods that define the behavior of a software application.

  • It is written in the Java programming language and can be executed on any platform that has a Java Virtual Machine (JVM).

  • Java Programs are compiled into bytecode, which is then interpreted and executed by the JVM.

  • They can perform various tasks such as data manipu...read more

View 1 answer
right arrow
Frequently asked in

Q224. What is public and instance class

Ans.

Public and instance classes are two types of classes in object-oriented programming.

  • Public classes can be accessed from anywhere in the program, while instance classes can only be accessed within their own instance.

  • Public classes are often used for utility classes or classes that need to be accessed globally, while instance classes are used for encapsulation and data hiding.

  • Example of public class: Math class in Java. Example of instance class: Person class with private insta...read more

Add your answer
right arrow
Frequently asked in

Q225. Why public static use

Ans.

public static use for accessing methods and variables without creating an object

  • Allows access to methods and variables without creating an object

  • Useful for utility classes where objects are not needed

  • Can be used to create global variables or constants

  • Example: Math class in Java has only static methods and constants

Add your answer
right arrow

Q226. Do you feel Java is safe for you?

Ans.

Yes, Java is safe for me.

  • Java is a secure programming language with built-in security features.

  • It has a strong security model that prevents unauthorized access to sensitive data.

  • Java also has a robust community that regularly updates and patches security vulnerabilities.

  • However, like any technology, it is important to stay up-to-date with the latest security best practices.

  • For example, using secure coding practices and keeping software up-to-date with security patches.

Add your answer
right arrow
Frequently asked in

Q227. How to override static

Ans.

Static methods cannot be overridden in Java.

  • Static methods belong to the class itself, not to any instance of the class.

  • They cannot be overridden as they are resolved at compile-time based on the class they are called on.

  • However, it is possible to hide a static method in a subclass by declaring a method with the same name and signature.

  • This is known as method hiding.

View 1 answer
right arrow

Q228. DIFFERENCIATE BETWEEN ABSTRACT CLASS AND INTERFACE?

Ans.

Abstract class can have implementation while interface cannot. A class can implement multiple interfaces but can only inherit from one abstract class.

  • Abstract class can have constructors while interface cannot.

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

  • Abstract class can have instance variables while interface cannot.

  • A class implementing an interface must implement all its methods while a class inheriting from an abstract class...read more

Add your answer
right arrow

Q229. When does a declare block become mandatory

Ans.

A declare block becomes mandatory when a variable needs to be declared before it can be used.

  • A declare block is used in programming languages to define variables and their types.

  • It becomes mandatory when a variable needs to be declared before it can be used in the code.

  • This ensures that the variable is properly initialized and can be accessed correctly.

  • For example, in C programming, a declare block is mandatory at the beginning of a function to declare local variables.

View 1 answer
right arrow

Q230. what is java core java 8

Ans.

Java Core Java 8 is a major release of the Java programming language that introduced new features and enhancements.

  • Java Core Java 8 introduced lambda expressions, which allow for functional programming.

  • It also introduced the Stream API, which provides a more concise and functional way to work with collections.

  • The Optional class was introduced to handle null values more effectively.

  • Java Core Java 8 also included the new Date and Time API, which improved upon the previous java....read more

View 1 answer
right arrow

Q231. choose one, java or c? --- java

Ans.

Java is a popular programming language known for its versatility and compatibility with various platforms.

  • Java is an object-oriented language

  • It is used for developing desktop, web, and mobile applications

  • Java Virtual Machine (JVM) enables cross-platform compatibility

Add your answer
right arrow
Frequently asked in

Q232. Difference between abstarct and interface.

Ans.

Abstract classes are classes that cannot be instantiated and can have both abstract and non-abstract methods. Interfaces are contracts that define the methods that a class must implement.

  • Abstract classes can have constructors and instance variables, while interfaces cannot.

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

  • Abstract classes are used when there is a need for a common base class for multiple related classes, while interfaces ...read more

Add your answer
right arrow

Q233. Difference between int and Int and Integer ?

Ans.

int is a primitive data type, Int is a class in Kotlin, and Integer is a class in Java.

  • int is a primitive data type in Java and Kotlin

  • Int is a class in Kotlin that represents a nullable integer

  • Integer is a class in Java that wraps an int value and provides utility methods

Add your answer
right arrow

Q234. 1. difference between abstract vs interface

Ans.

Abstract classes are classes that cannot be instantiated and can have both abstract and non-abstract methods. Interfaces are contracts that define the methods that a class must implement.

  • Abstract classes can have constructors while interfaces cannot

  • A class can implement multiple interfaces but can only inherit from one abstract class

  • Abstract classes can have instance variables while interfaces cannot

  • Interfaces can have default methods while abstract classes cannot

  • Abstract cla...read more

Add your answer
right arrow

Q235. When do we use inner class? What is the use case of static and non-static inner classes?

Ans.

Inner classes are used for encapsulation and code organization. Static inner classes are used for utility classes.

  • Inner classes are used to group related classes together and improve encapsulation.

  • Non-static inner classes have access to the outer class's fields and methods.

  • Static inner classes are used for utility classes that don't need access to the outer class's fields and methods.

  • Inner classes can also be used for anonymous classes and event listeners.

Add your answer
right arrow
Frequently asked in

Q236. servlet is interface or class ?

Ans.

Servlet is an interface in Java EE used to handle HTTP requests and responses.

  • Servlet interface is implemented by classes like HttpServlet

  • It has methods like init(), service(), and destroy()

  • Servlets are used to create dynamic web pages and web applications

Add your answer
right arrow

Q237. difference berween abstract class and interface

Ans.

Abstract class can have implementation while interface only has method signatures.

  • Abstract class can have constructors while interface cannot.

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

  • Abstract class can have non-abstract methods while interface only has abstract methods.

  • Abstract class is used for code reusability while interface is used for polymorphism.

  • Example of abstract class: Animal with abstract method 'makeSound' and non-abstract m...read more

Add your answer
right arrow

Q238. Difference between String, string buffer and string builder.

Ans.

String is immutable, StringBuffer and StringBuilder are mutable. StringBuffer is thread-safe while StringBuilder is not.

  • String is a final class and its value cannot be changed once created.

  • StringBuffer is synchronized and can be accessed by multiple threads safely.

  • StringBuilder is not synchronized and is faster than StringBuffer.

  • StringBuffer and StringBuilder both can be modified and their length can be increased or decreased.

  • Example: String name = "John"; StringBuffer sb = n...read more

Add your answer
right arrow

Q239. 8. Can we create private Constructor?

Ans.

Yes, we can create private constructors in Java.

  • Private constructors are used to restrict the creation of objects of a class.

  • They can only be accessed within the class.

  • They are commonly used in Singleton design pattern.

  • Private constructors can also be used in utility classes where all methods are static.

Add your answer
right arrow

Q240. can constructor be overridden? ---told

Ans.

Yes, constructor can be overridden.

  • Constructor can be overridden in Java using method overloading.

  • The subclass constructor can call the superclass constructor using super() keyword.

  • Overriding constructor can be used to provide different ways of initializing an object.

Add your answer
right arrow
Frequently asked in

Q241. 8.what is protected modifier.

Ans.

Protected modifier limits access to members within the same package or subclasses.

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

  • It provides a level of encapsulation and security.

  • Example: protected int age; can be accessed within the same package or subclasses.

Add your answer
right arrow
Frequently asked in

Q242. What is difference between abstract and interface?

Ans.

Abstract class is a class that cannot be instantiated, while interface is a contract that a class must implement.

  • 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 inherit from one abstract class.

  • Abstract class can have constructors, while interface cannot.

  • Abstract class can have instance variables, while interface cannot.

  • Abstract class is used to provide a comm...read more

Add your answer
right arrow

Q243. Wht is Java and wht u can you do with that?

Ans.

Java is a high-level programming language used for developing applications and software.

  • Java is platform-independent and can run on any operating system.

  • It is object-oriented and has a vast library of pre-built classes and functions.

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

  • It is also used for developing enterprise-level applications and software.

  • Java is widely used in the development of Android applications.

  • Some popula...read more

Add your answer
right arrow

Q244. What is private constructor and its use

Ans.

A private constructor is a constructor that can only be accessed within the class and not from outside the class.

  • Private constructors are used to prevent the creation of objects of a class from outside the class.

  • They are also used to implement the Singleton design pattern.

  • Private constructors can be used to create utility classes that contain only static methods and cannot be instantiated.

  • Private constructors can also be used to enforce a certain initialization order of stati...read more

Add your answer
right arrow
Frequently asked in

Q245. what is java and all

Ans.

Java is a high-level programming language used for developing applications and software.

  • Java is an object-oriented language

  • It is platform-independent

  • Java code is compiled into bytecode

  • Java Virtual Machine (JVM) is used to run Java programs

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

Add your answer
right arrow

Q246. How to stop the execution of finally code?

Ans.

Finally code cannot be stopped once executed.

  • Finally block is always executed, even if an exception is thrown or caught.

  • There is no way to stop the execution of finally block.

  • To prevent certain code from executing in finally block, use conditional statements.

View 1 answer
right arrow

Q247. How to check java version on server

Ans.

To check Java version on server, use command line interface and run java -version command.

  • Open command prompt or terminal

  • Type java -version and press enter

  • The installed Java version will be displayed

Add your answer
right arrow

Q248. what is string builder?

Ans.

StringBuilder is a class in .NET that allows for efficient manipulation of strings.

  • StringBuilder is mutable, meaning it can be modified without creating a new object.

  • It is useful for concatenating large amounts of text.

  • It has methods for inserting, replacing, and removing characters.

  • Example: StringBuilder sb = new StringBuilder(); sb.Append("Hello"); sb.Append("World");

  • Example: sb.Insert(5, " there"); sb.Replace("World", "Universe"); sb.Remove(11, 5);

Add your answer
right arrow
Frequently asked in,

Q249. What are the action class methods ?

Ans.

Action class methods are used to perform keyboard and mouse actions in Selenium.

  • Action class is part of Selenium's WebDriver API

  • Methods include click(), doubleClick(), contextClick(), dragAndDrop(), etc.

  • Used to simulate user interactions with web elements

  • Can be used for testing complex user interactions like drag and drop, hover, etc.

Add your answer
right arrow

Q250. Define marker interface

Ans.

A marker interface is an interface with no methods used to mark a class as having a particular property or behavior.

  • Marker interfaces are used to provide metadata about a class.

  • They are often used in frameworks and libraries to indicate that a class should be treated in a special way.

  • Examples include Serializable, Cloneable, and Remote interfaces in Java.

  • Marker interfaces can also be used to enforce design patterns, such as the Decorator pattern.

Add your answer
right arrow
Previous
1
2
3
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.1k 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
Recently Viewed
JOBS
Amgen
No Jobs
JOBS
Browse jobs
Discover jobs you love
REVIEWS
Mercer
No Reviews
DESIGNATION
REVIEWS
Kearney
No Reviews
COMPANY BENEFITS
Mercer
No Benefits
SALARIES
Amgen
Java 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
75 Lakh+

Reviews

5 Lakh+

Interviews

4 Crore+

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