Top 100 Java Interview Questions and Answers | Become Expert in Core JAVA

Top 100 Java Interview Questions for Freshers | Core Java Questions | Java Questions For Practice| Java Short Answers | Important Questions Of Java | Viva Questions On Java

1. What is a class in Java?

Answer: Java encapsulates the codes in various classes which define new data types. These new data types are used to create objects.

2. What is a JVM?

Answer: JVM is Java Virtual Machine which is a run time environment for the compiled java class files.

3. What is the right data type to represent a price in Java?

Answer: BigDecimal, if memory is not a concern and Performance, is not critical, otherwise double with predefined precision.

4. Does Java support multiple inheritances?

Answer: Java doesn’t support multiple inheritances.

5. What are the supported platforms by Java Programming Language?  

Answer: Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX/Linux like HP-Unix, Sun Solaris, Red hat Linux, Ubuntu, CentOS, etc.  

6. List any five features of Java?  

Answer: Some features include Object-Oriented  

  • Platform Independent  
  • Robust  
  • Interpreted  
  • Multi-threaded  

7. Explain method overloading?

Answer: When a Java program contains more than one method with the same name but different properties, then it is called method overloading.

8. What restrictions are placed on the location of a package statement within a source code file?

Answer: A package statement must appear as the first line in a source code file (eliminating blank lines and comments).

9. What method is used to specify a container’s layout?

Answer: The setLayout() method is used to specify a container’s layout.

10. What is the immediate superclass of the Applet class?

Answer: The Panel class is the immediate superclass of the Applet class.

11. What are the access modifiers in Java?

Answer: There are 3 access modifiers. Public, protected, and private, and the default one if no identifier is specified is called friendly, but the programmer cannot specify the friendly identifier explicitly.

12. What is are packages?

Answer: A package is a collection of related classes and interfaces providing access protection and namespace management.

13. What is meant by Inheritance and What are its advantages?

Answer: Inheritance is the process of inheriting all the features from a class. The advantages of inheritance are reusability of code and accessibility of variables and methods of the super class by subclasses.

14. Can we rethrow the same exception from the catch handler?

Answer: Yes, we can rethrow the same exception from our catch handler. If we want to rethrow checked exception from a catch block we need to declare that exception.

15. What value is a variable of the String type automatically initialized? Answer: The default value of a String type is null.

16. When a thread blocks on I/O, what state does it enter?

Answer: When it blocks on I/O, A thread enters the waiting state.

17. Which containers use a Flow Layout as their default layout?

Answer: The Panel and Applet classes use the Flow Layout as their default layout.

18. Explain Java Coding Standards for Constants ? Answer:

Constants in java are created using static and final keywords.

Constants contain only uppercase letters.

If the constant name is a combination of two words it should be separated by an underscore.

Constant names are usually nouns.


What is synchronization and why is it important?

Answer: The term synchronization is the ability to control the access of multiple threads to shared resources. And it is important because, without it, it is not possible for one thread to modify a shared object while another thread is in the process of using or updating that object’s value. This often leads to major errors.

Explain Java Coding Standards for variables? Answer:

Variable names should start with small letters.

Variable names should be nouns

Short meaningful names are recommended.

If there are multiple words every inner world should start with Uppercase character.

Ex : string,value,empName,MEP salary

What is an abstract class?

Answer: An abstract class is a class designed with implementation gaps for subclasses to fill in and is deliberately incomplete.

Name three Component subclasses that support painting?

Answer: The Canvas, Frame, Panel, and Applet classes support painting.

What is the difference between JDK and JVM?

Answer: Full-Form: Java Development Kit Full Form: Java Virtual Machine

For Development Purpose

To execute the java programs

It provides all the tools, executables and binaries required to compile, debug and execute a Java Program The execution part is handled by JVM to provide machine independence.

Why Java doesn’t support multiple inheritances?

Answer: Because of “Diamond Problem”, Java doesn’t support multiple inheritances in classes.

