Ans: yes, but it will not work as a thread rather it will work as a normal object so there will not be context-switching between the threads.
162) What about the daemon threads?
Ans: The daemon threads are basically the low priority threads that provides the background support to the user threads. It provides services to the user threads.
163) Can we make the user thread as daemon thread if thread is started?
Ans: No, if you do so, it will throw IllegalThreadStateException
164) What is shutdown hook?
Ans: The shutdown hook is basically a thread i.e. invoked implicitely before JVM shuts down. So we can use it perform clean up resource.
165) When should we interrupt a thread?
Ans: We should interrupt a thread if we want to break out the sleep or wait state of a thread.
166) What is synchronization?
Ans: Synchronization is the capabilility of control the access of multiple threads to any shared resource.It is used:
- To prevent thread interference.
- To prevent consistency problem.
167) What is the purpose of Synchronized block?
Ans: Synchronized block is used to lock an object for any shared resource.
Scope of synchronized block is smaller than the method.
168) Can Java object be locked down for exclusive use by a given thread?
Ans: Yes. You can lock an object by putting it in a "synchronized" block. The locked object is inaccessible to any thread other than the one that explicitly claimed it.
169) What is static synchronization?
Ans: If you make any static method as synchronized, the lock will be on the class not on object.
170) What is the difference between notify() and notifyAll()?
Ans: The notify() is used to unblock one waiting thread whereas notifyAll() method is used to unblock all the threads in waiting state.
171) What is deadlock?
Ans: Deadlock is a situation when two threads are waiting on each other to release a resource. Each thread waiting for a resource which is held by the other waiting thread.
JDBC Interview Questions
172) What is JDBC?
Ans: JDBC is a Java API that is used to connect and execute query to the database. JDBC API uses jdbc drivers to connects to the database.
173) What is JDBC Driver?
Ans: JDBC Driver is a software component that enables java application to interact with the database.There are 4 types of JDBC drivers:
- JDBC-ODBC bridge driver
- Native-API driver (partially java driver)
- Network Protocol driver (fully java driver)
- Thin driver (fully java driver)
174) What are the steps to connect to the database in java?
- Registering the driver class
- Creating connection
- Creating statement
- Executing queries
- Closing connection
175) What are the JDBC API components?
Ans: The java.sql package contains interfaces and classes for JDBC API.
- CallableStatement etc.
- SQLException etc.
176) What are the JDBC statements?
Ans: There are 3 JDBC statements.
177) What is the difference between Statement and PreparedStatement interface?
Ans: In case of Statement, query is complied each time whereas in case of PreparedStatement, query is complied only once. So performance of PreparedStatement is better than Statement.
178) How can we execute stored procedures and functions?
Ans: By using Callable statement interface, we can execute procedures and functions.
179) What is the role of JDBC DriverManager class?
Ans: The DriverManager class manages the registered drivers. It can be used to register and unregister drivers. It provides factory method that returns the instance of Connection.
180) What does the JDBC Connection interface?
Ans: The Connection interface maintains a session with the database. It can be used for transaction management. It provides factory methods that returns the instance of Statement, PreparedStatement, CallableStatement and DatabaseMetaData.