Tuesday, 19 August 2014

Java Interview Questions – Part5

Q: Which package has light weight components?
A: javax.Swing package. All components in Swing, except JApplet, JDialog, JFrame and JWindow are lightweight components.
Q: What is the difference between the paint() and repaint() methods?
A: The paint() method supports painting via a Graphics object. The repaint() method is used to cause paint() to be invoked by the AWT painting thread.
Q: What is the purpose of File class?
A: It is used to create objects that provide access to the files and directories of a local file system.
Q: What is the difference between the Reader/Writer class hierarchy and the InputStream/OutputStream class hierarchy?
A: The Reader/Writer class hierarchy is character-oriented, and the InputStream/OutputStream class hierarchy is byte-oriented.
Q: Which class should you use to obtain design information about an object?
A: The Class class is used to obtain information about an object’s design and java.lang.Class class instance represent classes, interfaces in a running Java application.
Q: What is the difference between static and non-static variables?
A: A static variable is associated with the class as a whole rather than with specific instances of a class. Non-static variables take on unique values with each object instance.
Q: What is Serialization and deserialization?
A: Serialization is the process of writing the state of an object to a byte stream. Deserialization is the process of restoring these objects.
Q: What are use cases?
A: It is part of the analysis of a program and describes a situation that a program might encounter and what behavior the program should exhibit in that circumstance.
Q: Explain the use of sublass in a Java program?
A: Sub class inherits all the public and protected methods and the implementation. It also inherits all the default modifier methods and their implementation.
Q: How to add menushortcut to menu item?
A: If there is a button instance called b1, you may add menu short cut by calling b1.setMnemonic(‘F’), so the user may be able to use Alt+F to click the button.
Q: Can you write a Java class that could be used both as an applet as well as an application?
A: Yes, just add a main() method to the applet.
Q: What is the difference between Swing and AWT components?
A: AWT components are heavy-weight, whereas Swing components are lightweight. Heavy weight components depend on the local windowing toolkit. For example, java.awt.Button is a heavy weight component, when it is running on the Java platform for Unix platform, it maps to a real Motif button.
Q: What’s the difference between constructors and other methods?
A: Constructors must have the same name as the class and can not return a value. They are only called once while regular methods could be called many times.
Q: Is there any limitation of using Inheritance?
A: Yes, since inheritance inherits everything from the super class and interface, it may make the subclass too clustering and sometimes error-prone when dynamic overriding or dynamic overloading in some situation.
Q: When is the ArrayStoreException thrown?
A: When copying elements between different arrays, if the source or destination arguments are not arrays or their types are not compatible, an ArrayStoreException will be thrown.
Q: Can you call one constructor from another if a class has multiple constructors?
A: Yes, use this() syntax.
Q: What’s the difference between the methods sleep() and wait()?
A: The code sleep(2000); puts thread aside for exactly two seconds. The code wait(2000), causes a wait of up to two second. A thread could stop waiting earlier if it receives the notify() or notifyAll() call. The method wait() is defined in the class Object and the method sleep() is defined in the class Thread.
Q: When ArithmeticException is thrown?
A: The ArithmeticException is thrown when integer is divided by zero or taking the remainder of a number by zero. It is never thrown in floating-point operations.
Q: What is a transient variable?
A: A transient variable is a variable that may not be serialized during Serialization and which is initialized by its default value during de-serialization,
Q: What is synchronization?
A: Synchronization is the capability to control the access of multiple threads to shared resources. synchronized keyword in java provides locking which ensures mutual exclusive access of shared resource and prevent data race.
Q: What is the Collections API?
A: The Collections API is a set of classes and interfaces that support operations on collections of objects.
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 possible for programs to use up memory resources faster than they are garbage collected. It is also possible for programs to create objects that are not subject to garbage collection.
Q: The immediate superclass of the Applet class?
A: Panel is the immediate superclass. A panel provides space in which an application can attach any other component, including other panels.
Q: Which Java operator is right associative?
A: The = operator is right associative.
Q: What is the difference between a break statement and a continue statement?
A: A break statement results in the 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.
Q: If a variable is declared as private, where may the variable be accessed?
A: A private variable may only be accessed within the class in which it is declared.
Q: What is the purpose of the System class?
A: The purpose of the System class is to provide access to system resources.
Q: List primitive Java types?
A: The eight primitive types are byte, char, short, int, long, float, double, and boolean.
Q: What is the relationship between clipping and repainting under AWT?
A: When a window is repainted by the AWT painting thread, it sets the clipping regions to the area of the window that requires repainting.
Q: Which class is the immediate superclass of the Container class?
A: Component class is the immediate super class.
Q: What class of exceptions are generated by the Java run-time system?
A: The Java runtime system generates RuntimeException and Error exceptions.
Q: Under what conditions is an object’s finalize() method invoked by the garbage collector?
A: The garbage collector invokes an object’s finalize() method when it detects that the object has become unreachable.
Q: How can a dead thread be restarted?
A: A dead thread cannot be restarted.
Q: Which arithmetic operations can result in the throwing of an ArithmeticException?
A: Integer / and % can result in the throwing of an ArithmeticException.
Q: Variable of the boolean type is automatically initialized as?
A: The default value of the boolean type is false.
Q: Can try statements be nested?
A: Yes
Q: What are ClassLoaders?
A: A class loader is an object that is responsible for loading classes. The class ClassLoader is an abstract class.
Q: What is the difference between an Interface and an Abstract class?
A: An abstract class can have instance methods that implement a default behavior. An Interface can only declare constants and instance methods, but cannot implement default behavior and all methods are implicitly abstract. An interface has all public members and no implementation.
Q: What will happen if static modifier is removed from the signature of the main method?
A: Program throws “NoSuchMethodError” error at runtime .
Q: What is the default value of an object reference declared as an instance variable?
A: Null, unless it is defined explicitly.
Q: Can a top level class be private or protected?
A: No, a top level class can not be private or protected. It can have either “public” or no modifier.
Q: Why do we need wrapper classes?
A: We can pass them around as method parameters where a method expects an object. It also provides utility methods.
Q: What is the difference between error and an exception?
A: An error is an irrecoverable condition occurring at runtime. Such as OutOfMemory error. Exceptions are conditions that occur because of bad input etc. e.g. FileNotFoundException will be thrown if the specified file does not exist.
Q: Is it necessary that each try block must be followed by a catch block?
A: It is not necessary that each try block must be followed by a catch block. It should be followed by either a catch block or a finally block.

For more Visit: http://www.quontrasolutions.com/blog/category/java

No comments:

Post a Comment