Java Reduction Final

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

1. Define DatabaseMetaData.

Metadata basically means the data that provide a structured description about some other data.
database metadata refers to data about database data or, more elaborately, the information about
tables, views, column types, column names, result sets, stored procedures, and databases.

2. What is the use of JDBC DriverManager class?

The DriverManager provides a basic service for managing a set of JDBC drivers. As part of its
initialization, the DriverManager class will attempt to load the driver classes referenced in the "jdbc.
drivers" system property. This allows a user to customize the JDBC Drivers used by their applications.

3. What is difference between List and Set?

List Set

1. The List is an indexed sequence. 1. The Set is an non-indexed sequence.

2. List allows duplicate elements 2. Set doesn’t allow duplicate elements.

3. Elements by their position can be accessed. 3. Position access to elements is not allowed.

4. Multiple null elements can be stored. 4. Null element can store only once.

5. List implementations are ArrayList, LinkedList, 5. Set implementations are HashSet,LinkedHashset


Vector, Stack.

4. What is use of Statement Interface?

The statement interface is used to create SQL basic statements in Java it provides methods to
execute queries with the database.

5. Which different classes extend List Interface?

The implementation classes of the List interface are ArrayList, LinkedList, Stack, and Vector.

6. What is the Collection framework in Java?

The Java collections framework provides a set of interfaces and classes to implement various data
structures and algorithms. For example, the LinkedList class of the collections framework provides
the implementation of the doubly-linked list data structure.

7. Define ResultSetMetaData.

The ResultSetMetaData provides information about the obtained ResultSet object like, the number
of columns, names of the columns, datatypes of the columns, name of the table etc.

8. What is the use of Connection Class?

The Connection class establishes a current database session that you can use to execute SQL
statements and return results.

9. What is difference between Iterator and ListIterator?

An Iterator is an interface in Java and we can traverse the elements of a list in a forward direction
whereas a ListIterator is an interface that extends the Iterator interface and we can traverse the
elements in both forward and backward directions.
10.Which different classes extend Set Interface?

HashSet, LinkedHashSet, EnumSet, TreeSet.

11.Which are various interfaces used in Collection framework?

Java Collection framework provides many interfaces (Set, List, Queue, Deque) and classes (ArrayList,
Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet).

12. What is use of PreapareStatement Interface?

A prepared statement is a feature used to execute the same (or similar) SQL statements repeatedly
with high efficiency.

13.What is Thread?

A thread in Java is the direction or path that is taken while a program is being executed.

14.Differentiate between sleep () and wait () method.

Wait() Sleep()

1. Wait() method belongs to Object class. 1. Sleep() method belongs to Thread class.

2. Wait() method releases lock during Synchronization. 2. Sleep() method does not release the lock
on object during Synchronization.

3. Wait() should be called only from Synchronized context. 3. There is no need to call sleep()
from Synchronized context.

4. Wait() is not a static method. 4. Sleep() is a static method.

15.What is the use of join() method.

Join method in Java allows one thread to wait until another thread completes its execution. In
simpler words, it means it waits for the other thread to die. It has a void type and throws
InterruptedException.

16.What is role of Scheduler in Multithreading?

Thread scheduler in Java is the component of JVM that determines the execution order of multiple
threads on a single processor (CPU). It decides the order in which threads should run. This process is
called thread scheduling in Java.

17.What is thread Priority?

a thread's priority is an integer in the range 1 to 10. The larger the integer, the higher the priority.
The thread scheduler uses this integer from each thread to determine which one should be allowed
to execute

18. What is the use of notify() method.

The notify() method is defined in the Object class, which is Java's top-level class. It's used to wake up
only one thread that's waiting for an object, and that thread then begins execution. The thread class
notify() method is used to wake up a single thread.
19.List different types of thread.

user threads and daemon threads.

20.What is the use of notifyAll() method.

notifyAll() wakes up all threads that are waiting on this object's monitor. A thread waits on an
object's monitor by calling one of the wait methods. The awakened threads will not be able to
proceed until the current thread relinquishes the lock on this object.

21.Define Interthread communication.

Inter-thread communication in Java is a mechanism in which a thread is paused running in its critical
section and another thread is allowed to enter (or lock) in the same critical section to be executed.

22.What is the goal behind multithreading.

to enable more than one user at a time without requiring multiple copies of the program running on
the computer. Multithreading can also handle multiple requests from the same user.

23.Explain different types of thread priorities.

public static int NORM_PRIORITY = 5, public static int MIN_PRIORITY = 1, public static int
MAX_PRIORITY = 10.

