Core Java Interview Questions-->1
1) What is the difference between an
Abstract class and Interface?
1. Abstract classes may have some executable
methods and methods left
unimplemented. Interfaces contain no
implementation code.
2. An class can implement any number of
interfaces, but subclass at most one abstract class.
3. An abstract class can have
nonabstract methods. All methods of an interface are abstract.
4. An abstract class can have instance
variables. An interface cannot.
5. An abstract class can define
constructor. An interface cannot.
6. An abstract class can have any
visibility: public, protected, private or none
(package). An interface's visibility
must be public or none (package).
7. An abstract class inherits from
Object and includes methods such as clone() and equals().
2) What are checked and unchecked
exceptions?
Java defines two kinds of exceptions :
• Checked exceptions : Exceptions
that inherit from the Exception class are
checked exceptions. Client code
has to handle the checked exceptions thrown by the API, either in a catch clause or by
forwarding it outward with the throws clause.
Examples - SQLException, IOxception.
• Unchecked exceptions : RuntimeException
also extends from Exception. However, all of the exceptions that inherit from
RuntimeException get special treatment.There is no requirement for the client
code to deal with them, and hence they are called unchecked exceptions. Example
Unchecked exceptions are NullPointerException, OutOfMemoryError,
DivideByZeroException typically,programming errors.
3) What is a user defined exception?
User-defined exceptions may be
implemented by
• defining a class to respond to the
exception and
• embedding a throw statement in the try
block where the exception can occur or
declaring that the method throws the
exception (to another method where it is
handled).
The developer can define a new exception
by deriving it from the Exception
class as follows:
public class MyException extends
Exception {
/* class definition of constructors (but
NOT the exception handling code) goes here public MyException() {
super();
}
public MyException( String errorMessage
) {
super( errorMessage );
}
}
The throw statement is used to signal
the occurance of the exception within a try block.
Often, exceptions are instantiated in
the same statement in which they are thrown using the
syntax.
throw new MyException("I threw my
own exception.")
To handle the exception within the
method where it is thrown, a catch statement that
handles MyException, must follow the try
block. If the developer does not want to handle
the exception in the method itself, the
method must pass the exception using the syntax:
public myMethodName() throws MyException
4) What is the difference between C++
& Java?
Well as Bjarne Stroustrup says
"..despite the syntactic similarities, C++ and Java are very
different languages. In many ways, Java
seems closer to Smalltalk than to C++..". Here are
few I discovered:
• Java is multithreaded
• Java has no pointers
• Java has automatic memory management
(garbage collection)
• Java is platform independent
(Stroustrup may differ by saying "Java is a platform"
• Java has built-in support for comment
documentation
• Java has no operator overloading
• Java doesn’t provide multiple
inheritance
• There are no destructors in Java
5) What are statements in JAVA ?
Statements are equivalent to sentences
in natural languages. A statement forms a complete unit of execution. The following types
of expressions can be made into a statement by terminating the expression with a
semicolon
• Assignment expressions
• Any use of ++ or --
• Method calls
• Object creation expressions
These kinds of statements are called
expression statements. In addition to these kinds of expression statements, there are two
other kinds of statements. A declaration statement declares a variable. A control flow
statement regulates the order in which statements get executed. The for loop and the if
statement are both examples of control flow statements.
6) What is JAR file?
JavaARchive files are a big glob of Java
classes, images, audio, etc., compressed to make one simple, smaller file to ease Applet
downloading. Normally when a browser encounters an applet, it goes and downloads all the
files, images, audio, used by the Applet separately.
This can lead to slower downloads.
7)What is JNI?
JNI is an acronym of Java Native
Interface. Using JNI we can call functions which are written in other languages from Java. Following
are its advantages and disadvantages.
Advantages:
• You want to use your existing library
which was previously written in other
language.
• You want to call Windows API function.
• For the sake of execution speed.
• You want to call API function of some
server product which is in c or c++ from java client.
Disadvantages:
• You can’t say write once run anywhere.
• Difficult to debug runtime error in
native code.
• Potential security risk.
• You can’t call it from Applet.
8) What is serialization?
Quite simply, object serialization
provides a program the ability to read or write a whole object to and from a raw byte stream. It
allows Java objects and primitives to be encoded into a byte stream suitable for
streaming to some type of network or to a file-system, or more generally, to a transmission medium
or storage facility. A seralizable object must implement the Serilizable interface. We
use ObjectOutputStream to write this object to a stream and ObjectInputStream to read it
from the stream.
9) Why there are some null interface in
java ? What does it mean ? Give me some null interfaces in
JAVA?
Null interfaces act as markers..they
just tell the compiler that the objects of this class need to be treated differently..some marker
interfaces are : Serializable, Remote, Cloneable
10) Is synchronised a
modifier?indentifier? what is it??
It's a modifier. Synchronized methods
are 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 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.
11) What is singleton class?where is it
used?
Singleton is a design pattern meant to
provide one and only one instance of an object. Other objects can get a reference to
this instance through a static method (class constructor is kept private). Why do we need one?
Sometimes it is necessary, and often sufficient, to create a single instance of a given
class. This has advantages in memory management, and for Java, in garbage collection.
Moreover, restricting the number of instances may be necessary or desirable for technological
or business reasons--for example, we may only
want a single instance of a pool of
database connections.
12) What is a compilation unit?
The smallest unit of source code that
can be compiled, i.e. a .java file.
13) Is string a wrapper class?
String is a class, but not a wrapper
class. Wrapper classes like (Integer) exist for each primitive type. They can be used to
convert a primitive data value into an object, and viceversa.
14) Why java does not have multiple
inheritance?
The Java design team strove to make
Java:
• Simple, object oriented, and familiar
• Robust and secure
• Architecture neutral and portable
• High performance
• Interpreted, threaded, and dynamic
The reasons for omitting multiple
inheritance from the Java language mostly stem from the "simple, object oriented, and
familiar" goal. As a simple language, Java's creators wanted a language that most developers could
grasp without extensive training. To that end, they worked to make the language as similar
to C++ as possible (familiar) without carrying over
C++'s unnecessary complexity (simple).
In the designers' opinion, multiple
inheritance causes more problems and confusion than it solves. So they cut multiple inheritance
from the language (just as they cut operator overloading). The designers' extensive
C++ experience taught them that multiple inheritance just wasn't worth the
headache.
15) Why java is not a 100% oops?
Many people say this because Java uses
primitive types such as int, char, double. But then all the rest are objects. Confusing
question..
16) What is a resource bundle?
In its simplest form, a resource bundle
is represented by a text file containing keys and a text value for each key.
17) What is transient variable?
Transient variable can't be serialize.
For example if a variable is declared as transient in a Serializable class and the class is
written to an ObjectStream, the value of the variable can't be written to the stream instead when
the class is retrieved from the ObjectStream the value of the variable becomes null.
18) What is Collection API?
The Collection API is a set of classes
and interfaces that support operation on collections of objects. These classes and interfaces
are more flexible, more powerful, and more regular than the vectors, arrays, and hashtables
if effectively replaces.
Example of classes: HashSet,
HashMap, ArrayList, LinkedList, TreeSet and TreeMap.
Example of interfaces: Collection, Set,
List and Map.
19) Is Iterator a Class or Interface?
What is its use?
Iterator is an interface which is used
to step through the elements of a Collection.
20) What is similarities/difference
between an Abstract class and Interface?
Differences are as follows:
• Interfaces provide a form of multiple
inheritance. A class can extend only one other class.
• Interfaces are limited to public
methods and constants with no implementation.
Abstract classes can have a partial
implementation, protected parts, static methods, etc.
• A Class may implement several
interfaces. But in case of abstract class, a class may extend only one abstract class.
• Interfaces are slow as it requires
extra indirection to to find corresponding method in in the actual class. Abstract classes
are fast.
Similarities:
• Neither Abstract classes or Interface
can be instantiated.
21) What is a transient variable?
A transient variable is a variable that
may not be serialized.
22) Which containers use a border Layout
as their default layout?
The window, Frame and Dialog classes use
a border layout as their default layout.
23) Why do threads block on I/O?
Threads block on i/o (that is enters the
waiting state) so that other threads may execute while the i/o Operation is performed.
24) How are Observer and Observable
used?
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.
25) What is synchronization and why is
it important?
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 object while another
thread is in the process of using or updating that object's value. This often leads to
significant errors.
26) Can a lock be acquired on a class?
Yes, a lock can be acquired on a class.
This lock is acquired on the class's Class object..
27) What's new with the stop(),
suspend() and resume() methods in JDK 1.2?
The stop(), suspend() and resume()
methods have been deprecated in JDK 1.2.
28) Is null a keyword?
The null value is not a keyword.
29) What is the preferred size of a
component?
The preferred size of a component is the
minimum component size that will allow the component to display normally.
30) What method is used to specify a
container's layout?
The setLayout() method is used to
specify a container's layout.
31) Which containers use a FlowLayout as
their default layout?
The Panel and Applet classes use the
FlowLayout as their default layout.
32) What state does a thread enter when
it terminates its processing?
When a thread terminates its processing,
it enters the dead state.
33) What is the Collections API?
The Collections API is a set of classes
and interfaces that support operations on collections of objects.
34) Which characters may be used as the
second character of an identifier, but not as the first
character of an identifier?
The digits 0 through 9 may not be used
as the first character of an identifier but they may be used after the first character of an
identifier.
35) What is the List interface?
The List interface provides support for
ordered collections of objects.
36) How does Java handle integer
overflows and under flows?
It uses those low order bytes of the
result that can fit into the size of the type allowed by the operation.
37) What is the Vector class?
The Vector class provides the capability
to implement a growable array of objects
38) What modifiers may be used with an
inner class that is a member of an outer class?
A (non-local) inner class may be
declared as public, protected, private, static, final, or abstract.
39) What is an Iterator interface?
The Iterator interface is used to step
through the elements of a Collection.
40) What is the difference between the
>> and >>> operators?
The >> operator carries the sign
bit when shifting right. The >>> zero-fills bits that have been shifted out.
0 comments:
Post a Comment