Java fresher interview question/Java Interview Questions and Answers for Freshers & Experienced

Which types of exceptions are caught at compile time?

Checked exceptions can be caught at the time of program compilation. Checked exceptions must be handled by using try catch block in the code in order to successfully compile the code.

What's the benefit of using inheritance?

Key benefit of using inheritance is reusability of code as inheritance enables sub-classes to reuse the code of its super class. Polymorphism (Extensibility ) is another great benefit which allow new functionality to be introduced without effecting existing derived classes.

Can a class have multiple constructors?

Yes, a class can have multiple constructors with different parameters. Which constructor gets used for object creation depends on the arguments passed while creating the objects.

Explain the Meaning of platform?

A platform is the hardware or software setting in which a program executes. Maximum platforms in JAVA can be defined as a grouping of the operating system and hardware, Windows 2000/XP, Linux, Windows 2000/XP, MacOs and Solaris.

What do you Understand about Request Dispatcher?

Request Dispatcher interface in JAVA is used to forward the request to another resource which can be HTML, JSP or any other servlet within the same application.
There are two methods defined in this interface:

1. void forward()
2. void include()

What is a Servlet?

In JAVA atmosphere a Java Servlet is a server-side technology used to extend the capability of the web servers by providing support for dynamic response and data persistence.

What is garbage collection in java?

Java garbage collection is an automatic process. The programmer does not need to explicitly mark objects to be deleted. The garbage collection implementation lives in the JVM. Each JVM can implement garbage collection however it pleases; the only requirement is that it meets the JVM specification. Although there are many JVMs, Oracle’s HotSpot is by far the most common. It offers a robust and mature set of garbage collection options.

How to take string input in java?

"import java.util.Scanner; // Import the Scanner class
class MyClass {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println(""Enter username"");

String userName = myObj.nextLine(); // Read user input
System.out.println(""Username is: "" + userName); // Output user input
}
}"

What is final keyword in Java?

final is a special keyword in Java that is used as a non-access modifier. A final variable can be used in different contexts such as:
final variable
When the final keyword is used with a variable then its value can’t be changed once assigned. In case the no value has been assigned to the final variable then using only the class constructor a value can be assigned to it.

final method
When a method is declared final then it can’t be overridden by the inheriting class.

final class
When a class is declared as final in Java, it can’t be extended by any subclass class but it can extend other class.

What do you Mean by Association?

Association refers to a relationship where all the objects of the class have got their own lifecycle and there is no owner as such. These relationships or associations as we call them can be one to one or one to many or many to one or many to many.

Why is Java Perceived to be Platform-Independent?

Java Perceived to be Platform-Independent because platform-independent is the term that means “write once run anywhere”. Java is referred to because of its bytecodes that have the capacity to run on any system or device whatsoever irrespective of the underlying operating system.

What is static in java?

In Java, a static member is a member of a class that isn’t associated with an instance of a class. Instead, the member belongs to the class itself. As a result, you can access the static member without first creating a class instance.

What is api in java?

Java application programming interface (API) is a list of all classes that are part of the Java development kit (JDK). It includes all Java packages, classes, and interfaces, along with their methods, fields, and constructors. These pre-written classes provide a tremendous amount of functionality to a programmer.

Can variables be used in Java without initialization?

In Java, if a variable is used in a code without prior initialization by a valid value, program
doesn't compile and gives an error as no default value is assigned to variables in Java.

What is data encapsulation and what's its significance?

Encapsulation is a concept in Object Oriented Programming for combining properties and methods in a single unit.
Encapsulation helps programmers to follow a modular approach for software development as each object has its own set of methods and variables and serves its functions independent of other objects. Encapsulation also serves data hiding purpose.

What happens if the static modifier is not included in the main method signature in Java?

There wouldn't be any compilation error. But then the program is run, since the JVM cant map the main method signature, the code throws “NoSuchMethodError” error at the runtime.

Why is the Runnable Interface used in Java?

Runnable interface has been used in java for executing multi-threaded applications. Java.Lang.Runnable interface is executed by a class to support the multi-threading.

Why pointers are not used in Java?

They are unsafe and increases the complexity of the program. Since, Java is known for its simplicity of code, adding the concept of pointers will be contradicting. Moreover, since JVM is responsible for implicit memory allocation, thus in order to avoid direct access to memory by the user, pointers are discouraged in Java.

In java why strings are known as Immutable?

String objects are known immutable in java once the value is assigned to a string, it cannot be changed. If it is changed, a new object is created.

A person says that he compiled a java class successfully without even having a main
method in it? Is it possible?

