30+ Frequently Asked Interview Questions on Java for Freshers

30+ Frequently Asked Interview Questions on Java for Freshers

Here we have curated a list of the Most Important Interview Questions on Java which will very useful for freshers.

Interview Questions on Java

1. What are the major features of Java?

Ans: A list of the most important features of Java languages is given below:

  • Simple
  • Object-Oriented
  • Robust
  • Platform Independent
  • Secure
  • Multi-Threading
  • Architecture Neutral
  • Interpreted
  • Distributed
  • Dynamic
  • Portable
  • High Performance

2. Java compiler is stored in JDK, JRE, or JVM?

Ans: The task of the Java compiler is to convert the Java programs into bytecode, we have the javac executable for that. So it must be stored in JDK, we don’t need it in JRE and JVM is just the specs.


3. What is the difference between JDK, JRE, and JVM?

Ans: JDK, JRE, and JVM are core concepts of the Java programming language. Although they all look similar & as a programmer, we do not care about these concepts a lot, but they are different & meant for specific purposes.

JDK

Java Development Kit is the core component of the Java environment and provides all the tools, executables, and binaries required to compile, debug and execute in Java Program. JDK is a platform-specific software and that’s why we have a separate installer for windows, Mac, and Unix systems. We can say that JDK is a superset of JRE since it contains JRE with Java compiler, debugger, and core classes.

JVM

JVM is the heart of the java programming language. When we run a program, JVM is responsible for converting byte code to machine-specific code. JVM is also platform-dependent and provides core java functions like memory management, garbage collection, security, etc.

JRE

Java Runtime Environment is the implementation of JVM, it provides a platform to execute java programs. JRE consists of JVM and java binaries and other classes to execute any program successfully. JRE doesn’t contain any program successfully. JRE doesn’t contain any development tool like java compiler, debugger, etc. If you want to execute any java program, you should have JRE installed but we don’t need JDK for running any java program.


5. What is the main method in Java?

Ans: the main method in Java is a standard method that is used by JVM to start the execution of any Java program. the main method is referred to as the entry point of any standalone java application.

the main method in Java is public so that it’s visible to every other class, even which are not part of its package. If it is not public, JVM classes might not able to access it.

the main method is void in Java because it doesn’t return anything to the caller which is JVM.


6. Can we execute any code, even before the main method?

Ans: Yes, we can execute any code, even before the main method. We are using a static block of code in the class when creating the objects at the load time of class. Any statements within this static block of code will get executed one time while loading in the main method.


7. Define class in Java. Which class is the superclass of all classes?

Ans: In Java, a class is a template used to create objects and define the data type. It acts as a building block for Java language-oriented systems. All Java codes are defined in a class. A class has variables and methods.

Variables are attributes that define the state of a class.

Methods are the place where the exact business logic has to be done. It contains a set of statements or instructions to satisfy a particular requirement.

Example:

public class addition { //class name declaration

int a = 5; //variable declaration

int b = 5;

public void add() { //Method declaration

int c = a+b;

}

}

java.lang.object is the superclass for all the java classes and we don’t need to extend it.


8. In Java, can two equal objects have a different hash code?

Ans: No. It is not possible for two equal objects to have different hash codes. But two objects with the same hash code may or may not be equal.


9. What are Constructors in Java? What are the types of Constructors?

Ans: In Java, the constructor is a block of code used to initialize an object. The constructor is a method that has the same name as the class name. There are two types of constructors:

  1. Default Constructor
  2. Parameterized Constructor

10. What is meant by the Local variable and Instance variable?

Ans:

  • Local Variable: Local variables are defined in the method and scope of the variables that have existed inside the method itself.
  • Instance Variable: An instance variable is defined inside the class and outside the method and scope of the variable exist throughout the class.
Interview Questions on Java
Interview Questions on Java

11. Can transient variables be declared as ‘static’?

Ans: There is no point in declaring a static member field as transient, since transient means: ‘do not serialize’, and static fields would not be serialized anyway. Surprisingly, the java compiler does not complain if you declare a static member field as transient.


12. What is an Anonymous Class?

