Thursday, April 11, 2013

Java Interview Questions and Answers Part 2


Q1. How are Observer and Observable used?

Objects that subclass the Observable class maintain a list of observers. When an Observable object is updated, it invokes the update() method of each of its observers to notify the observers that it has changed state. The Observer interface is implemented by objects that observe Observable objects.

Q2. What is Java?

Java is an object-oriented programming language developed initially by James Gosling and colleagues at Sun Microsystems. The language, initially called Oak (named after the oak trees outside Gosling's office), was intended to replace C++, although the feature set better resembles that of Objective C. Java should not be confused with JavaScript, which shares only the name and a similar C-like syntax. Sun Microsystems currently maintains and updates Java regularly.

Q3. What does a well-written OO program look like?

A well-written OO program exhibits recurring structures that promote abstraction, flexibility, modularity and elegance.

Q4. Can you have virtual functions in Java?

Yes, all functions in Java are virtual by default. This is actually a pseudo trick question because the word "virtual" is not part of the naming convention in Java (as it is in C++, C-sharp and VB.NET), so this would be a foreign concept for someone who has only coded in Java. Virtual functions or virtual methods are functions or methods that will be redefined in derived classes.

Q5. Jack developed a program by using a Map container to hold key/value pairs. He wanted to make a change to the map. He decided to make a clone of the map in order to save the original data on side. What do you think of it?

If Jack made a clone of the map, any changes to the clone or the original map would be seen on both maps, because the clone of Map is a shallow copy. So Jack made a wrong decision.

Q6. What is more advisable to create a thread, by implementing a Runnable interface or by extending Thread class?

Strategically speaking, threads created by implementing Runnable interface are more advisable. If you create a thread by extending a thread class, you cannot extend any other class. If you create a thread by implementing Runnable interface, you save a space for your class to extend another class now or in future.

Q7. What is NullPointerException and how to handle it?

Security Tip
Use Firefox instead of Internet Explorer and PREVENT Spyware !
Firefox is free and is considered the best free, safe web browser available today
Browse the web faster. Get Firefox with Google Toolbar
When an object is not initialized, the default value is null. When the following things happen, the NullPointerException is thrown:
--Calling the instance method of a null object.
--Accessing or modifying the field of a null object.
--Taking the length of a null as if it were an array.
--Accessing or modifying the slots of null as if it were an array.
--Throwing null as if it were a Throwable value.
The NullPointerException is a runtime exception. The best practice is to catch such exception even if it is not required by language design.

Q8. An application needs to load a library before it starts to run, how to code?

One option is to use a static block to load a library before anything is called. For
example,
class Test {
  static {
    System.loadLibrary("path-to-library-file");
  }
  ....
}
When you call new Test(), the static block will be called first before any initialization
happens. Note that the static block position may matter.

Q9. How could Java classes direct program messages to the system console, but error messages, say to a file?

The class System has a variable out that represents the standard output, and the variable err that represents the standard error device. By default, they both point at the system console. This how the standard output could be re-directed:

Stream st = new Stream(new FileOutputStream("output.txt")); System.setErr(st);
System.setOut(st);

Q10. What's the difference between an interface and an abstract class?

An abstract class may contain code in method bodies, which is not allowed in an interface. With abstract classes, you have to inherit your class from it and Java does not allow multiple inheritance. On the other hand, you can implement multiple interfaces in your class.

1 comment:

  1. hi guys, i am a beginner in development field.. i have been looking fr many conferences, tutorials nd references to learn more nd more abt java development.. i have heard abt many conferences like nasscom game development conference, javapolis, JavaOne... I have also received a mail from Oracle ppl fr their JavaOne conference this year.. they had this ( https://www.regpulse.com/javaone2013/register.php?pcode=737266&src=4003&Act=1 ) link in the email..
    Can u plz tell me what more developers’ oriented conferences i can enroll in to?
    Thanks

    ReplyDelete

Automatic Traffic Exchange

YallaTech Facebook page