Java is the most prominent high-level language which is used by the large number of developers to create web applications, games, commercial websites and many more. Thus, it’s trend is everlasting. Being a most sorted language, its demand is high in the market. The students who are willing to make their career in web development, must be in a dilemma that what sort of questions can be asked in an interview. Thus, we have decided to write on it and here we are providing you with some basic Java Interview Questions which will definitely help you to crack an Interview. So sit back, relax and go through the entire set of questions.

Q.1 What is JAVA?

Ans. Java is a popular programming language, owned by Oracle. Java is a collection of objects which is based on class and object oriented concept. Java is largely used to createmobile applications,web applications, and desktop applications etc.   

 

Q.2 What is JVM?

Ans. JVM is an acronym of Java Virtual Machine. JVM is one of the parts of Java Runtime Environment (JRE). It is the one which calls the Main method of Java Code.

 

Q.3 What is Java Runtime Environment(JRE)?

Ans. JRE is the abbreviation of the Java Runtime Environment. It is an environment in which bytecode run. In other words, JRE is a software package which has everything that needs to run a Java Program.

 

Q.4 What is Class in JAVA?

Ans. The class is a representation of similar kind of objects. A class in Java is a user-defined data type.

 

Q.5 What is Object?

Ans. An object in Java is a combination of data and procedures and it has states and behaviours.

 

Q.6 Explain the features of JAVA?

Ans. Java supports many features. Some of the main features are as follows:

  • Object-Oriented– Java is an object-oriented programming language. OOPs is nothing but just an abbreviation of Object-oriented Programming System in which program is considered a collection of objects. Java has all oops features like inheritance, polymorphism, encapsulation and abstraction.
  • Robust- Java has high garbage and better memory management feature which makes it robust.
  • Platform Independent- Java follows “Write Once Run Anywhere” motto. It is not dependent on the platform. Once the code is written, it can run on any platform without any modification irrespective of the underlying operating system.
  • Multi-Threaded– The flow of execution is known as a thread. JVM produces the main thread. Multithreading is the concurrent execution of two or more programs for the maximum utilization of CPU.  

 

Q.7 Explain Method Overloading and Overriding.

Ans. Method Overloading:

Method overloading is one of the important features of Java which allows a class to have more than one method with the same name and their argument list is different. Method overloading keeps the method name same.. It depends on a number of parameters and their data types.

 

Example:

class Adder

{

Static int add(int a, int b)

{

return a+b;

}

Static double add( double a, double b)

{

return a+b;

}

public static void main(String args[])

{

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

System.out.println(Adder.add(12.3,12.6));

}

}

 

Method Overriding:

Method overriding defined as the definition of superclass is used by a subclass using the same method signature.

 

Example:

class Car

{

void run()

{

System.out.println(“car is running”);

}

Class Audi extends Car

{

void run()

{

System.out.prinltn(“Audi is running safely with 100km”);

}

public static void main( String args[])

{

Car b=new Audi();

b.run();

}

}

 

Q.8 What is Heap Memory?

Ans. Heap memory is a space which is used by java runtime to allocate memory to Objects and JRE classes. Whenever an object is created, it’s always created in the Heap space.

 

Q.9 Is String a datatype in JAVA?

Ans. The string is not a primitive data type, while it’s an object of string.lang.string class that gets created when a string is created in Java. We can use all the built-in methods of a string class on the string object.

 

Q.10 Why Java is said to be Platform Independent?

Ans. Java is called platform independent because it’s bytecode can run on any system irrespective to the underlying operating system. But the necessary condition is the presence of JVM.

 

Q.11 What is Package in JAVA?

Ans. Packages in Java is a group of similar kind of classes, subclasses, interfaces. There is two categories of packages: Built-in and user-defined packages. The built-in packages are java, lang, awt, javax, swing, net, io, util, sql etc.

A keyword “package” is used to create a package in Java.

Example:

package mypack;  

public class Simple

{  

public static void main(String args[])

{  

System.out.println(“Welcome to package”);  

}

}

 

Q.12 What are the components of JVM?

Ans. The three main components of Java are JVM, JDK, JRE.

  • JVM is known as Java Virtual Machine. It is required to run a Java application.
  • JDK is known as Java Development Kit. It is a software development environment which is used for developing Java applications and applets.
  • JRE or Java Runtime Environment is a set of software which is used to create Java applications. JRE is that which combines JVM and JDK Together.

 

Q.13 How does bytecode run in JAVA?

Ans. Bytecode is a compiled format of java program. Class loader loads the bytecode into java runtime environment where JIT compiler converts bytecode into machine-dependent code.

 

Q.14 Explain local variable and instance variable?

Ans.Local variables are defined inside methods and constructors.

Instance variables are those variables which are present within a class but outside any method.

 

Q.15 Why JAVA is a statically typed language?

Ans. Java is referred to as statically typed language as the type of the variable is known at the compile time.

 

Q.16 What is Object-oriented programming?

Ans. When a programming language is based on an object based concept rather than actions then they are known as object-oriented programming languages. In Java, everything’s an object. Java can be easily extended as it is based on objects.

 

Q.17 What is Inheritance?

Ans. A process used to create new class from the existing class. The newly created class is known as a subclass and older classes are known as the superclass. Inheritance is done for code reusability.

 

Q.18 What do you understand by polymorphism?

Ans. Polymorphism means more than one form of a method. We do polymorphism for the convenience of end users. It is of two times:

