Tuesday, September 16, 2008

Core Java Interview Questions Part-3

31 Q Does garbage collection guarantee that a program will not run out of memory?

Garbage collection does not guarantee that a program will not run out of memory. It is also possible for programs to create objects that are not subject to garbage collection. And there is no guarantee that Garbage Collection thread will be executed.

32 Q What is a native method?

A native method is a method that is implemented in a language other than Java.

33 Q What are different type of exceptions in Java?

There are two types of exceptions in java. Checked exceptions and Unchecked exceptions. Any exception that is is derived from Throwable and Exception is called checked exception except RuntimeException and its sub classes. The compiler will check whether the exception is caught or not at compile time. We need to catch the checked exception or declare in the throws clause. Any exception that is derived from Error and RuntimeException is called unchecked exception. We don't need to explicitly catch a unchecked exception.

34 Q Can we catch an error in our java program ?

Yes. We can . We can catch anything that is derived from Throwable. Since Error is a sub class of Throwable we can catch an error also.

35 Q What is thread priority?

Thread Priority is an integer value that identifies the relative order in which it should be executed with respect to others. The thread priority values ranging from 1- 10 and the default value is 5. But if a thread have higher priority doesn't means that it will execute first. The thread scheduling depends on the OS.

36 Q How many times may an object's finalize() method be invoked by the garbage collector?

Only once.

37 Q What is the difference between a continue statement and a break statement?

Break statement results in the immediate termination of the statement to which it applies (switch, for, do, or while). A continue statement is used to end the current loop iteration and return control to the loop statement.

38 Q What must a class do to implement an interface?

It must identify the interface in its implements clause. Also it must provide definition for all the methods in the interface otherwise it must be declared abstract.

39 Q What is an abstract class?

An abstract class is an incomplete class. It is declared with the modifier abstract. We cannot create objects of the abstract class. It is used to specify a common behavioral protocol for all its child classes.

40 Q What is the difference between notify and notifyAll method ?

notify wakes up a single thread that is waiting for object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation. notifyAll Wakes up all threads that are waiting on this object's monitor. A thread waits on an object's monitor by calling one of the wait methods.

41 Q What does wait method do ?

It causes current thread to wait until either another thread invokes notify or notifyAll method of the current object, or a specified amount of time has elapsed.

42 Q What are the different states of a thread ?

The different thread states are ready, running, waiting and dead. 43 Q What is the difference between static and non static inner class ? A

A non-static inner class can have an object instances that are associated with instances of the class's outer class. A static inner class can not have any object instances.

44 Q What is the difference between String and StringBuffer class ?

Strings are immutable (constant), their values cannot be changed after they are created. StringBuffer supports mutable objects.

45 Q Which is the base class for all classes ?


No comments:

My Ad