0% found this document useful (0 votes)
2 views

adv java

Java imp que

Uploaded by

Revati Gadhave
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

adv java

Java imp que

Uploaded by

Revati Gadhave
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1. What is servlet?

A servlet is a Java program that runs on a web server and handles client requests (like from a browser). It is used to generate
dynamic content such as HTML pages. Servlets are part of the Java EE (Jakarta EE) platform.

2. What is protocol?
A protocol is a set of rules that defines how data is transmitted between computers over a network. Common protocols
include HTTP (for web communication), FTP (for file transfer), and TCP/IP (for general internet communication). They ensure
that the sender and receiver understand each other.

3. What is socket?
A socket is a connection point between two computers to communicate over a network. In Java, sockets are used for client-
server communication. The server creates a ServerSocket, and the client connects using a Socket. Data is sent and received
using input/output streams. It supports both TCP and UDP protocols.

4. What is JSP?
JSP (JavaServer Pages) is a technology used to create dynamic web content. It allows embedding Java code in HTML pages
using special tags. JSP is compiled into a servlet internally. It's part of the Java EE platform and is ideal for designing
presentation layers of web applications. It simplifies the creation of user interfaces compared to servlets.

5. List any 4 implicit objects of JSP:


JSP provides several built-in objects that can be used without declaration.
request: represents the HTTP request.
response: represents the HTTP response.
session: maintains user session information.
application: stores global information shared across users.
These are automatically available in every JSP page.

6. What is Hibernate?
Hibernate is a Java-based ORM (Object Relational Mapping) framework. It simplifies database interaction by mapping Java
classes to database tables. Instead of writing SQL queries manually, developers use Hibernate to perform CRUD operations. It
supports features like lazy loading, caching, and automatic table creation. It's widely used in enterprise applications.

7. Two methods of Connection interface:


In JDBC, the Connection interface provides methods to interact with the database.
createStatement(): Used to execute simple SQL queries
prepareStatement(String sql): Used for precompiled SQL queries, improving performance and security.
These are essential for executing SQL operations in Java programs.

8. What is JDBC?
JDBC (Java Database Connectivity) is an API that allows Java applications to interact with databases. It provides classes and
interfaces to connect, execute queries, and fetch results. JDBC supports all major databases like MySQL, Oracle, and
PostgreSQL. It's an essential part of Java for database-related programming.

9. What is ORM?
ORM (Object Relational Mapping) is a technique that connects Java objects to database tables. It helps developers work with
databases using Java code instead of SQL. Frameworks like Hibernate implement ORM. ORM handles data conversion
between Java and database formats, saving time and reducing errors.

10. What is cookies?


Cookies are small pieces of data stored on the client’s browser by the server. They're used to remember information like login
status or preferences across multiple pages or visits. In Java, cookies can be created using the javax.servlet.http.Cookie class.
They're often used for session tracking and personalization
11. What is UDP?
UDP (User Datagram Protocol) is a connectionless protocol used in network communication. It sends data without
establishing a connection, so it's faster but less reliable than TCP. There's no guarantee of data delivery or order. It’s used in
real-time applications like video streaming or online games. In Java, UDP can be implemented using DatagramSocket.

12. What is networking?


Networking refers to connecting multiple devices to share data and resources. In Java, networking is done using classes like
Socket, ServerSocket, URL, and InetAddress. It enables client-server communication over TCP/IP. Java provides a rich set of
APIs for developing network-based applications.

13. Differentiate between Statement and PreparedStatement:

14. Explain JSP Life Cycle (with diagram):


Translation: JSP is converted to servlet.
Compilation: Servlet is compiled to class.
Loading: Class is loaded into memory.
Instantiation: Servlet object is created.
Initialization: jspInit() method is called.
Request Handling: _jspService() handles requests.
Destruction: jspDestroy() cleans up resources.
(Include a diagram if needed.)

15. Life Cycle of a Thread , (with diagram):


-New Thread: When a new thread is created, it is in the new state. The thread has not yet started to run when the thread is in
this state.
-Runnable State: A thread that is ready to run is moved to a runnable state. In this state, a thread might actually be running or
it might be ready to run at any instant of time.
- Blocked: The thread will be in blocked state when it is trying to acquire a lock but currently the lock is acquired by the other
thread.
-Waiting state: The thread will be in waiting state when it calls wait() method or join() method. It will move to the runnable
state when other thread will notify or that thread will be terminated.
-Terminated: Execution completed.
16. What is Statement? Types of Statements in JDBC:
Statement is an interface in JDBC used to execute static SQL queries like SELECT, INSERT, UPDATE, and DELETE. It is part of the
java.sql package. You create it using Connection.createStatement(). It does not support parameters, so it's best for simple SQL
queries. It's less efficient and less secure compared to PreparedStatementTypes:

Statement: Simple SQL without parameters.


PreparedStatement: Precompiled, supports parameters.
CallableStatement: Used to execute stored procedures.

17. Explain Inter Thread Communication in Multithreading:


It allows threads to communicate and cooperate. Java provides wait(), notify(), and notifyAll() methods for this. These
methods help avoid busy waiting and allow efficient resource sharing. Typically used in producer-consumer problems. They
must be called inside synchronized blocks. One thread can pause (wait) and release the lock, while another thread can notify
it to resume. This prevents busy waiting and helps in scenarios like producer-consumer problems, where threads need to
coordinate.

18. List & Explain JDBC Drivers:


1. JDBC-ODBC Bridge Driver (Type 1):
Connects Java to databases via ODBC driver. Slower, now outdated.
2. Native-API Driver (Type 2):
Uses native C/C++ libraries. Platform-dependent and not portable.
3. Network Protocol Driver (Type 3):
Uses middleware server to communicate with the database. More flexible.
4. Thin Driver (Type 4):
Pure Java driver that connects directly to the database. Fast, platform-independent, and widely used.

19. Difference between JSP and Servlet:


20. Java Program to print from 100 to 1 using sleep():
java
Copy
Edit
public class Countdown {
public static void main(String[] args) throws InterruptedException {
for (int i = 100; i >= 1; i--) {
System.out.println(i);
Thread.sleep(100); // Delay of 100 milliseconds
}
}
}

21. JSP Program to display all prime numbers between 1 to n:


jsp
Copy
Edit
<%
int n = 100;
for(int i=2; i<=n; i++) {
boolean isPrime = true;
for(int j=2; j<=Math.sqrt(i); j++) {
if(i % j == 0) {
isPrime = false;
break;
}
}
if(isPrime) {
out.println(i + "<br>");
}
}
%>

22. Java Program to print "Hello Java" 10 times:


java
public class HelloPrinter {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
System.out.println("Hello Java");
}
}
}
23) JSP to accept username and greet the user
<form method="post">
Enter Name: <input type="text" name="username">
<input type="submit" value="Greet">
</form>
<%
String name = request.getParameter("username");
if (name != null && !name.isEmpty()) {
out.println("Hello, " + name + "!");
}
%>
24) Java program to display IP address of a machine
Java

import java.net.*;
public class IPAddress {
public static void main(String[] args) throws Exception {
InetAddress ip = InetAddress.getLocalHost();
System.out.println("IP Address: " + ip.getHostAddress());
}
}

25) JDBC Program – display employees from 'Computer Application'


java
Connection con = DriverManager.getConnection(url, user, pass);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM employees WHERE department='Computer Application'");
while (rs.next()) {
System.out.println(rs.getInt("eno") + " " + rs.getString("ename") + " " + rs.getString("department") + " " +
rs.getDouble("sal"));
}

26) JDBC Program – insert record into patient table


Java
Connection con = DriverManager.getConnection(url, user, pass);
PreparedStatement ps = con.prepareStatement("INSERT INTO patient VALUES (?, ?, ?)");
ps.setInt(1, 101);
ps.setString(2, "John Doe");
ps.setInt(3, 30);
ps.executeUpdate();

29) run() Method


Defined in Runnable or overridden in Thread.
Contains the code that will execute when the thread is started.
Called indirectly via start() method.

30) Connection Interface


Part of JDBC (java.sql.Connection)
Used to establish a connection with the database.
Provides methods like createStatement(), prepareStatement(), setAutoCommit(), and close().

31) Thread Synchronization


Used to control access to shared resources.
Prevents race conditions in multithreaded programs.
Done using synchronized keyword or blocks.

32) HttpServlet
Abstract class in javax.servlet.http.
Designed for handling HTTP requests.
Methods like doGet(), doPost(), doPut(), doDelete() are used.

33) notify(), notifyAll() & wait()


Method Purpose
wait() Makes thread wait and release lock
notify() Wakes up one waiting thread
notifyAll() Wakes up all waiting threads
27)Difference between doGet() and doPost()

28) Difference: HTTP Servlet vs Generic Servlet

Feature GenericServlet HttpServlet


Protocol Protocol-independent Works with HTTP

Methods Has service() only Has doGet(), doPost() etc.

Use Non-web protocols Web applications

29)explain thread synchronisation with sutitable example


-> Thread synchronization is the process of controlling access to shared resources in a multithreaded environment. It ensures
that only one thread can access the critical section (shared code or data) at a time, preventing issues like race conditions or
inconsistent results. Java provides the synchronized keyword (method or block) for this. It’s mostly used when multiple
threads try to read/write shared data simultaneously. Synchronization helps maintain data integrity and thread safety.

30) explain types of servlets in detail


-> A servlet receives a request from a client (usually a browser), processes it, and sends back a response.
It is a part of Java EE (Enterprise Edition), now known as Jakarta EE.Servlets run inside a Servlet Container (like Apache
Tomcat, GlassFish, etc.).Servlets use the HttpServlet class to handle HTTP requests like GET and POST.They are platform-
independent, secure, and efficient for creating web applications.
-types- 1. GenericServlet
It is an abstract class in the javax.servlet package.It is protocol-independent (not specific to HTTP).Used when you want to
build servlets for non-HTTP protocols (like FTP, SMTP, etc.)
2. HttpServlet
Most commonly used servlet, found in javax.servlet.http package.
Specifically designed to handle HTTP requests.

You might also like