24.What is use of sleep() method.

sleep causes the current thread to suspend execution for a specified period. This is an efficient
means of making processor time available to the other threads of an application or other
applications that might be running on a computer system.

25.List different types of thread.

1. user thread 2. Domain thread

26.What is the use of notifyAll() method.

notifyAll() wakes up all threads that are waiting on this object's monitor. A thread waits on an
object's monitor by calling one of the wait methods. The awakened threads will not be able to
proceed until the current thread relinquishes the lock on this object.

27.Define Interthread communication.

is a mechanism in which a thread is paused running in its critical section and another thread is
allowed to enter (or lock) in the same critical section to be executed.

28.What is the goal behind multithreading.

Multithreading is the ability of a program or an operating system to enable more than one user at a
time without requiring multiple copies of the program running on the computer. Multithreading can
also handle multiple requests from the same user.

1. What is the difference between execute, executeQuery, executeUpdate?


executeQuery() executeUpdate() execute()

executeQuery() method used to executeUpdate() method used execute() use for any
retrieve some data from for update or modify database. SQL statements.
database.

It returns an object of the class It returns an integer value. It returns a boolean value.
ResultSet executeQuery (String int executeUpdate(String sql) int executeUpdate(String
sql) throws SQLException throws SQLException sql) throws SQLException

This method is normally used This method Is used to execute This method can be used
to execute SELECT queries. non SELECT queries. to execute any type of
• DML as INSERT, DELETE, SQL statement.
UPDATE or
• DDL as CREATE. DROP

Example: Example: Example:


• ResultSet Ts= • int i= • Boolean b=
stmt.executeQuery(query); stmt.executeUpdate(query); stmt.execute(query);

2. What is JDBC ResultSet? Which are different types of ResultSet? Explain it with suitable program.

The Java JDBC ResultSet interface represents the result of a database query. The text about queries
shows how the result of a query is returned as a java. sql. ResultSet . This ResultSet is then iterated
to inspect the result.

Types : -

1) Forward Only (ResultSet. TYPE_FORWARD_ONLY)

2) Scroll Insensitive (ResultSet. TYPE_SCROLL_INSENSITIVE)

3) Scroll Sensitive (ResultSet. TYPE_SCROLL_SENSITIVE)

public class ResultSetDemo {

public static void main(String[] args) {

String query = "select empid, firstname, lastname, dob from tblemployee";

Connection conn = null; Statement stmt = null;

Class.forName("com.mysql.jdbc.Driver");

conn =DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/empdb", "root", "root");


stmt = conn.createStatement();

ResultSet rs = stmt.executeQuery(query);

while (rs.next()) { Integer empId = rs.getInt(1);

String firstName = rs.getString(2); String lastName = rs.getString(3);

Date dob = rs.getDate(4); System.out.println("empId:" + empId);

System.out.println("firstName:" + firstName); }

rs.close(); }
3. Explain ArrayList Class in detail.

The ArrayList class is a resizable array, which can be found in the java.util package. The difference
between a built-in array and an ArrayList in Java, is that the size of an array cannot be modified (if
you want to add or remove elements to/from an array, you have to create a new one). While
elements can be added and removed from an ArrayList whenever you want. The syntax is also
slightly different: import java.util.ArrayList; // import the ArrayList class
ArrayList<String> cars = new ArrayList<String>(); // Create an ArrayList object

Elements in an ArrayList are actually objects. In the examples above, we created elements (objects)
of type "String". Remember that a String in Java is an object (not a primitive type). To use other
types, such as int, you must specify an equivalent wrapper class: Integer. For other primitive types,
use: Boolean for boolean, Character for char, Double for double, etc:

4. Explain Comparator Interface with suitable example.

Java Comparator interface is used to order the objects inside a user-defined class. This interface is
available in java.util package and includes two crucial methods known as compare(Object obj1,
Object obj2) and equals(Object element).

Now, let’s understand the various methods of Java Comparator:

Methods of Java Comparator Interface

There are two methods of Comparators in Java, namely:

Methods Description

compare(Object obj1,Object obj 2) Compares the first object with another

equals(Object obj) Compares current object with specified obj

Below code depicts the usage of both the methods by the Java Comparator.

Comparable interface is used to sort the objects with natural ordering. Comparator in Java is used to
sort attributes of different objects. Comparable interface compares “this” reference with the object
specified. Comparator in Java compares two different class objects provided.

6. Explain different types of JDBC Drivers?

Type 1 JDBC drivers are used for testing JDBC applications against an ODBC data source. Type 2 JDBC
drivers require a native database API to be used. Both Type 1 and Type 2 JDBC driver types mix a
Java-based API with another API In contrast, Type 3 is a single JDBC driver used to access a
middleware server, which, in turn, makes the relevant calls to the database Type 4 drivers are the
most common and are designed for a particular vendor's database. Type 5 JDBC drivers (such as
DataDirect JDBC drivers) offer advanced functionality and superior performance over other driver
types.

