Signup/Sign In
PUBLISHED ON: NOVEMBER 11, 2022

Top Java Interview Questions and Answers for Freshers

Are you preparing for a job Interview? or just want to have some in-depth knowledge about Java or want to show off your Java Programming skills in front of your classmates? then you are at the right place.

We are going to discuss the Top Java Interview questions and answers for freshers that will help you gain Java programming Knowledge. The answers to these questions are short but on-point, make sure you read them all. But if you want to revise all the Java topics we would recomment check our detailed Java Language tutorial from here Let's get started.

Top Java  Interview Questions and Answers  for Freshers

Top Basic Java Interview Questions and Answers

1. What is Java Programming Language?

Java is a general-purpose, object-oriented, high-performance, robust programming language that is sued to develop applications. It was initially named "OAK" by James Gosling who is also known as the father of Java programming language.

2. List the features of Java Programming language.

  • Easy to learn: The syntax of Java is based on C++ which makes it easier to learn Java.
  • Object-Oriented: Java is an object-oriented language that allows us to write code with the help of different classes and objects.
  • Portable: Java program (.java) is converted to bytecode (.class) which allows us to execute the Java program on any machine. It supports a read-one-write-anywhere approach.
  • Platform Independent: Java comes with its own platform to execute. It doesn't depend on operating systems.
  • Secured: Java is a secured Programming language.
  • Robust: Java is more robust because has features like Automatic garbage collection, Exception handling, and strong memory management.
  • Architecture Neutral: Java is architectural neutral as it is not dependent on the architecture.
  • Interpreted: Java uses the Just-in-time (JIT) interpreter along with the compiler for the program execution.
  • High Performance: Java is faster than other interpreted languages.
  • Multithreaded: Multithreading allows us to write Java Programs for multiple tasks at once.
  • Dynamic: Java supports the dynamic loading of classes.

3. What is a Java virtual machine?

Java virtual machine is a software that executes Java bytecode. It was originally designed to execute code written in the Java programming language and facilitates the execution of any type of application.

A virtual machine is an abstract hardware platform for running programs, typically as an emulation of a hardware computer system or real physical machine.

4. What is JDK?

JDK stands for Jave Development Kit and it is the software that developers need to create or modify Java applications. JDK includes all the tools that are necessary to develop Java programs, such as compilers and debuggers.

5. What gives Java its 'write once and run anywhere nature?

Java programs can execute on any machine because its compiler converts the programs into class files(Byte Code) that can be understood by any machine and are not platform-specific.

Bonus Question: Should we Learn Java or Golang?

Java is a popular language but developers are loving Golang, check out why.

6. What is the syntax of for loop in Java?

Syntax-


for(initialization;condition;increment/decrement)
{  
//statement 
}  	
	

7. What is the output of the following program?


public class ForDemo2
{  
public static void main(String[] args) 
{  
for(inti=1;i<=5;i++)
{  
for(int j=1;j<=i;j++)
{  
				System.out.print("* ");  
}  
System.out.println(); 
}  
}  
}  
	

Java Interview Questions

8. What are the 3 types of loops in Java?

Java Programming Language has 3 types of loops :

  • for loop
  • while loop
  • do-while loop

9. What is the syntax of the do-while loop?

do{    
//code to be executed / loop body  
//update statement   
}while (condition);    

10. What are the various access specifiers in Java?

  • Public: Public can be accessed by any class or method.
  • Protected: Protected can be accessed by the class of the same package, by the sub-class of this class, or within the same class.
  • Default: Default can be accessed within the package only
  • Private: The private class, methods, or variables defined as private can be accessed within the class only.

11. What is a classloader?

A classloader is a program that loads Java programs into the Java runtime environment. It loads class files, tools, and other resources to construct a Java virtual machine from scratch.

There are different types of java classloaders:

  • Applet Class Loader
  • Bootstrap Class Loader
  • Extension Class Loader

12. If I don't provide any arguments on the command line, then what will the value stored in the String array pass into the main() method, empty or NULL?

The value stored in the String array passed into the main() method will be empty but not Null.

13. What is the purpose of static methods and variables?

Static methods and variables are type definitions of a class. They do not change throughout the life of an object, unlike instance methods and variables.

Static methods are helpful when you want to perform an operation without having to create an object. You can also use static methods for initializing or configuring your objects before they're used in a program.

Static variables are useful for storing information that does not change over the lifetime of the program, such as constants and default parameters for your classes.

Syntax to declare Static method-

Access_modifier static void methodName()
{ 
     // Method body.
} 

14. What are the advantages of Packages in Java?

Advantages of packages in Java-

  • Packages provide easier access control over the Java program.
  • Packages allow us to have hidden classes that are not visible.
  • Packages help to avoid name clashes.
  • It makes sit easier to locate the related classes.

15. What is an object-oriented paradigm?

The object-oriented paradigm is a programming paradigm based on the concept of "objects" and "classes."

Object-Oriented Programming is a set of concepts, methods, and practices for designing, implementing, and programming computer software using objects. It was one of the first programming paradigms to allow for object-oriented programming as well as one of the most popular.

Objects are data structures that contain data about an entity or an abstraction without describing their behavior. Classes are structures that define entities with which it shares a certain attribute or attributes in common.

16. What is an object?

Objects are what makes Java object-oriented. Objects are the fundamental unit of programming in Java, and they can be anything from a number to a car to an email message.

Objects are data structures used in object-oriented programming. They can contain both data and methods that operate on that data.

17. What is the output of the following Java program?

class Demo  
{  
    public static void main (String args[])   
    {  
        System.out.println(10 * 20 + "StudyTonight.com");   
        System.out.println("StudyTonight.com" + 10 * 20);  
    }  
}  

The output of the above program-


200StudyTonight.com
StudyTonight.com200

18. What is a class?

A class is a way to group certain types of objects together. This group is called a class. Classes are blueprints that are used to create objects.

You find the class keyword at the beginning of every Java program you write. It tells Java which classes you have written in this program and what they can do.

Programmers can reuse code, focus on one problem at a time, and think algorithmically by using classes as building blocks for their software or applications.

19. What will be the initial value of an object reference which is defined as an instance variable?

The initial value of every object reference is Null in Java.

20. What is the difference between an object-oriented programming language and an object-based programming language?

Object-oriented programming languages are based on the idea of modularity and abstraction, where data structures and the operations that can be performed on them are defined separately. Object-based programming language is similar to object-oriented programming languages but with a few differences.

Objects in an object-based programming language can hold both data and code, while objects in an object-oriented programming language only hold data. There are no restrictions on what types of data can be stored by objects in an object-based programing language whereas an object in an object-oriented programing language cannot store code or function definitions. Objects in object-based programming languages store the link to the type definition which defines the type of data it stores and the operations that it supports.

21. What is the constructor?

A constructor is a special type of function that initializes an object. It can be called with a new operator to create a new object or without a new one to return an existing object.

The constructor is the specialized method for initializing an object of a given class. It has no return type, it can have zero or more parameters, and its name must match the name of its class with the first letter being capitalized. Constructors are used in conjunction with other methods to form an object's implementation.

// Example of Constructor
public class Main {
  int x;

  public Main(int y) {
    x = y;
  }

  public static void main(String[] args) {
    Main myObj = new Main(5);
    System.out.println(myObj.x);
  }
}

// Outputs 5

22. What is the default constructor?

A default constructor is a constructor without parameters. A default constructor is a specific type of public class constructor that does not require any parameters to be passed in order to initialize the object. If the programmer does not provide any constructors.

Syntax-

<class_name>(){}  

23. What is the Parameterized Constructor?

A parameterized constructor is a constructor that can be called with specific arguments to create an object. It accepts the arguments.

24. Does the constructor return any value?

Yes, the constructor returns the current instance of the class implicitly. You can't use the constructor explicitly

25. Is the constructor inherited?

The constructor is not Inherited.

26. Can you overload the constructors?

Yes, you can overload the constructors by changing the number of arguments or the data type of the parameters. Look at the following example-

class Test   
{  
    int i;   
    public Test(int k)  
    {  
        i=k;  
    }  
    public Test(int k, int m)  
    {  
        System.out.println("Hi I am assigning the value max(k, m) to i");  
        if(k>m)  
        {  
            i=k;   
        }  
        else   
        {  
            i=m;  
        }  
    }  
}  
public class Main   
{  
    public static void main (String args[])   
    {  
        Test test1 = new Test(10);  
        Test test2 = new Test(12, 15);  
        System.out.println(test1.i);  
        System.out.println(test2.i);  
    }  
}  

Explanation-

In the above example, we have created a constructor "Test" that is overloaded with another constructor. When we call the constructor, the constructor having one argument is called and i is initialized with value 10. When we called the constructor a second time, the constructor having 2 arguments is called and i will be initialized with value 15.

27. What is the difference between the constructors and methods?

difference between the constructor and method-

Constructor Method
  • A constructor initializes the state of an object.
  • A method exposes the behavior of an object.
  • A constructor doesn't have a return type.
  • A method always has a return type.
  • The constructor is invoked implicitly.
  • The method is invoked explicitly.
  • The Java compiler provides a default constructor if you don't have any constructor in a class.
  • The method is not provided by the compiler in any case.
  • The constructor name must be the same as the class name.
  • The method name may or may not be the same as the class name.

28. What is the output of the following Java program?

public class Demo
{  
    Demo(int a, int b)  
    {  
        System.out.println("a = "+a+" b = "+b);  
    }  
    Demo(int a, float b)  
    {  
        System.out.println("a = "+a+" b = "+b);  
    }  
    public static void main (String args[])  
    {  
        byte a = 10;   
        byte b = 15;  
        Demo demo = new Demo(a,b);  
    }  
}  

The output of the above code -


a = 10 b = 15

29. What is the output of the following Java program?

class Demo  
{  
    int i;   
}  
public class Main   
{  
    public static void main (String args[])   
    {  
        Demo demo = new Demo();   
        System.out.println(demo.i);  
    }  
}  

The output of the above code-

The output is 0 because the I variable is initialized to 0 internally. If there is no constructor in the class then the default constructor is called implicitly.

30. What is the static variable?

A Static Variable is used as a common property to all objects of a class. e.g. Surname of family members, School of Students.

Static variables are defined in the class and are available to all objects of the class. It can be accessed by any instance or object of a class.

Syntax of Static variable-\

<class-name>.<variable-name>

Related Tutorial - Java Tutorial , Java Programs Tutorial , Java Online Compiler



About the author:
Adarsh Kumar Singh is a technology writer with a passion for coding and programming. With years of experience in the technical field, he has established a reputation as a knowledgeable and insightful writer on a range of technical topics.