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

Java interview questions and answers

Explain the importance of DriverManager.

The basic service to manage set of JDBC drivers.

What are the methods in Object?

clone, equals, wait, finalize, getClass, hashCode, notify, notifyAll, toString

Define a nested class.

If all the methods of a inner class is static then it is a nested class.

What is garbage collection?

Reclaiming the unused memory by the invalid objects. Garbage collector is responsible for this
process

Difference between LinkedList and ArrayList.

LinkedList are meant for sequential accessing.

ArrayList are meant for random accessing.

Define a package.

Packages provides wide namespace ability and allows to group set of classes into a single unit.

Raj 12-5-2011 03:10 AM

Java interview questions and answers

Explain the significance of ListIterator.

Using ListIterator you can iterate back and forth.

Can you explain inner class.

If the methods of the inner class can only be accessed via the instance of the inner class, then it
is called inner class.

Can you explain the meaning of aggregation and composition

Aggregation - It is a special type of composition. If you expose all the methods of a composite
class and route the method call to the composite method through its

reference, then it is called aggregation.

Composition - Holding the reference of the other class within some other class is known as
composition.

Is it possible to instantiate the Math class?

You can’t instantiate the math class.

Pankaj 12-5-2011 03:10 AM

Java interview questions and answers

Define Locale.

A Locale object represents a specific geographical, political, or cultural region.

How will you load a specific locale?

By using ResourceBundle.getBundle(…);

Is JVM a compiler or an interpreter?

Interpreter

Can you explain the usages of Class.forName()?

It loads the class into the ClassLoader. It returns the Class. Using that you can get the instance
( “class-instance”.newInstance() ).

Inq adds a question: Expain the reason for each keyword of

public static void main(String args[])


Akash 12-5-2011 03:10 AM

Java interview questions and answers

Define reflection.

Reflection allows programmatic access to information about the fields, methods and
constructors of loaded classes.

Can you tell me range of byte?

128 to 127

How to invoke external process in Java.

Runtime.getRuntime().exec(….)

What is the best way to findout the time/memory consuming process?

By using profiler

Rakesh 12-5-2011 03:09 AM

Java interview questions and answers

What is skeleton and stub? Explain their purposes.

Stub is a client side representation of the server, which takes care of communicating with the
remote server.

What kind of thread is the Garbage collector thread?

Daemon thread
Explain the purpose of Void class.

The Void class is an uninstantiable placeholder class to hold a reference to the Class object
representing the primitive Java type void.

Nitin 12-5-2011 03:09 AM

Java interview questions and answers

Explain the importance of finalize method.

Finalize method cleans up some resources before it get garbage collected.

Define mutable object and immutable object.

The value of Mutable object is changeable.


Ex., StringBuffer

The value of an immutable object can't be changed

Ex., String, Integer, Float

What are the base class for Error and Exception?

Throwable

Dharam 12-5-2011 03:09 AM

Java interview questions and answers

Difference between string and stringbuffer object.

String is an immutable object.

StringBuffer is a mutable object.

Define daemon thread.

Daemon thread are the threads which can run without user intervention.
The JVM can exit when there are daemon thread by killing them abruptly.
Define a DatabaseMetaData.

It represents comprehensive information about the database as a whole.

Jitu 12-5-2011 03:08 AM

Java interview

Explain preemptive scheduling and time slicing.

In preemptive scheduling, the highest priority task executes until it enters the waiting or dead
states or a higher priority task comes into existence.

In time slicing, a task executes for a predefined slice of time and then reenters the pool of
ready tasks.

Explain the different scopes for Java variables.

3 scopes of Java variables are defined below:

Instance

Initialized to default values at the time of creation of object, and remain accessible as long as
the object accessible.

Local

Defined within a method and remain accessbile only during the course of method excecution
and fall out of scope When the method finishes

execution.

Static

Static variables are the class level variables.


Static variables are initialized when the class is loaded in JVM for the first time and remain
there as long as the class remains loaded.
Static variables are not tied to any particular object instance.

Akash 11-23-2011 02:26 AM

Java interview questions and answers


Explain the purpose of finalization.

It provides an unreachable object the opportunity to perform any cleanup processing before the
object is garbage collected.

Explain the importance of daemon thread.

Daemon thread is a low priority thread.


It runs intermittently in the back ground doing the garbage collection operation for the java
runtime system.

A daemon thread is created using setDaemon method.

Synchronized methods and synchronized statements.

Synchronized methods

Methods that are used to control access to an object.


A thread only executes a synchronized method after it has acquired the lock for the method's
object or class.

Synchronized statements

They are similar to synchronized methods.


A synchronized statement can only be executed after a thread has acquired the lock for the
object or class referenced in the synchronized statement.

Nitin 11-23-2011 01:31 AM

Java interview questions and answers

What are the way of using thread?

Can be implemented by using runnable interface


By inheriting from the Thread class.

Difference between a constructor and a method.

Constructor

A constructor is a member function of a class that is used to create objects of that class.
It has the same name as the class itself.
It has no return type, and is invoked using the new operator.
Method

A method is an ordinary member function of a class.


It has its own name, a return type, and is invoked using the dot operator.

Explain how to serialize an object to a file.

The class whose instances are to be serialized should implement an interface Serializable, pass
the instance to the ObjectOutputStream which is connected to a fileoutputstream. This will
save the object to a file.

Naveen 11-23-2011 01:25 AM

Java questions and answers

Access Specifiers in Java.

Public- public classes, methods, and fields can be accessed from everywhere.

Protected-

Protected methods and fields can only be accessed within the same class, within its subclasses,
and within classes of the same package.

Default(no specifier)

Such a class, method, or field will be accessible from inside the same package to which the
class, method, or field belongs, but not from outside this package.

Private

Private methods and fields can only be accessed within the same class.
Private methods and fields are not visible within subclasses
They are not inherited by subclasses.

Explain static methods.

Static methods are declared with the keyword static as modifier.


They are called Static because they affect a class as a whole, not a particular instance of the
class.
They are always invoked without reference to a particular instance of a class.
Restrictions of using static methods:

It can only call other static methods.


It must only access static data.
It cannot reference to the current object using keywords super or this.

Priya 11-23-2011 12:55 AM

Java interview questions

Can you tell me the main Implementations of the Set interface?

HashSet
TreeSet
LinkedHashSet
EnumSet

Explain the importance of HashSet.

It is an unsorted, unordered Set.


It uses the hashcode of the object being inserted.
You can use it when you want a collection with no duplicates and you don’t care about order
when you iterate through it.

Explain the importance of TreeSet.

It is a set implementation keeping the elements in sorted order.


The elements are sorted according to the natural order of elements or by the comparator
provided at creation time.

Tina 11-23-2011 12:46 AM

Java interview questions and answers

Define native method.

A native method is implemented in a language other than Java.

Explain explicit casting.

In Explicit casting, the complier are specifically informed about transforming the object.
Example

long a = 890.20;

int b = (int) a; //Explicit casting

Explain implicit casting.

Assigning one entity to another without any transformation guidance to the compiler is implicit
casting.
This type of casting is not permitted in all kinds of transformations and may not work for all
scenarios.

Example

int a = 6000;

long b = a; //Implicit casting

Explain reflection API

Reflection is the process of introspecting the features and state of a class at runtime and
dynamically manipulate at run time.
This is supported using Reflection API with built-in classes like Class, Method, Fields,
Constructors etc.

Example: Using Java Reflection API we can get the class name, by using the getName method.

Rajeev 11-22-2011 04:07 AM

Java interview questions and answers

Explain the importance of Java Virtual Machine (JVM).

It converts .java file into .class file by using Compiler and Interpreter reads byte codes.

Explain the different types of access modifiers in Java.

They determine the type of access to the member of a class.

Types:

-Public : accessible to all classes


-Protected : accessible to the classes within the same package and any subclasses.
-Private : accessible only to the class to which they belong
-Default : accessible to the class to which they belong and to subclasses within the same
package

Why there are no global variables in Java?

Global variables are globally accessible and hence can create collisions in namespace.

What is the Java API?

It a large collection of software components that provide capabilities, such as graphical user
interface (GUI) widgets.

Rajeev 11-22-2011 03:48 AM

Java interview questions and answers

Explain StringTokenizer.

It is utility class that are used to break up string.

Example:

StringTokenizer str = new StringTokenizer(“Welcome”);

