Вы находитесь на странице: 1из 29

6/21/2017 Java Interview Questions and Answers | Java J2EEBrain - Part 2

J2EEBrain

JAVA INTERVIEW QUESTIONS

LEAVE A COMMENT

What are the important principles of object oriented concepts?


The three important OO principles are Encapsulation, Inheritance and polymorphism.

Explain the principle of encapsulation?


Encapsulation is a process of binding or wrapping the data and the codes that operates on the data into a
single entity. This keeps the data safe from outside

interface and misuse. One way to think about encapsulation is as a protective wrapper that prevents
code and data from being arbitrarily accessed by other code de ned

outside the wrapper.

Explain polymorphism principle?


The meaning of Polymorphism is something like one name many forms. Polymorphism enables one enti-
ty to be used as general category for different types of actions. The

http://www.j2eebrain.com/java-J2ee-java-interview-questions.html/2 1/3
6/21/2017 Java Interview Questions and Answers | Java J2EEBrain - Part 2

speci c action is determined by the exact nature of the situation. The concept of polymorphism can be
explained as one interface, multiple methods.

What are the different forms of polymorphism in java?


The three form of polymorphism in java point of view:

Method overloading
Method overriding

What feature of java support multiple inheritance?


Java does not directly support multiple inheritance. The concept of interface is where multiple inheri-
tance is achieved in java. Thus an interface contains only

abstract methods and constants which could be implemented by java classes.

What is the difference between getClass() and instanceOf in java?


instanceOf is an operator that compares an object of speci c type, where as getClass() method returns
the runtime class of an object. It fetches the java instance of

the given fully quali ed type name. Thus the getClass() and instanceOf does not perform the same
functionality.

Is it possible for an anonymous class to be de ned to implement an interface or


extend a class?
An anonymous class can implement an interface or extend a superclass but cannot be declared to do
both.

What is the difference between == operator and equals () method?


The == operator is used to compare object references and not the contents of the object. Where as the
equals () of object is used to compare the contents of the

object.

What is the purpose of nalize () method?

http://www.j2eebrain.com/java-J2ee-java-interview-questions.html/2 2/3
6/21/2017 Java Interview Questions and Answers | Java J2EEBrain - Part 2

Every class inherits the nalize() method from java.lang.Object. The garbage collector uses this method
when it determines that no more references to the object

exist. This method could be overridden to clean up non java resource.

In a try-catch- nally statement what is the use of nally clause?


The nally clause is used to provide the capability of for the application to execute a piece of code no
matter if the try block executed successfully or an exception

was thrown or caught.

Related Tutorials :
1. Java Generics
2. Blocks and overloading Java Tutorial
3. Introduction to Exception Handlers Java tutorial
4. Java Enums
5. Abstract Class Java Tutorial

9 0 0 0 1
1

Like

http://www.j2eebrain.com/java-J2ee-java-interview-questions.html/2 3/3
6/21/2017 Java Interview Questions and Answers | Java J2EEBrain - Part 3

J2EEBrain

JAVA INTERVIEW QUESTIONS

LEAVE A COMMENT

What does a default member mean?


A default member is one that does not have any of the modi ers like public, protected or private.

If a class contains more than one or more nal methods must the class be de-
clared nal?
No, it not required to make the class nal if it has any nal methods. But if a class is nal it is not neces-
sary that the methods should be declared as nal.

What is the difference between String class and StringBuffer class?


A String object is immutable which means the object cannot be changed. But in the case of StringBuffer
class it provides the option of modifying String.

What does Java Sandbox term mean?


Sandbox is the term used to describe the security model used by java when running programs. This mod-
el provides an interface between a java application and operating
http://www.j2eebrain.com/java-J2ee-java-interview-questions.html/3 1/4
6/21/2017 Java Interview Questions and Answers | Java J2EEBrain - Part 3

system resource that can be con gured by end user.

What is a Thread?
Thread is a light weight process. In java thread is the current program in execution. JVM allows an appli-
cation to have multiple threads of execution thus supporting

multithreading.

