Java/J2EE Programmer Practice Test 2   (page 1)



33. Consider the classes defined below:
import java.io.*;
class Super
{
int methodOne( int a, long b ) throws IOException
{ // code that performs some calculations
}
float methodTwo( char a, int b )
{ // code that performs other calculations
}
}
public class Sub extends Super
{

}
Which of the following are legal method declarations to add to the class Sub? Assume that each method is the only one being added.
a) public static void main( String args[] ){}
b) float methodTwo(){}
c) long methodOne( int c, long d ){}
d) int methodOne( int c, long d ) throws ArithmeticException{}
e) int methodOne( int c, long d ) throws FileNotFoundException{}

34. Assume that Sub1 and Sub2 are both subclasses of class Super.
Given the declarations:

Super super = new Super();
Sub1 sub1 = new Sub1();
Sub2 sub2 = new Sub2();

Which statement best describes the result of attempting to compile and execute the following statement:

super = sub1;

a) Compiles and definitely legal at runtime
b) Does not compile
c) Compiles and may be illegal at runtime

35. For the following code:
class Super
{ int index = 5;
public void printVal()
{ System.out.println( "Super" );
}
}
class Sub extends Super
{ int index = 2;
public void printVal()
{ System.out.println( "Sub" );
}
}
public class Runner
{ public static void main( String argv[] )
{ Super sup = new Sub();
System.out.print( sup.index + "," );
sup.printVal();
}
}
What will be printed to standard output?

a) The code will not compile.
b) The code compiles and "5, Super" is printed to standard output.
c) The code compiles and "5, Sub" is printed to standard output.
d) The code compiles and "2, Super" is printed to standard output.
e) The code compiles and "2, Sub" is printed to standard output.
f) The code compiles, but throws an exception.


36. How many objects are eligible for garbage collection once execution has reached the line labeled Line A?

String name;
String newName = "Nick";
newName = "Jason";
name = "Frieda";

String newestName = name;

name = null;
//Line A

a) 0
b) 1
c) 2
d) 3
e) 4


37. Which of the following statements about Java's garbage collection are true?

a) The garbage collector can be invoked explicitly using a Runtime object.
b) The finalize method is always called before an object is garbage collected.
c) Any class that includes a finalize method should invoke its superclass' finalize method.
d) Garbage collection behavior is very predictable.


38. What line of code would begin execution of a thread named myThread?




39. Which methods are required to implement the interface Runnable.

a) wait()
b) run()
c) stop()
d) update()
e) resume()


40. What class defines the wait() method?




41. For what reasons might a thread stop execution?

a) A thread with higher priority began execution.
b) The thread's wait() method was invoked.
c) The thread invoked its yield() method.
d) The thread's pause() method was invoked.
e) The thread's sleep() method was invoked.


42. Which method below can change a String object, s ?

a) equals( s )
b) substring( s )
c) concat( s )
d) toUpperCase( s )
e) none of the above will change s


43. If s1 is declared as:

String s1 = "phenobarbital";

What will be the value of s2 after the following line of code:
String s2 = s1.substring( 3, 5 );
1.
a) null
b) "eno"
c) "enoba"
d) "no"


44. What method(s) from the java.lang.Math class might method() be if the statement

method( -4.4 ) == -4;

is true.

a) round()
b) min()
c) trunc()
d) abs()
e) floor()
f) ceil()


45. Which methods does java.lang.Math include for trigonometric computations?

a) sin()
b) cos()
c) tan()
d) aSin()
e) aCos()
f) aTan()
g) toDegree()


46. This piece of code:

TextArea ta = new TextArea( 10, 3 );

Produces (select all correct statements):

a) a TextArea with 10 rows and up to 3 columns
b) a TextArea with a variable number of columns not less than 10 and 3 rows
c) a TextArea that may not contain more than 30 characters
d) a TextArea that can be edited


47. In the list below, which subclass(es) of Component cannot be directly instantiated:

a) Panel
b) Dialog
c) Container
d) Frame


48. Of the five Component methods listed below, only one is also a method of the class MenuItem. Which one?

a) setVisible( boolean b )
b) setEnabled( boolean b )
c) getSize()
d) setForeground( Color c )
e) setBackground( Color c )


49. If a font with variable width is used to construct the string text for a column, the initial size of the column is:

a) determined by the number of characters in the string, multiplied by the width of a character in this font
b) determined by the number of characters in the string, multiplied by the average width of a character in this font
c) exclusively determined by the number of characters in the string
d) undetermined