  • Compile Time Polymorphism
  • Runtime Polymorphism

 

Q.19 What is Bytecode?

Ans. Bytecode is a highly optimized set of instructions which is  read by JVM in an order to give a relevant output.

 

Q.20 Explain JIT compiler.

Ans. JIT Compiler is dynamic Compiler. It compiles the byte code into machine code according to demand.

 

Q.21 What do you understand by the finalize() method?

Ans. finalize( )method is called just before an object’s final destruction by the garbage collector, and it can be used to ensure that an object terminates appropriately.

 

Q.22 Distinguish println and print?

Ans. The major difference between print and println is that println prints the string and moves the cursor in the next line while print, prints the string and keep the cursor in the same line.

 

Q.23 Describe the usage of “This” keyword in JAVA.

Ans. When instance variable and formal parameter’s name are same; formal parameter hides an actual instance variable.Then the keyword use to the current object to remove the instance variable hiding concept.

Eg: this.cname= cname;

    this.color=color;

    this.price=price;

Q.24 What is the difference between Array and ArrayList?

Ans.

Array ArrayList
An array is a fixed length data structure.

Ex:

String[] name = new String[2]

It’s a variable length collection class.

Ex:

ArrayList name = new ArrayList

We need to specify the index to put an object in an array.

Ex:

name[1] = “cat”

No indexing required.

Ex:

name.add(“cat”)

Not type parameterized as ArrayList. It is type parameterized.

 

Q.25 Explain Public and Private access specifiers.

Ans: An access specifiers in java is used to restrict the scope of a class, constructor, variable, method or data member.

 

Public Specifiers: They are specified using the public keyword. The public members are visible in the same package as well as the outside package that is for other packages.

 

Private Specifiers: They are specified using the private keyword. Private members can be seen in the same class. They are not for the other classes in the same package as well as classes in the outside of packages.

 

Q.26 What is Thread in Java?

Ans: The flow of execution is referred to as a thread. The main which is used in every java program is also a thread. This main thread is created by JVM. The user can create their own thread by extending thread class.

 

Q.27 Why we declare Main as Static?

Ans. Main is the entry point of the program hence it is declared as static. For calling the static method we never need an object of a class.

 

Q.28 What is Super Keyword in JAVA and Where to use it?

Ans. If superclass and subclass both have the same method as well as the variable name, to identify superclass variables & methods in the subclass, a super keyword is used. It is always written in subclass only when the method name is the same. It is used to class superclass constructor explicitly.

 

Q.29 When a subclass has only parameterized constructor and superclass does not have a matching constructor, What will happen?

Ans. A default constructor of the superclass will get invokedwhen a subclass has only parameterized constructor and superclass does not have any matching constructor.

If superclass has parameterized constructor and subclass want to use that constructor then we must write “super” keyword to pass parameters in the superclass constructor.

Super keyword must be written in the very first line in the constructor of the subclass.

 

Q.30 Explain Public Static Void Main (String args[]).

Ans:

  • Public:A type of an access modifier. It is used to specify who can access this method. This means that this method can be accessed by any class.
  • Static: It is a keyword in java which identifies it can be accessed without creating the instance of a Class.
  • Void: Void is a return type of a method which will not return any value.
  • Main: It is referred to a method which is searched by JVM as a starting point for an application with a particular signature only.
  • String args[]: It is a parameter which is passed to the main method.

 

Q.31 What are Constructors in Java?

Ans. In Java, Constructors are the block of codes which are similar to a method. A constructor is called when an instance of an object is created. The name of the constructor is kept the same as the name of the class. There is no return type of a constructor, not even void. If we don’t create an explicit constructor in our class, java compiler supplies it by default.

 

Q.32 What do you understand by a Wrapper class?

Ans. A wrapper class is a class which converts java primitives into reference type objects. They “wrap” the primitive data type into an object of that class, therefore they are referred to as wrapper class.

 

Q.33 What is singleton Class?

Ans. A class whose only one instance can be created at any given time, in one JVM is called singleton class.

 

Q.34 What is Access Modifier?

Ans.Access modifier is also known as Access specifiers. Access modifiers in Java are those clarify the classes which can access a given class and its fields, constructors and methods. Access modifiers can be specified separately for a class, constructors, and methods. Java access modifiers have four types as:

