Java Interview Questions For Freshers With Answers

Java Interview Questions For Freshers with Answers

“Java Interview Questions For Freshers and Answers”

Here you will get all the questions and answers of JAVA that are frequently asked in the Interviews. You can prepare for the interview on this page. Here you will find the most asked questions of the interview.

Java is a general-purpose programming language that is class-based, object-oriented, and designed to have as few implementation dependencies as possible.

Go through the page to know more about java interview questions for freshers with answers.

What is JAVA?

Before reading java interview questions for freshers with answers you must know What is Java?

Java is a widely used programming language expressly designed for use in the distributed environment of the internet. Java can be used to create complete applications that may run on a single computer or be distributed among servers and clients in a network.

Features of JAVA:-

  • Object Oriented
  • Platform Independent
  • Portable
  • Robust
  • High Performance
  • Dynamic

Study in detail java interview questions for freshers with answers:

Java Interview Questions For Freshers with Answers

1. What is an object?

Solution:- The Object is the real-time entity having some state and behavior. In Java, Object is an instance of the class having the instance variables as the state of the object and the methods as the behavior of the object. The object of a class can be created by using the new keyword.

2. Why Java is platform independent?

Solution:- Java is called platform independent because of its byte codes which can run on any system irrespective of its underlying operating system.

3. What are constructors in Java?