What modifiers may be used with an inner class that is a member of an outer class?

Answer: A (non-local) inner class may be declared as public, protected, private, static, final, or abstract.

Which java. util classes and interfaces support event handling?

Answer: The Event Object class and the Event Listener interface support event processing.

What is a transient variable?

Answer: A transient variable is a variable that may not be serialized.

Is null a keyword?

Answer: No, the null is not a keyword.

What is an applet?

Answer: Applet is a dynamic and interactive program that runs inside a web page displayed by a java capablebrowser

What is the lifecycle of an applet?


init() method – Can be called when an applet is first loaded start() method – Can be called each time an applet is started. paint() method – Can be called when the applet is minimized or maximized. stop() method – Can be used when the browser moves off the applet’s page. destroy() method – Can be called when the browser is finished with the applet.

What’s new with the stop(), suspend() and resume() methods in JDK 1.2 ?


These methods have been deprecated in JDK 1.2.

What is the Vector class?

Answer: The term Vector class provides the ability to implement a growable array of objects.

What is the difference between the >> and >>> operators?

Answer: The >> operator carries the sign bit when shifting right while the >>> zero-fills bits that have been shifted out.

What is the difference between this() and super()?

Answer: this() can be used to invoke a constructor of the same class whereas super() can be used to invoke a super class constructor.

What is a native method?

Answer: A native method is a method that is applied in a language other than Java.

What value does read Line() return when it has reached the end of a file?

Answer:The readLine() method returns null when it has reached the end of a file.

What is the Java API?

Answer: The Java API is a large collection of ready-made software components that provide many usefulcapabilities, such as graphical user interface (GUI) widgets.

Why there are no global variables in Java?

Answer: Global variables are globally accessible. Java does not support globally accessible variables due to following reasons:

The global variables breaks the referential transparency Global variables creates collisions in namespace.

What are different types of access modifiers?

Answer: public: Any thing declared as public can be accessed from anywhere. private: Any thing declared asprivate can’t be seen outside of its class. protected: Any thing declared as protected can be accessedby classes in the same package and subclasses in the other packages. default modifier : Can beaccessed only to classes in the same package.

What is Constructor?

Answer: A constructor is a special method whose task is to initialize the object of its class.

It is special because its name is the same as the class name.

They do not have return types, not even void and therefore they cannot return values.

They cannot be inherited, though a derived class can call the base class constructor. Constructor is invoked whenever an object of its associated class is created.

What is an Iterator ?

Answer: The Iterator interface is used to step through the elements of a Collection.

Iterators let you process each element of a Collection.

Iterators are a generic way to go through all the elements of a Collection no matter Define How it is organized.

Iterator is an Interface implemented a different way for every Collection.

What is the difference between Reader/Writer and InputStream/Output Stream?

Answer: The Reader/Writer class is character-oriented and the InputStream/OutputStream class is byteoriented.

What is servlet?

Answer: Servlets are modules that extend request/response-oriented servers, such as java-enabled webservers. For example, a servlet might be responsible for taking data in an HTML order-entry form and applying the business logic used to update a company’s order database.

What is clipping?

Answer: Clipping is the process of confining paint operations to a limited area or shape.

What is memory leak?

A memory leak is where an unreferenced object that will never be used again still hangs around in memory and doesnt get garbage collected.

Can a for statement loop indefinitely?

Answer: Yes, a for statement can loop indefinitely. For example, consider the following: for(;;)

Explain Java Coding standards for Methods? Answer:

Method names should start with small letters.

Method names are usually verbs

If a method contains multiple words, every inner word should start with an uppercase letter.

Ex : toString()

Method name must be combination of verb and noun Ex : getCarName(),getCarNumber()

Why Java is not a pure Object Oriented language?

Answer: Java supports primitive types such as int, byte, short, long, etc that why it is not said to be a pure object-oriented language.

What are the access modifiers?

Answer: Java provides three access controls such as public, private and protected access modifier. When none of these are used, it’s called default access modifier.

