Sunteți pe pagina 1din 103

www.hirist.

com

TOP 100
JAVA

INTERVIEW QUESTIONS & ANSWERS

hirist
.com

QUESTION 1

QUESTION

Is Java platform independent?

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

hirist
.com

QUESTION 2

QUESTION

What all memory areas are allocated by JVM?

ANSWER
a)Class(Method) Area
b)Heap,

c) Stack,
d)Program Counter Register and
e)Native Method Stack

For the hottest Java jobs, please visit www.hirist.com

hirist
.com

QUESTION 3

QUESTION

What is class?

ANSWER
Class is nothing but a template that describes the data and behavior associated with instances of that class

For the hottest Java jobs, please visit www.hirist.com

hirist
.com

QUESTION 4

QUESTION

Can a .java file contain more than one java classes?

ANSWER
Yes. A .java file contain more than one java classes, provided at the most one of them is a public class.

For the hottest Java jobs, please visit www.hirist.com

hirist
.com

QUESTION 5

QUESTION

Different Data types in Java.

ANSWER
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)

For the hottest Java jobs, please visit www.hirist.com

hirist
.com

QUESTION 6

QUESTION

What is Type casting in Java?

ANSWER
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).

For the hottest Java jobs, please visit www.hirist.com

hirist
.com

QUESTION 7

QUESTION

Four main principles of OOPS language?

ANSWER
Inheritance
Polymorphism

Data Encapsulation
Abstraction

For the hottest Java jobs, please visit www.hirist.com

hirist
.com

QUESTION 8

QUESTION

What is Function Overriding and Overloading in Java ?

ANSWER
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

For the hottest Java jobs, please visit www.hirist.com

hirist
.com

QUESTION 9

QUESTION

What is static and dynamic binding?

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

10

hirist
.com

QUESTION 10

QUESTION

What is the difference between a synchronized method and a synchronized


block ?
ANSWER
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)

For the hottest Java jobs, please visit www.hirist.com

11

hirist
.com

QUESTION 11

QUESTION

What is difference between Array and ArrayList ? When will you use Array
over ArrayList ?
ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

12

hirist
.com

QUESTION 12

QUESTION

What is OOPs?

ANSWER
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

For the hottest Java jobs, please visit www.hirist.com

13

hirist
.com

QUESTION 13

QUESTION

What is Casting?

ANSWER
Casting is used to convert the value of one type to another.

For the hottest Java jobs, please visit www.hirist.com

14

hirist
.com

QUESTION 14

QUESTION

What is JIT compiler?

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

15

hirist
.com

QUESTION 15

QUESTION

What is the main difference between Java platform and other platforms?

ANSWER
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)

For the hottest Java jobs, please visit www.hirist.com

16

hirist
.com

QUESTION 16

QUESTION

What are Constructors?

ANSWER
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 doesnt have a return type

For the hottest Java jobs, please visit www.hirist.com

17

hirist
.com

QUESTION 17

QUESTION

What is default and parameterized constructors?

ANSWER
Default: Constructors with no arguments are known as default constructors, when you dont declare any constructor
in a class, compiler creates a default one automatically.
Parameterized: Constructor with arguments are known as parameterized constructors.

For the hottest Java jobs, please visit www.hirist.com

18

hirist
.com

QUESTION 18

QUESTION

What is difference between aggregation and composition?

ANSWER
Aggregation represents weak relationship whereas composition represents strong relationship. For example: bike
has an indicator (aggregation) but bike has an engine (compostion).

For the hottest Java jobs, please visit www.hirist.com

19

hirist
.com

QUESTION 19

QUESTION

What are Encapsulation, Inheritance and Polymorphism?

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

20

hirist
.com

QUESTION 20

QUESTION

What is final, finalize() and finally?


ANSWER
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 cant be
overridden. A final variable cant 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.

For the hottest Java jobs, please visit www.hirist.com

21

hirist
.com

QUESTION 21

QUESTION

What is Garbage Collection and how to call it explicitly?

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

22

hirist
.com

QUESTION 22

QUESTION

What is meant by Inheritance and what are its advantages?

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

23

hirist
.com

QUESTION 23

QUESTION

What is the difference between superclass and subclass?

ANSWER
A super class is a class that is inherited whereas sub class is a class that does the inheriting.

For the hottest Java jobs, please visit www.hirist.com

24

hirist
.com

QUESTION 24

QUESTION

What is the difference between Integer and int?

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

25

hirist
.com

QUESTION 25

QUESTION

How do I serialize an object to a file?

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

26

hirist
.com

QUESTION 26

QUESTION

What is the difference between String and String Buffer?

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

27

hirist
.com

QUESTION 27

QUESTION

What is the difference between Array and vector?

ANSWER
Array is a set of related data type and static whereas vector is a growable array of objects and dynamic.

