Java Interview Questions-4
Java Interview Questions
1. What is the difference between the paint and repaint methods?
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.
2. What is the purpose of File class?
It is used to create objects that provide access to the files and directories of a local file system.
3. What is the difference between the Reader/Writer class hierarchy and the InputStream/OutputStream class hierarchy?
The Reader/Writer class hierarchy is character-oriented, and the InputStream/OutputStream class hierarchy is byte-oriented.
4. Which class should you use to obtain design information about an object?
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.
5. 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. Non-static variables take on unique values with each object instance.
6. What is Serialization and deserialization?
Serialization is the process of writing the state of an object to a byte stream. Deserialization is the process of restoring these objects.
7. What are use cases?
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.
8. Explain the use of sublass in a Java program?
Sub class inherits all the public and protected methods and the implementation. It also inherits all the default modifier methods and their implementation.
9. How to add menushortcut to menu item?
If there is a button instance called b1, you may add menu short cut by calling b1.setMnemonic,so the user may be able to use Alt+F to click the button.
10. Can you write a Java class that could be used both as an applet as well as an application?
Yes, just add a main method to the applet.
11. What is the difference between Swing and AWT components?
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.
12. What’s the difference between constructors and other methods?
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.
13. Is there any limitation of using Inheritance?
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.
14. When is the ArrayStoreException thrown?
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.
15. Can you call one constructor from another if a class has multiple constructors?
Yes, use this syntax.
16. What’s the difference between the methods sleep and wait?
The code sleep2000; puts thread aside for exactly two seconds. The code wait2000, causes a wait of up to two second. A thread could stop waiting earlier if it receives the notify or notifyAll call. Themethod wait is defined in the class Object and the method sleep is defined in the class Thread.
17. When ArithmeticException is thrown?
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.
18. What is a transient variable?
A transient variable is a variable that may not be serialized during Serialization and which is initialized by its default value during de-serialization,
19. What is synchronization?
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.
20. What is the Collections API?
The Collections API is a set of classes and interfaces that support operations on collections of objects.
21. 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 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.
22. The immediate superclass of the Applet class?
Panel is the immediate superclass.A panel provides space in which an application can attach any other component, including other panels.
23. Which Java operator is right associative?
The = operator is right associative.
24. What is the difference between a break statement and a continue statement?
A break statement results in the termination of the statement to which it applies switch, for, do, orwhile.A continue statement is used to end the current loop iteration and return control to the loop statement.
25. If a variable is declared as private, where may the variable be accessed?
A private variable may only be accessed within the class in which it is declared.