Java Interview Questions & Answers Part 5

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

No comments:

Post a Comment

Pages