What are the two approaches for writing code that create a new Thread of
control?
The two approaches are:

1. Extend the Thread class in java.lang package and override the run() method
2. Implement the Runnable interface and implement run() method

What method is used to start the execution of the thread?


start() method is used to start the execution of the thread. This inturn calls the run() method.

What is the purpose of synchronized keyword?


The keyword synchronized provides a solution to protect object data from being accessed by more than
one thread, which could make the object have an invalid state.

What is collection?
A collection is a group of objects which will be treated as a single object.

What is an Iterator?
Some of the collection classes provide traversal of their contents via a java.util.Iterator interface. This
interface allows us to walk through a collection of

objects, operating on each object in turn. Generally it is not advisable to modify the collection itself while
traversing an Iterator.

Difference between hash map and hash table?

http://www.j2eebrain.com/java-J2ee-java-interview-questions.html/3 2/4
6/21/2017 Java Interview Questions and Answers | Java J2EEBrain - Part 3

The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits
nulls. (HashMap allows null values as key and value whereas

Hashtable does not allow). HashMap does not guarantee that the order of the map will remain constant
over time. HashMap is unsynchronized and Hashtable is synchronized

What is serialization?
Serialization is a mechanism by which you can save the state of an object by converting it to a byte
stream.

Difference between swing and AWT?


Both the AWT and swing supports GUI programming in Java. AWT are heavy-weight components as it
has the native code called peer. Swings are light-weight components as

it does not have the native code but built entirely in java programming language. Hence swing works
faster than AWT.

What is the difference between ServerSocket and DatagramSocket transport


protocol?
DatagramSocket is used for UDP communication which is a connectionless protocol whereas Server-
Socket is used for TCP communication which is a connection oriented

protocol

What are checked and unchecked Exception?


Checked exceptions are those which occur at compile time. Eg: IOException. Unchecked exceptions are
those which occur at run time. Eg: StringIndexOutOfBoundsException.

http://www.j2eebrain.com/java-J2ee-java-interview-questions.html/3 3/4
6/21/2017 Java Interview Questions and Answers | Java J2EEBrain - Part 3

Related Tutorials :
1. Java Generics
2. Blocks and overloading Java Tutorial
3. Introduction to Exception Handlers Java tutorial
4. Java Enums
5. Abstract Class Java Tutorial

9 0 0 0 1
1

Like

http://www.j2eebrain.com/java-J2ee-java-interview-questions.html/3 4/4
6/21/2017 Java Interview Questions and Answers | Java J2EEBrain - Part 4

J2EEBrain

JAVA INTERVIEW QUESTIONS

LEAVE A COMMENT

What is the difference between an interface and an abstract class?


An abstract class can have instance methods that implement a default behavior. An interface can only declare

constants and instance methods, but cannot implement default behavior. An class is abstract if it is de ned with the key-
word abstract or if any

functions in the class are de ned with the keyword abstract.

Describe synchronization with respect to multithreading.


With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared

resources. Without synchronization it is possible for one thread to modify a shared variable while another thread is in the
process of using or updating

the same shared variable, which can lead to signi cant errors.

What are checked and unchecked exceptions?


A checked exception is some subclass of Exception (or Exception itself), excluding class RuntimeException and its

subclasses. Making an exception checked forces client programmers to deal with the possibility that the exception will be
thrown. Unchecked

http://www.j2eebrain.com/java-J2ee-java-interview-questions.html/4 1/4
6/21/2017 Java Interview Questions and Answers | Java J2EEBrain - Part 4

exceptions are RuntimeException and any of its subclasses. Class Error and its subclasses are also unchecked. With an
unchecked exception

the compiler doesnt force client programmers to catch or declare it in a throws clause. Client programmers may not even
know that the exception could

be thrown. Checked exception must be caught at compile time, runtime exceptions do not.

What is Overriding?
When a class de nes a method using the same name, return type and arguments as a method in its superclass, the method
in the

class overrides the method in the superclass. When the method is invoked for an object of the class it is the new de nition of
the method that is

called. Methods may be overridden to be more public, not more private.

What is the default value of an object reference declared as an instance variable?


The value will be null unless de ned explicitly.

What are pass by reference and pass by value?


Pass by reference means the passing address itself rather than passing the value. Pass by value means passing a

copy of the value.

Are Objects in java passed by value or by reference?


Java only supports pass by value. With objects, the object reference itself is passed by value and so both the

original reference and parameter copy both refer to the same object.

What is serialization?
Serialization is the mechanism used to convert an object to a byte stream.

How do I serialize an object to a le?


The class whose instances are to be serialized shoule implement an interface Serializable. Then you pass the

instance to the Object OutputStream which is connected to a leoutputstream. This will save the object to a le.

Which methods of Serializable interface should I implement?


The Serializable interface is an empty interface.

http://www.j2eebrain.com/java-J2ee-java-interview-questions.html/4 2/4
6/21/2017 Java Interview Questions and Answers | Java J2EEBrain - Part 4

What are some common uses of serialization?


Objects must be serialized in order to be sent over a network or saved to disk.

What happens to object references during serialization?


The serialization mechanism generates an object graph for serialization. This is a recursive process all

referenced objects are serialized along with the target object. Therefore all referenced objects should be serializable. If any
o fthe

objects is not serializable then a NotSerializableException will be thrown.

What happens to the static elds of a class during serialization?Are these elds serialized as part
of each serialized object?

Yes, the static elds do get serialized. If the static eld is an object then it must have implemented the

Serializable interface. The static elds are serialized as part fo every object, but the commonness of the static elds across
all of the instances is

maintained even after serialization.

What is the purpose of nalization?


The purpose of nalization is to give an unreachable object the opportunity to perform an cleanup processing before the
object

is garbage collected.

What is the difference between static and non-static variables?


A static variable is associated with the class as a whole rather than with the speci c instances of a class.

Non-static variables take on unique values with each object instance.

What does the keyword nal mean in front of a class, method and member variable?
Final classes cannot be extended. Final methods cannot be overridden. Final member variables cannot

be reassigned (assigned a new value).

What is the purpose of the keyword transient?


Transient member variables are not serialized with objects.

http://www.j2eebrain.com/java-J2ee-java-interview-questions.html/4 3/4
6/21/2017 Java Interview Questions and Answers | Java J2EEBrain - Part 4

What can go wrong if you replace && w/& in the following code?

String a = null;

If (a != null && a.length() > 10) {.}

Related Tutorials :
1. Java Generics
2. Blocks and overloading Java Tutorial
3. Introduction to Exception Handlers Java tutorial
4. Java Enums
5. Abstract Class Java Tutorial

9 0 0 0 1
1

Like

http://www.j2eebrain.com/java-J2ee-java-interview-questions.html/4 4/4
6/21/2017 Java Interview Questions and Answers | Java J2EEBrain - Part 5

J2EEBrain

JAVA INTERVIEW QUESTIONS

LEAVE A COMMENT

Java Interview Questions

Can a method be overloaded based on different return type but same argument type ?

No, because the methods can be called without using their return type in which case there is ambiquity
for thecompiler.

What happens to a static var that is de ned within a method of a class ?

Cant do it. Youll get a compilation error.

How many static init can you have ?

As many as you want, but the static initializers and class variable initializers are executed in textual or-
der and maynot refer to class variables declared in the class whose declarations appear textually after
the use, even though these class variables are in scope

http://www.j2eebrain.com/java-J2ee-java-interview-questions.html/5 1/4
6/21/2017 Java Interview Questions and Answers | Java J2EEBrain - Part 5

Describe the Garbage Collection process in Java ?


The JVM spec mandates automatic garbage collection outside of the programmers control. The Sys-
tem.gc() or Runtime.gc()is merely a suggestion to the JVM to run the

GC process but is NOT guaranteed.

How do you ensure size of a primitive data type ?


You cant.

What is the difference amongst JVM Spec, JVM Implementation, JVM Runtime ?
The JVM spec is the blueprint for the JVM generated and owned by Sun. The JVM implementation is the
actualimplementation of the spec by a vendor and the JVM.Runtime is the actual running instance of a
JVM implementation.