Main method is an entry point of Java class and is required for execution of the program
however; a class gets compiled successfully even if it doesn't have a main method. It can't be run
though.

Describe different states of a thread.

A thread in Java can be in either of the following states:

• Ready: When a thread is created, it's in Ready state.
• Running: A thread currently being executed is in running state.
• Waiting: A thread waiting for another thread to free certain resources is in waiting state.
• Dead: A thread which has gone dead after execution is in dead state.

What is annotation?

Annotation is a tag you use to symbolize metadata that represents your class, interface, and fields among others.
They are used by the compiler and the JVM and don’t directly influence the operations.

What is aggregation?

It is a type of weak relation you can create between two classes, where one contain references to another class contained within it.

What is the function of ClassLoader?

You can use ClassLoader to load class files before running the java program.

What is Polymorphism?

Polymorphism means many forms.
A single object can refer to the super-class or sub-class depending on the reference type which is called polymorphism.

Example:

Public class Manipulation(){ //Super class
public void add(){
}
}
public class Addition extends Manipulation(){ // Sub class
public void add(){
}
public static void main(String args[]){
Manipulation addition = new Addition();//Manipulation is reference type and Addition is reference type
addition.add();
}
}

What is meant by looping?

Loops are used to repeatedly execute a certain statement or block of statements.
They are of three types- For Loops, While Loops and Do While Loops.

What is singleton class in Java and how can we make a class singleton?

Singleton class is a class whose only one instance can be created at any given time, in one JVM. A class can be made singleton by making its constructor private.

Why Java is not 100% Object-oriented?

Java is not 100% Object-oriented because it makes use of eight primitive data types such as boolean, byte, char, int, float, double, long, short which are not objects.

How does Java enable high performance?

Java uses Just In Time compiler to enable high performance. It is used to convert the instructions into bytecodes.

Why Runnable Interface is used in Java?

Multi threaded applications can be developed in Java by using any of the following two
methodologies:
1. By using Java.Lang.Runnable Interface. Classes implement this interface to enable multi
threading. There is a Run() method in this interface which is implemented.
2. By writing a class that extend Java.Lang.Thread class.

What's the difference between an array and Vector?

An array groups data of same primitive type and is static in nature while vectors are dynamic
in nature and can hold data of different data types.

What is thread in Java?

Threads allow a program to operate more efficiently by doing multiple things at the same time.
Threads can be used to perform complicated tasks in the background without interrupting the main program.
It can be created by extending the Thread class and overriding its run() method:
Extend Syntax

public class MyClass extends Thread {
public void run() {
System.out.println("This code is running in a thread");
}
}

How an object is serialized in java?

In java, to convert an object into byte stream by serialization, an interface with the name
Serializable is implemented by the class. All objects of a class implementing serializable interface
get serialized and their state is saved in byte stream.

Can we declare the main method of our class as private?

Main method must be public static in order to run any application correctly. If main
method is declared as private, developer won't get any compilation error however, it will not get
executed and will give a runtime error.

What is known as a final keyword in Java?

A constant is proclaimed using the keyword final in Java. The value may be assigned once and after the assignment, the value of a constant cannot be changed.
A constant with the name const_val has been assigned and declared a value in the below example:
Private Final int const_val=100
It cannot be overridden by the subclasses if a method is declared as final. This method is considered to be faster than any other method because they have been resolved at the compiled time.
It can’t be subclassed, when a class is declared as final. E.g., integer, string, and other wrapper classes.

Can main() method in Java can return any data?

: In java, main() method can’t return any data and hence, it’s always declared with a void
return type.

How can you generate random numbers in Java?

• Using Math.random() you can generate random numbers in the range greater than or equal
to 0.1 and less than 1.0
• Using Random class in package java.util

What makes a HashSet different from a TreeSet?

Although both HashSet and TreeSet are not synchronized and ensure that duplicates are not present, there are certain properties that distinguish a HashSet from a TreeSet.
Implementation: For a HashSet, the hash table is utilized for storing the elements in an unordered manner. However, TreeSet makes use of the red-black tree to store the elements in a sorted manner.
Complexity/ Performance: For adding, retrieving, and deleting elements, the time amortized complexity is O(1) for a HashSet. The time complexity for performing the same operations is a bit higher for TreeSet and is equal to O(log n). Overall, the performance of HashSet is faster in comparison to TreeSet.
Methods: hashCode() and equals() are the methods utilized by HashSet for making comparisons between the objects. Conversely, compareTo() and compare() methods are utilized by TreeSet to facilitate object comparisons.
Objects type: Heterogeneous and null objects can be stored with the help of HashSet. In the case of a TreeSet, runtime exception occurs while inserting heterogeneous objects or null objects.

