Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
218 views

Java Interview Questions For Freshers: Why Java Is Platform Independent?

The document provides an overview of common Java interview questions for freshers, covering topics such as: - Why Java is platform independent (due to bytecodes that can run on any system) - Why Java is not 100% object-oriented (it uses primitive data types) - What constructors are in Java and the two types: default and parameterized - Why pointers are not used in Java (for safety and simplicity) - The differences between arrays and array lists - What a Map is in Java (maps unique keys to values) - What a classloader does in Java - The four types of access modifiers in Java - The definition of a class - What an object is and
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
218 views

Java Interview Questions For Freshers: Why Java Is Platform Independent?

The document provides an overview of common Java interview questions for freshers, covering topics such as: - Why Java is platform independent (due to bytecodes that can run on any system) - Why Java is not 100% object-oriented (it uses primitive data types) - What constructors are in Java and the two types: default and parameterized - Why pointers are not used in Java (for safety and simplicity) - The differences between arrays and array lists - What a Map is in Java (maps unique keys to values) - What a classloader does in Java - The four types of access modifiers in Java - The definition of a class - What an object is and
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

JAVA INTERVIEW QUESTIONS FOR FRESHERS

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.

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.

What are constructors in Java?

In Java, constructor refers to a block of code which is used to initialize an object. It


must have the same name as that of the class. Also, it has no return type and it is
automatically called when an object is created.

There are two types of constructors:

1. Default Constructor: In Java, a default constructor is the one which does


not take any inputs. In other words, default constructors are the no argument
constructors which will be created by default in case you no other
constructor is defined by the user. Its main purpose is to initialize the
instance variables with the default values. Also, it is majorly used for object
creation. 
2. Parameterized Constructor: The parameterized constructor in Java, is the
constructor which is capable of initializing the instance variables with the
provided values. In other words, the constructors which take the arguments
are called parameterized constructors.

Why pointers are not used in Java?

Java doesn’t use pointers because 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.
What is the difference between an array and an array list?

Array ArrayList
Cannot contain values of different
Can contain values of different data types.
data types
Size must be defined at the time of
Size can be dynamically changed
declaration
Need to specify the index in order to
No need to specify the index
add data
Arrays are not type parameterized Arraylists are type 
Arrays can contain primitive data Arraylists can contain only objects, no
types as well as objects primitive data types are allowed

What is a Map in Java?

In Java, Map is an interface of Util package which maps unique keys to values.
The Map interface is not a subset of the main Collection interface and thus it
behaves little different from the other collection types. Below are a few of the
characteristics of Map interface: 

1. Map doesn’t contain duplicate keys.


2. Each key can map at max one value.

What is a classloader in Java?

The Java ClassLoader is a subset of JVM (Java Virtual Machine) that is


responsible for loading the class files. Whenever a Java program is executed it is
first loaded by the classloader. Java provides three built-in classloaders:

1. Bootstrap ClassLoader
2. Extension ClassLoader
3. System/Application ClassLoader
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

Define a Java Class.

A class in Java is a blueprint which includes all your data.  A class contains fields
(variables) and methods to describe the behavior of an object. Let’s have a look at
the syntax of a class.

1 class Abc {
2 member variables // class body
3 methods}

What is an object in Java and how is it created?

An object is a real-world entity that has a state and behavior. An object has three
characteristics:

1. State
2. Behavior
3. Identity

An object is created using the ‘new’ keyword. For example:

ClassName obj = new ClassName();

OOPS Java Interview Questions


What is run time polymorphism and compile time polymorphism?
Compile time polymorphism is method overloading whereas Runtime time
polymorphism is done using inheritance and interface.

In Java, runtime polymorphism or dynamic method dispatch is a process in which


a call to an overridden method is resolved at runtime rather than at compile-time.
In this process, an overridden method is called through the reference variable of a
superclass. Let’s take a look at the example below to understand it better.

What is abstraction in Java?

Abstraction refers to the quality of dealing with ideas rather than events. It
basically deals with hiding the details and showing the essential things to the user.
Thus you can say that abstraction in Java is the process of hiding the
implementation details from the user and revealing only the functionality to them.
Abstraction can be achieved in two ways:

1. Abstract Classes (0-100% of abstraction can be achieved)


2. Interfaces (100% of abstraction can be achieved)

What do you mean by an interface in Java?

An interface in Java is a blueprint of a class or you can say it is a collection of


abstract methods and static constants. In an interface, each method is public and
abstract but it does not contain any constructor. Thus, interface basically is a group
of related methods with empty bodies.

Example:

public interface Animal {


  public void eat();
  public void sleep();
  public void run();
}

What is inheritance in Java?


Inheritance in Java is the concept where the properties of one class can be inherited
by the other. It helps to reuse the code and establish a relationship between
different classes. Inheritance is performed between two types of classes:

1. Parent class (Super or Base class)


2. Child class (Subclass or Derived class)

A class which inherits the properties is known as Child Class whereas a class
whose properties are inherited is known as Parent class.

What are the different types of inheritance in Java?

Java supports four types of inheritance which are:

1. Single Inheritance: In single inheritance, one class inherits the properties of