Can RMI and Corba based applications interact ?


Yes they can. RMI is available with IIOP as the transport protocol instead of JRMP

What is the diffrence between an Abstract class and Interface ?


Interface de nes the methods. There is no implementation.

Whereas Abstract class may have few implemented methods.If some common functionality needs to be
shared.

What do you know about the garbage collector ?


Java has an inbuilt mechanisim for garbage collection.

All those objects not longer being referenced are garbage collected.i.e the memory used by these ob-
jects isreleased.

What is the use of Servlets ?


To generate dynamic content based on the request.

What is JDBC? How do you connect to the Database ?


Java Database connectivity.

Using jdbc drivers provided by db drivers.

http://www.j2eebrain.com/java-J2ee-java-interview-questions.html/5 2/4
6/21/2017 Java Interview Questions and Answers | Java J2EEBrain - Part 5

What is the difference between RMI & Corba ?


Remote method invocation : java only. uses JRMP to communication between java objects

Common object request broker architecture : platform independent. Uses IIOP to communicate be-
tween objects.

What is JAR le ?
Jar le is an archive of java classes.

What are 4 drivers available in JDBC ?

jdbc-odbc bridge

Type 4 : pure java driver. no need to have db client installed on client mcs.

What is serialization ?
Process of persisting an object.

How to call EJB from Struts?


We can call EJB from struts by using the service locator design patteren or by Using initial context with
create homeobject and getting return remote referenc object.

What are the four corner stones of OOP ?


Abstraction, Encapsulation, Polymorphism and Inheritance.

What is Downcasting ?
Downcasting is the casting from a general to a more speci c type, i.e. casting down the hierarchy.

What is the restriction on an Overridden methods accessibility ?


Overriding methods cannot reduce accessibility they can either have same or greater accessibility.

What is a stateless protocol ?


Without getting into lengthy debates, it is generally accepted that protocols like HTTP are stateless i.e.
there is noretention of state between a transaction which is a single request response combination.

http://www.j2eebrain.com/java-J2ee-java-interview-questions.html/5 3/4
6/21/2017 Java Interview Questions and Answers | Java J2EEBrain - Part 5

Related Tutorials :
1. Java Generics
2. Blocks and overloading Java Tutorial
3. Introduction to Exception Handlers Java tutorial
4. Java Enums
5. Abstract Class Java Tutorial

9 0 0 0 1
1

Like

http://www.j2eebrain.com/java-J2ee-java-interview-questions.html/5 4/4
6/21/2017 Java Interview Questions and Answers | Java J2EEBrain - Part 6

J2EEBrain

JAVA INTERVIEW QUESTIONS

LEAVE A COMMENT

Why is the main method static?


So that it can be invoked without creating an instance of that class

What is the difference between class variable, member variable and


automatic(local) variable ?
class variable is a static variable and does not belong to instance of class but rather shared across all the
instances member variable belongs to

a particular instance of class and can be called from any method of the class automatic or local variable is
created on entry to a method and has only method

scope

When are static and non static variables of the class initialized ?
The static variables are initialized when the class is loadedNon static variables are initialized just before
the constructor is called.
http://www.j2eebrain.com/java-J2ee-java-interview-questions.html/6 1/5
6/21/2017 Java Interview Questions and Answers | Java J2EEBrain - Part 6

When are automatic variable initialized?


Automatic variable have to be initialized explicitly

What is a modulo operator % ?


This operator gives the value which is related to the remainder of a divisione.g x=7%4 gives remainder 3
as an answer

What is garbage collection ?


The runtime system keeps track of the memory that is allocated and is able to determine whether that
memory is still useable. This work is usually

done in background by a low-priority thread that is referred to as garbage collector. When the gc nds
memory that is no longer accessible from any live thread it

takes steps to release it back to the heap for reuse

Does System.gc and Runtime.gc() guarantee garbage collection ?


No

What are different types of operators in java ?