Can the static methods be overloaded?

Yes! There can be two or more static methods in a class with the same name but differing input parameters.

Why is Java not a pure object oriented language?

Java supports primitive data types - byte, boolean, char, short, int, float, long, and double and hence it is not a pure object-oriented language.

What is a singleton class?

In Java, a singleton class can have only one instance and thus all its variables and methods belong to one instance. The concept of a singleton class is used for the situations while there is a requirement to limit the no of objects for a class.

How to reverse a string in Java?

"String str = ""Hello"";
String reverse(String str){
StringBuilder sb = new StringBuilder();
sb.append(str);
sb.reverse();
return sb.toString();
}"

What is the difference between an Inner class and a Subclass?

An inner class has been nested within the other class. An inner class has access rights for the class that is nesting it and can access all methods and variables that are defined in the outer class.
A subclass inherits from the other class known as the superclass. Sub-class can access all protected and public fields and methods of its superclass.

What's the purpose of Static methods and static variables?

When there is a requirement to share a method or a variable between multiple objects of a
class instead of creating separate copies for each object, we use static keyword to make a method
or variable shared for all objects.

What are the various access specifiers for Java classes?

In Java, access specifiers are the keywords used before a class name which defines the access
scope. The types of access specifiers for classes are:
1. Public : Class,Method,Field is accessible from anywhere.
2. Protected:Method,Field can be accessed from the same class to which they belong or from the
sub-classes,and from the class of same package,but not from outside.
3. Default: Method,Field,class can be accessed only from the same package and not from outside
of it’s native package.
4. Private: Method,Field can be accessed from the same class to which they belong.

What is the difference between an Inner Class and a Sub-Class?

An Inner class is a class which is nested within another class. An Inner class has access rights
for the class which is nesting it and it can access all variables and methods defined in the outer
class.

What are the features of JAVA?

Features of Java are as follows:
1. OOP concepts
2. Object-oriented
3. Inheritance
4. Encapsulation
5. Polymorphism
6. Abstraction
Platform independent: A single program works on different platforms without any modification.
High Performance: JIT (Just In Time compiler) enables high performance in Java. JIT converts the bytecode into machine language and then JVM starts the execution.
Multi-threaded: A flow of execution is known as a Thread. JVM creates a thread which is called the main thread. The user can create multiple threads by extending the thread class or by implementing the Runnable interface.

Why Java is platform independent?

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

How to install Java?

Install Java through command prompt so that it can generate necessary log files to troubleshoot the issue.
Go to java.com and click on the Free Java Download button.

Click on the Save button and save Java software on the Desktop

Verify that Java software is saved on the desktop.

Open Windows Command Prompt window.

Windows XP: Click Start -> Run -> Type: cmd

Windows Vista and Windows 7: Click Start -> Type: cmd in the Start Search field.
cd <Java download directory> (for example Downloads or Desktop etc.)

IRun the installer and follow onscreen instructions.

What is Java?

Java is a general-purpose programming language that is class-based, object-oriented and is very popular. It’s one of the most popular programming languages in the world.It was developed by James Gosling in June 1991. It can also be known as the platform as it provides its own JRE and API.

Search
R4R Team
R4R provides Java Freshers questions and answers (Java Interview Questions and Answers) .The questions on R4R.in website is done by expert team! Mock Tests and Practice Papers for prepare yourself.. Mock Tests, Practice Papers,Java fresher interview question,Java Freshers & Experienced Interview Questions and Answers,Java Objetive choice questions and answers,Java Multiple choice questions and answers,Java objective, Java questions , Java answers,Java MCQs questions and answers Java, C ,C++, ASP, ASP.net C# ,Struts ,Questions & Answer, Struts2, Ajax, Hibernate, Swing ,JSP , Servlet, J2EE ,Core Java ,Stping, VC++, HTML, DHTML, JAVASCRIPT, VB ,CSS, interview ,questions, and answers, for,experienced, and fresher R4r provides Python,General knowledge(GK),Computer,PHP,SQL,Java,JSP,Android,CSS,Hibernate,Servlets,Spring etc Interview tips for Freshers and Experienced for Java fresher interview questions ,Java Experienced interview questions,Java fresher interview questions and answers ,Java Experienced interview questions and answers,tricky Java queries for interview pdf,complex Java for practice with answers,Java for practice with answers You can search job and get offer latters by studing r4r.in .learn in easy ways .