another i.e there will be only one parent as well as one child class.
2. Multilevel Inheritance: When a class is derived from a class which is also
derived from another class, i.e. a class having more than one parent class but
at different levels, such type of inheritance is called Multilevel Inheritance.
3. Hierarchical Inheritance: When a class has more than one child classes
(subclasses) or in other words, more than one child classes have the same
parent class, then such kind of inheritance is known as hierarchical.
4. Hybrid Inheritance: Hybrid inheritance is a combination of two or more
types of inheritance.

What is method overloading and method overriding?

Method Overloading :

 In Method Overloading, Methods of the same class shares the same name
but each method must have a different number of parameters or parameters
having different types and order.
 Method Overloading is to “add” or “extend” more to the method’s behavior.
 It is a compile-time polymorphism.
 The methods must have a different signature.
 It may or may not need inheritance in Method Overloading.

Method Overriding:  
 In Method Overriding, the subclass has the same method with the same
name and exactly the same number and type of parameters and same return
type as a superclass.
 Method Overriding is to “Change” existing behavior of the method.
 It is a run time polymorphism.
 The methods must have the same signature.
 It always requires inheritance in Method Overriding.

 What is multiple inheritance? Is it supported by Java?

If a child class inherits the property from multiple classes is known as multiple
inheritance. Java does not allow to extend multiple classes.

The problem with multiple inheritance is that if multiple parent classes have the
same method name, then at runtime it becomes difficult for the compiler to decide
which method to execute from the child class.

Therefore, Java doesn’t support multiple inheritance. The problem is commonly


referred to as Diamond Problem.

In case you are facing any challenges with these java interview questions, please
comment on your problems in the section below.

What is encapsulation in Java?

Encapsulation is a mechanism where you bind your data(variables) and


code(methods) together as a single unit. Here, the data is hidden from the outer
world and can be accessed only via current class methods. This helps in protecting
the data from any unnecessary modification. We can achieve encapsulation in Java
by:

 Declaring the variables of a class as private.


 Providing public setter and getter methods to modify and view the values of
the variables.

Servlets – Java Interview Questions  


What is Request Dispatcher?
RequestDispatcher interface is used to forward the request to another resource that
can be HTML, JSP or another servlet in same application. We can also use this to
include the content of another resource to the response.

There are two methods defined in this interface:

1.void forward()

2.void include()

What is the life-cycle of a servlet?

There are 5 stages in the lifecycle of a servlet:

1. Servlet is loaded
2. Servlet is instantiated
3. Servlet is initialized
4. Service the request
5. Servlet is destroyed

. What are the different methods of session management in servlets?

Some of the common ways of session management in servlets are:

1. User Authentication
2. HTML Hidden Field
3. Cookies
4. URL Rewriting
5. Session Management API

JDBC – Java Interview Questions 


What is JDBC Driver?

JDBC Driver is a software component that enables java application to interact with
the database. There are 4 types of JDBC drivers:

1. JDBC-ODBC bridge driver


2. Native-API driver (partially java driver)
3. Network Protocol driver (fully java driver)
4. Thin driver (fully java driver)

What are the JDBC API components?

The java.sql package contains interfaces and classes for JDBC API.

Interfaces:

 Connection
 Statement
 PreparedStatement
 ResultSet
 ResultSetMetaData
 DatabaseMetaData
 CallableStatement etc.

Classes:

 DriverManager
 Blob
 Clob
 Types
 SQLException etc.

What is the role of JDBC DriverManager class?

The DriverManager class manages the registered drivers. It can be used to register


and unregister drivers. It provides factory method that returns the instance of
Connection.

What is JDBC Connection interface?


The Connection interface maintains a session with the database. It can be used for
transaction management. It provides factory methods that returns the instance of
Statement, PreparedStatement, CallableStatement and DatabaseMetaData.

What are the steps to connect to a database in java?

 Registering the driver class


 Creating connection
 Creating statement
 Executing queries
 Closing connection

What is JDBC DatabaseMetaData interface?

The DatabaseMetaData interface returns the information of the database such as


username, driver name, driver version, number of tables, number of views etc.

  Spring Framework – Java Interview Questions 

List some of the important annotations in annotation-based Spring


configuration.

The important annotations are:

 @Required
 @Autowired
 @Qualifier
 @Resource
 @PostConstruct
 @PreDestroy

What is autowiring in Spring? What are the autowiring modes?

Autowiring enables the programmer to inject the bean automatically. We don’t


need to write explicit injection logic. 

The autowiring modes are given below:


No, by name, by type, constructor

Name the types of transaction management that Spring supports.

Programmatic transaction management

Declarative transaction management

JSP – Java Interview Questions

 What are the different tags provided in JSTL?

There are 5 type of JSTL tags.

1. core tags
2. sql tags
3. xml tags
4. internationalization tags
5. functions tags

How to disable session in JSP?

1. <%@ page session=“false” %>   

Exception and Thread Java Interview Questions


How can you handle Java exceptions?

There are five keywords used to handle exceptions in Java: 

1. try
2. catch
3. finally
4. throw
5. throws

What are the important methods of Java Exception Class?

String getMessage() 

String getLocalizedMessage() 
Synchronized Throwable getCause() 

String toString() 

void printStackTrace() 

What is OutOfMemoryError in Java?

OutOfMemoryError is the subclass of java.lang.Error which generally occurs when


our JVM runs out of memory.

What are the two ways to create a thread?

In Java, threads can be created in the following two ways:- 

 By implementing the Runnable interface.
 By extending the Thread

You might also like