Core Java - Interview Questions and Answers VOL-III

Q.1  What is the purpose of the Runtime class?

Ans:  The purpose of the Run time class is to provide access to the Java run time system.


Q.2  Can a public class MyClass be defined in a source file named YourClass.java?

Ans:  No the source file name, if it contains a public class, must be the same as the public class name itself with a .java extension.

Q.3  Can main method be declared final?

Ans:  Yes, the main method can be declared final, in addition to being public static.

Q.4  What will be the output of the following statement?

System.out.println ("1" + 3);

Ans:  It will print 13.

Q.5  When a thread blocks on I/O, what state does it enter?

Ans:  A thread enters the waiting state when it blocks on I/O.

Q.6  To what value is a variable of the String type automatically initialized?

Ans:  The default value of a String type is null.

Q.7  What is the catch or declare rule for method declarations?

Ans:  If a checked exception may be thrown within the body of a method, the method must either catch the exception or declare it in its throws clause.

Q.8  What is the difference between a MenuItem and a CheckboxMenuItem?

Ans:  The CheckboxMenuItem class extends the MenuItem class to support a menu item that may be checked or unchecked.

Q.9  What is a task's priority and how is it used in scheduling?

Ans:  A task's priority is an integer value that identifies the relative order in which it should be executed with respect to other tasks. The scheduler attempts to schedule higher priority tasks before lower priority tasks.

Q.10  What is the purpose of the enableEvents() method?

Ans:  The enableEvents() method is used to enable an event for a particular object. Normally, an event is enabled when a listener is added to an object for a particular event. The enableEvents() method is used by objects that handle events by overriding their event-dispatch methods.

Q.11  What is the difference between the File and RandomAccessFile classes?

Ans:  The File class encapsulates the files and directories of the local file system. The RandomAccessFile class provides the methods needed to directly access data contained in any part of a file.

Q.12  What class is the top of the AWT event hierarchy?

Ans:  The java.awt.AWTEvent class is the highest-level class in the AWT event-class hierarchy.

Q.13  When a thread is created and started, what is its initial state?

Ans:  A thread is in the ready state after it has been created and started.

Q.14  What is the difference between preemptive scheduling and time slicing?

Ans:  Under preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a

higher priority task comes into existence. Under time slicing, a task executes for a predefined slice of time and then reenters the pool of ready tasks. The scheduler then determines which task should execute next, based on priority and other factors.

Q.15  Is the numeric promotion available in other platform?

Ans:  Yes. Because Java is implemented using a platform-independent virtual machine, bitwise operations always yield the same result, even when run on machines that use radically different CPUs.

Q.16  What is the difference between the Boolean & operator and the && operator?

Ans:  If an expression involving the Boolean & operator is evaluated, both operands are evaluated. Then the & operator is applied to the operand. When an expression involving the && operator is evaluated, the first operand is evaluated. If the first operand returns a value of true then the second operand is evaluated. The && operator is then applied to the first and second operands. If the first operand evaluates to false, the evaluation of the second operand is skipped.Operator & has no chance to skip both sides evaluation and && operator does. If asked why, give details as above.

Q.17  Name Component subclasses that support painting ?

Ans:  The Canvas, Frame, Panel, and Applet classes support painting.

Q.18  What is a native method?

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

Q.19  How can you write a loop indefinitely?

Ans:  for(;;)--for loop; while(true)--always true, etc.

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

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

Q.21  What is the purpose of finalization?

Ans:  The purpose of finalization is to give an unreachable object the opportunity to perform any cleanup processing before the object is garbage collected.

Q.22  When should the method invokeLater()be used?

Ans:  This method is used to ensure that Swing components are updated through the event-dispatching thread.

Q.23  How does Java handle integer overflows and underflows?

Ans:  It uses low order bytes of the result that can fit into the size of the type allowed by the operation.

Q.24  What class allows you to read objects directly from a stream?

Ans:  The ObjectInputStream class supports the reading of objects from input streams.

Q.25  How are this() and super() used with constructors?

Ans:  this() is used to invoke a constructor of the same class. super() is used to invoke a superclass constructor.

Q.26  How is it possible for two String objects with identical values not to be equal under the == operator? How are this() and super() used with constructors?

Ans:  The == operator compares two objects to determine if they are the same objects in memory. It is possible for two String objects to have the same value, but located in different areas of memory.

Q.27  What is an IO filter?

Ans:  An IO filter is an object that reads from one stream and writes to another, usually altering the data in some way as it is passed from one stream to another.

Q.28  What will be the initial value of an object reference which is defined as an instance variable?

Ans:  The object references are all initialized to null in Java. However in order to do anything useful with these references, you must set them to a valid object, else you will get NullPointerException everywhere you try to use such default initialized references.

Q.29  What are the different scopes for Java variables?

Ans:  The scope of a Java variable is determined by the context in which the variable is declared. Thus a java variable can have one of the three scopes at any given point in time.
1. Instance : - These are typical object level variables, they are initialized to default values at the time of creation of object, and remain accessible as long as the object accessible.
2. Local : - These are the variables that are defined within a method. They remain accessible only during the course of method execution. When the method finishes execution, these variables fall out of scope.3. Static: - These are the class level variables. They are initialized when the class is loaded in JVM for the first time and remain there as long as the class remains loaded. They are not tied to any particular object instance.

Q.30  What is the default value of the local variables?

Ans:  The local variables are not initialized to any default value, neither primitives nor object references. If you try to use these variables without initializing them explicitly, the java compiler will not compile the code. It will complain about the local variable not being initialized..

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Best Hostgator Coupon Code