Sie sind auf Seite 1von 10

SERVLET & JSP QUESTIONS Duration : 1hour 1) Which one of the following is suitable for life cycle of Servlets?

a) b) c) d) init, start, stop, destroy init, service, destroy constructor, init, service, destroy constructor, init, stop, destroy

2) Write a sample program using the methods in the Cookies class. 3) What is mean by Servlet Chaining? Write any one of the uses of Servlet Chaining. 4) What is the result of the following operation? System.out.println(4 | 3); a) 6 b) 0 c) 1 d) 7

5) What will be the result when you attempt to compile and run the following code?. public class Conv{ public static void main(String argv[]){ Conv c=new Conv(); String s=new String("ello"); c.amethod(s); } public void amethod(String s){ char c='H'; c+=s; System.out.println(c); } } a) Compilation and output the string "Hello" b) Compilation and output the string "ello" c) Compilation and output the string elloH d) Compile time error 6) Which of the following wrapper classes can not take a "String" in constructor

a) Boolean b) Integer c) Long d) Character e) Byte f) Short 7) What is the output of the following: int x = 0,y = 2,z = -1; int k = x++ + y-- * (z-- * x); System.out.println(k); a. b. c. d. e. -1 0 2 1 -2

8) What are the values of x, y, and z after execution? int x=3, y=4, z=5; switch(y) { case 2: x *= y; case 4: y = (x + z)/y; case 6: z += y++; break; case 8: z++; default: x -= y; } a) b) c) d) e) x=12, y=5, z=9 x=3, y=3, z=8 x=0, y=3, z=8 x=12, y=5, z=10 x=3, y=3, z=7

9) static { Additional code here

} In reference to the above, what does a static code block mean? a) b) c) d) e) The internal code will be executed once when the class is first loaded All internal variables must be "static". A compiler error will occur due to an incomplete method definition Internal variables are erased when the object is written to persistent storage. All methods and variables defined inside block are implicitly static

10) class A { int doubleValue(int i) { return i*2; } } Referring to the above, which code segment properly overrides the doubleValue() method? a) class B extends A { int doubleValue(int i) { return super.doubleValue(i); } } b) int doubleValue(float f) { return ((int)f*2); } c) class B extends A { double doubleValue(int i) { return (double) super.doubleValue(i); } } d) float doubleValue(float f) { return f*2; } 11) Which describes a class that implements the Enumeration interface? a) b) c) d) It can work with the StringTokenizer class It contains implementations of the hasMoreElements() and nextElement() methods It can be used to store associative arrays It is a subclass of Vector

e) It has implementations of the toString() and getNextString() methods 12) What code sample would declare a two-dimensional array of integers called "intArray"? a) b) c) d) e) int intArray[][]; Two-dimensional arrays are not allowed in java. int **intArray; int intArray{int intArray[]}; int intArray[] = {int intArray[]};

13) String sql = "SELECT FIRST_NAME, LAST_NAME FROM EMPLOYEE WHERE ID='123'"; Statement st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATEABLE); ResultSet rs = st.executeQuery(sql); rs.first(); rs.updateString(1,"Tom"); rs.updateString(2,"Smith"); What statement is next statement in the above program? a) b) c) d) e) con.commit(); rs.updateRow(); st.saveChanges(); rs.next(); rs.close();

14) class A { int i, j, k; public A() { i=3; } public A(int i1) { i = i1; } public A(int i1, int k1) { this(i1); k = k1; } } What is wrong with class A? a) b) c) d) e) there are multiple methods named () all methods must declare return datatypes nothing a method can not have the same name as its class methods can not invoke other methods in their own class

15) What are the methods involved in managing session life cycle? 16) What are the methods available in the request object used to get the Information about the client? 17) String sql = select * from emp; ResultSet rs = statement.executeQuery(sql); After execution of these two statements, where will be the record pointer in ResultSet.? a) b) c) d) e) At the last line of the result set First line of the result set Before the first line after the last line center of the result set

18) What is mean by Prepared Statements? 19) If you want to override a JSP file's initialization method, within what type of tags must you declare the method? a) b) c) d) e) <@ @> <% @> <% %> <%! %> Cannot override default initialization methods