For the hottest Java jobs, please visit www.hirist.com

28

hirist
.com

QUESTION 28

QUESTION

What is source and listener?

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

29

hirist
.com

QUESTION 29

QUESTION

What is Thread in Java?

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

30

hirist
.com

QUESTION 30

QUESTION

What is FutureTask in Java?

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

31

hirist
.com

QUESTION 31

QUESTION

There are three threads T1, T2 and T3? How do you ensure sequence T1,
T2, T3 in Java?
ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

32

hirist
.com

QUESTION 32

QUESTION

What is blocking method in Java?

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

33

hirist
.com

QUESTION 33

QUESTION

Difference between volatile and atomic variable in Java?

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

34

hirist
.com

QUESTION 34

QUESTION

What is the Difference between JDK and JRE?

ANSWER
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) JREs along with the various development tools like the Java source
compilers, bundling and deployment tools, debuggers, development libraries, etc.

For the hottest Java jobs, please visit www.hirist.com

35

hirist
.com

QUESTION 35

QUESTION

What is Annotation in Java?

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

36

hirist
.com

QUESTION 36

QUESTION

Explain Java Thread Life cycle.


ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

37

hirist
.com

QUESTION 37

QUESTION

Which way a developer should use for creating thread, i.e. Sub classing
Thread or implementing Runnable.
ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

38

hirist
.com

QUESTION 38

QUESTION

What is the use of volatile in Java?

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

39

hirist
.com

QUESTION 39

QUESTION

What is Downcasting ?

ANSWER
Downcasting means casting from a general to a more specific type.

For the hottest Java jobs, please visit www.hirist.com

40

hirist
.com

QUESTION 40

QUESTION

What is the difference between a constructor and a method?

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

41

hirist
.com

QUESTION 41

QUESTION

What is the common usage of serialization? What exceptions occur during


serialization?
ANSWER
The object need to be serialized when its sent over a network and when its 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.

For the hottest Java jobs, please visit www.hirist.com

42

hirist
.com

QUESTION 42

QUESTION

What is the difference between preemptive scheduling and time slicing?

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

43

hirist
.com

QUESTION 43

QUESTION

How applets communicate with each other?

ANSWER
Applets may communicate with other applets running in the same virtual machine but its 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.

For the hottest Java jobs, please visit www.hirist.com

44

hirist
.com

QUESTION 44

QUESTION

What is the similarity between Dynamic Binding and linking?

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

45

hirist
.com

QUESTION 45

QUESTION

How are Observer and Observable used?

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

46

hirist
.com

QUESTION 46

QUESTION

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

ANSWER
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

For the hottest Java jobs, please visit www.hirist.com

47

hirist
.com

QUESTION 47

QUESTION

What is data encapsulation and whats its significance?

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

48

hirist
.com

QUESTION 48

QUESTION

What is a singleton class? Give a practical example of its usage.

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

49

hirist
.com

QUESTION 49

QUESTION

What are Loops in Java? What are three types of loops?

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

50

hirist
.com

QUESTION 50

QUESTION

What is the difference between continue and break statement?

ANSWER
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

For the hottest Java jobs, please visit www.hirist.com

51

hirist
.com

QUESTION 51

QUESTION

What is ternary operator? Give an example.

ANSWER
Ternary operator, also called conditional operator is used to decide which value to assign to a variable based on a
Boolean value evaluation. Its denoted as?
In the below example, if rank is 1, status is assigned a value of Done else Pending.
1 public class conditionTest {
2 public static void main(string args[]) {
3 String status;
4 int rank;
5 status= (rank == 1) ? "Done": "Pending";
6}
7}

For the hottest Java jobs, please visit www.hirist.com

52

hirist
.com

QUESTION 52

QUESTION

What are 6 different types of operators in Java?

ANSWER
In java, operators can be classified in following six types:

Arithmetic Operators

Logical Operators

Used for arithmetic calculations. E.g. +,-,*,/,%,++,

Used for logical comparisons. E.g. &&,||,!

Relational Operators

Used for relational comparison. E.g. ==,!=, >,<,<=,>=

Assignment Operators

Used for assigning values to variables. E.g. =,+=,-=,*=,/=

Bitwise operators

Used for bit by bit operations. E.g. &,|,^,~

For the hottest Java jobs, please visit www.hirist.com

53

hirist
.com

QUESTION 53

QUESTION

Whats the base class in Java from which all classes are derived?

ANSWER
java.lang.object

For the hottest Java jobs, please visit www.hirist.com

54

hirist
.com

QUESTION 54

QUESTION

When the constructor of a class is invoked?


ANSWER
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

10
11 const_example c1=new const_example();
12
13 const_example c2=new const_example();
14

15 }
16
17 }

9 Public static void main(String args[]) {

For the hottest Java jobs, please visit www.hirist.com

55

hirist
.com

QUESTION 55

QUESTION

Can we override static methods of a class?

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

56

hirist
.com

QUESTION 56

QUESTION

Is String a data type in java?

ANSWER
String is not a primitive data type in java. When a string is created in java, its 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.

For the hottest Java jobs, please visit www.hirist.com

57

hirist
.com

QUESTION 57

QUESTION

What is multi-threading?

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

58

hirist
.com

QUESTION 58

QUESTION

How objects of a class are created if no constructor is defined in the class?

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

59

hirist
.com

QUESTION 59

QUESTION

Give an example of use of Pointers in Java class.

ANSWER
There are no pointers in Java. So we cant use concept of pointers in Java.

For the hottest Java jobs, please visit www.hirist.com

60

hirist
.com

QUESTION 60

QUESTION

Whats difference between Stack and Queue?

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

61

hirist
.com

QUESTION 61

QUESTION

What are the two environment variables that must be set in order to run
any Java programs?
ANSWER
Java programs can be executed in a machine only once following two environment variables have been properly
set:
1. PATH variable
2. CLASSPATH variable

For the hottest Java jobs, please visit www.hirist.com

62

hirist
.com

QUESTION 62

QUESTION

What are checked exceptions?

ANSWER
Checked exception are those which the Java compiler forces you to catch.Example:IOException are checked
exceptions.

For the hottest Java jobs, please visit www.hirist.com

63

hirist
.com

QUESTION 63

QUESTION

What are runtime exceptions?

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

64

hirist
.com

QUESTION 64

QUESTION

What are wrapper classes?

ANSWER
Java provides specialized classes corresponding to each of the primitive data types. These are called wrapper
classes.They are example: Integer, Character, Double etc.

For the hottest Java jobs, please visit www.hirist.com

65

hirist
.com

QUESTION 65

QUESTION

What is an abstract class?

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

66

hirist
.com

QUESTION 66

QUESTION

What is the default value of the local variables?

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

67

hirist
.com

QUESTION 67

QUESTION

What is the difference between a Choice and a List?

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

68

hirist
.com

QUESTION 68

QUESTION

What is clipping?

ANSWER
Clipping is defined as the process of confining paint operations to a limited area or shape.

For the hottest Java jobs, please visit www.hirist.com

69

hirist
.com

QUESTION 69

QUESTION

Does Java support multiple inheritance?

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

70

hirist
.com

QUESTION 70

QUESTION

Whats a deadlock ?

ANSWER
A condition that occurs when two processes are waiting for each other to complete, before proceeding. The result is
that both processes wait endlessly.

For the hottest Java jobs, please visit www.hirist.com

71

hirist
.com

QUESTION 71

QUESTION

What is difference between Path and Classpath ?

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

72

hirist
.com

QUESTION 72

QUESTION

What are instance variables ?

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

73

hirist
.com

QUESTION 73

QUESTION

How to define a constant variable in Java ?

ANSWER
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 cant be changed also. static final int PI = 2.14; is an example for constant.

For the hottest Java jobs, please visit www.hirist.com

74

hirist
.com

QUESTION 74

QUESTION

Can a class be declared as protected ?

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

75

hirist
.com

QUESTION 75

QUESTION

What is the access scope of a protected method ?

ANSWER
A protected method can be accessed by the classes within the same package or by the subclasses of the class in
any package.

For the hottest Java jobs, please visit www.hirist.com

76

hirist
.com

QUESTION 76

QUESTION

What are the restriction imposed on a static method or a static block of code ?

ANSWER
A static method should not refer to instance variables without creating an instance and cannot use this operator to
refer the instance.

For the hottest Java jobs, please visit www.hirist.com

77

hirist
.com

QUESTION 77

QUESTION

Can we declare a static variable inside a method ?

ANSWER
Static varaibles are class level variables and they cant be declared inside a method. If declared, the class will not
compile.

For the hottest Java jobs, please visit www.hirist.com

78

hirist
.com

QUESTION 78

QUESTION

Can a method inside a Interface be declared as final ?

ANSWER
No not possible. Doing so will result in compilation error. public and abstract are the only applicable modifiers for
method declaration in an interface.

For the hottest Java jobs, please visit www.hirist.com

79

hirist
.com

QUESTION 79

QUESTION
Why is an Interface be able to extend more than one Interface but a Class cant extend
more than one Class ?
ANSWER
Basically Java doesnt allow multiple inheritance, so a Class is restricted to extend only one Class. But an Interface
is a pure abstraction model and doesnt 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.

For the hottest Java jobs, please visit www.hirist.com

80

hirist
.com

QUESTION 80

QUESTION

What are wrapper classes?

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

81

hirist
.com

QUESTION 81

QUESTION

Under what circumstances might you use the yield method of the Thread class

ANSWER
-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

For the hottest Java jobs, please visit www.hirist.com

82

hirist
.com

QUESTION 82

QUESTION
If I want an object of my class to be thrown as an exception object, what should I
do?
ANSWER
The class should extend from Exception class. Or you can extend your class from some more precise exception
type also

For the hottest Java jobs, please visit www.hirist.com

83

hirist
.com

QUESTION 83

QUESTION

How are Observer and Observable used?

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

84

hirist
.com

QUESTION 84

QUESTION
If a class is declared without any access modifiers, where may the class be
accessed?
ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

85

hirist
.com

QUESTION 85

QUESTION

What is the purpose of the finally clause of a try-catch-finally statement?

ANSWER
The finally clause is used to provide the capability to execute code no matter whether or not an exception is thrown
or caught.

For the hottest Java jobs, please visit www.hirist.com

86

hirist
.com

QUESTION 86

QUESTION

Can a double value be cast to a byte?

ANSWER
Yes, a double value can be cast to a byte.

For the hottest Java jobs, please visit www.hirist.com

87

hirist
.com

QUESTION 87

QUESTION

What must a class do to implement an interface?

ANSWER
It must provide all of the methods in the interface and identify the interface in its implements clause.

For the hottest Java jobs, please visit www.hirist.com

88

hirist
.com

QUESTION 88

QUESTION

What are cookies and how will you use them?

ANSWER
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().

For the hottest Java jobs, please visit www.hirist.com

89

hirist
.com

QUESTION 89

QUESTION

What is Servlet chaining?

ANSWER
Servlet chaining is a technique in which two or more servlets can cooperate in servicing a single request. In servlet
chaining, one servlets output is piped to the next servlets input. This process continues until the last servlet is
reached. Its output is then sent back to the client.

For the hottest Java jobs, please visit www.hirist.com

90

hirist
.com

QUESTION 90

QUESTION

What is the difference between TCP/IP and UDP?

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

91

hirist
.com

QUESTION 91

QUESTION

For which statements does it make sense to use a label?

ANSWER
The only statements for which it makes sense to use a label are those statements that can enclose
a break orcontinue statement.

For the hottest Java jobs, please visit www.hirist.com

92

hirist
.com

QUESTION 92

QUESTION

How are the elements of a CardLayout organized?

ANSWER
The elements of a CardLayout are stacked, one on top of the other, like a deck of cards

For the hottest Java jobs, please visit www.hirist.com

93

hirist
.com

QUESTION 93

QUESTION
What is the difference between a while statement and a do while statement?

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

94

hirist
.com

QUESTION 94

QUESTION

When does the compiler supply a default constructor for a class?

ANSWER
The compiler supplies a default constructor for a class if no other constructors are provided.

For the hottest Java jobs, please visit www.hirist.com

95

hirist
.com

QUESTION 95

QUESTION

What is Domain Naming Service(DNS)?

ANSWER
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 mascoms server.

For the hottest Java jobs, please visit www.hirist.com

96

hirist
.com

QUESTION 96

QUESTION

What is a Jar file?

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

97

hirist
.com

QUESTION 97

QUESTION

What are JSP Directives?

ANSWER
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

For the hottest Java jobs, please visit www.hirist.com

98

hirist
.com

QUESTION 98

QUESTION

How can I set a cookie in JSP?

ANSWER
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);
%>

