Java Questions and Answers
Java Questions and Answers
Question#01
Q. Write a Java program to read the details from the user (Name, City, Contact No., Mail ID etc.) and
display it on Console.
Solution :
Program Name : Read_Stud_info.java
import java.io.*;
try
{
in = new InputStreamReader(System.in);
bin = new BufferedReader(in);
Question#02
Q. Write a Java Program to read the content from file and display it on the console line by line.
Solution :
Program Name : Read_File.java
import java.io.*;
public class Read_File
{
public static void main(String args[])
{
FileReader in = null;
BufferedReader bin = null;
String str;
try
{
in = new FileReader("abc.txt");
bin = new BufferedReader(in);
while((str= bin.readLine()) != null)
{
System.out.println(str);
}catch(IOException e)
{
System.out.println(e);
}
}
}
Question#03
Q. Write a Java Program to swap the content of one file into another, read the filenames from user.
Solution :
Program Name : File_Swap.java
import java.io.*;
try
{
in = new InputStreamReader(System.in);
bin = new BufferedReader(in);
bout1.flush();
fin1.close();
fout1.close();
bin1.close();
bout1.close();
bout2.flush();
fin2.close();
fout2.close();
bin2.close();
bout2.close();
fin3 = new FileReader("temp.txt");
fout3 = new FileWriter(file2);
bin3 = new BufferedReader(fin3);
bout3 = new PrintWriter(fout3);
}catch(IOException e)
{
System.out.println(e);
}
}
}
Question#04
Q. Write a Java Program to count the number of characters, words and lines from the file.
Solution :
Program Name : Count_File_line.java
import java.io.*;
try
{
in = new FileReader("abc.txt");
try
{
in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("\n Enter File Name#1");
file1 = in.readLine();
fin3.seek(len+2);
}catch(IOException e)
{
System.out.println(e);
}
}
}
Question#06
Q. Write a Java Program to read the content from file and copy the odd number of lines in odd.txt and
even number of lines in even.txt.
Solution :
Program Name : ODD_EVEN_File.java
import java.io.*;
try
{
in = new FileReader("abc.txt");
bin = new BufferedReader(in);
fout1 = new FileWriter("even.txt");
fout2 = new FileWriter("odd.txt");
bout1 = new PrintWriter(fout1);
bout2 = new PrintWriter(fout2);
bout1.close();
bout2.close();
}catch(IOException e)
{
System.out.println(e);
}
}
}
Question#07
Q. Write a Java Program which creates the connection between the Client and Server and ones it establish
then server will send the welcome message to the client.
Solution :
1. Program Name : ClientSide.java
import java.io.*;
import java.net.*;
public class ClientSide
{
public static void main(String args[])
{
Socket soc = null;
BufferedReader sin = null;
PrintWriter out = null;
try
{
soc = new Socket("127.0.0.1",7676);
out.println("Data Received");
out.flush();
}catch(Exception e)
{
System.out.println(e);
}
}
}
2. Program Name : ServerSide.java
import java.io.*;
import java.net.*;
try
{
ssoc = new ServerSocket(7676);
System.out.println("\n Server has started .....");
while(true)
{
soc = ssoc.accept();
if(soc != null)
{
System.out.println("\n Client has called ..."+ soc);
}
}
}catch(Exception e)
{
System.out.println(e);
}
}
}
Question#08
Q. Write a Serverside & Clientside Java Program to implement the Echo Server.
Solution :
1. Program Name : EchoClient.java
import java.io.*;
import java.net.*;
public class EchoClient
{
public static void main(String args[])
{
Socket soc = null;
BufferedReader bin = null;
BufferedReader sin = null;
PrintWriter out = null;
try
{
soc = new Socket("127.0.0.1",7676);
out.println(str);
out.flush();
String str1 = sin.readLine();
System.out.println(str1);
}catch(Exception e)
{
System.out.println(e);
}
}
}
2. Program Name : EchoServer.java
import java.io.*;
import java.net.*;
try
{
ssoc = new ServerSocket(7676);
System.out.println(“\n Server has started …..”);
while(true)
{
soc = ssoc.accept();
if(soc != null)
{
System.out.println(“\n Client has called …”+ soc);
}
}
}catch(Exception e)
{
System.out.println(e);
}
}
}
Question#09
Q. Write a Serverside & Clientside Java Program implement the Chat Server.
Solution :
1. Program Name : ChatClient.java
import java.io.*;
import java.net.*;
public class ChatClient
{
public static void main(String args[])
{
Socket soc = null;
BufferedReader bin = null;
BufferedReader sin = null;
PrintWriter out = null;
try
{
soc = new Socket("127.0.0.1",7676);
out.println(str);
out.flush();
}catch(Exception e)
{
System.out.println(e);
}
}
}
2. Program Name : ChatServer.java
import java.io.*;
import java.net.*;
try
{
ssoc = new ServerSocket(7676);
System.out.println("\n Server has started .....");
while(true)
{
soc = ssoc.accept();
if(soc != null)
{
System.out.println("\n Client has called ..."+ soc);
}
}
}catch(Exception e)
{
System.out.println(e);
}
}
}
Question#10
Q. Write a Serverside & Clientside Java Program implement the File Server.
Solution :
1. Program Name : FileClient.java
import java.io.*;
import java.net.*;
public class FileClient
{
public static void main(String args[])
{
Socket soc = null;
BufferedReader bin = null;
PrintWriter out = null;
try
{
soc = new Socket("127.0.0.1",7676);
out.println(fname);
out.flush();
}catch(Exception e)
{
System.out.println(e);
}
}
}
2. Program Name : FileServer.java
import java.io.*;
import java.net.*;
while(true)
{
soc = ssoc.accept();
if(soc != null)
{
System.out.println("\n Client has called ..."+ soc);
}
}
}catch(Exception e)
{
System.out.println(e);
}
}
}
Question#11
Q. Write Java Program to read the content from the table and display it on Console
Solution :
Program Name : DisplayData.java
import java.sql.*;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDrriver");
con = DriverManager.getConnection("jdbc.odbc.STUD");
stmt = con.createStatement();
rs = stmt.executeQuery("Select * from Student");
System.out.println("\n RollNo.\t Name \t Marks");
while(rs.next())
{
System.out.println("\n "+ rs.getInt("rno")+"\t"+ rs.getString("name")+"\t"+
rs.getInt("Marks"));
}
}catch(ClassNotFoundException e)
{
System.out.println(e);
}catch(SQLException e)
{
System.out.println(e);
}
}
}
Question#12
Q. Write a Java Program to read the values from the user and update the same in database.
Solution :
Program Name : UpdateData.java
import java.sql.*;
import java.io.*;
try
{
in = new InputStreamReader(System.in);
bin = new BufferedReader(in);
System.out.println("\n Enter The Roll No. of Student");
num = Integer.parseInt(bin.readLine());
Class.forName("sun.jdbc.odbc.JdbcOdbcDrriver");
con = DriverManager.getConnection("jdbc.odbc.STUD");
stmt = con.createStatement();
stmt.executeUpdate("Update Student set name = "+name+" where rno ="+ num);
System.out.println("\n Record Updated");
}catch(IOException e)
{
System.out.println(e);
}
catch(ClassNotFoundException e)
{
System.out.println(e);
}catch(SQLException e)
{
System.out.println(e);
}
}
}
Question#13
Q. Write a Java Program to read the value from the command line argument and delete the same record
from the database.
Solution :
Program Name : DeleteData.java
import java.sql.*;
import java.io.*;
public class DeleteData
{
public static void main(String args[])
{
Connection con = null;
Statement stmt = null;
int n;
try
{
n = Integer.parseInt(args[0]);
Class.forName("sun.jdbc.odbc.JdbcOdbcDrriver");
con = DriverManager.getConnection("jdbc.odbc.EMP");
stmt = con.createStatement();
stmt.executeUpdate("delete Employee where eno ="+ n);
System.out.println("\n Record Deleted");
}catch(ClassNotFoundException e)
{
System.out.println(e);
}catch(SQLException e)
{
System.out.println(e);
}
}
}
Question#14
Q. Write a Java Program to insert the data into database by using prepared statement.
Solution :
Program Name : InsertData.java
import java.sql.*;
import java.io.*;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDrriver");
con = DriverManager.getConnection("jdbc.odbc.EMP");
pstmt = con.prepareStatement("Insert into Employee values(?,?,?)");
pstmt.setInt(1,102);
pstmt.setString(2,"Ramesh");
pstmt.setInt(3,15000);
pstmt.executeUpdate();
System.out.println("\n Record Inserted");
}catch(ClassNotFoundException e)
{
System.out.println(e);
}catch(SQLException e)
{
System.out.println(e);
}
}
}
Question#15
Q. Write Java Program to build RMI application which sends welcome message to the user.
Solution :
1. Program Name : MsgRemote.java
import java.rmi.*;
import java.rmi.server.*;
try
{
MsgRemote obj = (MsgRemote)Naming.lookup("/msg");
System.out.println(obj.getMsg());
}catch(Exception e)
{
System.out.println(e);
}
}
}
Question#16
Q. Write Java Program to build RMI application which having remote method for the calculation of sum
of two integers.
Solution :
1. Program Name : ADDRemote.java
import java.rmi.*;
import java.rmi.server.*;
try
{
ADDRemote obj = (ADDRemote)Naming.lookup("/add");
System.out.println(obj.getSum(100,54));
}catch(Exception e)
{
System.out.println(e);
}
}
}
Question#17
Q. Write a Java Program to design a Java bean for the employee.
Solution :
Program Name : EMPLOYEE.java
}
Question#18
Q. Write a simple Java Servlet Program to welcome to the user.
Solution :
Program Name : GreetUser.java
import java.io.*;
import java.servlet.*;
import java.servlet.http.*;
public class GreetUser extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,
IOException
{
res.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD>Hello World</HEAD>");
out.println("<BODY>");
out.println("Weclcome to Java Servlet");
out.println("</BODY>");
out.println("</HTML>");
}
}
Question#19
Q. Write a Java Servlet Program to read the data from database and display in tabular form.
Solution :
Program Name : DisplayData.java
import java.io.*;
import java.sql.*;
import java.servlet.*;
import java.servlet.http.*;
out.println("<HTML>");
out.println("<HEAD>Hello World</HEAD>");
out.println("<BODY>");
out.println("<Table>");
out.println("<TR><TH>EMPNO</TH><TH>EMPNAME</TH><TH>SALARY</TH></TR>");
while(rs.next())
{
out.println("<TR><TD>");
out.println(rs.getInt("eno"));
out.println("</TD><TD>");
out.println(rs.getString("ename"));
out.println("</TD><TD>");
out.println(rs.getInt("sal"));
out.println("</TD><TR>");
}
out.println("</TABLE>");
out.println("</BODY>");
out.println("</HTML>");
}catch(ClassNotFoundException e)
{
System.out.println(e);
}catch(SQLException e)
{
System.out.println(e);
}
}
}
Question#20
Q. Write a Java Servlet Program to authenticate the login page and display appropriate message if login
fails.
Solution :
1. Program Name : login.html
<HTML>
<HEAD>
<TITLE>Insert Data Demo </TITLE>
</HEAD>
<BODY>
<H2> LOGIN FORM</H2>
<Form method="Get" action="Http://localhost:8080/Servlet/Validate.class">
<br>
<br>
User Name : <Input Type ="text" name="txtUser" size = "10">
<br>
Password : <Input Type ="text" name="txtPass" size = "10">
<br>
<Input Type="submit" name="Button" value="SUBMIT">
<Input Type="reset" name="Button" value="RESET">
</Form>
</Body>
</Html>
2. Program Name : Validate.java
import java.io.*;
import java.sql.*;
import java.servlet.*;
import java.servlet.http.*;
while(rs.next())
{
id = rs.getString("user");
pass = rs.getString("pass");
<HTML>
<HEAD>
<TITLE>Session Management Demo</TITLE>
</HEAD>
<BODY>
<H2> USER FORM</H2>
<Form method="Get" action="Http://localhost:8080/Servlet/Intermediate.class">
<br>
<br>
User Name : <Input Type ="text" name="txtUser" size = "10">
<br>
Password : <Input Type ="text" name="txtPass" size = "10">
<br>
<Input Type="submit" name="Button" value="SUBMIT">
<Input Type="reset" name="Button" value="RESET">
</Form>
</Body>
</Html>
2. Program Name : Intermediate.java
import java.io.*;
import java.sql.*;
import java.servlet.*;
import java.servlet.http.*;
out.println("<HTML>");
out.println("<HEAD>Session Management</HEAD>");
out.println("<BODY>");
out.println("Weclcome to Java Servlet");
out.println("</BODY>");
out.println("<Form method="Get" action="Http://localhost:8080/Servlet/Final.class">"):
out.println("<Input Type ="password" name="txtUser" size = "10" value="+user+">");
out.println("<Input Type="submit" name="Button" value="NEXT">"):
out.println("</Form>");
out.println("</HTML>");
}
}