Java Interview Questions – 48
1. What is the purpose of the wait (), notify (), and notifyAll() methods?
The wait() method makes the thread to halt, thus allowing other thread to perform
2. What is the relationship between an event-listener interface and an event-adapter class?
An event-listener interface allows describing the methods which must be implemented by one of the event handler for a specific event
3. What is the purpose of the enableEvents() method?
An event-listener interface allows describing the methods which must be implemented by one of the event handler for a specific event
4. What is the difference between the File and RandomAccessFile classes?
The OS based file system services such as creating folders, files, verifying the permissions, changing file names etc., are provided by the java.io.File class
5. What are synchronized methods and synchronized statements?
Synchronized methods are utilized to control the access to an object especially in multi threaded programming
6. Explain Java class loaders? Explain dynamic class loading?
The classes are available to the JVM and are referenced by the name. After JVM starts, it introduces the classes into it
7. Difference between an abstract class and an interface and when should you use them
An abstract has one or more abstract methods apart from concrete methods. All the abstract methods must be overridden by its subclasses
8. What is the main difference between an ArrayList and a Vector? What is the main difference between Hashmap and Hashtable?
Differences between ArrayList and Vector
9. Explain the Java Collection framework? What are the benefits of the Java collection framework?
A collection is a set of heterogeneous objects. A framework makes the applications efficient, less hard coded, similar implementation for various
10 What is the main difference between shallow cloning and deep cloning of objects?
Shallow cloning just allows cloning the object but not their internal parts
11. What is the difference between final, finally and finalize() in Java?
final – a key word / access modifier to define constants
12. What is type casting? Explain up casting vs down casting? When do you get ClassCastException?
The conversion of a given expression from one type to another type is referred as ‘type casting’
13. What are different types of inner classes?
There are 4 types of inner classes – Member inner class, Local inner class, Static inner class and Anonymous inner class
14. What are packages in Java?
As the size of a project grows larger, the manageability of the files in it becomes tedious.
15. What are Native methods in Java?
Java applications can call code written in C, C++, or assembler. This is sometimes done
16.What is Reflection API in Java?
The Reflection API allows Java code to examine classes and objects at run time
17. Explain Shallow and deep cloning.
Cloning of objects can be very useful if you use the prototype pattern or if you want to store an internal copy
18. Explain the impact of private constructor.
Private Constructors can’t be access from any derived classes neither from another class
19. What are static Initializers?
A static initializer block resembles a method with no name, no arguments, and no return type
20. What is the purpose of the wait (), notify (), and notifyAll() methods?
notify(): Wakes up a single thread that is waiting on this object’s monitor
21. What is the advantage of the event-delegation model over the earlier event inheritance model?
Event-delegation model allows a separation between a component’s design and its use as it enables event handling to be handled by objects other than the ones that generate the events
22. What is a task’s priority and how is it used in scheduling?
A priority is an integer value
23. What is the difference between preemptive scheduling and time slicing?
If a certain task is running and the scheduling method used is preemptive, and then if there is another task that has a higher priority than the executing task
24. What is synchronization and why is it important? Describe synchronization in respect to multithreading.
Threads communicate by sharing access to fields and the objects
25. How are Observer and Observable used?
The observable class represents an observable object.
The object to be observed can be represented by sub-classing observable class
26. What is ResultSetMetaData?
ResultSetMetaData is an object that gets information about the types and properties of the columns in a ResultSet object
27. Explain CacheRowset, JDBCRowset and WebRowset.
JdbcRowSet is a connected type of rowset as it maintains a connection to the data source using a JDBC driver
28. Explain the difference between static and non-static member of a class.
A static field belongs to a class. The objects of the class can not have the copy of these fields. These fields are referred by the class name. Ex: Employee. company. Without creating an instance of a class, these fields can be accessed
29. Describe the use of “instanceof” keyword.
“instanceof” keyword is used to check what is the type of object
30. What is the disadvantage of garbage collector?
Although garbage collector runs in its own thread, still it has impact on performance. It adds overheads since JVM has to keep constant track of the object
31. What is an Applet?
An applet is a small server side application that can be loaded and controlled on the browser by the client application
32. Explain the life cycle of an applet.
Below are sequences of methods that describes the life cycle of an applet
33. What is the purpose of finalization?
Finalization is the facility to invoke finalized() method. The purpose of finalization is to perform some action before the objects get cleaned up
34. What is the difference between the File and RandomAccessFile classes?
The File class is used to perform the operations on files and directories of the file system of an operating system. This operating system is the platform for the java application that uses the File class objects.
35. Define class and object. Explain them with an example using java.
Class: A class is a program construct which encapsulates data and operations on data. In object oriented programming, the class can be viewed as a blue print of an object
36. Explain class vs. instance with example using java.
A class is a program construct which encapsulates data and operations on data. It is a blue print of an object
37. What is a method? Provide several signatures of the methods.
A java method is a set of statements to perform a task. A method is placed in a class.Signatures of methods: The name of the method, return type and the nunmber of parameters comprise the method signature
38. Explain the difference between instance variable and a class variable
An instance variable is a variable which has one copy per object / instance. That means every object will have one copy of it
39. Explain how to create instance of a class by giving an example.
Java supports 3 ways of creating instances – By using new operator – Ex : Product prodMouse = new Product();
40. What is an abstract class? Explain its purpose.
A class with one of more abstract methods is called an Abstract class. An abstract method is a method with signature with out code in it
41. Difference between an Abstract class and Interface
An abstract class can have both abstract and concrete methods whereas an interface can have only method signatures
42. What is singleton class? Where is it used?
A class is defined as singleton class, if and only if it can create only one object. This class is useful when only one object is to be used in the application
43. Difference between a public and a non-public class.
A class with access specifier ‘public’ can be accessed across the packages. A package is like a folder in GUI based operating systems. For example, the “public class String” class is a public class which can be accessed across the packages………………
44. What is fail-fast iterator in Java? What is it used for?
The unsynchronized collections and their modifications, iterations are termed as ‘fail-fast’ iterators. An iterator that implements Iterable interface
45. Explain how to store and retrieve serialized objects to and from a file in Java.
The object’s current state can be persisted and retrieved by using the concept known as ‘serialization’. This is done on an object to a stream
46. What is relation between JAXP and Xerces?
Xerces is a group of libraries for parsing, serializing, validating and manipulating XML
47. What type of garbage collection does a System.gc() do?
Among various types of garbage collections, the System.gc() performs an explicit collection of garbage calls. The other garbage collection types are
48. When do I need to use reflection feature in Java?
Reflection feature in java allows an executing java program for introspecting upon itself. It also allows for manipulating internal properties of the program………………
49. Explain how to measure time in nanoseconds.
Time is measured in nanoseconds by using the method nanoTime() method. This method returns the current value of the most precise system timer available in nanoseconds………….
50. Java Thread: run() vs start() method
The method start() invokes the run() method. If the run() method of two threads invoked separately, they will execute one after the other
51. What is Java Isolates and why do we need it?
Java Isolates is an API. It provides a mechanism to manage Java application life cycles which are isolated from each other. They can potentially share the underlying implementation resources…………
52. What are the system properties that applets are allowed to read by default?
The following are the system properties that Java applets read by default with System.getProperty() method:
53. Can you run the java program without main method? Explain with an example
A java applet application, a web application can run without main method. A Java program can run without using ‘ public static void main(String args) ‘ method by using a static block…………
54. Can you explain why servlets extend HttpServlet.
Most of the web applications uses HTTProtocol. The user requests need to be received and processed through the HTTProtocol
55. What is Servlet Filter and how does it work?
Filters are powerful tools in servlet environment. Filters add certain functionality to the servlets apart from processing request and response paradigm of servlet processing………….
56. Explain the use of <logic:iterate>tag in struts application
The tag is utilized for repeating the nested body content over a collection. Every element / object in a specified collection
57. Difference between DataInputStream and BufferedReader
The differences are: The DataInputStream works with the binary data, while the BufferedReader work with character data
58. Difference between throw and throws
The throw key word is used to explicitly throw an exception, while throws is utilized to handle checked exceptions for re-intimating the compiler that exceptions are being handled……………
59. Explain the ways to serialize the java object
Object serialization could be used in different ways: – Simple persistence of the object
60. What are the similarities between an array and an ArrayList?
The following are the similarities between an array and an ArrayList: – Both array and ArrayList can have duplicate elements in them
61. Can we decide a session bean as stateless or stateful without seeing jar file? Explain how
A bean can be determined whether is a stateless or stateful by analyzing the deployment descriptor ejb-jar.xml
62. What is non static block in java?
The non-static block code is executed when a new class is instantiated. It executes before the constructor’s execution
63. Explain how to sort the elements in HashMap.
The elements of HashMap can be sorted by using the static method Collections.sort()
64. Difference between Checked and Unchecked exception? Give some examples
A checked exception throws a block of code and represented by the throws keyword
65. Explain why Runnable interface is preferable than extending the Thread class.
Runnable interface is always preferred because, the class implementing it can implement as many interfaces as a developer can, and also extend another class……………
66. Difference between a java object reference and c++ pointer.
The prime difference is that pointers are to locate the address of the primitive variables only
67. What is JasperReports?
Jasper Report is a very popular open source reporting tool written in Java. The reports can be seen on the screen, on a printer
68. Spring framework vs. Struts framework
Struts: Struts is a MVC pattern framework, Generating integration logic is done dynamically using Struts
69. Explain how to convert java file to jar files.
The following is the process: – Type jar –cvf <jarfilename.jar> <.java file(s)>
70. Describe how to use crystal reports in java. Give example
Create an object of CrystalReport class. – Invoke aboutBox() method – for confirmation whether it is loaded properly – Establish the connection by using setConnect() method……………
71. Explain why java doesn’t support multiple inheritance.
Java does not support multiple inheritances. To avoid ambiguity problem – Diamonds of Death – and complexity of multiple inheritance
72. What is the use of private constructor in Java? Provide an example for a private constructor
A private constructor is used when there is no requirement of another class to invoke that. It is used mostly in implementing singletons
73. Explain how to make a class immutable? Why do we make a class immutable
In order to make a class immutable, the changing state of the class object must be restricted. This means restricting an assignment to a variable
74. Java methods are virtual by default. Comment.
Java methods are virtual by default. The virtual methods are the methods of subclasses. They are invoked by a reference of their super class
75. Explain with an example how a class implements an interface.
When a class implements an interface, it has to implement the methods defined inside that interface. This is enforced at build time by the compiler
76. Concrete class vs. Abstract class vs. Interface.
A concrete class has concrete methods, i.e., with code and other functionality. This class a may extend an abstract class or implements an interface..
77. How do I design a class so that it supports cloning?
The Cloneable should be implemented.
clone( ) should be overridden in which super.clone( ) is called…
78. Explain the complete syntax for using the Applet tag.
The <Applet> tag has the following attributes:
Code: To specify the .class
Width: To indicate the width of the applet window at first time loading.
Height: To indicate the height of the applet window at first time loading..
79. Where should we put applet class files, and how to indicate their location using the Applet tag?
An applet class file may present in any of the folder. The .class file name along with its current path( in which this .html file is placed) is to be specified in the code attribute of <Applet> tag……………..
80. Explain the methods that control a Applet’s on-screen appearance, update and paint.
To refresh a page in an applet window without flashing, the update() method is to be overridden. It clears the background of the component, before invoking paint()………………
81. Explain how Java interacts with database. Give an example to explain it.
Java interacts with database using an API called Java Database Connectivity. JDBC is used to connect to the database, regardless the name of the database management software. Hence , we can say the JDBC is a cross-platform API………………
82. How can we handle SQL exception in Java?
SQL Exception is associated with a failure of a SQL statement. This exception can be handled like an ordinary exception in a catch block..
83. What is DatabaseMetaData?
DatabaseMetaData provides comprehensive information about the database. This interface is implemented by the driver vendors to allow the user to obtain information about the tables of a relational database as a part of JDBC application……………….
84. What is ResultSetMetaData?
ResultSetMetaData is a class which provides information about a result set that is returned by an executeQuery() method….
85. Explain CacheRowset, JDBCRowset and WebRowset.
A CacheRowSet object is a container for rows, that ‘caches’ the rows in the memory..
86. What are the different types of locks in JDBC? Explain them
A lock is a preventive software mechanism that other users can not use the data resource..
87. How are Observer and Observable used?
All the objects which are the instances of the sub class Observable, maintains a list of observers.
88. What is synchronization and why is it important? Describe synchronization in respect to multithreading.
Synchronization is the process of allowing threads to execute one after another.
89. What is the difference between preemptive scheduling and time slicing?
Preemptive scheduling enables the highest priority task execution until waiting or dead states entered. It also executes, until a higher priority task enters………………
90. What is a task’s priority and how is it used in scheduling?
A task’s priority is an integer value. This value is used to identify the relative order of execution with respect to other tasks.
91. What is the difference between the Boolean & operator and the && operator?
A single ampersand is used to perform ‘bit-wise AND’ operation on integer arguments.
92. What is the advantage of the event-delegation model over the earlier event inheritance model?
Event-delegation model has two advantages over event-inheritance model.
93. Describe how to implement singleton design pattern in struts.
A singleton implementation is done using action class in struts framework. It performs as a front controller and every request will go through it.
94. Difference between length and length()? Explain with an example for each.
The ‘length’ is an instance constant which is the size of an array. While the ‘length()’ is a method that returns the no. of the characters in a string.
95. Difference between >> and >>>? Explain with an example for each.
The >> is right shift operator. It shifts bits towards right. For example: 5 >> 1 returns 2. It shifts one bit towards right and one bit is lost.
96. What is an Anonynous inner class? Explain with an example
Anonymous Inner Classes – An inner class without a name. It allows the declaration of the class, creation of the object and execution of the methods in it at one shot……………
97. What is object deep copy and shallow copy? Explain their purposes
In deep copy the copy operations would respect the semantics of the object. For example, copying an object along with the objects to which it refers to.
98. Explain the advantages of JTA over JTS.
Java Transaction API is simple and more flexible to use. There are two levels in JTA API
99. What is a resource leak?
Usually all operating systems have limitations for using the number of file handles, sockets etc., which can be open.
100. What is Bootstrap loader program? Explain its purpose
Bootstrapping is a technique which activates more complex / complicated system of programs.
101. Explain the two ways that the members of a package can be used by other packages.
There are two ways of affecting access levels. One, when the classes in the Java platform are used within the developer defined classes, the access levels determine the members of those class which can be used by the developer defined classes.
102. Explain Java’s delegation event model.
The event model is based on the Event Source and Event Listeners. Event Listener is an object that receives the messages / events. The Event Source is any object which creates the message / event.
103. Describe the assert keyword.
The programmer assumes certain code while developing when handling exceptions. For example, a number is passed as parameter to a method.
104. Explain why a native method might be useful to some types of programs.
Java Native methods are useful when an application can not be written completely in Java language. For instance, when a Java application needs to modify an existing application which was developed.
105.Explain the use of shift operator in Java. Can you give some examples?
Using shift operators in Java we can 1. Integer division and multiplication is done faster. Example 84547 * 4 can be done by using 84547 << 2 .
106. Why do we need wrappers like Integer, Boolean for int, boolean in Java?
Wrapper classes are used to represent primitive data types as objects. Dealing primitive types as objects is sometimes easier. Many utility methods are available in wrapper classes..
107. Array vs ArrayList vs LinkedList vs Vector in java
Array vs ArrayList: ArrayList is much better than Array, when the size need to be increased dynamically. Efficiency is possible with arrays. ArrayList permits null elements..
108. Define Autoboxing with an example.
The automatic conversion of primitive int type into a wrapper class object is called autoboxing. It does not require to type cast the int value.
109. Can Java communicate with ActiveX objects? Explain how.
Java can communicate with ActiveX objects by using Bridge2Java from IBM. Bridge2Java is a tool for allowing the java applications for communicating with ActiveX objects.
110. Why do you recommend that the main thread be the last to finish?
In an application, a program continues to run until all of the threads have ended. Hence, the main thread to finish at last is not a requirement. It is a good programming practice to make it to run last to finish.