while (str.hasMoreTokens()) {

System.out.println(st.nextToken());

Nadeem 11-22-2011 03:42 AM

Java interview questions and answers

Difference between the boolean & operator and the && operator.

When boolean & operator is evaluated, both operands are evaluated


&& operator is a short cut operator.

When && operator is evaluated, the first operand is evaluated.


When first operand returns a value of true then the second operand is evaluated.
When first operand evaluates to false, the evaluation of the second operand is skipped.
Nadeem 11-22-2011 03:40 AM

Java interview questions

Does Java support pointers?

Java doesn't support the usage of pointers. Improper handling of pointers leads to memory
leaks which is why pointer concept hasn't found place in Java.

Swing and Awt

AWT are heavy-weight componenets.


Swings are light-weight components and this is reason why swing works faster than AWT.

Pass by reference and passby value

Pass By Reference is the passing the address itself rather than passing the value.
Passby Value is passing a copy of the value to be passed.

Rakesh S 11-4-2011 01:26 AM

Java interview questions

Abstract class

It must be extended or subclassed.


It acts as a template.
It may contain static data.
A class may be declared abstract even if it has no abstract methodsand this prevents it from
being instantiated.

Annie 11-4-2011 01:21 AM

Java interview questions and answers - April 18, 2011


What is an abstract method?

An abstract method is a method which doesn’t have a body, just declared with modifier abstract.

Explain the use of the finally block.


Finally block is a block which always executes. The block executes even when an exception is
occurred. The block won't execute only when the user calls System.exit()

What is the initial state of a thread?

It is in a ready state.

What is time slicing?

In time slicing, the task continues its execution for a predefined period of time and reenters the
pool of ready tasks.

What are Wrapper Classes?

Wrapper Classes allow to access primitives as objects.

What is List interface?

List is an ordered collection of objects.

Can you explain transient variables in java?

They are the variables that cannot be serialized.

What is synchronization?

Synchronization ensures only one thread to access a shared resource, thus controls the access of
multiple threads to shared resources.

What is serialization?

Serialization helps to convert the state of an object into a byte stream.

What is HashMap and Map?

Map is Interface and Hashmap is class that implements that.

Java interview questions and answers - April


20, 2011
What is StringBuffer class?
StringBuffer class is same as String class with the exception that it is mutable. It allows change
and doesn’t create a new instance on change of value.

How can you force garbage collection?

It is not possible to force GC. We can just request it by calling System.gc().

Is it possible an exception to be rethrown?

Yes, an exception can be rethrown.

What is the return type of a program’s main() method?

A program’s main() method has a void return type.

Which package is always imported by default?

The java.lang package is always imported by default.

What is a Class?

A class implements the behavior of member objects by describing all the attributes of objects and
the methods.

What is an Object?

An object is the members of a class. It is the basic unit of a system. It has attributes, behavior and
identity.

Explain the use of "instanceOf" keyword.

"instanceOf" keyword is used to check the type of object.

How do you refer to a current instance of object?

You can refer the current instance of object using "this" keyword.

What is the use of JAVAP tool?

JAVAP is used to disassemble compiled Java files. This option is useful when original source
code is not available.

In which package is the applet class located?

Applet classes are located in "java.applet" package.


Java array vs. ArrayList class.

ArrayList is a dynamic array that can grow depending on demand whereas Java arrays are fixed
length.

Explain Enumeration Interface.

It defines the methods using which we can enumerate the elements in a collection of objects.

What are access modifiers?

Access modifiers determine if a method or a data variable can be accessed by another method in
another class.

Explain the impact of private constructor.

Private constructor prevents a class from being explicitly instantiated by callers.

What is an exception?

An exception is an abnormal condition that arises in a code sequence at run time

Java interview questions and answers - April


21, 2011
What are ways to create threads?

There are two ways to create a thread:

extend the java.lang.Thread class


implement the java.lang.Runnable interface

How can we stop a thread programmatically?

thread.stop;

What are daemon threads?

Daemon threads are designed to run in background. An example of such thread is garbage
collector thread.

What are the different types of locks in JDBC?


There are four types of major locks in JDBC:

Exclusive locks
Shared locks
Read locks
Update locks

What are Servlets?

Servlets are program that run under web server environments.

What are the different ways to maintain state between requests?

There are four different ways:

URL rewriting
Cookies
Hidden fields
Sessions

What are wrapper classes?

In Java we have classes for each primitive data types. These classes are called as wrapper class.
For example, Integer, Character, Double etc.

What are checked exceptions?

There are exceptions that are forced to catch by Java compiler, e.g IOException. Those
exceptions are called checked exceptions.

What is the Locale class?

Locale class is a class that converts the program output to a particular geographic, political, or
cultural region

Is main a keyword in Java?

No, main is not a keyword in Java.

What is the most important feature of Java?

Platform independency makes Java a premium language.

What is a JVM?
JVM is Java Virtual Machine which is a run time environment for the compiled java class files.

Does Java support multiple inheritances?

No, Java doesn't support multiple inheritances.

What is the base class of all classes?

java.lang.Object

Can a class be declared as protected?

A class can't be declared as protected. Only methods can be declared as protected.

Can an abstract class be declared final?

No, since it is obvious that an abstract class without being inherited is of no use.

Java interview questions and answers - April


22, 2011
Can we declare a variable as abstract?

Variables can't be declared as abstract. Only classes and methods can be declared as abstract.

Define Marker Interface.

An Interface which doesn't have any declaration inside but still enforces a mechanism.

What is an abstract method?

An abstract method is a method whose implementation is deferred to a subclass.

When can an object reference be cast to an interface reference?

An object reference is cast to an interface reference when the object implements the referenced
interface.

Which class is extended by all other classes?

The Object class is extended by all other classes.

What is the return type of a program's main() method?


void.

What are the eight primitive Java types?

The eight primitive types are byte, char, short, int, long, float, double, and boolean.

Difference between a public and a non-public class.

A public class may be accessed outside of its package. A non-public class may not be accessed
outside of its package.

Which Java operator is right associative?

The = operator is right associative.

What is a transient variable?

Transient variable is a variable that may not be serialized

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