Can we overload the main method?


Yes, we can overload the main method with syntax as public static void main(String args[]).

What is the method in java?

Answer: It contains the executable body that can be applied to the specific object of the class.

The method includes method name, parameters or arguments and return type and a body of executable code.

Syntax : type methodName(Argument List){

ex: public float add(int a, int b, int c) methods can have multiple arguments. Separate with commas when we have multiple arguments. thrown in the method are instances of their subclass.

Explain about Automatic type conversion in java?


Java automatic type conversion is done if the following conditions are met:

When two types are compatible

Ex: int, float int can be assigned directly to float variable.

Destination type is larger than source type. Ex: int, long.

Int can be assigned directly to long .Automatic type conversion takes place if int is assigned to long because long is larger datatype than int.

Widening Conversion comes under Automatic type conversion.

What is the difference between the prefix and postfix forms of the ++ operator?

Answer: The prefix form performs the increment operation and returns the value of the increment operation. The postfix form returns the current value all of the expression and then performs the increment operation on that value.

In how many ways we can do exception handling in java? Answer:

We can handle exceptions in either of the two ways :

By specifying a try-catch block where we can catch the exception.

Declaring a method with throws clause.

What does null mean in java?


When a reference variable doesn’t point to any value it is assigned null.

Example: Employee employee;

In the above example employee object is not instantiated so it is pointed nowhere.

Can we define a package statement after the import statement in java?

Answer: We can’t define a package statement after the import statement in java. a package statement must be the first statement in the source file. We can have commented before the package statement.

Define How many objects are created in the following piece of code? MyClass c1, c2, c3; c1 = new MyClass (); c3 = new MyClass ();

Answer: Only 2 objects are created, c1 and c3. The reference c2 is only declared and not initialized.

What is JSP?

Answer: JSP is a technology that returns dynamic content to the Web client using HTML, XML, and JAVAelements. JSP page looks like an HTML page but is a servlet. It contains the Presentation logic and business logic of a web application.

What is the purpose of the apache tomcat?

Answer: Apache server is a standalone server that is used to test servlets and create JSP pages. It is free and open-source that is integrated into the Apache webserver. It is a fast, reliable server to configure the applications but it is hard to install. It is a servlet container that includes tools to configure and manage the server to run the applications. It can also be configured by editing XML configuration files.

Explain where variables are created in memory?

Answer: When we declare variables are created in the stack. So when the variable is out of scope those variables get garbage collected. 

Can we use a catch statement for checked exceptions?

Answer: If there is no chance of raising an exception in our code then we can’t declare a catch block for handling checked exceptions. This raises a compile-time error if we try to handle checked exceptions when there is no possibility of causing an exception.

Explain a situation where finally block will not be executed?

Answer: Finally, the block will not be executed whenever JVM shutdowns. If we use system.exit(0) in try statement finally block if present will not be executed.

63. What is UNICODE?

Answer: Unicode is used for the internal representation of characters and strings and it uses 16 bits to represent each other.

Explain the main() method in java?

Answer: The main () method is the starting point of execution for all java applications. public static void main(String[] args) {}

String args[] are an array of string objects we need to pass from command line arguments. Every Java application must have at least one main method.

Define How destructors are defined in Java?

Answer: In Java, there are no destructors defined in the class as there is no need to do so. Java has its owngarbage collection mechanism which does the job automatically by destroying the objects when no longer referenced

What will be the output of Round(3.7) and Ceil(3.7)?

Answer: Round(3.7) returns 4 and Ceil(3.7) returns 4.

What is a constructor in java?

Answer: A constructor is a special method used to initialize objects in java.

we use constructors to initialize all variables in the class when an object is created. As and when an object is created it is initialized automatically with the help of a constructor in java.

We have two types of constructors

Default Constructor

Parameterized Constructor

How can we find the actual size of an object on the heap?

Answer: In Java, there is no way to find out the actual size of an object on the heap.

Can a variable be local and static at the same time?

Answer: No a variable can’t be static as well as local at the same time. Defining a local variable as static gives a compilation error.

Can we have static methods in an Interface?

Answer: Static methods can’t be overridden in any class while any methods in an interface are by default abstract and are supposed to be implemented in the classes being implemented in the interface. So it makes no sense to have static methods in an interface in Java.

In how many ways we can do synchronization in java? Answer:

There are two ways to do synchronization in java:

Synchronized methods

Synchronized blocks

To do synchronization we use the synchronized keyword.

When do we use synchronized blocks and advantages of using synchronized blocks?

Answer: If very few lines of code require synchronization then it is recommended to use synchronized blocks. The main advantage of synchronized blocks over synchronized methods is it reduces the waiting time of threads and improve the performance of the system.

What is the difference between access specifiers and access modifiers in java?

Answer: In C++ we have access specifiers as public, private, protected, and default and access modifiers as static, final. But there is no such division of access specifiers and access modifiers in java. In Java, we have access to modifiers and non-access modifiers. Access Modifiers: public, private, protected, default Non Access Modifiers: abstract, final, strip.

Define How objects are stored in Java?

Answer: In java, each object when created gets memory space from a heap. When an object is destroyed by a garbage collector, the space allocated to it from the heap is re-allocated to the heap and becomes available for any new objects.

What access modifiers can be used for class?

Answer: We can use only two access modifiers for class public and default.

public: A class with a public modifier can be visible

In the same class

In the same package subclass

In the same package nonsubclass 4) In the different package subclass 5) In the different package nonsubclass. default: A class with default modifier can be accessed

