Professional Documents
Culture Documents
Advance Java Q&A 2022-2023 SEM VI
Advance Java Q&A 2022-2023 SEM VI
The runnable interface is used to write applications which can run in a separate thread.
Any class that implements the runnable interface is called a thread. The code of the
thread is executed by the interpreter after the thread is started.
Thread priority in Java is a number assigned to a thread that is used by Thread
scheduler to decide which thread should be allowed to execute. In Java, each thread is
assigned a different priority that will decide the order (preference) in which it is
scheduled for running.
Hibernate Query Language (HQL) is an object-oriented query language, similar to SQL,
but instead of operating on tables and columns, HQL works with persistent objects and
their properties. HQL queries are translated by Hibernate into conventional SQL
queries, which in turns perform action on database.
f) What is networking?
Java networking is the concept of connecting two or more computing devices to share
resources. The application layer of a Java program communicates with the network.
The java.net package contains all of the Java networking classes and interfaces.
The java.net package in the Java platform provides a class, Socket , that implements
one side of a two-way connection between your Java program and another program on
the network. The Socket class sits on top of a platform-dependent implementation,
hiding the details of any particular system from your Java program.
j) What is servlet?
A servlet is a Java programming language class that is used to extend the capabilities
of servers that host applications accessed by means of a request-response
programming model. Although servlets can respond to any type of request, they are
commonly used to extend the applications hosted by web servers.
3
JSP directives are the elements of a JSP source code that guide the web container on
how to translate the JSP page into its respective servlet.
Syntax :
<%@ directive attribute = "value"%>
Directives can have a number of attributes that you can list down as key-value pairs and
separate by commas. The blanks between the @ symbol and the directive name, and
between the last attribute and the closing %>, are optional.
There are three different JSP directives available. They are as follows:
Page Directives : JSP page directive is used to define the properties applying the JSP
page, such as the size of the allocated buffer, imported packages, and
classes/interfaces, defining what type of page it is, etc. The syntax of JSP page
directive is as follows:
<%@page attribute = "value"%>
Different properties/attributes :
The following are the different properties that can be defined using page directive :
import: This tells the container what packages/classes are needed to be
imported into the program.
Syntax:
<%@page import = "value"%>
b) Explain inter thread communication with an example.
o wait()
o notify()
o notifyAll()
4
Statement PreparedStatement
It is used when SQL query is to be It is used when SQL query is to be executed
executed only once. multiple times.
You can not pass parameters at
runtime. You can pass parameters at runtime.
Used for CREATE, ALTER, DROP Used for the queries which are to be
statements. executed multiple times.
Performance is very low. Performance is better than Statement.
It is base interface. It extends statement interface.
Used to execute normal SQL queries. Used to execute dynamic SQL queries.
We can not use statement for reading We can use Preparedstatement for reading
binary data. binary data.
It is used for DDL statements. It is used for any SQL Query.
We can not use statement for writing We can use Preparedstatement for writing
binary data. binary data.
No binary protocol is used for
communication. Binary protocol is used for communication.
5
New: Whenever a new thread is created, it is always in the new state. For a thread in the new
state, the code has not been run yet and thus has not begun its execution.
Active: When a thread invokes the start() method, it moves from the new state to the active
state. The active state contains two states within it: one is runnable, and the other is running.
Blocked or Waiting: Whenever a thread is inactive for a span of time (not permanently) then,
either the thread is in the blocked state or is in the waiting state.
Timed Waiting: Sometimes, waiting for leads to starvation. For example, a thread (its name is A)
has entered the critical section of a code and is not willing to leave that critical section. In such a
scenario, another thread (its name is B) has to wait forever, which leads to starvation.
Terminated: A thread reaches the termination state because of the following reasons:
o When a thread has finished its job, then it exists or terminates normally.
o Abnormal termination: It occurs when some unusual events such as an unhandled
exception or segmentation fault.
6
ServerSocket class
The ServerSocket class can be used to create a server socket. This object is used to establish
communication with the clients.
Important methods
Method Description
1) public Socket accept() returns the socket and establish a connection between server
and client.
The Hibernate architecture includes many objects such as persistent object, session factory,
transaction factory, connection factory, session, transaction etc.
This is the high level architecture of Hibernate with mapping file and configuration file.
8
The Socket class acts as an endpoint for communication between two machines. It implements .
Methods
Method Description
bind(SocketAddress bindpoint) This method binds the given socket to the specified
local address.
Example 1
1. import java.io.IOException;
2. import java.net.*;
9
3. public class JavaSocketExample1 {
4. public static void main(String[] args) throws IOException {
5. Socket socket = new Socket();
6. InetAddress inetAddress=InetAddress.getByName("localhost");
7. //the port should be greater or equal to 0, else it will throw an error
8. int port=1085;
9. //calling the constructor of the SocketAddress class
10. SocketAddress socketAddress=new InetSocketAddress(inetAddress, port);
11. //binding the socket with the inetAddress and port number
12. socket.bind(socketAddress);
13. //connect() method connects the specified socket to the server
14. socket.connect(socketAddress);
15. //setSendBufferSize() sets the send buffer size or SO_SNDBUF option with the specified valu
e
16. socket.setSendBufferSize(67);
17. //getSendBufferSize() method returns the buffer size(SO_SNDBUF) used by the platform for
output
18. System.out.println("Send Buffer size: "+socket.getSendBufferSize());
19. //enabling the boolean value true
20. boolean on=false;
21. //setKeepAlive() method either enables or disables the SO_KEEPALIVE option
22. socket.setKeepAlive(on);
23. //getKeepAlive() returns a boolean indicating whether SO_KEEPALIVE option is enabled or
not
24. if(socket.getKeepAlive()){
25. System.out.println("SO_KEEPALIVE option is enabled");
26. }
27. else{
28. System.out.println("SO_KEEPALIVE option is disabled");
29. }
30. //getRemoteSocketAddress() returns the address of the endpoint of the specified socket if t
he socket is connected
31. System.out.println("Remote socket address: "+socket.getRemoteSocketAddress());
32. } }
Output:
Send Buffer size: 67
SO_KEEPALIVE option is disabled
Remote socket address: localhost/127.0.0.1:1085
10
d) Write a JSP program to accept Name & age of voter and check whether he/she is eligible
for voting or not.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form action="Slip29.jsp" method="post">
Name : <input type="text" name="name">
JSP FILE:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
if(age >=18)
{
out.println(name + "\nAllowed to vote");
}
else
{
out.println(name + "\nNot allowed to vote");
}
%>
11
e) Write a JDBC program to delete the records of employees whose names are starting with
‘A’ character.
with �A�character.
importjava.sql.*;
class Slip9_1
{
public static void main(String args[])
Connection con;
Statement stmt;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:dsn");
if(con==null)
{
System.out.println("Connection Failed....");
System.exit(1);
}
System.out.println("Connection Established...");
stmt=con.createStatement();
int no = stmt.executeUpdate("Delete from employee where name like 'A
%'");
if(no!=0)
System.out.println("Delete Data sucessfully.....");
else
System.out.println("Data NOT Deleted");
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
12
Java Spring
There are a lot of tools that come with the Spring framework and allows us to reap the benefit
of the out of box solutions. We do not require to write thousands of lines of code. It saves both
time and energy. Let's discuss the advantages and disadvantages of the Spring framework.
Spring Pros
Spring Cons
JSP Tags
JSP scripting language include several tags or scripting elements that
performs various tasks such as declaring variables and methods, writing
expressions, and calling other JSP pages. These are known as JSP
scripting elements.
13
JSP Tags
JSP Tag Brief Description Tag Syntax
Directive <%@ directives %>
Action <jsp:actionName />
d) Write servlet program to accept two numbers from user and print addition of that in blue
colour.
package servletjsp;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
int k=i+j;
System.out.println("the result is "+ k);
}
}
e) Write a JDBC program to display the details of employees (eno,
ename, department, sal) whose department is ‘Computer
Application’.
15
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
}
}
}
a) Run method
16
The run() method is available in the thread class constructed using a separate Runnable
object. Otherwise, this method does nothing and returns. We can call the run() method
multiple times.
The run() method can be called in two ways which are as follows:
1. Using the start() method.
2. Using the run() method itself.
Syntax:
public void run()
{
//statements
}
b) Statement interface
c) HttpServlet.