50. Which of the following methods from the java.awt.Graphics class would be used to draw the outline of a rectangle with a single method call?

a) fillRect()
b) drawRect()
c) fillPolygon()
d) drawPolygon()
e) drawLine()


51. The Container methods add( Component comp ) and add( String name, Component comp ) will throw an IllegalArgumentException if comp is a:

a) button
b) list
c) window
d) textarea
e) container that contains this container


52. Of the following AWT classes, which one(s) are responsible for implementing the components layout?

a) LayoutManager
b) GridBagLayout
c) ActionListener
d) WindowAdapter
e) FlowLayout


53. A component that should resize vertically but not horizontally should be placed in a:

a) BorderLayout in the North or South location
b) FlowLayout as the first component
c) BorderLayout in the East or West location
d) BorderLayout in the Center location
e) GridLayout


54. What type of object is the parameter for all methods of the MouseListener interface?



55. Which of the following statements about event handling in JDK 1.1 and later are true?

a) A class can implement multiple listener interfaces
b) If a class implements a listener interface, it only has to overload the methods it uses
c) All of the MouseMotionAdapter class methods have a void return type.


56. Which of the following describe the sequence of method calls that result in a component being redrawn?

a) invoke paint() directly
b) invoke update which calls paint()
c) invoke repaint() which invokes update(), which in turn invokes paint()
d) invoke repaint() which invokes paint directly


57. Which of these is a correct fragment within the web-app element of deployment descriptor. Select the two correct answer.
A.<error-page> <error-code>404</error-code> <location>/error.jsp</location> </error-page>
B.<error-page> <exception-type>mypackage.MyException</exception-type> <error-code>404</error-code> <location>/error.jsp</location> </error-page>
C.<error-page> <exception-type>mypackage.MyException</exception-type> <error-code>404</error-code> </error-page>
D.<error-page> <exception-type>mypackage.MyException</exception-type> <location>/error.jsp</location> </error-page>
58. A bean with a property color is loaded using the following statement
<jsp:usebean id="fruit" class="Fruit"/>
Which of the following statements may be used to set the of color property of the bean. Select the one correct answer.
1. <jsp:setColor id="fruit" property="color" value="white"/>
2. <jsp:setColor name="fruit" property="color" value="white"/>
3.<jsp:setValue name="fruit" property="color" value="white"/>
4.<jsp:setProperty name="fruit" property="color" value="white">
5.<jsp:setProperty name="fruit" property="color" value="white"/>
6.<jsp:setProperty id="fruit" property="color" value="white">
59. What gets printed when the following JSP code is invoked in a browser. Select the one correct answer.
<%= if(Math.random() < 0.5){ %>
  hello
<%= } else { %>
  hi
<%= } %>
a.The browser will print either hello or hi based upon the return value of random.
b.The string hello will always get printed.
c.The string hi will always get printed.
d.The JSP file will not compile.
60. Given the following web application deployment descriptor:
<web-app>
<servlet>
<servlet-name>myServlet</servlet-name>
<servlet-class>MyServlet</servlet-class>
...
</servlet>
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>

which statements are true?

1) servlet-mapping element should be inside servlet element
2) url-pattern can't be defined that way.
3) if you make the http call: http://host/Hello.jsp the servlet container will execute MyServlet.
4) It would work with any extension excepting jsp,html,htm
61.Name the class that includes the getSession method that is used to get the HttpSession object.
A.HttpServletRequest
B.HttpServletResponse
C.SessionContext
D.SessionConfig
62. What will be the result of running the following jsp file taking into account that the Web server has just been started and this is the first page loaded by the server?
<html><body>
<%= request.getSession(false).getId() %>
</body></html>
1)It won't compile
2)It will print the session id.
3)It will produce a NullPointerException as the getSession(false) method's call returns null, cause no session had been created.
4)It will produce an empty page.

63. The page directive is used to convey information about the page to JSP container. Which of these are legal syntax of page directive. Select the two correct statement
A.<% page info="test page" %>
B.<%@ page info="test page" session="false"%>
C.<%@ page session="true" %>
D.<%@ page isErrorPage="errorPage.jsp" %>
E.<%@ page isThreadSafe=true %>
64. Which of the following are methods of the Cookie Class?
1) setComment
2) getVersion
3) setMaxAge
4) getSecure.