Solution:- The purpose of the default constructor is to assign the default value to the objects. The java compiler creates a default constructor implicitly if there is no constructor in the class.
class Student3{
int id;

String name;

void display(){System.out.println(id+" "+name);}

public static void main(String args[]){

Student3 s1=new Student3();

Student3 s2=new Student3();





4.What is this keyword in java?

Solution:- The this keyword is a reference variable that refers to the current object. There are the various uses of this keyword in Java. It can be used to refer to current class properties such as instance methods, variable, constructors, etc. It can also be passed as an argument into the methods or constructors. It can also be returned from the method as the current class instance.

5. What is a package in Java? List down various advantages of packages.


Packages in Java, are the collection of related classes and interfaces which are bundled together. By using packages, developers can easily modularize the code and optimize its reuse. Also, the code within the packages can be imported by other classes and reused. Below I have listed down a few of its advantages:

  • Packages help in avoiding name clashes
  • They provide easier access control on the code
  • Packages can also contain hidden classes which are not visible to the outer classes and only used within the package
  • Creates a proper hierarchical structure which makes it easier to locate the related classes

6.What are access modifiers in Java?


In Java, access modifiers are special keywords which are used to restrict the access of a class, constructor, data member and method in another class. Java supports four types of access modifiers:

  1. Default
  2. Private
  3. Protected
  4. Public

7.What are the main concepts of OOPs in Java?


Object-Oriented Programming or OOPs is a programming style that is associated with concepts like:

  1. Inheritance: Inheritance is a process where one class acquires the properties of another.
  2. Encapsulation: Encapsulation in Java is a mechanism of wrapping up the data and code together as a single unit.
  3. Abstraction: Abstraction is the methodology of hiding the implementation details from the user and only providing the functionality to the users. 
  4. Polymorphism: Polymorphism is the ability of a variable, function or object to take multiple forms.

8.Can we assign the reference to this variable?


No, this cannot be assigned to any value because it always points to the current class object and this is the final reference in Java. However, if we try to do so, the compiler error will be shown. Consider the following example.

public class Test  


    public Test()  


        this = null;   

        System.out.println("Test class constructor called");  


    public static void main (String args[])  


        Test t = new Test();  



Output error: cannot assign a value to final variable this
        this = null; 
1 error

9. What are the advantages of passing this into a method instead of the current class object itself?


As we know, that this refers to the current class object, therefore, it must be similar to the current class object. However, there can be two main advantages of passing this into a method instead of the current class object.

  • this is a final variable. Therefore, this cannot be assigned to any new value whereas the current class object might not be final and can be changed.
  • this can be used in the synchronized block.

10. What is the Inheritance?


Inheritance is a mechanism by which one object acquires all the properties and behavior of another object of another class. It is used for Code Reusability and Method Overriding. The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover, you can add new methods and fields in your current class also. Inheritance represents the IS-A relationship which is also known as a parent-child relationship.

There are five types of inheritance in Java.

  • Single-level inheritance
  • Multi-level inheritance
  • Multiple Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance

11. Why is Inheritance used in Java?


There are various advantages of using inheritance in Java that is given below.

  • Inheritance provides code reusability. The derived class does not need to redefine the method of base class unless it needs to provide the specific implementation of the method.
  • Runtime polymorphism cannot be achieved without using inheritance.
  • We can simulate the inheritance of classes with the real-time objects which makes OOPs more realistic.
  • Inheritance provides data hiding. The base class can hide some data from the derived class by making it private.
  • Method overriding cannot be achieved without inheritance. By method overriding, we can give a specific implementation of some basic method contained by the base class.

12. What is aggregation?


Aggregation can be defined as the relationship between two classes where the aggregate class contains a reference to the class it owns. Aggregation is best described as a has-a relationship. For example, The aggregate class Employee having various fields such as age, name, and salary also contains an object of Address class having various fields such as Address-Line 1, City, State, and pin-code. In other words, we can say that Employee (class) has an object of Address class.

13. What is super in java?


The super keyword in Java is a reference variable that is used to refer to the immediate parent class object. Whenever you create the instance of the subclass, an instance of the parent class is created implicitly which is referred by super reference variable. The super() is called in the class constructor implicitly by the compiler if there is no super or this.

class Animal{
Animal(){System.out.println("animal is created");}


class Dog extends Animal{


System.out.println("dog is created");



class TestSuper4{

public static void main(String args[]){

Dog d=new Dog();





animal is created
dog is created

14. What is method overloading?


Method overloading is the polymorphism technique which allows us to create multiple methods with the same name but different signature. We can achieve method overloading in two ways.

  • Changing the number of arguments
  • Changing the return type

15. What is method overriding?


If a subclass provides a specific implementation of a method that is already provided by its parent class, it is known as Method Overriding. It is used for runtime polymorphism and to implement the interface methods.

Rules for Method overriding

  • The method must have the same name as in the parent class.
  • The method must have the same signature as in the parent class.
  • Two classes must have an IS-A relationship between them.

16. Can we change the scope of the overridden method in the subclass?


Yes, we can change the scope of the overridden method in the subclass. However, we must notice that we cannot decrease the accessibility of the method. The following point must be taken care of while changing the accessibility of the method.

  • The private can be changed to protected, public, or default.
  • The protected can be changed to public or default.
  • The default can be changed to public.
  • The public will always remain public.

17. Can we modify the throws clause of the superclass method while overriding it in the subclass?


Yes, we can modify the throws clause of the superclass method while overriding it in the subclass. However, there are some rules which are to be followed while overriding in case of exception handling.

  • If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception, but it can declare the unchecked exception.
  • If the superclass method declares an exception, subclass overridden method can declare same, subclass exception or no exception but cannot declare parent exception.

18. What is the final variable?

Solution:-In Java, the final variable is used to restrict the user from updating it. If we initialize the final variable, we can’t change its value. In other words, we can say that the final variable once assigned to a value, can never be changed after that. The final variable which is not assigned to any value can only be assigned through the class constructo

19. What is the final class?


If we make any class final, we can’t inherit it into any of the subclasses.

final class Bike{}
class Honda1 extends Bike{

void run(){System.out.println("running safely with 100kmph");}

public static void main(String args[]){

Honda1 honda= new Honda1();;



20. Can you declare the main method as final?

Solution:- Yes, We can declare the main method as public static final void main(String[] args){}.

21. What is the difference between the final method and abstract method?

Solution:- The main difference between the final method and abstract method is that the abstract method cannot be final as we need to override them in the subclass to give its definition.

22. Why is the main method static?

Solution:-Because the object is not required to call the static method. If we make the main method non-static, JVM will have to create its object first and then call main() method which will lead to the extra memory allocation.