Sample Questions

advertisement
1.Every doXxx method in the HttpServlet class has a parameter of the
__________ type, which is an object that contains HTTP request information,
including parameter name and values, attributes, and an input stream.
A. HttpServletResponse
B. HttpServletRequest
C. HttpSession
D. Cookie
2. _________ is a subinterface of ServletRequest.
A. Servlet
B. HttpServletRequest
C. HttpServletResponse
D. HttpServlet
3.Suppose the two parameters in the doGet or doPost method is request
and response. To send output to a client, create a PrintWriter using
_____________.
A. response.getWrite()
B. response.writer()
C. response.getPrintWriter()
D. response.getWriter()
4.Which of the following statements are true?
A. JSP is translated into HTML by a Web server when a JSP is called.
B. JSP is translated into Java servlet by a Web server when a JSP is called.
C. YOu can embed Java code in JSP.
D. JSP is translated into XML by a Web server when a JSP is called.
5.______________ is a JSP declaration.
A. <%! private long computeFactorial(int n) { if (n == 0) return 1; else return n *
computeFactorial(n - 1); } %>
B. <!-- HTML Comment -->
C. <% for (int i = 0; i <= 10; i++) { %>
D. <%= Math.pow(2, 3) %>
E. <%= i %>
6.A class is a JavaBeans component if ____________________.
A. it is a public class
B. it has a public constructor with no arguments
C. it is serializable.
7. _______________ is a JSP expression.
A. <%= Math.pow(2, 3) %>
B. <%= new Date().toString() %>
C. <%= i %>
D. <% for (int i = 0; i <= 10; i++) { %>
8. Given the following code, which set of code can be used to replace the
comment so that the program displays time to the console every second?
import java.applet.*;
import java.util.*;
public class Test extends Applet implements Runnable {
public void init() {
Thread t = new Thread(this);
t.start();
}
public void run() {
for(; ;) {
//display time every second
System.out.println(new Date().toString());
}
}
}
A. try { Thread.sleep(1000); } catch(InterruptedException e) { }
B. try { sleep(1000); } catch(InterruptedException e) { }
C. try { Thread.sleep(1000); } catch(RuntimeException e) { }
D. try { t.sleep(1000); } catch(InterruptedException e) { }
9. Which of the following expressions must be true if you create a thread
using Thread = new Thread(object)?
A. object instanceof Applet
B. object instanceof Thread
C. object instanceof Frame
D. object instanceof Runnable
10. You should not directly invoke the run() method of a thread object.
A. true
B. false
11. Show the output of running the class Test in the following code:
interface A {
void print();
}
class C {}
class B extends C implements A {
public void print() { }
}
public class Test {
public static void main(String[] args) {
B b = new B();
if (b instanceof A)
System.out.println("b is an instance of A");
if (b instanceof C)
System.out.println("b is an instance of C");
}
}
A.
B.
C.
D.
Nothing.
b is an instance of A.
b is an instance of C.
b is an instance of A followed by b is an instance of C.
12. What is the output of running class C?
class A {
public A() {
System.out.println(
"The default constructor of A is invoked");
}
}
class B extends A {
public B(String s) {
System.out.println(s);
}
}
public class C {
public static void main(String[] args) {
B b = new B("The constructor of B is invoked");
}
}
a.
b.
c.
d.
none
"The constructor of B is invoked"
"The default constructor of A is invoked"
"The constructor of B is invoked"
"The default constructor of A is invoked"
13. Analyze the following code:
public class Test1 {
public Object max(Object o1, Object o2) {
if ((Comparable)o1.compareTo(o2) >= 0) {
return o1;
}
else {
return o2;
}
}
}
a.
The program has a syntax error because Test1 does not have a main
method.
b.
The program has a syntax error because o1 is an Object instance and
it does not have the compareTo method.
c.
The program has a syntax error because you cannot cast an Object
instance o1 into Comparable.
d.
The program would compile if ((Comparable)o1.compareTo(o2) >= 0)
is replaced by (((Comparable)o1).compareTo(o2) >= 0).
e.
b and d are both correct.
14. Which of the following possible modifications will fix the errors in this
code?
public class Test {
private double code;
public double getCode() {
return code;
}
protected abstract void setCode(double code);
}
a.
b.
c.
d.
Remove abstract in the setCode method declaration.
Change protected to public.
Add abstract in the class declaration.
b and c.
15. Analyze the following code.
class Test {
public static void main(String[] args) {
Object x = new Integer(2);
System.out.println(x.toString());
}
}
a.
The program has syntax errors because an Integer object is assigned
to x.
b.
When x.toString() is invoked, the toString() method in the Object class
is used.
c.
When x.toString() is invoked, the toString() method in the Integer class
is used.
d.
None of the above.
16. Analyze the following code:
public class Test {
public static void main(String args[]) {
Test nc = new Test();
nc.t = nc.t++;
}
int t;
Test() {
}
}
a.
The program has a compilation error because t is not initialized.
b.
The program does not compile because the parameter list of the
main method is wrong.
c.
The program compiles, but has a runtime error because t has no
initial value.
d.
The program has a compilation error because you attempt to
create an object of the Test inside the Test class.
e.
The program compiles and runs fine.
Keys:
1. B
2. B
3. D
4. BC
5. A
6. ABC
7. ABC
8. A
9. D
10. A
11. D
12. C
13. E
14. C
15. C
16. E
Download