Anda di halaman 1dari 7

Q: What is Thread?

Ans: A process in execution is known as Thread.

Q: Explain why it may be preferable to implement Runnable interface rather than


subclassing from the Thread class?
Ans: Because multiple inheritance in java is not possible.

Q: Define a subclass of the Thread class called MyThread, include a String member
called myStr and a constructor that takes in one String argument and initializes the
data member of the class.
(b) Define the run method such that it prints out the string 50 times.However it
should pause for 3 seconds when the String has been printed out 20 times and
another 5 seconds when it has been printed out 30 times.
(c) In the main function instantiate 3 threads called T1,T2,T3 and pass in “A” into
T1,”B” into T2 and “”C” into T3 and execute the threads.
Ans:
public class MyThread extends Thread {
String myStr;
Thread t;
MyThread(String s){
myStr = s;
}
public void run(){
for(int i=1;i<=50;i++){
System.out.println(myStr);
if(i==20){
try{
Thread.sleep(3000);

}catch(Exception e){}
}
if(i==30){
try{
Thread.sleep(5000);
}catch(Exception e){}
}
}
}
public static void main(String as[]){
MyThread T1 = new MyThread("A");
MyThread T2 = new MyThread("B");
MyThread T3 = new MyThread("C");
T1.start();
T2.start();
T3.start();
}
}
Q: How can know that a server is resident on your system and also write for
defining status of the port number on which it is resident by strong information
about each port in some physical file?
Ans:
Import java.net.*;
Import java.io.*;
Class server {
Server(){
//Int c = 0;
Char ch = ‘ ‘;
String s;
Try{
FileOutputStream fos = new FileOutputStream(”serverRes.txt”);
For(int i = 1024;i<65536;i++){
Try{
ServerSocket ss = new ServerSocket(i);
ss.close();
}catch(Exception e){
s =“server is resident at :”+i+” “;
System.out.println(s);
Byte b[] = s.getBytes();
fos.write(b);
}
}
}catch(Exception e){}

}
Public static void main(String as[]){
server s = new server();
}
}

Q :Why we use JavaScript?


Ans: Basically JavaScript is used for the client side scripting to build smarter forms
(validation) , working with cookies and creating rollovers.

Q: how would you handle event in Javascript?


Ans we can handle event by using appropriate event handlers. E.g.
.onChange , .onLoad , .onSubmit , onFocus etc

Q: How can you perform form validation using javascript?


Ans: watch my videos…:D
Q: how can you write functions in java?
Ans: <html>
<head>
<title>
Welcome to my page.
</title>
<script language=”javascript”>
Function fun(){
Var x = 10;
Var y = 15;
Var z = x + y;
Document.write(“The Sum of X and Y is : “+z);
}
</script>
</head>
<body>
<script language=”javascript”>
Fun();
</script>
</body>
</html>

Q: write Steps for the connectivity on Access Database with java considering DSN
“UAF” and table name as “Student”, write the difference between
executeUpdate(…) and executeQuery(…) method.

Ans:
Connection con = null;
Statement st = null;
ResultSet rs = null;

try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:UAF");
st = con.createStatement();
}
catch(Exception e){}
• Load the JDBC driver.
• Define the connection URL.
• Establish the connection.
• Create a statement object.
• Execute a query or update.
• Process the results.
• Close the connection.
(b) difference between executeUpdate() and executeQuery()
Ans: executeUpdate() - is used to update or modify the table, it
Will returns how many rows added/updated in the table.

executeQuery() - is used to retrieve the data from the


Table. Only for "select" query, this will return a
Resultset metadata

Q: write a simple program using javascript that calculates the tax to be paid by the
customer. The program should read in the total purchase value and add in the tax
rate of 6.0%. the tax amount and the total purchase value with tax should be
displayed.
Ans:
<html>
<head>
<title>
Welcome to my page.
</title>
<script language=”javascript”>
Function fun(){
Var amt,tax,pValue;
amt = parseInt(prompt(“Enter prinicipal amount”));
tax = amt * 0.06;
pValue=tax+amt;
document.write(“The amt of Tax is :”+tax+”<br />”);
document.write(“Total purchase value:”+pValue);
}
</script>
</head>
<body>
<script language=”javascript”>
fun();
</script>
</body>
</html>
Q; create a simple application called DBTest.java for servlet that emulates a small
database,which stores and retrieves records relating to products.
Record in the database should consist of name and quantity for different product.

import javax.servlet.*;
import java.io.*;
import java.sql.*;
public class DBTest extends GenericServlet {
String name;
int quant;
PrintWriter pw;
Connection con = null;
ResultSet rs = null;
Statement st = null;

public void service(ServletRequest req,ServletResponse res){


try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:products");
st = con.createStatement();

res.setContentType("text/html");

name = req.getParameter("name");
quant = req.getParameter("quantity");

//retrieve records
pw=res.getWriter();
pw.println("<br />"+name);
pw.println("<br />"+quant);

rs=st.executeQuery(“select * from table where name=”+name);


while(rs.next){
pw.println(“<br />”+rs.getString(1));
pw.println(“<br />”+rs.getString(2));
}
//stores records
st.executeUpdate("insert into user
values('"+name+"','"+quant+"')");
pw.println("<br />"+"Values are added");

}catch(Exception e){}
}
}
Q: write java script code that displays two text boxes and a submit button to input
name and email. write code for the validation of email and name fields.
Ans:
<html>
<head>
<script language="javascript">
function abc(){
var name = document.form1.name.value;
var email = document.form1.email.value;
var at = email.indexOf("@");
var dot = email.lastIndexOf(".");
if(name==""){
alert("Plz type in ur name");
}
else if(!isNaN(name)){
alert("Name should only contain A-Z or a-z");
}
else if(email==""){
alert("plz Type email Email");
}
else if(at==-1 || dot==-1){
alert("Invalid Email");
}
else if(dot < at){
alert("Invalid Email");
}
else if(dot-at==1){
alert("Invalid Email");
}
else if(at==0){
alert("Invalid Email");
}
else if(dot==email.length-1){
alert("Invalid Email");
}
else{
document.form1.submit();
}

}
</script>
</head>
<body>
<script language="javascript">
</script>
<form method="post" name="form1"action="http://Localhost:8080/servlet/demo" >
Name:<input type="text" name="name" /><br /><br />
Email:<input type="text" name="email" /><br /><br />
<input type="button" name="click" value="Submit" onClick="abc()" />
</form>
</body>
</html>

Q: Write the architecture of web Application also explain the role of web server?
Ans: The basic architecture of Web application is based on client side and server side
scripting also known as client liaison .
Client Side Coding:
• Html
• Css
• Javascript etc

Server Side Coding:


• Asp
• Servlet
• CGI etc
The main role of web server is that it hosts the web site while the server program helps
deliver the web pages and their associated files like images and flash movies. It also waits
for requests from web browsers (also known as clients) and responds by sending the
required data back.

Anda mungkin juga menyukai