top 100 java interview questions and answers
DESCRIPTION
Looking for the top 100 Java Interview Questions and Answers in 2014? We've compiled a list of the top 100 java technical interview questions and answers from various reputed sources and put it together in this pdf format for easy reference. Make sure to download and go through this list of the best java interview questions and answers before appearing for an interview as this pdf contains a list of the most popular java technical questions that are asked in interviews. This list includes java interview questions in the below categories: top 100 java interview questions top 100 java interview questions and answers top 100 java interview questions pdf monster top 100 interview questions top 10 java interview questions best java interview questions best java interview questions and answers site best core java interview questions top 100 interview questions on java language top 100 core java interview questions core java interview questions and answers for experienced core java interview questions pdf core java interview questions for senior developers core java interview questions and answers for freshers core java interview questions and answers pdf free download core java interview questions and answers for 2 years experienced core java interview questions for experienced candidates top 100 java interview questions with answers java interview questions and answers for freshers java interview questions and answers for experienced java interview questions and answers for 3 years experience java interview questions and answers pdf downloadTRANSCRIPT
www.hirist.com
JAVA
TOP 100INTERVIEW QUESTIONS & ANSWERS
For the hottest Java jobs, please visit www.hirist.com
hirist.com
Is Java platform independent?
2
QUESTION
Yes. Java is a platform independent language. We can write java code on one platform and run it on another
platform. For e.g. we can write and compile the code on windows and can run it on Linux or any other supported
platform. This is one of the main features of java.
ANSWER
QUESTION 1
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What all memory areas are allocated by JVM?
3
QUESTION
a)Class(Method) Area
b)Heap,
c) Stack,
d)Program Counter Register and
e)Native Method Stack
ANSWER
QUESTION 2
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is class?
4
QUESTION
Class is nothing but a template that describes the data and behavior associated with instances of that class
ANSWER
QUESTION 3
For the hottest Java jobs, please visit www.hirist.com
hirist.com
Can a .java file contain more than one java classes?
5
QUESTION
Yes. A .java file contain more than one java classes, provided at the most one of them is a public class.
ANSWER
QUESTION 4
For the hottest Java jobs, please visit www.hirist.com
hirist.com
Different Data types in Java.
6
QUESTION
byte – 8 bit (are esp. useful when working with a stream of data from a network or a file).
short – 16 bit
char – 16 bit Unicode
int – 32 bit (whole number)
float – 32 bit (real number)
long – 64 bit (Single precision)
double – 64 bit (double precision)
ANSWER
QUESTION 5
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is Type casting in Java?
7
QUESTION
To create a conversion between two incompatible types, we must use a cast. There are two types of casting in java:
automatic casting (done automatically) and explicit casting (done by programmer).
ANSWER
QUESTION 6
For the hottest Java jobs, please visit www.hirist.com
hirist.com
Four main principles of OOPS language?
8
QUESTION
Inheritance
Polymorphism
Data Encapsulation
Abstraction
ANSWER
QUESTION 7
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is Function Overriding and Overloading in Java ?
9
QUESTION
Method overloading in Java occurs when two or more methods in the same class have the exact same name, but
different parameters. On the other hand, method overriding is defined as the case when a child class redefines
the same method as a parent class. Overridden methods must have the same name, argument list, and return
type. The overriding method may not limit the access of the method it overrides
ANSWER
QUESTION 8
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is static and dynamic binding?
10
QUESTION
Binding refers to the linking of method call to its body. A binding that happens at compile time is known as static
binding while binding at runtime is known as dynamic binding.
ANSWER
QUESTION 9
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is the difference between a synchronized method and a synchronized
block ?
11
QUESTION
In Java programming, each object has a lock. A thread can acquire the lock for an object by using the synchronized
keyword. The synchronized keyword can be applied in a method level (coarse grained lock) or block level of code
(fine grained lock)
ANSWER
QUESTION 10
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is difference between Array and ArrayList ? When will you use Array
over ArrayList ?
12
QUESTION
The Array and ArrayListclasses differ on the following features:
a. Arrays can contain primitive or objects, while an ArrayList can contain only objects.
b. Arrays have fixed size, while an ArrayList is dynamic.
c. An ArrayListprovides more methods and features, such as addAll, removeAll, iterator, etc.
d. For a list of primitive data types, the collections use autoboxing to reduce the coding effort. However, this
approach makes them slower when working on fixed size primitive data types.
ANSWER
QUESTION 11
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is OOPs?
13
QUESTION
Object oriented programming organizes a program around its data i,e. objects and a set of well defined interfaces to
that data. An object-oriented program can be characterized as data controlling access code
ANSWER
QUESTION 12
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is Casting?
14
QUESTION
Casting is used to convert the value of one type to another.
ANSWER
QUESTION 13
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is JIT compiler?
15
QUESTION
Just-In-Time(JIT) compiler:It is used to improve the performance. JIT compiles parts of the byte code that have
similar functionality at the same time, and hence 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.
ANSWER
QUESTION 14
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is the main difference between Java platform and other platforms?
16
QUESTION
The Java platform differs from most other platforms in the sense that it's a software-based platform that runs on top
of other hardware-based platforms. It has two components:
1. Runtime Environment
2. API(Application Programming Interface)
ANSWER
QUESTION 15
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What are Constructors?
17
QUESTION
Constructors are used for creating an instance of a class, they are invoked when an instance of class gets created.
Constructor name and class name should be same and it doesn’t have a return type
ANSWER
QUESTION 16
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is default and parameterized constructors?
18
QUESTION
Default: Constructors with no arguments are known as default constructors, when you don’t declare any constructor
in a class, compiler creates a default one automatically.
Parameterized: Constructor with arguments are known as parameterized constructors.
ANSWER
QUESTION 17
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is difference between aggregation and composition?
19
QUESTION
Aggregation represents weak relationship whereas composition represents strong relationship. For example: bike
has an indicator (aggregation) but bike has an engine (compostion).
ANSWER
QUESTION 18
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What are Encapsulation, Inheritance and Polymorphism?
20
QUESTION
Encapsulation is the mechanism that binds together code and data it manipulates and keeps both safe from outside
interference and misuse. Inheritance is the process by which one object acquires the properties of another object.
Polymorphism is the feature that allows one interface to be used for general class actions.
ANSWER
QUESTION 19
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is final, finalize() and finally?
21
QUESTION
final : final keyword can be used for class, method and variables. A final class cannot be subclassed and it prevents
other programmers from subclassing a secure class to invoke insecure methods. A final method can’t be
overridden. A final variable can’t change from its initialized value.
finalize() : finalize() method is used just before an object is destroyed and can be called just prior to garbage
collection.
finally : finally, a key word used in exception handling, creates a block of code that will be executed after a try/catch
block has completed and before the code following the try/catch block. The finally block will execute whether or
not an exception is thrown. For example, if a method opens a file upon exit, then you will not want the code that
closes the file to be bypassed by the exception-handling mechanism. This finally keyword is designed to address
this contingency.
ANSWER
QUESTION 20
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is Garbage Collection and how to call it explicitly?
22
QUESTION
When an object is no longer referred to by any variable, java automatically reclaims memory used by that object.
This is known as garbage collection. System. gc() method may be used to call it explicitly.
ANSWER
QUESTION 21
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is meant by Inheritance and what are its advantages?
23
QUESTION
Inheritance is the process of inheriting all the features from a class. The advantages of inheritance are reusability of
code and accessibility of variables and methods of the super class by subclasses.
ANSWER
QUESTION 22
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is the difference between superclass and subclass?
24
QUESTION
A super class is a class that is inherited whereas sub class is a class that does the inheriting.
ANSWER
QUESTION 23
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is the difference between Integer and int?
25
QUESTION
a) Integer is a class defined in the java. lang package, whereas int is a primitive data type defined in the Java
language itself. Java does not automatically convert from one to the other.
b) b) Integer can be used as an argument for a method that requires an object, whereas int can be used for
calculations.
ANSWER
QUESTION 24
For the hottest Java jobs, please visit www.hirist.com
hirist.com
How do I serialize an object to a file?
26
QUESTION
The class whose instances are to be serialized should implement an interface Serializable. Then you pass the
instance to the ObjectOutputStream which is connected to a fileoutputstream. This will save the object to a file.
ANSWER
QUESTION 25
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is the difference between String and String Buffer?
27
QUESTION
a) String objects are constants and immutable whereas StringBuffer objects are not.
b) b) String class supports constant strings whereas StringBuffer class supports growable and modifiable strings.
ANSWER
QUESTION 26
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is the difference between Array and vector?
28
QUESTION
Array is a set of related data type and static whereas vector is a growable array of objects and dynamic.
ANSWER
QUESTION 27
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is source and listener?
29
QUESTION
source : A source is an object that generates an event. This occurs when the internal state of that object changes in
some way.
listener : A listener is an object that is notified when an event occurs. It has two major requirements. First, it must
have been registered with one or more sources to receive notifications about specific types of events. Second, it
must implement methods to receive and process these notifications.
ANSWER
QUESTION 28
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is Thread in Java?
30
QUESTION
Thread is an independent path of execution. It's way to take advantage of multiple CPU available in a machine. By
employing multiple threads you can speed up CPU bound task. For example, if one thread takes 100 millisecond
to do a job, you can use 10 thread to reduce that task into 10 millisecond. Java provides excellent support for
multi-threading at language level, and its also one of strong selling point.
ANSWER
QUESTION 29
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is FutureTask in Java?
31
QUESTION
FutureTask represents a cancellable asynchronous computation in concurrent Java application. This class provides
a base implementation of Future, with methods to start and cancel a computation, query to see if the computation
is complete, and retrieve the result of the computation. The result can only be retrieved when the computation has
completed; the get methods will block if the computation has not yet completed. A FutureTask object can be used
to wrap a Callable or Runnable object. Since FutureTask also implements Runnable, it can be submitted to an
Executor for execution.
ANSWER
QUESTION 30
For the hottest Java jobs, please visit www.hirist.com
hirist.com
There are three threads T1, T2 and T3? How do you ensure sequence T1,
T2, T3 in Java?
32
QUESTION
Sequencing in multi-threading can be achieved by different means but you can simply use join() method of thread
class to start a thread when another one is finished its execution. To ensure three threads execute you need to
start the last one first e.g. T3 and then call join methods in reverse order e.g. T3 calls T2. join, and T2 calls
T1.join, this ways T1 will finish first and T3 will finish last.
ANSWER
QUESTION 31
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is blocking method in Java?
33
QUESTION
A blocking method is a method which blocks until task is done, for example accept() method of ServerSocket blocks
until a client is connected. here blocking means control will not return to caller until task is finished. On the other
hand there are asynchronous or non-blocking method which returns even before task is finished.
ANSWER
QUESTION 32
For the hottest Java jobs, please visit www.hirist.com
hirist.com
Difference between volatile and atomic variable in Java?
34
QUESTION
This is an interesting question for Java programmer, at first, volatile and atomic variable look very similar, but they
are different. Volatile variable provides you happens-before guarantee that a write will happen before any
subsequent write, it doesn't guarantee atomicity. For example count++ operation will not become atomic just by
declaring count variable as volatile. On the other handAtomicInteger class provides atomic method to perform
such compound operation atomically e.g. getAndIncrement() is atomic replacement of increment operator. It can
be used to atomically increment current value by one. Similarly you have atomic version for other data type and
reference variable as well.
ANSWER
QUESTION 33
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is the Difference between JDK and JRE?
35
QUESTION
The “JDK” is the Java Development Kit. I.e., the JDK is bundle of software that you can use to develop Java based
software.
The “JRE” is the Java Runtime Environment. I.e., the JRE is an implementation of the Java Virtual Machine which
actually executes Java programs.
Typically, each JDK contains one (or more) JRE’s along with the various development tools like the Java source
compilers, bundling and deployment tools, debuggers, development libraries, etc.
ANSWER
QUESTION 34
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is Annotation in Java?
36
QUESTION
An annotation, in the java programming language is a special form of syntactic metadata that can be added to Java
Source Code.
Classes, methods, variables parameters and packages may be annotated.
Unlike Java doc tags, Java annotation are reflective, in that they are embedded in class files generated by the
compiler and may be retained by the java VM to make retrievable at run-time. Annotation is basically to attach
metadata to method, class or package. Metadata is used by the compiler to perform some basic compile-time
checking.
ANSWER
QUESTION 35
For the hottest Java jobs, please visit www.hirist.com
hirist.com
Explain Java Thread Life cycle.
37
QUESTION
The life cycle of threads in Java is very similar to the life cycle of processes running in an operating system. During its life
cycle the thread moves from one state to another depending on the operation performed by it or performed on it. A Java
thread can be in one of the following states:
• NEW: A thread that is just instantiated is in new state. When a start () method is invoked, the thread moves to the ready
state from which it is automatically moved to runnable state by the thread scheduler.
• RUNNABLE (ready_running) A thread executing in the JVM is in running state.
• BLOCKED A thread that is blocked waiting for a monitor lock is in this state. This can also occur when a thread performs
an I/O operation and moves to next (runnable) state.
• WAITING A thread that is waiting indefinitely for another thread to perform a particular action is in this state.
• TIMED_WAITING (sleeping) A thread that is waiting for another thread to perform an action up to a specified waiting time
is in this state.
• TERMINATED (dead) A thread that has exited is in this state.
ANSWER
QUESTION 36
For the hottest Java jobs, please visit www.hirist.com
hirist.com
Which way a developer should use for creating thread, i.e. Sub classing
Thread or implementing Runnable.
38
QUESTION
There are two ways of creating Thread in java (i.e. sub classing or implementing Runnable). It is very important to
understand the implication of using these two approaches.
There are two different points about using these two approaches.
• By extending the thread class, the derived class itself is a thread object and it gains full control over the thread life cycle.
Implementing the Runnable interface does not give developers any control over the thread itself, as it simply defines the
unit of work that will be executed in a thread.
• Another important point is that when extending the Thread class, the derived class cannot extend any other base classes
because Java only allows single inheritance. By implementing the Runnable interface, the class can still extend other
base classes if necessary.
To summarize, if developer needs a full control over the Thread life cycle, sub classing Thread class is a good choice, and if
programs need more flexibility by extending other class developer, should go with implementing Runnable interface.
ANSWER
QUESTION 37
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is the use of volatile in Java?
39
QUESTION
Threads are allowed to hold the values of variables in local memory (e.g. in a machine register).
If a variable is marked as volatile, every time when the variable is used, it must be read from the main memory,
similarly every time the variable is written, the value must be stored in main memory.
ANSWER
QUESTION 38
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is Downcasting ?
40
QUESTION
Downcasting means casting from a general to a more specific type.
ANSWER
QUESTION 39
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is the difference between a constructor and a method?
41
QUESTION
Method is just an ordinary member function which consists of its own return type (can be a void), name and always
invoked by a dot operator whereas a constructor is the member function of that class which is used to create
objects in it, the name of the class and constructor is always same, no return type and always invoked by a new
operator.
ANSWER
QUESTION 40
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is the common usage of serialization? What exceptions occur during
serialization?
42
QUESTION
The object need to be serialized when it’s sent over a network and when it’s state is saved. Exceptions which occur
during serialization are:
a. transient fields
b. when the base class is serialized then only base class fields are handled.
c. Static fields are ignored because they are not part of any state.
ANSWER
QUESTION 41
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is the difference between preemptive scheduling and time slicing?
43
QUESTION
Under preemptive scheduling, is the highest priority task which is executed until it enters the waiting or dead states
or a higher priority task. Under time slicing, a task executes for a predefined slice of time and then reenters the
pool of ready tasks. The scheduler then determines which task should execute next, based on priority and other
factors.
ANSWER
QUESTION 42
For the hottest Java jobs, please visit www.hirist.com
hirist.com
How applets communicate with each other?
44
QUESTION
Applets may communicate with other applets running in the same virtual machine but it’s not necessary that they
communicate. If the applets are of the same class, they can communicate via shared static variables. If the
applets are of different classes, then each will need a reference to the same class with static variables. In any
case the basic idea is to pass the information back and forth through a static variable.
ANSWER
QUESTION 43
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is the similarity between Dynamic Binding and linking?
45
QUESTION
Dynamic binding is orthogonal to dynamic linking. Binding refers to the linking of a procedure call to the code to be
executed in response to the call. Dynamic binding It is associated with polymorphism and inheritance, it(also
known as late binding) means that the code associated with a given procedure call is not known until the time of
the call at run-time.
ANSWER
QUESTION 44
For the hottest Java jobs, please visit www.hirist.com
hirist.com
How are Observer and Observable used?
46
QUESTION
List of observers are maintained by the objects that are subclasses the observable class. When an Observable
object is updated it invokes the update() method of each of its observers to notify the observers that it has
changed state. The Observer interface is implemented by objects that observe Observable objects.
ANSWER
QUESTION 45
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is the difference between an Inner Class and a Sub-Class?
47
QUESTION
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.
A sub-class is a class which inherits from another class called super class. Sub-class can access all public and
protected methods and fields of its super class
ANSWER
QUESTION 46
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is data encapsulation and what’s its significance?
48
QUESTION
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.
ANSWER
QUESTION 47
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is a singleton class? Give a practical example of its usage.
49
QUESTION
A singleton class in java can have only one instance and hence all its methods and variables belong to just one
instance. Singleton class concept is useful for the situations when there is a need to limit the number of objects for
a class.
The best example of singleton usage scenario is when there is a limit of having only one connection to a database
due to some driver limitations or because of any licensing issues.
ANSWER
QUESTION 48
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What are Loops in Java? What are three types of loops?
50
QUESTION
Looping is used in programming to execute a statement or a block of statement repeatedly. There are three types of loops in
Java:
1) For Loops
For loops are used in java to execute statements repeatedly for a given number of times. For loops are used when number
of times to execute the statements is known to programmer.
2) While Loops
While loop is used when certain statements need to be executed repeatedly until a condition is fulfilled. In while loops,
condition is checked first before execution of statements.
3) Do While Loops
Do While Loop is same as While loop with only difference that condition is checked after execution of block of statements.
Hence in case of do while loop, statements are executed at least once.
ANSWER
QUESTION 49
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is the difference between continue and break statement?
51
QUESTION
break and continue are two important keywords used in Loops. When a break keyword is used in a loop, loop is
broken instantly while when continue keyword is used, current iteration is broken and loop continues with next
iteration
ANSWER
QUESTION 50
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is ternary operator? Give an example.
52
QUESTION
Ternary operator, also called conditional operator is used to decide which value to assign to a variable based on a
Boolean value evaluation. It’s denoted as?
In the below example, if rank is 1, status is assigned a value of “Done” else “Pending”.
ANSWER
QUESTION 51
1234567
public class conditionTest {public static void main(string args[]) {String status;int rank;status= (rank == 1) ? "Done": "Pending";}}
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What are 6 different types of operators in Java?
53
QUESTION
In java, operators can be classified in following six types:
• Arithmetic Operators
Used for arithmetic calculations. E.g. +,-,*,/,%,++,–
• Relational Operators
Used for relational comparison. E.g. ==,!=, >,<,<=,>=
• Bitwise operators
Used for bit by bit operations. E.g. &,|,^,~
• Logical Operators
Used for logical comparisons. E.g. &&,||,!
• Assignment Operators
Used for assigning values to variables. E.g. =,+=,-=,*=,/=
ANSWER
QUESTION 52
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What’s the base class in Java from which all classes are derived?
54
QUESTION
java.lang.object
ANSWER
QUESTION 53
For the hottest Java jobs, please visit www.hirist.com
hirist.com
When the constructor of a class is invoked?
55
QUESTION
The constructor of a class is invoked every time an object
is created with new keyword. For example, in the
following class two objects are created using new
keyword and hence, constructor is invoked two times.
1 public class const_example {
2
3 const_example() {
4
5 System.out.println("Inside constructor");
6
7 }
8
9 Public static void main(String args[]) {
10
11 const_example c1=new const_example();
12
13 const_example c2=new const_example();
14
15 }
16
17 }
ANSWER
QUESTION 54
For the hottest Java jobs, please visit www.hirist.com
hirist.com
Can we override static methods of a class?
56
QUESTION
We cannot override static methods. Static methods belong to a class and not to individual objects and are resolved
at the time of compilation (not at runtime).Even if we try to override static method,we will not get an complitaion
error,nor the impact of overriding when running the code.
ANSWER
QUESTION 55
For the hottest Java jobs, please visit www.hirist.com
hirist.com
Is String a data type in java?
57
QUESTION
String is not a primitive data type in java. When a string is created in java, it’s actually an object of Java.Lang.String
class that gets created. After creation of this string object, all built-in methods of String class can be used on the
string object.
ANSWER
QUESTION 56
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is multi-threading?
58
QUESTION
Multi threading is a programming concept to run multiple tasks in a concurrent manner within a single program.
Threads share same process stack and running in parallel. It helps in performance improvement of any program.
ANSWER
QUESTION 57
For the hottest Java jobs, please visit www.hirist.com
hirist.com
How objects of a class are created if no constructor is defined in the class?
59
QUESTION
Even if no explicit constructor is defined in a java class, objects get created successfully as a default constructor is
implicitly used for object creation. This constructor has no parameters.
ANSWER
QUESTION 58
For the hottest Java jobs, please visit www.hirist.com
hirist.com
Give an example of use of Pointers in Java class.
60
QUESTION
There are no pointers in Java. So we can’t use concept of pointers in Java.
ANSWER
QUESTION 59
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What’s difference between Stack and Queue?
61
QUESTION
Stack and Queue both are used as placeholder for a collection of data. The primary difference between a stack and
a queue is that stack is based on Last in First out (LIFO) principle while a queue is based on FIFO (First In First
Out) principle.
ANSWER
QUESTION 60
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What are the two environment variables that must be set in order to run
any Java programs?
62
QUESTION
Java programs can be executed in a machine only once following two environment variables have been properly
set:
1. PATH variable
2. CLASSPATH variable
ANSWER
QUESTION 61
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What are checked exceptions?
63
QUESTION
Checked exception are those which the Java compiler forces you to catch.Example:IOException are checked
exceptions.
ANSWER
QUESTION 62
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What are runtime exceptions?
64
QUESTION
Runtime exceptions are those exceptions that are thrown at runtime because of either wrong input data or because
of wrong business logic etc. These are not checked by the compiler at compile time.
ANSWER
QUESTION 63
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What are wrapper classes?
65
QUESTION
Java provides specialized classes corresponding to each of the primitive data types. These are called wrapper
classes.They are example: Integer, Character, Double etc.
ANSWER
QUESTION 64
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is an abstract class?
66
QUESTION
Abstract class must be extended/subclassed (to be useful). It serves as a template. A class that is abstract may not
be instantiated (ie. you may not call its constructor), abstract class may contain static data.Any class with an
abstract method is automatically abstract itself, and must be declared as such. A class may be declared abstract
even if it has no abstract methods. This prevents it from being instantiated.
ANSWER
QUESTION 65
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is the default value of the local variables?
67
QUESTION
The local variables are not initialized to any default value, neither primitives nor object references. If you try to use
these variables without initializing them explicitly, the java compiler will not compile the code. It will complain abt
the local varaible not being initilized.
ANSWER
QUESTION 66
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is the difference between a Choice and a List?
68
QUESTION
A Choice is displayed in a compact form that must be pulled down, in order for a user to be able to see the list of all
available choices. Only one item may be selected from a Choice. A List may be displayed in such a way that
several List items are visible. A List supports the selection of one or more List items.
ANSWER
QUESTION 67
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is clipping?
69
QUESTION
Clipping is defined as the process of confining paint operations to a limited area or shape.
ANSWER
QUESTION 68
For the hottest Java jobs, please visit www.hirist.com
hirist.com
Does Java support multiple inheritance?
70
QUESTION
No, Java does not support multiple inheritance. Each class is able to extend only on one class, but is able to
implement more than one interfaces.
ANSWER
QUESTION 69
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What’s a deadlock ?
71
QUESTION
A condition that occurs when two processes are waiting for each other to complete, before proceeding. The result is
that both processes wait endlessly.
ANSWER
QUESTION 70
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is difference between Path and Classpath ?
72
QUESTION
Path and Classpath are operating system level environment variales. Path is used define where the system can find
the executables(.exe) files and classpath is used to specify the location .class files.
ANSWER
QUESTION 71
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What are instance variables ?
73
QUESTION
Instance variables are those which are defined at the class level. Instance variables need not be initialized before
using them as they are automatically initialized to their default values.
ANSWER
QUESTION 72
For the hottest Java jobs, please visit www.hirist.com
hirist.com
How to define a constant variable in Java ?
74
QUESTION
The variable should be declared as static and final. So only one copy of the variable exists for all instances of the
class and the value can’t be changed also. static final int PI = 2.14; is an example for constant.
ANSWER
QUESTION 73
For the hottest Java jobs, please visit www.hirist.com
hirist.com
Can a class be declared as protected ?
75
QUESTION
The protected access modifier cannot be applied to class and interfaces. Methods, fields can be declared protected,
however methods and fields in a interface cannot be declared protected.
ANSWER
QUESTION 74
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is the access scope of a protected method ?
76
QUESTION
A protected method can be accessed by the classes within the same package or by the subclasses of the class in
any package.
ANSWER
QUESTION 75
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What are the restriction imposed on a static method or a static block of code ?
77
QUESTION
A static method should not refer to instance variables without creating an instance and cannot use “this” operator to
refer the instance.
ANSWER
QUESTION 76
For the hottest Java jobs, please visit www.hirist.com
hirist.com
Can we declare a static variable inside a method ?
78
QUESTION
Static varaibles are class level variables and they can’t be declared inside a method. If declared, the class will not
compile.
ANSWER
QUESTION 77
For the hottest Java jobs, please visit www.hirist.com
hirist.com
Can a method inside a Interface be declared as final ?
79
QUESTION
No not possible. Doing so will result in compilation error. public and abstract are the only applicable modifiers for
method declaration in an interface.
ANSWER
QUESTION 78
For the hottest Java jobs, please visit www.hirist.com
hirist.com
Why is an Interface be able to extend more than one Interface but a Class can’t extend
more than one Class ?
80
QUESTION
Basically Java doesn’t allow multiple inheritance, so a Class is restricted to extend only one Class. But an Interface
is a pure abstraction model and doesn’t have inheritance hierarchy like classes(do remember that the base class
of all classes is Object). So an Interface is allowed to extend more than one Interface.
ANSWER
QUESTION 79
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What are wrapper classes?
81
QUESTION
Java provides specialized classes corresponding to each of the primitive data types. These are called wrapper
classes. They are e.g. Integer, Character, Double etc.
ANSWER
QUESTION 80
For the hottest Java jobs, please visit www.hirist.com
hirist.com
Under what circumstances might you use the yield method of the Thread class
82
QUESTION
-To call from the currently running thread to allow another thread of the same or higher priority to run
-To call on a waiting thread to allow it to run
-To allow a thread of higher priority to run
-To call from the currently running thread with a parameter designating which thread should be allowed to run
ANSWER
QUESTION 81
For the hottest Java jobs, please visit www.hirist.com
hirist.com
If I want an object of my class to be thrown as an exception object, what should I
do?
83
QUESTION
The class should extend from Exception class. Or you can extend your class from some more precise exception
type also
ANSWER
QUESTION 82
For the hottest Java jobs, please visit www.hirist.com
hirist.com
How are Observer and Observable used?
84
QUESTION
Objects that subclass the Observable class maintain a list of observers. When an Observable object is updated it
invokes the update() method of each of its observers to notify the observers that it has changed state. The
Observer interface is implemented by objects that observe Observable objects.
ANSWER
QUESTION 83
For the hottest Java jobs, please visit www.hirist.com
hirist.com
If a class is declared without any access modifiers, where may the class be
accessed?
85
QUESTION
A class that is declared without any access modifiers is said to have package access. This means that the class can
only be accessed by other classes and interfaces that are defined within the same package.
ANSWER
QUESTION 84
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is the purpose of the finally clause of a try-catch-finally statement?
86
QUESTION
The finally clause is used to provide the capability to execute code no matter whether or not an exception is thrown
or caught.
ANSWER
QUESTION 85
For the hottest Java jobs, please visit www.hirist.com
hirist.com
Can a double value be cast to a byte?
87
QUESTION
Yes, a double value can be cast to a byte.
ANSWER
QUESTION 86
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What must a class do to implement an interface?
88
QUESTION
It must provide all of the methods in the interface and identify the interface in its implements clause.
ANSWER
QUESTION 87
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What are cookies and how will you use them?
89
QUESTION
Cookies are a mechanism that a servlet uses to have a client hold a small amount of state-information associated
with the user. a) Create a cookie with the Cookie constructor: public Cookie(String name, String value)
b) A servlet can send a cookie to the client by passing a Cookie object to the addCookie() method of
HttpServletResponse: public void HttpServletResponse. addCookie(Cookie cookie)
c) A servlet retrieves cookies by calling the getCookies() method of HttpServletRequest: public Cookie[ ]
HttpServletRequest. getCookie().
ANSWER
QUESTION 88
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is Servlet chaining?
90
QUESTION
Servlet chaining is a technique in which two or more servlets can cooperate in servicing a single request. In servlet
chaining, one servlet’s output is piped to the next servlet’s input. This process continues until the last servlet is
reached. Its output is then sent back to the client.
ANSWER
QUESTION 89
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is the difference between TCP/IP and UDP?
91
QUESTION
TCP/IP is a two-way communication between the client and the server and it is a reliable and there is a confirmation
regarding reaching the message to the destination. It is like a phone call. UDP is a one-way communication only
between the client and the server and it is not a reliable and there is no confirmation regarding reaching the
message to the destination. It is like a postal mail.
ANSWER
QUESTION 90
For the hottest Java jobs, please visit www.hirist.com
hirist.com
For which statements does it make sense to use a label?
92
QUESTION
The only statements for which it makes sense to use a label are those statements that can enclose
a break orcontinue statement.
ANSWER
QUESTION 91
For the hottest Java jobs, please visit www.hirist.com
hirist.com
How are the elements of a CardLayout organized?
93
QUESTION
The elements of a CardLayout are stacked, one on top of the other, like a deck of cards
ANSWER
QUESTION 92
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is the difference between a while statement and a do while statement?
94
QUESTION
A while statement checks at the beginning of a loop to see whether the next loop iteration should occur. A do
while statement checks at the end of a loop to see whether the next iteration of a loop should occur. The do
whilestatement will always execute the body of a loop at least once.
ANSWER
QUESTION 93
For the hottest Java jobs, please visit www.hirist.com
hirist.com
When does the compiler supply a default constructor for a class?
95
QUESTION
The compiler supplies a default constructor for a class if no other constructors are provided.
ANSWER
QUESTION 94
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is Domain Naming Service(DNS)?
96
QUESTION
It is very difficult to remember a set of numbers(IP address) to connect to the Internet. The Domain Naming
Service(DNS) is used to overcome this problem. It maps one particular IP address to a string of characters. For
example, www. mascom. com implies com is the domain name reserved for US commercial sites, moscom is the
name of the company and www is the name of the specific computer, which is mascom’s server.
ANSWER
QUESTION 95
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is a Jar file?
97
QUESTION
Jar file allows to efficiently deploying a set of classes and their associated resources. The elements in a jar file are
compressed, which makes downloading a Jar file much faster than separately downloading several
uncompressed files. The package java. util. zip contains classes that read and write jar files.
ANSWER
QUESTION 96
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What are JSP Directives?
98
QUESTION
A JSP directive affects the overall structure of the servlet class. It usually has the following form:<%@ directive
attribute=”value” %> However, you can also combine multiple attribute settings for a single directive, as
follows:<%@ directive attribute1=”value1″ attribute 2=”value2″ . . . attributeN =”valueN” %> There are two main
types of directive: page, which lets to do things like import classes, customize the servlet superclass, and the like;
and include, which lets to insert a file into the servlet class at the time the JSP file is translated into a servlet
ANSWER
QUESTION 97
For the hottest Java jobs, please visit www.hirist.com
hirist.com
How can I set a cookie in JSP?
99
QUESTION
response. setHeader(”Set-Cookie”, “cookie string”); To give the response-object to a bean, write a method
setResponse (HttpServletResponse response) - to the bean, and in jsp-file:<% bean. setResponse (response);
%>
ANSWER
QUESTION 98
For the hottest Java jobs, please visit www.hirist.com
hirist.com
What is numeric promotion?
100
QUESTION
Numeric promotion is the conversion of a smaller numeric type to a larger numeric type, so that integer and floating-
point operations may take place. In numerical promotion, byte, char, and short values are converted to int values.
The int values are also converted to long values, if necessary. The long and float values are converted to double
values, as required.
ANSWER
QUESTION 99
For the hottest Java jobs, please visit www.hirist.com
hirist.com
How are Servlets and JSP Pages related?
101
QUESTION
JSP pages are focused around HTML (or XML) with Java codes and JSP tags inside them. When a web server that
has JSP support is asked for a JSP page, it checks to see if it has already compiled the page into a servlet. Thus,
JSP pages become servlets and are transformed into pure Java and then compiled, loaded into the server and
executed.
ANSWER
QUESTION 100
For the hottest Java jobs, please visit www.hirist.com
hirist.com
List of Resources
102
http://top100interviewquestions.com/java-basics-part1/http://www.javacodegeeks.com/2014/04/java-interview-questions-and-answers.html#2http://beginnersbook.com/2013/05/java-interview-questions/#h3http://www.slideshare.net/p2cinfotech/java-frequently-asked-interview-questions-and-answershttp://www.javatpoint.com/corejava-interview-questions-1http://www.techinterviews.com/master-list-of-java-interview-questionshttp://javarevisited.blogspot.com/2014/07/top-50-java-multithreading-interview-questions-answers.htmlhttps://www.udemy.com/blog/java-interview-questions/http://careerride.com/Interview-Questions-Core-Java.aspxhttp://career.guru99.com/top-100-core-java-interview-questions/1/http://careerbaba.in/2014/01/top-50-core-java-interview-questions-and-answers/
www.hirist.com
JAVA
TOP 100INTERVIEW QUESTIONS & ANSWERS