7. Explain Treeset Class in detail.

The TreeSet class consists of various constructors which allow the possible creation of the TreeSet.
The following are the constructors available in this class:
TreeSet(): This constructor is used to build an empty TreeSet object in which elements will get stored
in default natural sorting order.

TreeSet ts = new TreeSet();

TreeSet(Comparator): This constructor is used to build an empty TreeSet object in which elements
will need an external specification of the sorting order.

TreeSet(SortedSet): This constructor is used to build a TreeSet object containing all the elements
from the given sortedset in which elements will get stored in default natural sorting order. In short,
this constructor is used to convert the SortedSet object to the TreeSet object.

Here we will be performing various operations over the TreeSet object to get familiar with the
methods and concepts of TreeSet in java. Let’s see how to perform a few frequently used operations
on the TreeSet. They are listed as follows:

Adding elements

Accessing elements

Removing elements

Iterating through elements

8. Explain Enumeration Interface with suitable example.

The Enumeration interface defines the methods by which you can enumerate (obtain one at a time)
the elements in a collection of objects. This legacy interface has been superceded by Iterator.
Although not deprecated, Enumeration is considered obsolete for new code

enum Level { LOW, MEDIUM, HIGH }

9. Explain Thread lifecycle in detail

New − A new thread begins its life cycle in the new state. It remains in this state until the program
starts the thread. It is also referred to as a born thread.

Runnable − After a newly born thread is started, the thread becomes runnable. A thread in this state
is considered to be executing its task.

Waiting − Sometimes, a thread transitions to the waiting state while the thread waits for another
thread to perform a task. Thread transitions back to the runnable state only when another thread
signals the waiting thread to continue executing.

Timed Waiting − A runnable thread can enter the timed waiting state for a specified interval of time.
A thread in this state transitions back to the runnable state when that time interval expires or when
the event it is waiting for occurs.

Terminated (Dead) − A runnable thread enters the terminated state when it completes its task or
otherwise terminates.

11.Explain Thread Synchronization using synchronized Method with suitable example.

Synchronized method is used to lock an object for any shared resource.


When a thread invokes a synchronized method, it automatically acquires the lock for that object and
releases it when the thread completes its task. Thread synchronization is the concurrent execution
of two or more threads that share critical resources. Threads should be synchronized to avoid critical
resource use conflicts. Otherwise, conflicts may arise when parallel-running threads attempt to
modify a common variable at the same time

13.Explain Servlet lifecycle in detail.

Servlet class is loaded. The classloader is responsible to load the servlet class. The servlet class is
loaded when the first request for the servlet is received by the web container.

Servlet instance is created. The web container creates the instance of a servlet after loading the
servlet class. The servlet instance is created only once in the servlet life cycle

init method is invoked. The web container calls the init method only once after creating the servlet
instance. The init method is used to initialize the servlet. It is the life cycle method of the
javax.servlet.Servlet interface.

service method is invoked. The web container calls the service method each time when request for
the servlet is received. If servlet is not initialized, it follows the first three steps as described above
then calls the service method. If servlet is initialized, it calls the service method. Notice that servlet is
initialized only once.

destroy method is invoked. The web container calls the destroy method before removing the servlet
instance from the service. It gives the servlet an opportunity to clean up any resource for example
memory, thread etc

1)Write a JDBC program to display the details of employees (eno, ename, department, sal) whose
department is �Computer Science�.

importjava.sql.*;

public class Slip3_1

{ public static void main(String args[]) {

Connection con;

try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

con=DriverManager.getConnection("jdbc:odbc:dsn");

if(con==null) { System.out.println("Connection Failed....");

System.exit(1); }

Statement stmt=con.createStatement();

ResultSetrs=stmt.executeQuery("select * From employee Where dept='computer science'");

System.out.println("eno\t"+"ename\t"+"department\t"+"sal");

while(rs.next()) {
System.out.println("\n"+rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3)+""+rs.getI

nt(4)); con.close(); rs.close(); stmt.close() } catch(Exception e) { System.out.println }}}

You might also like