Uniary ++, , +, -, |, ~, ()
Arithmetic *, /, %,+,
Shift <<, >>, >>>
Comparison =, instanceof, = =,!=Bitwise &, ^, |Short Circuit &&, ||Ternary ?:Assignment =

How does bitwise (~) operator work ?


It converts all the 1 bits in a binary value to 0s and all the 0 bits to 1s, e.g 11110000 coverts to
00001111
Can shift operators be applied to oat types.
No, shift operators can be applied only to integer or long types

What happens to the bits that fall off after shifting?


They are discarded

http://www.j2eebrain.com/java-J2ee-java-interview-questions.html/6 2/5
6/21/2017 Java Interview Questions and Answers | Java J2EEBrain - Part 6

What values of the bits are shifted in after the shift ?


In case of signed left shift >> the new bits are set to zero But in case of signed right shift it takes the val-
ue of most signi cant bit

before the shift, that is if the most signi cant bit before shift is 0 it will introduce 0, else if it is 1, it will in-
troduce 1

What are access modi ers ?


These public, protected and private, these can be applied to class, variables, constructors and methods.
But if you dont specify an access

modi er then it is considered as Friendly

Can protected or friendly features be accessed from different packages ?


No when features are friendly or protected they can be accessed from all the classes in that package but
not from classes in another package

How can you access protected features from another package ?


You can access protected features from other classes by subclassing the that class in another package,
but this cannot be done for friendly

features

What are the rules for overriding ?


Private method can be overridden by private, friendly, protected or public methods
Friendly method can be overridden by friendly, protected or public methods
Protected method can be overridden by protected or public methods
Public method can be overridden by public method

Explain modi er nal?


Final can be applied to classes, methods and variables and the features cannot be changed. Final class
cannot be subclassed, methods cannot be

overridden

http://www.j2eebrain.com/java-J2ee-java-interview-questions.html/6 3/5
6/21/2017 Java Interview Questions and Answers | Java J2EEBrain - Part 6

Can you change the reference of the nal object ?


No the reference cannot be change, but the data in that object can be changed

Can abstract modi er be applied to a variable ?


No it is applied only to class and methods

Can abstract class be instantiated ?


No abstract class cannot be instantiated i.e you cannot create a new object of this class

When does the compiler insist that the class must be abstract ?
If one or more methods of the class are abstract.
If class inherits one or more abstract methods from the parent abstract class and no implementation is
provided for that method.
If class implements an interface and provides no implementation for those methods

How is abstract class different from nal class?


Abstract class must be subclassed and nal class cannot be subclassed

Where can static modi ers be used ?


They can be applied to variables, methods and even a block of code, static methods and variables are not
associated with any instance of

class

When are the static variables loaded into the memory ?


During the class load time

When are the non static variables loaded into the memory ?
They are loaded just before the constructor is called

How can you reference static variables ?

http://www.j2eebrain.com/java-J2ee-java-interview-questions.html/6 4/5
6/21/2017 Java Interview Questions and Answers | Java J2EEBrain - Part 6

Via reference to any instance of the class


Computer comp = new Computer ();
comp.harddisk where hardisk is a static variable
comp.compute() where compute is a method
Via the class name
Computer.harddisk
Computer.compute()
Can static method use non static features of there class
No they are not allowed to use non static features of the class, they can only call static methods and can
use static data

Ads by

Related Tutorials :
1. Java Generics
2. Blocks and overloading Java Tutorial
3. Introduction to Exception Handlers Java tutorial
4. Java Enums
5. Abstract Class Java Tutorial

9 0 0 0 1
1

Like

http://www.j2eebrain.com/java-J2ee-java-interview-questions.html/6 5/5
6/21/2017 Java Interview Questions and Answers | Java J2EEBrain - Part 7

J2EEBrain

JAVA INTERVIEW QUESTIONS

SR QA Analyst /Selenium tester

Please call me 732 491 8674(Sree) Job Role SR QA Analyst


with Mobile Experience Selenium tester Duration 3 years

Java Developer

Title Sr Java Developer Location San Francisco, CA


Duration 6+ months Note ndash Looking for local candida

