|
Java Interview Questions and Answers
What is the relationship between
synchronized and volatile keyword?
The JVM is guaranteed to treat reads and writes of data of 32 bits or
less as atomic.(Some JVM might treat reads and writes of data of 64 bits
or less as atomic in future) For long or double variable, programmers
should take care in multi-threading environment. Either put these
variables in a synchronized method or block, or declare them volatile.
This class (IncrementImpl) will be used by various threads
concurrently; can you see the inherent flaw(s)? How would you improve
it?
public class IncrementImpl {
private static int counter = 0;
public synchronized void increment() {
counter++;
}
public int getCounter() {
return counter;
}
}
The counter is static variable which is shared by multiple instances of
this class. The increment() method is synchronized, but the getCounter()
should be synchronized too. Otherwise the Java run-time system will not
guarantee the data integrity and the race conditions will occur. The
famous producer/consumer example listed at Sun's thread tutorial site
will tell more.
one of solutions
public class IncrementImpl {
private static int counter = 0;
public synchronized void increment() {
counter++;
}
public synchronized int getCounter() {
return counter;
}
}
What are the drawbacks of inheritance?
Since inheritance inherits everything from the super class and
interface, it may make the subclass too clustering and sometimes
error-prone when dynamic overriding or dynamic overloading in some
situation. In addition, the inheritance may make peers hardly understand
your code if they don't know how your super-class acts and add learning
curve to the process of development.
Usually, when you want to use a functionality of a class, you may use
subclass to inherit such function or use an instance of this class in
your class. Which is better, depends on your specification.
Is there any other way that you can achieve inheritance in Java?
There are a couple of ways. As you know, the straight way is to
"extends" and/or "implements". The other way is to get an instance of
the class to achieve the inheritance. That means to make the
supposed-super-class be a field member. When you use an instance of the
class, actually you get every function available from this class, but
you may lose the dynamic features of OOP
Two methods have key words static synchronized and synchronized
separately. What is the difference between them?
Both are synchronized methods. One is instance method, the other is
class method. Method with static modifier is a class method. That means
the method belongs to class itself and can be accessed directly with
class name and is also called Singleton design. The method without
static modifier is an instance method. That means the instance method
belongs to its object. Every instance of the class gets its own copy of
its instance method.
When synchronized is used with a static method, a lock for the entire
class is obtained. When synchronized is used with a non-static method, a
lock for the particular object (that means instance) of the class is
obtained.
Since both methods are synchronized methods, you are not asked to
explain what is a synchronized method. You are asked to tell the
difference between instance and class method. Of course, your
explanation to how synchronized keyword works doesn't hurt. And you may
use this opportunity to show your knowledge scope.
How do you create a read-only collection?
The Collections class has six methods to help out here:
1. unmodifiableCollection(Collection c)
2. unmodifiableList(List list)
3. unmodifiableMap(Map m)
4. unmodifiableSet(Set s)
5. unmodifiableSortedMap(SortedMap m)
6. unmodifiableSortedSet(SortedSet s)
If you get an Iterator from one of these unmodifiable collections, when
you call remove(), it will throw an UnsupportedOperationException.
Can a private method of a superclass be declared within a subclass?
Sure. A private field or method or inner class belongs to its declared
class and hides from its subclasses. There is no way for private stuff
to have a runtime overloading or overriding (polymorphism) features.
Why Java does not support multiple inheritance ?
This is a classic question. Yes or No depends on how you look at Java.
If you focus on the syntax of "extends" and compare with C++, you may
answer 'No' and give explanation to support you. Or you may answer
'Yes'. Recommend you to say 'Yes'.
Java DOES support multiple inheritance via interface implementation.
Some people may not think in this way. Give explanation to support your
point.
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
|