  • Default
  • Public
  • Private
  • Protected

Q.35 When is ParseInt() Method used?

Ans. Sometimes in Java, we need to convert a number represented as a string into an integer type. ParseInt() method is used to execute such type of tasks to convert String into Integer.

 

Q.36 What is the purpose of a Volatile Variable?

Ans. The value of the Volatile variable is are always read from the main memory and not from thread’s cache memory. This is used mainly during synchronization. It is applicable only for variables.

 

Q.37 What do you understand by Exception Handling?

Ans. Exception Handling is a mechanism to handle runtime error. It is done to maintain the normal flow of the application.  

 

Q.38 Explain enumeration.

Ans. It is an interface containing methods for accessing the underlying data structure from which the enumeration is obtained. It allows sequential access to all the elements stored in a collection.

 

Q.39 What is Applet? Which class does Applet extend?

Ans. An applet is a Java program that runs in the browser. It can be a fully functional Java application as it has the entire Java API. Class which is used for Applet extends is Java.applet.Applet.class.

 

Q.40 Differentiate between Throw and Throws.

Ans.

Throw Throws
As per the word itself, Throw keyword is used to explicitly throw an exception. It is used to declare an exception.
A checked exception cannot be propagated using throw only. A checked exception cannot be propagated using throws.
It is followed by an instance. It is followed by a class.
This is not allowed to throw multiple exceptions. You can declare multiple exceptions using throws keyword.

 

Q.41 Explain Garbage Collection in JAVA.

Ans. Java uses garbage collection to free memory. It is done by removing those objects which are not referenced in the program anymore.

 

Q.42 Explain Multi-threaded Program.

Ans. A multi-threaded program is a combination of 2-3 parts that can run concurrently. These parts are referred to the thread. Every thread has its own separate paths.

 

public class MultipleThreads implements Runnable

{

public static void main (String[] args)

{

Runnable r = new runnable ();

Thread t=new thread ();

t.start ();//User thread starts here

Addition add=new addition ();

}

public void run()

{

go();

} //User thread ends here

}

 

Q.43 Differentiate between Swing and AWT Components.

Ans.

AWT Swing
It is a heavy-weighted component. Its contains light-weighted components.
Pluggable look and feel is not supported in AWT. It supports a pluggable look and feel.
AWT programs are not portable. Swing programs are portable.
It is not based on MVC. It follows MVC.
It is an old framework for creating GUIs. It is a new framework for creating GUIs.

 

Q.44 What is Downcasting?

Ans. When a subclass refers to an object of parent class then it is known as downcasting.

Ex.

class Animal { }  

 

class Dog3 extends Animal

{  

static void method(Animal a)

{  

if(a instanceof Dog3)

{

Dog3 d=(Dog3)a; //downcasting  

System.out.println(“ok downcasting performed”);  

  }

}

public static void main (String [] args)

{

Animal a=new Dog3();  

Dog3.method(a);  

}

}

 

Q.45 What is an Interface?

Ans. An interface is a collection of static constants and abstract methods. The interface is implemented by class thereby inheriting the abstract methods of the interface. Here abstract methods means only method signature, no body.

 

Q.46 Why multiple inheritances are not supported in Java?

Ans. Java does not support multiple inheritances through the class to avoid errors. For multiple inheritances, the interface can be used.  

 

Q.47 What is Object Cloning?

Ans. Object cloning is a way through which an exact copy of an object can be created. The clone() method of an object class is used to clone an object.

 

Q.48 Can Private Methods be Overridden?

Ans. A private method can’t overridden. If you’ll create a similar method with the same return type and same method arguments in child class then it will abstract the superclass method; this is known as method hiding. In the same way, you cannot override a private method in a subclass because it’s not accessible there. What you can do is that you can create another private method with the same name in the child class.

 

Q.49 What are the use cases? Explain.

Ans. Use cases describe a situation that a program might encounter and what behavior the program should exhibit in that circumstance. It is a part of the analysis of the program.

 

Q.50 What is a priority queue?

Ans. Linked list and queue are related to each-other as class has been enhanced to implement the queue interface and Queues can be handled using linked list. Purpose of a queue is “Priority-in, Priority-out”.

 

Q.51 What are Ternary Operators? Explain with Example.

Ans. The Java ternary operator functions as a simplified Java if statement. The ternary operator consists of a condition that evaluates to either true or false and one value that is returned if the condition is true and another value is returned when the condition is false.

 

Q.52 How do you differentiate inner and nested class?

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

 

Q.53 What is the difference between Stack and Queue?

Ans.

Stack Queue
In the stack, objects are inserted and removed from the same end. Objects are inserted and removed from the different end in the queue.
There is only one pointer used in Stack. It points to the top of the stack. In queue, two different pointers are used for front and rear end.
Stack follow LIFO (Last in First Out) Queue follows FIFO (First In )
Stack operations are called push and pop. Queue operations are called enqueue and dequeue.

 

Q.54 Describe different states of Thread.

Ans. The different states of thread are:

  • New
  • Runnable
  • Running
  • Blocked
  • Terminated

 

Q.55 Which type of Exceptions is caught at Compile time?

Ans. If code within a method throws a checked exception, then the method must either handle the exception or the code must specify the exception using “Throws” keyword. Therefore, this is the Checked exceptions that checked at compile time.

One thought on “Top 50+ Java Interview Questions”

Leave a Reply

Your email address will not be published. Required fields are marked *