LEAVE A COMMENT

What are the rules for casting primitive types ?


You can cast any non Boolean type to any other non boolean type
You cannot cast a boolean to any other type; you cannot cast any other type to a boolean

What are the rules for object reference assignment and method call conversion?
An interface type can only be converted to an interface type or to object. If the new type is an interface,
it must be a superinterface of the old

type. A class type can be converted to a class type or to an interface type. If converting to a class type
the new type should be superclass of the old type. If

converting to an interface type new type the old class must implement the interface
An array maybe converted to class object, to the interface cloneable, or to an array. Only an array of ob-
ject references types may be converted to

an array, and the old element type must be convertible to the new element

http://www.j2eebrain.com/java-J2ee-java-interview-questions.html/7 1/4
6/21/2017 Java Interview Questions and Answers | Java J2EEBrain - Part 7

When do we say an exception is handled ?


When an exception is thrown in a try block and is caught by a matching catch block, the exception is con-
sidered to have been handled

When do we say an exception is not handled ?


There is no catch block that names either the class of exception that has been thrown or a class of excep-
tion that is a parent class of the one

that has been thrown, then the exception is considered to be unhandled, in such condition the execution
leaves the method directly as if no try has been executed

In what sequence does the nally block gets executed ?


If you put nally after a try block without a matching catch block then it will be executed after the try
block
If it is placed after the catch block and there is no exception then also it will be executed after the try
block
If there is an exception and it is handled by the catch block then it will be executed after the catch block

What can prevent the execution of the code in nally block ?


The death of thread
Use of system.exit()
Turning off the power to CPU
An exception arising in the nally block itself

What are the rules for catching multiple exceptions ?


A more speci c catch block must precede a more general one in the source, else it gives compilation er-
ror
Only one catch block, that is rst applicable one, will be executed

What does throws statement declaration in a method indicate ?


This indicates that the method throws some exception and the caller method should take care of han-
dling it

What are checked exception ?


http://www.j2eebrain.com/java-J2ee-java-interview-questions.html/7 2/4
6/21/2017 Java Interview Questions and Answers | Java J2EEBrain - Part 7

Checked exceptions are exceptions that arise in a correct program, typically due to user mistakes like en-
tering wrong data or I/O problems

What are runtime exceptions ?


Runtime exceptions are due to programming bugs like out of bond arrays or null pointer exceptions.

What is difference between Exception and errors ?


Errors are usually compile time and exceptions can be runtime or checked

How will you handle the checked exceptions ?


You can provide a try/catch block to handle it.
OR
Make sure method declaration includes a throws clause that informs the calling method an exception
might be thrown from this particular

method

When you extend a class and override a method, can this new method throw ex-
ceptions other than those that were declared by the original method ?
No it cannot throw, except for the subclasses of those exceptions

Is it legal for the extending class which overrides a method which throws an ex-
ception, not o throw in the overridden class ?
Yes it is perfectly legal

http://www.j2eebrain.com/java-J2ee-java-interview-questions.html/7 3/4
6/21/2017 Java Interview Questions and Answers | Java J2EEBrain - Part 7

Ads by

Related Tutorials :
1. Java Generics
2. Blocks and overloading Java Tutorial
3. Introduction to Exception Handlers Java tutorial
4. Java Enums
5. Abstract Class Java Tutorial

9 0 0 0 1
1

Like

PREVIOUS POST

JMS Interview Questions

NEXT POST

Servlet interview questions

http://www.j2eebrain.com/java-J2ee-java-interview-questions.html/7 4/4
6/21/2017 Java Interview Questions and Answers | Java J2EEBrain

J2EEBrain

JAVA INTERVIEW QUESTIONS

LEAVE A COMMENT

Java Interview Questions and Answers


Below is a good collection ofJava Interview Questions and Answers which will help you to prepare for
any interview related to Java technology.

What is the name of the class that is super class for every class?
Object

Is it possible to extend the java.lang.String class?

No. java.lang.String is declared as nal. Class declared as nal cannot be used as a parent class in
inheritance.

What is the difference between a static and non-static method?


A static method is associated with the class as a whole rather than a speci c instance of a class. Whereas
a non static method can be called only through an object

http://www.j2eebrain.com/java-J2ee-java-interview-questions.html 1/5
6/21/2017 Java Interview Questions and Answers | Java J2EEBrain

instance.

What is the purpose of garbage collection in java?


The purpose of garbage collection is to identify and discard objects that are no longer referenced by a
program so that they can be reclaimed and reused.

How many public classes are permitted within a single java class le?
We can de ne only one public class within a single java class le.

What is the command line utility used to compile java source code into bytecode?
javac is the command line utility to compile java source code.

Why is the main() method always declared as a static method?


The main() method is declared as a static method so that it can be invoked without having to create an
instance of the corresponding class.

How are constants declared in java?


Declaring a variable as nal makes the variable constant.

What is an object wrapper?


An object wrapper is a set of java classes that are used to change basic data type such as int or oat into
Objects.

Eg. java.lang.Integer, java.lang.Float etc.

How are command line arguments passed in java?


The command line arguments are passed as an array of Strings. If the application requires some input in
any particular data type format then the string passed should

be type casted to the required data type.

What is static initializer code ?

http://www.j2eebrain.com/java-J2ee-java-interview-questions.html 2/5
6/21/2017 Java Interview Questions and Answers | Java J2EEBrain

A class can have a block of initializer code that is simply surrounded by curly braces and labeled as static
e.g.
public class Demo{
static int =10;
static{
System.out.println(Hello world);
}
}
And this code is executed exactly once at the time of class load

Where is native modi er used?


It can refer only to methods and it indicates that the body of the method is to be found else where and it
is usually written in non java

language

What are transient variables?


A transient variable is not stored as part of objects persistent state and they cannot be nal or static

What is synchronized modi er used for ?


It is used to control access of critical code in multithreaded programs

What are volatile variables?


It indicates that these variables can be modi ed asynchronously

What are the rules for Object reference casting ?


Casting from Old types to Newtypes

Compile time rules

When both Oldtypes and Newtypes are classes, one should be subclass of the other

When both Oldtype ad Newtype are arrays, both arrays must contain reference types (not primitive),
and it must be legal to cast an element of Oldtype to an element

http://www.j2eebrain.com/java-J2ee-java-interview-questions.html 3/5
6/21/2017 Java Interview Questions and Answers | Java J2EEBrain

ofNewtype

You can always cast between an interface and a non- nal object

Runtime rules

If Newtype is a class. The class of the expression being converted must be Newtype or must inherit from
Newtype

If NewType is an interface, the class of the expression being converted must implement Newtype

When do you use continue and when do you use break statements ?
When continue statement is applied it prematurely completes the iteration of a loop. When break state-
ment is applied it causes the entire loop to be abandoned.

What is the base class from which all exceptions are subclasses?
All exceptions are subclasses of a class called java.lang.Throwable

How do you intercept and thereby control exceptions ?


We can do this by using try/catch/ nally blocks

You place the normal processing code in try block.

You put the code to deal with exceptions that might arise in try block in catch block

Code that must be executed no matter what happens must be place in nally block

What are the rules for primitive arithmetic promotion conversion ?

For Unary operators :

If operant is byte, short or a char it is converted to an int


If it is any other type it is not converted

For binary operands :

If one of the operands is double, the other operand is converted to double


http://www.j2eebrain.com/java-J2ee-java-interview-questions.html 4/5
6/21/2017 Java Interview Questions and Answers | Java J2EEBrain

Else If one of the operands is oat, the other operand is converted to oat

Else If one of the operands is long, the other operand is converted to long

Else both the operands are converted to int

Related Tutorials :
1. Java Generics
2. Blocks and overloading Java Tutorial
3. Introduction to Exception Handlers Java tutorial
4. Java Enums
5. Abstract Class Java Tutorial

9 0 0 0 1
1

Like

http://www.j2eebrain.com/java-J2ee-java-interview-questions.html 5/5

Вам также может понравиться