Ans: The class defined without a name is known as an anonymous class.


13. What is SerialVersionUID?

Ans: Whenever an object is Serialized, the object is stamped with a version ID number for the object class. This ID is called the SerialVersionUID. This is used during deserialization to verify that the sender and receiver are compatible with the Serialization.


14. What is Garbage Collection?

Ans: In Java, the process of deallocating memory is handled automatically by the garbage collector. Garbage collection is the process of looking at heap memory, identifying which objects are in use and which are not, and deleting the unused objects. The aim of the Garbage collector is to give the highest available memory to the program.


15. Can we have multiple public classes in a java source file?

Ans: We can’t have more than one public class in a single java source file. A single source file can have multiple classes that are not public.


16. What is this keyword?

Ans: this keyword provides the reference to the current object and it’s mostly used to make sure that object variables are used, not the local variables having the same name.

//constructor

public Point(int x, int y) {

this.x = x;

this.y = y;

}

we can also use this keyword to invoke other constructors from a constructor.

public Rectangle() {

this(0,0,0,0);

}

public Rectangle(int width, int height) {

this(0,0,width,height);

}

public Rectangle(int x, int y, int width, int height) {

this.x = x;

this.y = y;

this.width = width;

this.height = height;

}


17. What is Abstraction in Java?

Ans:

Abstraction refers to the act of representing essential features without including the background details or explanation.

In Object-oriented programming, abstraction is a process of hiding the implementation details from the user, only the functionality will be provided to the user. In other words, the user will have the information on what the object does instead of how it does it.

Abstract Class

A class that is declared as abstract is known as an abstract class. It can have abstract and concrete methods. IT needs to be extended and cannot be instantiated.

Interface

In Java, Interface is another way of providing abstraction, Interfaces are by default abstract and only contain public, static, final constants or abstract methods, no method body. We can achieve 100% abstraction using interfaces.


18. What is an interface?

Ans: An interface is a reference type in Java. It is similar to a class. It is similar to a class. It is a blueprint of a class. Interfaces specify what a class must do and not how.

An interface can have methods and variables like the class but the methods declared in the interface are by default abstract. The class that implements the interface must implement all the methods of that interface. Also, the variables declared in an interface are public, static & final by default.

The interface keyword is used to declare an interface. Following is an example of an interface,

/* Filename : NameOfInterface.java */

import java.lang.*;

// Any number of import statements

public interface NameOfInterface {

// Any number of final, static fields

// Any number of abstract method declarations

)


19. What is an abstract class?

Ans: Abstract classes are used in Java to create a class with some default method implementation for subclasses. An abstract class can have an abstract method without a body and it can have methods with implementation also.


20. What is Encapsulation in Java?

Ans:

Encapsulation in java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit. In Encapsulation, the variable of the class will be hidden from other classes and can be accessed only through the methods of their current class. Therefore, it is also known as data hiding.

To achieve encapsulation in Java –

  • Declare the variables of a class as private.
  • Provide public setter and getter methods to view and modify the variable’s value.

Example:

Following is an example that demonstrates how to achieve Encapsulation in Java –

Public Class Employee {

private String name;

private String idNum;

Public String getName() {

return name;

}

public void setName(String newName) {

name = newName;

}

public String getIdNum() {

return idNum;

}

public void setIdNum(String newId) {

idNum = newId;

}

}

Interview Questions on Java
Interview Questions on Java

21. What is instanceof keyword?

Ans: The java instanceof keyword is used to test whether the object is an instance of the specified type (class or subclass or interface).

The instanceof in java is also known as type comparison operator because it compares the instance with type. It returns either true or false. If we apply the instanceof operator with any variable that has a null value. it returns false.


22. Can an interface implement or extend another interface?

Ans: Interfaces can’t implement another interface, since interfaces can’t have method implementations. An interface can extend to another interface.


23. What is Marker Interface?

Ans: A marker interface is an empty interface without any method but used to force some functionality in implementing classes by Java. Some of the well-known marker interfaces are Serializable and cloneable.


24. Define a StringJoiner and write sample code?

