General Interview Questions
Que 1 - Difference
between == and equals method ?
Ans : The == operator compares two objects to determine if they are the same object in memory i.e. present in the same memory location. It is possible for two String objects to have the same value, but located in different areas of memory. == compares references while .equals compares contents.
public class EqualsTest
{
public static void main(String[] args) {
String s1 = ?abc?;
String s2 = s1;
String s5 = ?abc?;
String s3 = new
String(?abc?);
String s4 = new
String(?abc?);
System.out.println(?==
comparison : ? + (s1 == s5));
System.out.println(?==
comparison : ? + (s1 == s2));
System.out.println(?Using
equals method : ? + s1.equals(s2));
System.out.println(?==
comparison : ? + s3 == s4);
System.out.println(?Using
equals method : ? + s3.equals(s4));
}
}
Output
== comparison : true == comparison : true Using equals method : true false Using equals method : true |
|||||||||||||||||||
Que
2 - What is the difference between an Abstract class and Interface ?
Ans :
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 non
abstract 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().
|
|||||||||||||||||||
14.Can
overloaded methods be override too?
Yes,
derived classes still can override the overloaded methods. Polymorphism can
still happen. Compiler will not binding the method calls since it is
overloaded, because it might be overridden now or in the future.
15.Is it possible to override the main method?
NO,
because main is a static method. A static method can't be overridden in Java.
|
|||||||||||||||||||
27.What
are the differences between Interface and Abstract class?
28.When should I use abstract classes and when should I use interfaces?
Use
Interfaces when?
·
You see that something in your design will change
frequently.
·
If various implementations only share method signatures
then it is better to use Interfaces.
·
you need some classes to use some methods which you don't
want to be included in the class, then you go for the interface, which makes
it easy to just implement and make use of the methods defined in the
interface.
Use
Abstract Class when?
·
If various implementations are of the same kind and use
common behavior or status then abstract class is better to use.
·
When you want to provide a generalized form of abstraction
and leave the implementation task with the inheriting subclass.
·
Abstract classes are an excellent way to create planned
inheritance hierarchies. They're also a good choice for nonleaf classes in
class hierarchies.
29.When you declare a method as abstract, can other nonabstract methods access it?
Yes,
other nonabstract methods can access a method that you declare as abstract.
30.Can there be an abstract class with no abstract methods in it?
Yes,
there can be an abstract class without abstract methods.
|
|||||||||||||||||||
36.What
are the differences between Class Methods and Instance Methods?
|
|||||||||||||||||||
38.What
are Access Specifiers?
One
of the techniques in object-oriented programming is encapsulation.
It concerns the hiding of data in a class and making this class available
only through methods. Java allows you to control access to classes, methods,
and fields via so-called access specifiers..
39.What are Access Specifiers available in Java?
Java
offers four access specifiers, listed below in decreasing accessibility:
·
Public- public classes, methods, and
fields can be accessed from everywhere.
·
Protected- protected methods and fields
can only be accessed within the same class to which the methods and fields
belong, within its subclasses, and within classes of the same package.
·
Default(no specifier)- If you do not set access to
specific level, then 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 to which the methods and fields
belong. privatemethods and fields are not visible within
subclasses and are not inherited by subclasses.
40.What is final modifier?
The final modifier
keyword makes that the programmer cannot change the value anymore. The actual
meaning depends on whether it is applied to a class, a variable, or a method.
·
final Classes-
A final class cannot have subclasses.
·
final Variables-
A final variable cannot be changed once it is initialized.
·
final Methods-
A final method cannot be overridden by subclasses.
|
|||||||||||||||||||
54.Difference
between ArrayList and Vector ?
|
|||||||||||||||||||
69.Difference
between HashMap and Hashtable ?
Note: Only one NULL is allowed as a key in
HashMap. HashMap does not allow multiple keys to be NULL. Nevertheless, it
can have multiple NULL values.
|
|||||||||||||||||||
Que
- Difference between Class.forName() and ClassLoader.loadClass() ?
Both
methods try to dynamically locate and load a java.lang.Class object
corresponding to a given class name. However, their behavior differs
regarding which java.lang.ClassLoader they use for class loading and whether
or not the resulting Class object is initialized.
The
most common form of Class.forName(), the one that takes a single String
parameter, always uses the caller's classloader. This is the classloader that
loads the code executing the forName() method. By comparison,
ClassLoader.loadClass() is an instance method and requires you to select a
particular classloader, which may or may not be the loader that loads that
calling code. If picking a specific loader to load the class is important to
your design, you should use ClassLoader.loadClass() or the three-parameter
version of forName() added in Java 2 Platform, Standard Edition (J2SE):
Class.forName(String, boolean, ClassLoader).
Additionally,
Class.forName()'s common form initializes the loaded class. The visible
effect of this is the execution of the class's static initializers as well as
byte code corresponding to initialization expressions of all static fields
(this process occurs recursively for all the class's superclasses). This
differs from ClassLoader.loadClass() behavior, which delays initialization
until the class is used for the first time.
You
can take advantage of the above behavioral differences. For example, if you
are about to load a class you know has a very costly static initializer, you
may choose to go ahead and load it to ensure it is found in the classpath but
delay its initialization until the first time you need to make use of a field
or method from this particular class.
The
three-parameter method Class.forName(String, boolean, ClassLoader) is the
most general of them all. You can delay initialization by setting the second
parameter to false and pick a given classloader using the third parameter. I
recommend always using this method for maximum flexibility.
|
Wednesday, September 3, 2014
Java Interview Questions for Experience and Freshers
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment