Nothing Special   »   [go: up one dir, main page]

Untitled

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 48

What will be the output of following code?

import java.awt.*;

import java.applet.*;

import java.awt.event.*;

public class ButtonDemo1 extends Applet implements ActionListener


{

String msg=" ";

Button b1,b2,b3;

public void init()

b1=new Button("Red");

b2=new Button("Green");

b3=new Button("Blue");

add(b1);

add(b2);

add(b3);

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);

public void actionPerformed(ActionEvent ae)

String str=ae.getActionCommand();

if (str.equals("Red"))

setBackground(Color.red);

else if (str.equals("Green"))

setBackground(Color.green);
}

else if (str.equals("Blue"))

setBackground(Color.blue);

repaint();

public void paint(Graphics g)

g.drawString(msg,0,100);

/*<applet code="ButtonDemo1.class" width=200 height=400>


</applet>*/

Find the missing statement in the program.

/*

<APPLET Code="InnerClassDemo" Width=200 Height=250>

</APPLET>

*/

public class InnerClassDemo extends Applet

public void init()

addMouseListener(new MyMouseAdapter());

class MyMouseAdapter extends MouseAdapter


{

public void mousePressed(MouseEvent me)

showStatus("Mouse Pressed")

The following program will display _____________________

import java.net.*;

public class MyNetwork

public static void main(String args[]) throws UnknownHostException


{

InetAddress ip [] = InetAddress.getAllByName("www.google.com");

for(int i=0;i<ip.length;i++)

System.out.println(ip[i].getHostAddress());

What is output of following code: import java.io.*;

import java.net.*;

public class portscanner

public static void main(String args[])

{
String host=”localhost”;

for(int i=0;i<1024;i++)

try{

Socket s= new Socket(host,i);

System.out.println(“There is server on port” +i+ ”of” +host);

catch(UnknownHostexception ex)

Break;

catch(IOException e){}

}
Consider the following program.

What should be the correction done in the program to get correct output?

import java.sql.*;

class Ddemo1

public static void main(String args[]) throws Exception

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

Connection c=DriverManager.getConnection();

Statement s=c.createStatement();

ResultSet rs=s.executeQuery("select *from StudTable");

System .out.println("Name" + " t " + "Roll_No" + " t " + "Avg");

while(rs.next())

System.out.println(rs.getString(1)+" t "+rs.getInt(2)+" t t"+rs.getDouble(3));


}

s.close();

c.close();

Observe following code and find out missing statement.

import java.io.*;

import java.sql.*;

class updatdb

{public static void main(String args[])

{try

{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con =DriverManager.getConnection("jdbc:odbc:stud");

Statement s = con.createStatement();

int r = s.executeUpdate("update stud set name=priti where name='preety'");

System.out.println("No. of row updated:"+r)

ResultSet rs=s.executeQuery("select * from stud");

while(rs.next())

{System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getInt(3));

con.close();

catch(Exception e){ }

}
Consider the following program and identify the missing statement.
public class SimpleDemo

{ public static void main(String[] args)

Connection con=null;

Statement stmt=null;

ResultSet rs=null;

try

{ String jdbcurl="jdbc:mysql://localhost:3306/test";

con=DriverManager.getConnection(jdbcurl, "root", "root");

System.out.println("Connection established");

stmt=con.createStatement();

rs=stmt.executeQuery("select * from emp");

System.out.println("Empid t Emap name t City t Salary");

while(rs.next())

System.out.print(rs.getInt(1)+"t");

System.out.print(rs.getString(2)+"t");

System.out.print(rs.getString(3)+"t");

System.out.print(rs.getFloat(4));

System.out.println();

catch(ClassNotFoundException e) { e.printStackTrace();}

catch(SQLException e) {e.printStackTrace()}

finally
{try {

rs.close();

stmt.close();

con.close();

catch(Exception e){}

}} }
Consider following code.Fill _____________statement in following code from option given below

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class CookieExample extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws IOException, ServletException

response.setContentType("text/html");

PrintWriter out = response.getWriter();

Cookie[] cookies = request.getCookies();

for (int i = 0; i < cookies.length; i++) {

Cookie c = cookies[i];
String name = c.getName();

String value = c.getValue();

out.println(name + " = " + value);

String name = ______________________

if (name != null && name.length() > 0) {

String value = request.getParameter("cookieValue");

Cookie c = new Cookie(name, value);

response.addCookie(c);

}
following servlet read parameters passed through the html page. Write the correct HTML code for given
servlet. Import jba. Io.*;

import java.util.&;

import javax.servlet.*;

public class Example2servlet extends Generic servlet {

public viod serice request reg, ServletResponse res) throws ServletException, IOException {

PrintWriter pw = res.getWriter();

Enumeration E= reg.get PaarameterNames();

while ( e. has More Elecment()) {

strignpaname =(string)e.nextElecment(); pw.printin(pname+="");

String pvalue = req.getParameter(pname);

pw . printIn (pvalue); } pw. close();

<html> <head> <title><title> <meta htt-equiv="Content-type" content ="text/html; charset= UTF-8">


</head> <body> <form name = "forml" method ="post" action =
http://localhost:8080/WebApplication1/Example2Servlet> <table>
_____________________________________ ___________________________________
______________________________</table> <input type = "subit" value = "submit" > </body> </html>

In following Java program fill statement showing *** . Select any one option from given options
import java.io.*;

import java.util.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class DateServlet extends HttpServlet{

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,


IOException{

HttpSession hs = *****************

response.setContentType(“text/html”);

PrintWriter pw=response.getWriter();

pw.print(“<B>”);

Date date= (Date)hs.getAttribute(“date”);


if(date!=null){

pw.print(“Last access:”+date+”<br>”);

date =new Date();

hs.setAttribute(“date”, date);

pw.println(“Current date:”+date);

}
}

You might also like