In the same class

In the same package subclass

In the same package nonsubclass

In the different package subclass

In the different package nonsubclass. ( )

  1. 76. Explain about abstract classes in java?

Answer: Sometimes we may come across a situation where we cannot provide implementation to all the methods in a class. We want to leave the implementation to a class that extends it. In such a case, we declare a class as abstract. To make a class abstract we use keyword abstract. Any class that contains one or more abstract methods is declared as abstract. If we don’t declare a class as abstract which contains abstract

methods we get a compile-time error. We get the following error. “The type must be an abstract class to define abstract methods.” Signature; abstract class. 

For example, if we take a vehicle class we cannot provide implementation to it because there may be two-wheelers, four-wheelers, etc. At that moment we make a vehicle class abstract. All the common features of vehicles are declared as abstract methods in vehicle class. Any class which extends the vehicle will provide its method implementation. It’s the responsibility of the subclass to provide the implementation.

The important features of abstract classes are:

Abstract classes cannot be instantiated.

An abstract class contains abstract methods, concrete methods, or both.

Any class which extends abstract class must override all methods of an abstract class. 4) An abstract class can contain either 0 or more abstract methods.

Though we cannot instantiate abstract classes we can create object references. Through superclass references, we can point to subclass.

Can we create a constructor in an abstract class?

Answer: We can create a constructor in the abstract class, it doesn’t give any compilation error. But when we cannot instantiate class there is no use in creating a constructor for abstract class.

String and StringBuffer both represent String objects. Can we compare String and StringBuffer in Java?

Answer: Although String and StringBuffer both represent String objects, we can’t compare them with each other and if we try to compare them, we get an error.

In how many ways we can create threads in java? Answer:

We can create threads in java by any of the two ways :

By extending Thread class

By implementing the Runnable interface.

Explain creating threads by implementing Runnable class?

Answer: This is the first and foremost way to create threads. By implementing the runnable interface and implementing the run() method we can create a new thread.

Method signature : public void run()

Run is the starting point for execution for another thread within our program.