20) <jsp:setProperty name="beanid" property="beanVariable" param="reqParam" /> In which of the cases will the above call the appropriate setter method ? a) If the bean has a property named beanVariable. b) If the request has a parameter reqParam. c) Always, If the request has a parameter reqParam its value will be assigned else null will be assigned. d) Will not compile. property="*" is only valid value. e) Will not compile. class is not specified. 21) Which of the following are true? a) b) c) d) <%@ include file="somefile.jsp" %> is included at compile time. <jsp:include page="somefile.jsp" %> is included at runtime. <%@ include file="somefile.jsp" %> is included at parsing time. <jsp:include page="somefile.jsp" %> is included at compile time.

22) What is the difference between doing an <jsp:include > and a <jsp:forward > tag? a) b) c) d) e) The <jsp:include > tag does not maintain the original request and response objects. The <jsp:forward > tag does not maintain the original request and response objects. In <jsp:forward > tag is deprecated with <jsp:include > In <jsp:include > tag control is resumed after the included page finishes. In <jsp:forward > tag control is resumed after the included page finishes.

23) Which of the following will define a taglib uri used in jsp file? a) b) c) d) <taglib uri='/uripath' location='/filelocation' > </taglib> <taglib><uri>uripath</uri><location>filelocation</location></taglib> <taglib-uri location='/filelocation'>uripath</taglib-uri> <taglib> <taglib-uri>/uripath</taglib-uri> <taglib-location>/filelocation</tagliblocation></taglib> e) <taglib-uri uri='/uripath' >filelocation</taglib-uri> 24) True or False: JSP error pages cannot access a database? a) True. b) False. 25) Write a servlet program to fetch and display a student record from the database (any database) table name : student Field Name Regno Name Marks Data type Integer Character(25) Integer Constraints Primary Key

Assume that user enters the Regno from a html file, name of the textbox in th html file are regno. You have to display Name of the student and marks obtained by the student.

26) What is the XML schema code used to define an element as having character data? a) (CHARACTERDATA) b) (STRING)

c) (CHAR) d) (CDATA) e) None of the above 27) In XML, in the root tag, which attribute identifies the namespace used? a) xmlns b) ns c) ?xmlns d) ?ns e) None of the above 28) Consider the code below. Which element would nodRoot.childNodes(0) return? <Authors> <Author> <au_id>172-32-1776</au_id> <au_lname>White</au_lname> <au_fname>Johnson</au_fname> </Author> </Authors> a) b) c) d) e) Authors Author au_id au_fname None of the above

29) For the XML parser to ignore a certain section of your XML document, which syntax is correct? a) <xml:CDATA[ Text to be ignored ]> b) <PCDATA> Text to be ignored </PCDATA> c) <CDATA> Text to be ignored </CDATA> d) <![CDATA[ Text to be ignored ]]> 30) What are the types of attributes available in DTD?

Answers 1) B. Init, service, destroy

2) Sample program using the following methods, Req.getCookies(); Cookies.getName(); Cookies.getValue(); New Cookie(name,value); Req.addCookie(cookie);

3) 4) d - 7 5) d Compile time error 6) d - Character 7) e (-2) 8) e x=3, y=3, z=7 9) a The internal code will be executed once when the class first loaded. 10) a 11) b 12) a int intArray[][]; 13) b rs.updateRow(); 14) c nothing 15) req.getSession(true); session.getValue(); session.putValue(); session.getValueNames();

16) req.getRemoteAddr();req.getRemoteHost();

17) c before the first line 18) Prepared statement are statements which is precompiled by the database for faster execution. Prepared statements are useful in applications that have to run the same general SQL command over and over. 19) d <%! %> 20) a,b,c 21) a 22) a 23) a 24) b - false 25) Import java.sql.*; Import javax.servlet.*; Import javax.servlet.http.*; Public class Test { Connection CON; Statement SMT; public void init(ServletConfig config) throws ServletException { super.init(config); try { Class.forName("org.gjt.mm.mysql.Driver").newInstance(); CON = DriverManager.getConnection("jdbc:odbc:emp_odbc"); SMT = CON.createStatement(); } catch(Exception e) { } }

public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { PrintWriter out = res.getWriter(); res.setContentType("text/html"); String regNo = req.getParameter(reg_no); String sql = "select * from employee"; String sql = "select name, marks from student where reg_no ="+regNo+; ResultSet rs = smt.executeQuery(sql); while(rs.next()) { String str = ""; str = ""+rs.getInt("name"); str += " " +rs.getString("marks"); out.println("Str : "+str); } rs.close(); CON.close(); } } 26) d 27) a 28) b 29) d 30) default, implied, Required, fixed, [enumerated]

Das könnte Ihnen auch gefallen