Wednesday, May 22, 2013

Java Interview Questions and Answers Part 15

Q131. 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.


Q132. 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;
  }
}

Q133. 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.

Q134. 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

Q135. 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.

Q136. 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.

Q137. 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.

Q138. 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.

Q139. What is the difference between final, finally and finalize?

Short answer:
  • final - declares constant
  • finally - relates with exception handling
  • finalize - helps in garbage collection

Q140. What kind of security tools are available in J2SE 5.0?

There are three tools that can be used to protect application working within the scope of security policies set at remote sites.
keytool -- used to manage keystores and certificates.
jarsigner -- used to generate and verify JAR signatures.
policytool -- used for managing policy files.
There are three tools that help obtain, list and manage Kerberos tickets.
kinit -- used to obtain Kerberos V5 tickets.
tklist -- used to list entries in credential cache and key tab.
ktab -- used to help manage entries in the key table.

No comments:

Post a Comment

Automatic Traffic Exchange

YallaTech Facebook page