Sep 22, 2008

Core Java Interview Questions Part-II

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 ?


java.lang.Object.

46 Q What is the difference between readers and streams?

Readers are character oriented where streams are byte oriented. The readers are having full support for Unicode data.

47 Q What is constructor chaining ?


When a constructor of a class is executed it will automatically call the default constructor of the super class (if no explicit call to any of the super class constructor) till the root of the hierarchy.

48 Q What are the different primitive data type in java ?

There are 8 primitive types in java. boolean , char, byte, short, int long, float, double.

49 Q What is static ?

static means one per class. static variables are created when the class loads. They are associated with the class. In order to access a static we don't need objects. We can directly access static methods and variable by calling classname.variablename.

50 Q Why we cannot override static methods?

Static means they are associated with a class. In static methods , the binding mechanism is static binding. So it must be available at the compile time.

51 Q What is the difference between static and non static variables ?

A static variable is associated with the class as a whole rather than with specific instances of a class. There will be only one value for static variable for all instances of that class. Non-static variables take on unique values with each object instance.

52 Q When does a compiler supplies a default constructor for a class?


If there is no other constructor exist in a class, the compiler will supply a default constructor.

53 Q What are the restrictions placed on overriding a method ?


The overridden method have the exact signature of the super class method, including the return type. The access specified cannot be less restrictive than the super class method. We cannot throw any new exceptions in overridden method.

54 Q What are the restrictions placed on overloading a method ?


Overloading methods must differ in their parameter list, or number of parameters.

55 Q What is casting ?

Casting means converting one type to another. There are mainly two types of casting. Casting between primitive types and casting between object references. Casting between primitive numeric types is used to convert larger data types to smaller data types. Casting between object references is used to refer to an object by a compatible class, interface, or array type reference.

56 Q What is the difference between == and equals ?


The equals method can be considered to perform a deep comparison of the value of an object, whereas the == operator performs a shallow comparison. If we are not overriding the equals method both will give the same result. == will is used to compare the object references. It is used to check whether two objects are points to the same reference.

57 Q What is a void return type ?

A void indicates that the method will not return anything.

58 Q What will happen if an exception is not caught ?

An uncaught exception results in the uncaughtException() method of the thread's ThreadGroup, which results in the termination of the program.

59 Q What are the different ways in which a thread can enter into waiting state?

There are three ways for a thread to enter into waiting state. By invoking its sleep() method, by blocking on I/O, by unsuccessfully attempting to acquire an object's lock, or by invoking an object's wait() method.

60 Q What is a ResourceBundle class?


The ResourceBundle class is used to store locale-specific resources that can be loaded by a program to create the program's appearance to the particular locale in which it is being run.



Contact me If any further Information


All The Best

No comments: