Monday, May 4, 2015

Best Java Interview Questions With Answer

Que. Can an anonymous class be declared as implementing an interface and extending a class ?

Ans. An anonymous class may implement an intferface or extend a superclass,but may not be declared to do both.
                                                                                -----------

Que. Why isn't there operator overloading ?

Ans. Becuase C++ has proven by example that operator overloading makes code almost impossible to maintain.
                                                                               -----------

Que. What is transient variable ?

Ans. a variable that may not be serialized.
                                                                                -----------

Que. Is sizeof a keyword ?

Ans. The sizeof operator is not a keyword.
                                                                                -----------

Que. What is differance between >> and >>> operators?

Ans. The >> operator carries the sign bit when shifting right.The >>> zero-fills bits that have been shifted out.
                                                                                -----------

Que. When to use transient variable in Java?

Ans. Transient in Java is  used to indicate that the variable should not be serialized. Serialization is a process of saving an object's state in Java. When we want to persist and object's state by default all instance variables in the object is stored. In some cases, if you want to avoid persisting some variables because we don’t have the necessity to transfer across the network. So, declare those variables as transient. If the variable is declared as transient, then it will not be persisted.
                                                                                -----------

Que. Difference between Hashtable and HashMap in Java?

Ans. This is another frequently asked question from Java interview. Main difference between HaspMap and Hashtable are following :
          -HashMap allows null values as key and value whereas Hashtable doesn't allow nulls.
          -Hashtable is thread-safe and can be shared between multiple threads whereas HashMap cannot be shared between multiple threads without proper synchronization.
          -Because of synchronization, Hashtable is considerably slower than HashMap, even in case of single threaded application.
          -Hashtable is a legacy class, which was previously implemented Dictionary interface. It was later retrofitted into Collection framework by implementing Map interface. On the other hand, HashMap was part of framework from it's inception.
          -You can also make your HashMap thread-safe by using Collections.synchronizedMap() method. It's performance is similar to Hashtable.

                                                                                -----------

Que. Difference between wait and sleep in Java?

Ans. Here are some important differences between wait and sleep in Java

          -wait() method release the lock when thread is waiting but sleep() method hold the lock when thread is waiting.wait() is a instance method and sleep is a static method .
          -wait() method is always called from synchronized block or method but for sleep there is no such requirement.waiting thread can be awake by calling notify() and notifyAll() while sleeping thread can not be awaken by calling notify method.
          -wait method is condition based while sleep() method doesn't require any condition. It is just used to put current thread on sleep.
          -wait() is defined in java.lang.Object class while sleep() is defined in java.lang.Thread class.

                                                                                -----------

Que. Difference between extends Thread and implements Runnable in Java

Ans. One of the main point to put across while answering this question is Java's multiple inheritance support. You cannot more than one class, but you can implement more than one interface. If you extend Thread class just to           override run() method, you lose power of extending another class, while in case of Runnable, you can still implement another interface or another class. One more difference is that Thread is abstraction of independent path of execution, while Runnable is abstraction of independent task, which can be executed by any thread. That's why it's better to implement Runnable than extending Thread class in Java.
                                                                                -----------

Que. What is the JLS?

Ans. JLS is The Java Language Specification.Every developer should buy or download (free) this specification and read it, a bit at a time.(http://docs.oracle.com/javase/specs)
                                                                                -----------

Que. Why is there no printf-like function in Java?

Ans. Actually there are! This was fixed in Java 5; see Java Cookbook (2nd Edition) Chapter 9. Java 5 (J2SE 1.5) includes printf (and scanf), String.format(), and lots more.
                                                                                -----------

Que. What other Java sites do I need to know about?

Ans. java.sun.com, Sun's main technology site
          java.net, a collaborative site (run by Sun)
          java.com, an advocacy/news site (run by Sun)
          developer.java.sun.com, Sun's main developer site
          www.javalobby.org, independent advocacy group
          www.javaworld.com, Java news
          www.theserverside.com, Java Review
          http://www.darwinsys.com/java/, my own Java site
                                                                                -----------

Que. What else do I need to know?

Ans. Everything! But nobody can know everything about Java - the subject is now too vast. Imagine somebody saying that they know everything about every single Microsoft product and technology. If someone like that calls me, I'm always out.
                                                                                -----------

2 comments: