Quote

We are what we repeatedly do. Excellence, therefore,is not an act but a habit.

Aristotle

Tuesday, January 26, 2010

Java Core Interview Questions Set - 2

21 Q How can you call the constructor of super class ?
A By using super() syntax.
22 Q What's the difference between normal methods and constructors?
A Constructors must have the same name of the class and can not have a return type. They are called only once, while regular methods can be called whenever required. We cannot explicitly call a constructor.

23 Q What is the use of packages in java ?
A Packages are a way to organize files in java when a project consists of more than one module. It helps in resolving name conflicts when different modules have classes with the same names.

24 Q What must be the order of catch blocks when catching more than one exception?
A The sub classes must come first. Otherwise it will give a compile time error.

25 QHow can we call a method or variable of the super class from child class ?
A We can use super.method() or super.variable syntax for this purpose.

26 Q If you are overriding equals() method of a class, what other methods you might need to override ?
A hashCode
27 Q How can you create your own exception ?
A Our class must extend either Exception or its sub class

28 Q What is serialization ?
A Serialization is the process of saving the state of an object.

29 Q What is de-serialization?
A De-serialization is the process of restoring the state of an object.

30 Q What is externalizable ?
A It is an interface that extends Serializable. It is having two different methods writeExternal() and readExternal. This interface allows us to customize the output.

31 Q Does garbage collection guarantee that a program will not run out of memory?
A 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 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?
A 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 ?
A 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?
A 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?
A Only once.

37 Q What is the difference between a continue statement and a break statement?
A 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?
A 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?
A 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 ?
A 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.

No comments:

Post a Comment