|
Java Interview Questions and Answers
What are some alternatives to inheritance?
Delegation is an alternative to inheritance. Delegation means that you
include an instance of another class as an instance variable, and
forward messages to the instance. It is often safer than inheritance
because it forces you to think about each message you forward, because
the instance is of a known class, rather than a new class, and because
it doesn't force you to accept all the methods of the super class: you
can provide only the methods that really make sense. On the other hand,
it makes you write more code, and it is harder to re-use (because it is
not a subclass).
Why isn't there operator overloading?
Because C++ has proven by example that operator overloading makes code
almost impossible to maintain. In fact there very nearly wasn't even
method overloading in Java, but it was thought that this was too useful
for some very basic methods like print(). Note that some of the classes
like DataOutputStream have unoverloaded methods like writeInt() and
writeByte().
What does it mean that a method or field is "static"?
Static variables and methods are instantiated only once per class. In
other words they are class variables, not instance variables. If you
change the value of a static variable in a particular object, the value
of that variable changes for all instances of that class.
Static methods can be referenced with the name of the class rather than
the name of a particular object of the class (though that works too).
That's how library methods like System.out.println() work. out is a
static field in the java.lang.System class.
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.
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.
Is null a keyword?
The null value is not a keyword.
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.
What is the difference between notify() and notifyAll()?
notify() is used to unblock one waiting thread; notifyAll() is used to
unblock all of them. Using notify() is preferable (for efficiency) when
only one blocked thread can benefit from the change (for example, when
freeing a buffer back into a pool). notifyAll() is necessary (for
correctness) if multiple threads should resume (for example, when
releasing a "writer" lock on a file might permit all "readers" to
resume). Why can't I say just abs() or sin() instead of Math.abs() and Math.sin()?
The import statement does not bring methods into your local name space.
It lets you abbreviate class names, but not get rid of them altogether.
That's just the way it works, you'll get used to it. It's really a lot
safer this way.
However, there is actually a little trick you can use in some cases that
gets you what you want. If your top-level class doesn't need to inherit
from anything else, make it inherit from java.lang.Math. That *does*
bring all the methods into your local name space. But you can't use this
trick in an applet, because you have to inherit from java.awt.Applet.
And actually, you can't use it on java.lang.Math at all, because Math is
a "final" class which means it can't be extended.
Why are there no global variables in Java?
Global variables are considered bad form for a variety of reasons: ·
Adding state variables breaks referential transparency (you no longer
can understand a statement or expression on its own: you need to
understand it in the context of the settings of the global variables).
· State variables lessen the cohesion of a program: you need to know
more to understand how something works. A major point of Object-Oriented
programming is to break up global state into more easily understood
collections of local state.
· When you add one variable, you limit the use of your program to one
instance. What you thought was global, someone else might think of as
local: they may want to run two copies of your program at once.
For these reasons, Java decided to ban global variables.
What does it mean that a class or member is final?
A final class can no longer be subclassed. Mostly this is done for
security reasons with basic classes like String and Integer. It also
allows the compiler to make some optimizations, and makes thread safety
a little easier to achieve. Methods may be declared final as well. This
means they may not be overridden in a subclass.
Fields can be declared final, too. However, this has a completely
different meaning. A final field cannot be changed after it's
initialized, and it must include an initializer statement where it's
declared. For example,
public final double c = 2.998;
It's also possible to make a static field final to get the effect of
C++'s const statement or some uses of C's #define, e.g. public static
final double c = 2.998;
Page Numbers :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
Have a Question ?
post your questions here. It
will be answered as soon as possible.
Check
Structs Interview
Questions for more Structs Interview Questions with answers
|