Example: public class MyClass implements Runnable {


public void run()

When do we use synchronized methods in java?

Answer: If multiple threads try to access a method where the method can manipulate the state of the object, in such a scenario we can declare a method as synchronized.

Can we cast any other type to Boolean Type with typecasting?

Answer: No, we can neither cast any other primitive type to Boolean data type nor can cast Boolean data type to any other primitive data type.

What are synchronized methods and synchronized statements?

Answer: Synchronized methods are methods that are used to control access to an object. A synchronized statement can only be executed after a thread has acquired the lock for the object or class referenced in the synchronized statement.

Explain the importance of finally block in java?

Answer: Finally block is used for cleaning up of resources such as closing connections, sockets, etc. if try block executes with no exceptions then finally is called after try block without executing catch block. If there is an exception thrown in try block finally block executes immediately after the catch block. If an exception is thrown, finally block will be executed even if the no catch block handles the exception.

Can we catch more than one exception in a single catch block?

Answer: From Java 7, we can catch more than one exception with a single catch block. This type of handling reduces code duplication.

Note: When we catch more than one exception in a single catch block, the catch parameter is implicitly final.

We cannot assign any value to the catch parameter.

Ex : catch(ArrayIndexOutOfBoundsException || ArithmeticException e)

In the above example, e is final we cannot assign any value or modify e in the catch statement.

What are abstract methods in java?

Answer: An abstract method is a method that doesn’t have anybody. An abstract method is declared with keyword abstract and semicolon in place of the method body.

Signature : public abstract void ();

Ex : public abstract void get details();

It is the responsibility of the subclass to provide implementation to an abstract method defined in the abstract class.

State some situations where exceptions may arise in java? Answer:

Accessing an element that does not exist in the array.

Invalid conversion of number to string and string to a number.


The invalid casting of class

(Class cast Exception)

Trying to create an object for interface or abstract class

(Instantiation Exception)

What is an exception in java?


In java, an exception is an object. Exceptions are created when abnormal situations arise in our program. Exceptions can be created by JVM or by our application code. All Exception classes are defined in java.lang. In other words, we can say Exception as a run time error.

What is an error in Java?

Answer: Error is the subclass of Throwable class in java. When errors are caused by our program we call that as Exception, but some times exceptions are caused due to some environmental issues such as running out of memory. In such cases, we can’t handle the exceptions. Exceptions which cannot be recovered are called as errors in java. Ex: Out of memory issues.

What are the advantages of Exception handling in java?


Separating normal code from exception handling code to avoid abnormal termination of the program.

Categorizing into different types of Exceptions so that rather than handling all exceptions with Exception root class we can handle with specific exceptions. It is recommended to handle exceptions with specific Exception instead of handling with Exception root class.

Call stack mechanism: If a method throws an exception and it is not handled immediately, then that exception is propagated or thrown to the caller of that method. This propagation continues till it finds an appropriate exception handler, if it finds handler it would be handled otherwise program terminates Abruptly.

What’s the difference between constructors and other methods?

Answer: Constructors must have the same name as the class and can not return a value. They are only called once while regular methods could be called many times.

Is there any limitation of using Inheritance?

Answer: Yes, since inheritance inherits everything from the super class and interface, it may make the subclass too clustering and sometimes error-prone when dynamic overriding or dynamic overloading in some situation.

Where and how can you use a private constructor?

Answer: Private constructor is used if you do not want other classes to instantiate the object and to prevent subclassing.

What is type casting?

Answer: Type casting means treating a variable of one type as though it is another type.

What is the difference between the >> and >>> operators?

Answer: The >> operator carries the sign bit when shifting right. The >>> zero-fills bits that have been shifted out.

What is the difference between inner class and nested class?

Answer: When a class is defined within the scope of another class, then it becomes inner class. If the access modifier of the inner class is static, then it becomes a nested class.

Can you call one constructor from another if a class has multiple constructors?

Answer: Yes, use this() syntax.

Why do we need wrapper classes?

Answer: We can pass them around as method parameters where a method expects an object. It also provides utility methods.

Does Java allow Default Arguments?

Answer: No, Java does not allow Default Arguments.

Which number is denoted by leading zero in java?

Answer: Octal Numbers are denoted by leading zero in java, for example 0

Leave a Comment

Ads Blocker Image Powered by Code Help Pro
Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.