JAVA Interview Questions and Answers 1
JAVA Interview Questions and Answers 1
2. Question: Name the container which uses Border Layout as their default layout?
Answer: Containers which uses Border Layout as their default are: window,
Frame and Dialog classes.
• 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:
• Scrollable result sets- using new methods in the ResultSet interface allows
programmatically move the to particular row or to a position relative to its
current position
• JDBC 2.0 Core API provides the Batch Updates functionality to the java
applications.
• Java applications can now use the ResultSet.updateXXX methods.
• New data types - interfaces mapping the SQL3 data types
• Custom mapping of user-defined types (UTDs)
• Miscellaneous features, including performance hints, the use of character
streams, full precision for java.math.BigDecimal values, additional security,
and support for time zones in date, time, and timestamp values.
• Method overloading
• Method overriding through inheritance
• Method overriding through the Java interface
• Public
• Protected
• Private
• Defaults
Following table lists the primitive types and the corresponding wrapper classes:
Primitive Wrapper
boolean java.lang.Boolean
byte java.lang.Byte
char java.lang.Character
double java.lang.Double
float java.lang.Float
int java.lang.Integer
long java.lang.Long
short java.lang.Short
void java.lang.Void
23. Question: What is the difference between the instanceof and getclass, these
two are same or not ?
Answer: instanceof is a operator, not a function while getClass is a method of
java.lang.Object class. Consider a condition where we use
if(o.getClass().getName().equals("java.lang.Math")){ }
This method only checks if the classname we have passed is equal to java.lang.Math.
The class java.lang.Math is loaded by the bootstrap ClassLoader. This class is an
abstract class. This class loader is responsible for loading classes. Every Class object
contains a reference to the ClassLoader that defines. getClass () method returns the
runtime class of an object. It fetches the java instance of the given fully qualified
type name. The code we have written is not necessary, because we should not
compare getClass.getName (). The reason behind it is that if the two different class
loaders load the same class but for the JVM, it will consider both classes as different
classes so, we can't compare their names. It can only gives the implementing class
but can't compare a interface, but instanceof operator can.
The instanceof operator compares an object to a specified type. We can use it to test
if an object is an instance of a class, an instance of a subclass, or an instance of a
class that implements a particular interface. We should try to use instanceof operator
in place of getClass() method. Remember instanceof operator and getClass are not
same. Try this example; it will help you to better understand the difference between
the two.
Interface one{
}