For the hottest Java jobs, please visit www.hirist.com

99

hirist
.com

QUESTION 99

QUESTION

What is numeric promotion?

ANSWER
Numeric promotion is the conversion of a smaller numeric type to a larger numeric type, so that integer and floatingpoint 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.

For the hottest Java jobs, please visit www.hirist.com

100

hirist
.com

QUESTION 100

QUESTION

How are Servlets and JSP Pages related?

ANSWER
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.

For the hottest Java jobs, please visit www.hirist.com

101

hirist
.com

List of Resources

http://top100interviewquestions.com/java-basics-part1/
http://www.javacodegeeks.com/2014/04/java-interview-questions-and-answers.html#2
http://beginnersbook.com/2013/05/java-interview-questions/#h3
http://www.slideshare.net/p2cinfotech/java-frequently-asked-interview-questions-andanswers
http://www.javatpoint.com/corejava-interview-questions-1
http://www.techinterviews.com/master-list-of-java-interview-questions
http://javarevisited.blogspot.com/2014/07/top-50-java-multithreading-interviewquestions-answers.html
https://www.udemy.com/blog/java-interview-questions/
http://careerride.com/Interview-Questions-Core-Java.aspx
http://career.guru99.com/top-100-core-java-interview-questions/1/
http://careerbaba.in/2014/01/top-50-core-java-interview-questions-and-answers/

For the hottest Java jobs, please visit www.hirist.com

102

www.hirist.com

TOP 100
JAVA

INTERVIEW QUESTIONS & ANSWERS

S-ar putea să vă placă și