Ans: StringJoiner is a util method to construct a string with the desired delimiter.

StringJoiner strJoiner = new StringJoiner(“.”);

strJoiner.add(“AAA”).add(“BBB”);

System.out.println(strJoiner);

OutPut:

AAA.BBB


25. What does super keyword do?

Ans: super keyword can be used to access superclass method when you have overridden the method in the child class.

We can use the super keyword to invoke superclass constructor in child class constructor but in that case, it should be the first statement in the constructor.


26. What is a static block?

Ans: java static block is the group of statements that gets executed when the class is loaded into memory by Java ClassLoader. It is used to initialize static variables of the class. Mostly it’s used to create static resources when the class is loaded.


27. What is the difference between import and static import?

Ans: The import allows the java programmer to access classes of a package without package qualification whereas the static import feature allows accessing of the static members of a class without the class qualification. The import provides accessibility to classes and interfaces whereas static import provides accessibility to static members of the class.


28. Can we import the same package/class two times? will the JVM load the package twice at runtime?

Ans: A package or class can be imported multiple times in a program code. JVM and compiler will not create any issues. Moreover, JVM automatically loads the class internally once regardless of the times it is called in the program.


29. Which is the most commonly used class in Java?

Ans: java.lang.String class

String class will be the undisputed champion on any day by popularity and usage, none will deny that. This is a final class and used to create/ operate immutable string literals. The string class is available from JDK 1.0


30. Can we use String with switch case?

Ans: One of the java 7 features was an improvement of the switch case to allow Strings. So if you are using Java 7or the higher version, you can use String in switch-case statements.

private static void printColorUsingSwitch(String color) {

switch (color) {

case “blue”:

System.out.println(“Blue”);

case “red”:

System.out.println(“Red”);

break;

default:

system.out .printn(“Invalid color code”);

}

}

Some Important Interview Questions on Core Java

Interview Questions on Core Java
Interview Questions on Core Java

1. What is the JIT compiler?

Ans: Just-In-Time(JIT) compiler. It is used to improve performance. JIT compiles parts of the bytecode that reduces the amount of time needed for compilation. Here the term “compiler” refers to a translator from the instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU.


2. What are the advantages of Packages in Java?

Ans: There are various advantages of defining packages in Java.

  • Packages avoid name clashes.
  • The package provides easier access control.
  • We can also have the hidden classes that are not visible outside and used by the package.
  • It is easier to locate the related classes.

3. What is an object?

Ans: The object is the real-time entity having some state and behavior. In Java, Object is an instance variable as the state of the class having the instance variables like 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.


4. Can we make constructors static?

Ans: As we know that the static context (method, block, or variable) belongs to the class, not the object. Since constructors are invoked only when the object is created, there is no sense to make the constructors static. However, if you try to do so, the compiler will show the compiler error.


5. Can this keyword be used to refer static members?

Ans: Yes, it is possible to use this keyword to refer to static members because this is just a reference variable that refers to the current class object. However, as we know that, it is unnecessary to access static variables through objects, therefore, it is not the best practice to use this to refer static members. Consider the following example.

public class Test

{

static int i = 10;

public Test()

{

system.out.println(this.i);

}

public static void main (String args[])

{

Test t = new Test();

}

}

Output:

10


6. What is method overloading not possible by changing the return type in Java?

Ans: In java, method overloading is not possible by changing the return type of the program due to avoid ambiguity.

class Adder {

static int add(int a, int b) {return a+b;}

static double add(int a , int b){return a+b;}

}

class TestOverloading3{

public static void main(String[] args){

System.out.println(Adder.add(11,11));//ambiguity

} }

Output:

Compile Time Error: method add(int, int) is already defined in class Adder.


7. Can we declare a Constructor as final?

Ans: The constructor can never be declared as final because it is never inherited. Constructors are not ordinary methods; therefore, there is no sense to declare constructors as final. However, if you try to do so, The compiler will throw an error.


If you have any doubt related to these Interview Questions on Java ask me in the comment section…


Leave a Reply

Your email address will not be published.

%d bloggers like this: