Sun Certified Programmer
Practice Exam (The
answers)
Here are the rules:
Allow 2 hours and 15 minutes to complete this
exam. You should turn off the telephone, go someplace you won’t be disturbed,
check the time on your watch, and begin. Don’t bring any books with you,
because you won’t have them during the test. You can take breaks, but don’t
look anything up. You can have a piece of scratch paper and a pen or pencil,
but you can’t have any crib sheets!
If you get 49 questions right, you’ve hit the
70% mark, and you’ve passed. Good luck!
Questions
Question 1: Which of
the following class definitions defines a legal abstract
class?
Select all right answers.
a)
class Animal {
abstract void growl();
} |
b)
abstract Animal {
abstract void growl();
} |
c)
class abstract Animal {
abstract void growl();
} |
d)
abstract class Animal {
abstract void growl();
} |
e)
abstract class Animal {
abstract void growl() {
System.out.println("growl");
}
} |
Question 2: For an object to be a target
for a Thread, that object must be of type:
Fill in the blank.
Question 3: What is the proper way of defining
a class named Key so that it cannot be subclassed?
a)
b)
abstract final class Key { } |
c)
d)
e)
Question 4: What modes are legal for creating
a new RandomAccessFile object?
Select all valid answers.
-
"w"
-
"r"
-
"x"
-
"rw"
-
"xrw"
Question 5: Given the following code:
class Tester {
public static void main(String[] args) {
CellPhone cell = new CellPhone();
cell.emergency();
}
}
class Phone {
final void dial911() {
// code to dial 911 here . . ..
}
}
class CellPhone extends Phone {
void emegency() {
dial911();
}
} |
What will happen when you try to compile and run
the Tester class?
-
The code will not compile because Phone is not also
declared as final.
-
The code will not compile because you cannot invoke
a final method from a subclass.
-
The code will compile and run fine.
-
The code will compile but will throw a NoSuchMethodException
when Tester is run.
-
Phone and CellPhone are fine, but Tester will not
compile because it cannot create an instance of a class that derives from
a class defining a final method.
Question 6: Which assignments are legal?
Select all valid answers.
-
long test = 012;
-
float f = -412;
-
int other = (int)true;
-
double d = 0x12345678;
-
short s = 10;
Question 7: Given this class definitions:
abstract class Transaction implements
Runnable { }
class Deposit extends Transaction {
protected void process() {
addAmount();
}
void undo(int i) {
System.out.println("Undo");
}
} |
What will happen if we attempted to compile the code?
Select the one right answer.
-
This code will not compile because the parameter
i
is not used in undo().
-
This code will not compile because there is no main()
method.
-
This code will not compile because Deposit must be
an abstract class.
-
This code will not compile because Deposit is not
declared public.
-
Everything will compile fine.
Question 8: Which exception might wait()
throw?
Fill in the blank.
Question 9: Which of the following are
not Java keywords:
abstract |
double |
int |
static |
boolean |
else |
interface |
super |
break |
extends |
long |
superclass |
byte |
final |
native |
switch |
case |
finally |
new |
synchronized |
catch |
float |
null |
this |
char |
for |
open |
throw |
class |
goto |
package |
throws |
close |
if |
private |
transient |
const |
implements |
protected |
try |
continue |
import |
public |
void |
default |
instanceof |
return |
volatile |
do |
integer |
short |
while |
Select all valid answers.
-
superclass
-
goto
-
open
-
close
-
integer
-
goto, import
-
goto, superclass, open, close
-
they are all valid keywords
Question 10: Which of the following represents
an octal number?
Select all that apply.
-
0x12
-
32O
-
032
-
(octal)2
-
1
Question 11: What will appear in the standard
output when you run the Tester class?
class Tester {
int var;
Tester(double var) {
this.var = (int)var;
}
Tester(int var) {
this("hello");
}
Tester(String s) {
this();
System.out.println(s);
}
Tester() {
System.out.println("good-bye");
}
public static void main(String[] args) {
Tester t = new Tester(5);
}
} |
-
nothing
-
"hello"
-
5
-
"hello" followed by "good-bye"
-
"good-bye" followed by "hello"
Question 12: Write a line of code to use the
String’s substring() method to obtain the substring
"lip" from a String instance named s that is
set to "tulip".
Fill in the blank.
Question 13: There are a number of labels
in the source code below. These are labeled a
through j. Which label identifies the earliest
point where, after that line has executed, the object referred to by the
variable first may be garbage collected?
class Riddle {
public static void main(String[] args) {
String first, second;
String riddle;
if (args.length < 2)
return; a: first = new String(args[0]);
b: second = new String(args[1]);
c: riddle = "When is a " + first;
d: first = null;
e: riddle += " like a " + second + "?";
f: second = null;
g: System.out.println(riddle);
h: args[0] = null;
i: args[1] = null;
j:
}
}
|
Select the one right answer.
-
d:
-
e:
-
h:
-
i:
-
j:
Question 14: What are the range of values
for a variable of type byte?
Select the one right answer.
-
-27 to 27 - 1
-
0 to 27
-
-215 to 215
-
-215 to 215 -1
-
-215 - 1 to 215
Question 15: What will happen when you try
to compile and run the following program?
class Car {
int milesPerGallon;
int index;
Car(int mpg) {
milesPerGallon = mpg;
index = 0;
}
Car() {
}
public static void main(String[] args) {
int index;
Car c = new Car(25);
if (args.length > 0)
if (args[index].equals("Hiway"))
milesPerGallon*= 2; System.out.println("mpg:
" + milesPerGallon);
}
}
|
Select the one right answer.
-
The code compiles and displays "mpg: 50" if the command-line
argument is "Hiway". If the command-line argument is not "Hiway", the code
displays "mpg: 25".
-
The code compiles and displays "mpg: 50" if the command-line
argument is "Hiway". If the command-line argument is not "Hiway", the code
throws an ArrayIndexOutOfBoundsException.
-
The code does not compile because the automatic variable
named index has not been initialized.
-
The code does not compile because milesPerGallon
has not been initialized.
-
The code does not compile because the no-args constructor
is not written correctly.
Question 16: What will happen when you compile
and run this program:
class Array {
public static void main(String[] args) {
int length = 100;
int[] d = new int[length];
for (int index = 0; index < length; index++)
System.out.println(d[index]); }
}
|
Select the one right answer.
-
The code will not compile because the int[]
array is not declared correctly.
-
The code will compile but will throw an IndexArrayOutOfBoundsException
when it runs and nothing will appear in the standard output.
-
The code will display the numbers 0 through 99 in
the standard output, and then throw an IndexOutOfBoundsException.
-
The code will compile but the println()
method will throw a NoSuchMethodException.
-
This code will work fine and display 100 zeroes in
the standard output.
Question 17: What is the result of attempting
to compile and run the following class?
class Ar {
public static void main(String[] args) {
int[] seeds = new int[3];
for (int i = 0; i < seeds.length; i++)
System.out.println(i); }
}
|
Select all valid answers.
-
0
-
1
-
2
-
3
-
the program does not compile because the seeds array
is not initialized
Question 18: What method name can you use
from the applet to read a String passed to an applet via the <param>
tag? (Supply the method name only, without parameters.)
Fill in the blank.
Question 19: Given these class definitions:
class Superclass { }
class Subclass1 extends Superclass { } |
and these objects:
Superclass a = new Superclass();
Subclass1 b = new Subclass1(); |
which of the following explains the result of the
statement:
Select the one right answer.
-
Illegal at compile time
-
Legal at compile time but possibly illegal at runtime
-
Definitely legal at runtime
Question 20: Given these
class definitions:
class Superclass { }
class Subclass1 extends Superclass { }
class Subclass2 extends Superclass { } |
and these objects:
Superclass a = new Superclass();
Subclass1 b = new Subclass1();
Subclass2 c = new Subclass2(); |
which of the following explains the result of the
statement:
Select the one right answer.
-
Illegal at compile time
-
Legal at compile time but possibly illegal at runtime
-
Definitely legal at runtime
Question 21: How you can use the escape notation
\u to set the variable c, declared as a char,
to the Unicode character whose value is hex 0x30A0?
Fill in the blank.
Question 22: Which operators are overloaded
for String objects?
Question 20: Given these
class definitions:
class Superclass { }
class Subclass1 extends Superclass { }
class Subclass2 extends Superclass { } |
and these objects:
Superclass a = new Superclass();
Subclass1 b = new Subclass1();
Subclass2 c = new Subclass2(); |
which of the following explains the result of the
statement:
Select the one right answer.
-
Illegal at compile time
-
Legal at compile time but possibly illegal at runtime
-
Definitely legal at runtime
Question 21: How you can use the escape notation
\u to set the variable c, declared as a char,
to the Unicode character whose value is hex 0x30A0?
Fill in the blank.
Question 22: Which operators are overloaded
for String objects?
Select all valid answers.
-
-
-
+=
-
>>
-
&
none of these
Question 23: How can you change the
break
statement below so that it breaks out of both the inner and middle loops
and continues with the next iteration of the outer loop?
outer: for (int x = 0; x < 3;
x++) {
middle: for (int y = 0; y < 3; y++) {
inner: for (int z = 0; z < 3; z++) {
if (arr(x, y, z) == targetValue)
break;
}
}
} |
Select the one right answer.
-
break inner;
-
break middle;
-
break outer;
-
continue;
-
continue middle;
Question 24: Given this code snippet:
try {
tryThis();
return;
} catch (IOException x1) {
System.out.println("exception 1");
return;
} catch (Exception x2) {
System.out.println("exception 2");
return;
} finally {
System.out.println("finally");
} |
What will appear in the standard output if tryThis()
throws a NumberFormatException?
Select the one right answer.
-
Nothing
-
"exception 1", followed by "finally"
-
"exception 2", followed by "finally"
-
"exception 1"
-
"exception 2"
Question 25: Given these class definitions:
class Superclass { }
class Subclass1 extends Superclass { } |
and these objects:
Superclass a = new Superclass();
Subclass1 b = new Subclass1(); |
which of the following explains the result of the
statement:
Select the one right answer.
-
Illegal at compile time
-
Legal at compile time but possibly illegal at runtime
-
Definitely legal at runtime
Question 26: Given these class definitions:
class Superclass { }
class Subclass1 extends Superclass { } |
and these objects:
Superclass a = new Superclass();
Subclass1 b = new Subclass1(); |
which of the following explains the result of the
statement:
Select the one right answer.
-
Illegal at compile time
-
Legal at compile time but possibly illegal at runtime
-
Definitely legal at runtime
Question 27: To invoke read()
from an InputStream subclass, you must handle what type of exception?
Fill in the blank.
Question 28: Imagine there are two exception
classes called Exception1 and Exception2 that descend from the Exception
class. Given these two class definitions:
class First {
void test() throws Exception1, Exception2 { .
. . }
}
class Second extends First {
void test() { . . . }
} |
Create a class called Third that extends Second and
defines a test() method. What exceptions can
Third’s test() method throw?
Select all valid answers.
-
Exception1
-
Exception2
-
no checked exceptions
-
any exceptions declared in the throws
clause of the Third’s test() method.
Question 29: What is the result of executing
the following code:
class Test {
public static void main(String[] args) {
Test t = new Test();
t.test(1.0, 2L, 3);
}
void test(double a, double b, short c) {
System.out.println("1");
}
void test(float a, byte b, byte c) {
System.out.println("2");
}
void test(double a, double b, double c) {
System.out.println("3");
}
void test(int a, long b, int c) {
System.out.println("4");
}
void test(long a, long b, long c) {
System.out.println("5");
}
} |
Select the one right answer.
-
1
-
2
-
3
-
4
-
5
Question 30: Given this code snippet:
double a = 90.7;
double b = method(a);
System.out.println(b); |
If this snippet displays 90
in the standard output, what Math method did method()
invoke?
Select all valid answers.
-
abs()
-
min()
-
floor()
-
round()
-
ceil()
Question 31: In Java 1.0.2, to make a Button
object non-responsive to mouse clicks, you can invoke which method? (Only
supply the method name, without a parameter list.)
Fill in the blank.
Question 32: Given this code snippet:
double a = 14.9;
double b = method(a);
System.out.println(b); |
If this snippet displays 15 in the standard output,
what Math method(s) could method() have invoke?
Select the one right answer.
-
ceil() and round()
-
floor() and round()
-
ceil() only
-
floor() only
-
round() only
Question 33: What methods does Java define
in the Math class specifically for trigonometric calculations?
Select all valid answers.
-
cos()
-
asin()
-
tan()
-
sin()
-
angle()
Question 34: What String instance method would
return true when invoked like this:
where a = "GROUNDhog" and b = "groundHOG"?
Select the one right answer.
-
equals()
-
toLowerCase()
-
toUpperCase()
-
equalsIgnoreCase()
-
none of the above
Question 35: At the end of these two lines
of code:
String s = "hypertext";
String t = s.substring(2, 5); |
What does the object reference t
contain?
Select the one right answer.
-
"yper"
-
"ype"
-
"pert"
-
"per"
-
"perte"
Question 36: What access control keyword should
you use to allow other classes to access a method freely within its package,
but to restrict classes outside of the package from accessing that method?
Select all valid answers.
-
public
-
protected
-
private
-
do not supply an access control keyword
Question 37:
After these two lines of code:
String s = "Dolly ";
String t = s.concat("Hello, "); |
What characters will the object reference t
contain?
Select the one right answer.
-
"Hello, Dolly "
-
"Dolly Hello, "
-
"Hello, "
-
"Dolly "
-
none of the above
Question 38: What does the following code
do?
File f = new File("hello.test");
FileOutputStream out = new FileOutputStream(f); |
Select the one right answer.
Create a new file named "hello.test" if
it does not yet exist. It also opens the file so you can write to it and
read from it.
-
Create a new file named "hello.test" if it does not
yet exist. The file is not opened.
-
Open a file named "hello.test" so that you can write
to it and read from it, but does not create the file if it does not yet
exist.
-
Open a file named "hello.test" so that you can write
to it but cannot read from it.
-
Create an object that you can now use to create and
open the file named "hello.test," and write to and read from the file.
Question 39: Which expressions are illegal?
Select all valid answers.
-
(true & true)
-
(4 & 5)
-
(int myInt = 0 > 3)
-
float myFloat = 40.0;
-
boolean b = (boolean)99;
Question 40: Which label
name(s) are illegal?
Select all valid answers.
-
here:
-
_there:
-
this:
-
that:
-
2to1odds:
Question 41: Given this code:
import java.io.*;
class Write {
public static void main(String[] args) throws
Exception {
File file = new File("temp.test");
FileOutputStream stream = new FileOutputStream(file);
// write integers here. . .
}
} |
How can you replace the comment at the end of main()
with code that will write the integers 0 through 9?
Select the one right answer.
a)
DataOutputStream filter = new DataOutputStream(stream);
for (int i = 0; i < 10; i++)
filter.writeInt(i);
|
b)
for (int i = 0; i < 10; i++)
file.writeInt(i);
|
c)
for (int i = 0; i < 10; i++)
stream.writeInt(i);
|
d)
DataOutputStream filter = new DataOutputStream(stream);
for (int i = 0; i < 10; i++)
filter.write(i);
|
e)
for (int i = 0; i < 10; i++)
stream.write(i);
|
Question 42: What keyword, when used in
front of a method, must also appear in front of the class?
Fill in the blank.
Question 43: What letters get written to the
standard output with the following code?
class Unchecked {
public static void main(String[] args) {
try {
method();
} catch (Exception e) {
}
}
static void method() {
try {
wrench();
System.out.println("a");
} catch (ArithmeticException e) {
System.out.println("b");
} finally {
System.out.println("c");
}
System.out.println("d");
}
static void wrench() {
throw new NullPointerException();
}
} |
Select all valid answers.
-
"a"
-
"b"
-
"c"
-
"d"
-
none of these
Question 44: What happens if the file "Ran.test"
does not yet exist and you attempt to compile and run the following code?
import java.io.*;
class Ran {
public static void main(String[] args) throws
IOException {
RandomAccessFile out = new RandomAccessFile("Ran.test",
"rw");
out.writeBytes("Ninotchka");
}
} |
Select the one right answer.
-
The code does not compile because RandomAccessFile
is not created correctly.
-
The code does not compile because RandomAccessFile
does not implement the writeBytes() method.
-
The code compiles and runs but throws an IOException
because "Ran.test" does not yet exist.
-
The code compiles and runs but nothing appears in
the file "Ran.test" that it creates.
-
The code compiles and runs and "Ninotchka" appears
in the file "Ran.test" that it creates.
Question 45: If you run the following code
on a on a PC from the directory c:\source:
import java.io.*;
class Path {
public static void main(String[] args) throws
Exception {
File file = new File("Ran.test");
System.out.println(file.getAbsolutePath());
}
} |
what do you expect the output to be?
Select the one right answer.
-
Ran.test
-
source\Ran.test
-
c:\source\Ran.test
-
c:\source
-
null
Question 46: If you supply a target object
when you create a new Thread, as in:
Thread t = new Thread(targetObject); |
What test of instanceof
does targetObject have to pass for this to
be legal?
Select the one right answer.
-
targetObject instanceof Thread
-
targetObject instanceof Object
-
targetObject instanceof Applet
-
targetObject instanceof Runnable
-
targetObject instanceof String
Question 47: What appears in the standard
output when you run the Dots class?
class Dots implements Runnable {
DotThread t;
public static void main(String[] args) {
Dots d = new Dots();
d.t = new DotThread();
}
public void init() {
t.start();
t = new DashThread().start();
}
}
class DotThread extends Thread {
public void run() {
for (int index = 0; index < 100; index++)
System.out.print(".");
}
}
class DashThread extends Thread {
public void run() {
for (int index = 0; index < 100; index++)
System.out.print("-");
}
} |
-
nothing
-
100 dots (.)
-
200 dots (.)
-
100 dashes (-)
-
100 dots (.) and 100 dashes(-)
Question 48: When you invoke repaint()
for a Component, the AWT package calls which Component method?
-
repaint()
-
update()
-
paint()
-
draw()
-
show()
Question 49: How you can you test whether
an object referenced by ref implements an interface
named MyInterface? Replace your test here with
this test:
if (your test here) {
System.out.println("ref implements MyInterface");
|
Fill in the blank.
Question 50: What does the following line
of code do?
TextField tf = new TextField(30); |
Select the one right answer.
-
This code is illegal; there is no such constructor
for TextField.
-
Creates a TextField object that can hold 30 rows,
but since it is not initialized to anything, it will always be empty.
-
Creates a TextField object that can hold 30 columns,
but since it is not initialized to anything, it will always be empty.
-
This code creates a TextField object that can hold
30 rows of text.
-
Creates a new TextField object that is 30 columns
of text.
Question 51: Given these code snippets:
Boolean b1 = new Boolean(true);
Boolean b2 = new Boolean(true); |
Which expressions are legal Java expressions that
return true?
Select all valid answers.
-
b1 == b2
-
b1.equals(b2)
-
b1 & b2
-
b1 | b2
-
b1 && b2
-
b1 || b2
Question 52: Which LayoutManager arranges
components left to right, then top to bottom, centering each row as it
moves to the next?
Select the one right answer.
-
BorderLayout
-
FlowLayout
-
GridLayout
-
CardLayout
-
GridBagLayout
Question 53: A component can be resized horizontally,
but not vertically, when it is placed in which region of a BorderLayout?
Select the one right answer.
-
North or South
-
East or West
-
Center
-
North, South, or Center
-
any region
Question 54: How can you place three Components
along the bottom of a Container?
Select the one right answer.
-
Set the Container's LayoutManager to be a BorderLayout
and add each Component to the "South" of the Container .
-
Set the Container's LayoutManager to be a FlowLayout
and add each Component to the Container.
-
Set the Container's LayoutManager to be a BorderLayout;
add each Component to a different Container that uses a FlowLayout, and
then add that Container to the "South" of the first Container.
-
Use a GridLayout for the Container and add each Component
to the Container.
-
Do not use a LayoutManager at all and add each Component
to the Container.
Question 55: What will happen when you attempt
to compile and run the following program by passing the Test class to the
Java interpreter?
class Test {
public static void main() {
System.out.println("hello");
}
} |
Select the one right answer.
-
The program does not compile because main()
is not defined correctly.
-
The program compiles but when you try to run the
interpreter complains that it cannot find the main()
method it needs to run.
-
The program compiles but you cannot run it because
the class is not declared as public.
-
The program compiles and runs without an error but
does not display anything in the standard output.
-
The program compiles and displays "hello" in the
standard output when you run it.
Question 56: Which of the following is a valid
way to embed an applet class named Q56 into a Web page?
Select all right answers.
a)
<applet class=Q56.class width=100
height=100>
</applet> |
b)
<applet code=Q56 width=100 height=100>
</applet> |
c)
<applet code=Q56.class width=100
height=100>
</applet> |
d)
<applet param=Q56.class width=100
height=100>
</applet> |
e)
<applet param=Q56 width=100 height=100>
</applet> |
Question 57: How would you make the background
color red for a Panel referenced by the variable p?
Fill in the blank.
Question 58: How can you retrieve a circle’s
radius value that’s passed to an applet?
a)
public void init() {
String s = getParameter("radius");
doSomethingWithRadius(s);
} |
b)
public static void main(String[]
args) {
String s = args[0];
DoSomethingWithRadius(s);
} |
c)
public static void main(String[]
args) {
String s = getParameter("radius");
DoSomethingWithRadius(s);
} |
d)
public void init() {
int radius = getParameter("radius");
doSomethingWithRadius(radius);
} |
e)
public void init() {
int radius = getParameter();
doSomethingWithRadius(radius);
} |
Question 59: What is the result of invoking
main()
for the classes D and E?
class D {
public static void main(String[] args) {
String s1 = new String("hello");
String s2 = new String("hello");
if (s1.equals(s2))
System.out.println("equal");
else
System.out.println("not equal");
}
}
class E {
public static void main(String[] args) {
StringBuffer sb1 = new StringBuffer("hello");
StringBuffer sb2 = new StringBuffer("hello");
if (sb1.equals(sb2))
System.out.println("equal");
else
System.out.println("not equal");
}
} |
Select the one right answer.
-
D: equal; E: equal
-
D: not equal; E: not equal
-
D: equal; E: not equal
-
D: not equal; E: not equal
-
nothing appears in the standard output for either
class
Question 60: What does
the following code do?
drawArc(50, 40, 20, 20, 90, 180); |
Select the one right answer.
-
Draw an arc that is centered at x = 50, y = 40, is
20 pixels wide, as a semicircle from the top of the circle to the bottom
along the left-hand side.
-
Draw an arc that is centered at x = 50, y = 40, is
20 pixels wide, as a semicircle from the 3:00 position to the 9:00 position
along the bottom of the circle.
-
Draw an arc that is centered at x = 50, y = 40, is
20 pixels wide, as a semicircle from the top of the circle to the bottom
along the right-hand side.
-
Draw an arc in a circle whose left side is at 50,
whose top is at 40, is 20 pixels wide, as a semicircle from the top of
the circle to the bottom along the left-hand side.
-
Draw an arc in a circle whose left side is at 50,
whose top is at 40, is 20 pixels wide, as a semicircle from the top of
the circle to the bottom along the right-hand side.
Question 61: What does the following code
do (if anything)?
Select the one right answer.
-
draw a line from x = 0, y = 20 to x = 10, y = 30
-
draw a line from x = 0, y = 10 to the coordinates
x = 20, y = 30
-
draw the outline of a box whose left, top corner
is at 0, 10 and that is 20 pixels wide and 30 pixels high
-
nothing—this code does not compile because it does
not provide the correct number of arguments
-
nothing—this code does not compile because the arguments
make no sense
Question 62: What Graphics methods will draw
the outline of a square?
Select all right answers.
-
drawRect()
-
fillRect()
-
drawPolygon()
-
fillPolygon()
Question 63: What method from Java 1.0.2 can
you use to remove a Component from a user interface display?
Select all right answers.
-
disable()
-
hide()
-
remove()
-
delete()
-
unhook()
Question 64: Returning a value of false
in Java 1.0.2 from an event handler:
Select the one right answer.
-
passes that event is passed up the container hierarchy
-
stops that event from being passed up the container
hierarchy
-
has no effect on whether the event is passed up the
container heirarchy
Question 65: Which statements about garbage
collection are true?
Select all valid answers.
-
You can directly free the memory allocated by an
object.
-
You can directly run the garbage collector whenever
you want to.
-
The garbage collector informs your object when it
is about to be garbage collected.
-
The garbage collector reclaims an object’s memory
as soon as it becomes a candidate for garbage collection.
-
The garbage collector runs in low-memory situations.
Question 66: If you’d like to change the size
of a Component, you can use the Java 1.1-specific method:
Select the one right answer.
-
size()
-
resize()
-
area()
-
setSize()
-
dimension()
Question 67: The setForeground()
and setBackground() methods are defined in
class:
Select the one right answer.
-
Graphics
-
Container
-
Component
-
Object
-
Applet
Question 68: How many bits are used to maintain
a char data type?
Fill in the blank.
Question 69: The &&
operator works with which data types?
Select all valid answers.
-
int
-
long
-
double
-
boolean
-
float
Question 70: To place a 1 in the high-bit
of an int named ref
that’s set to 0x00000001, you can write:
Select the one right answer.
-
ref >> 31;
-
ref >>= 31;
-
ref << 31;
-
ref <<= 31;
-
Shifts the bits in an integer to the left by the
number of bits specified and fills the right-most bit with 1.
-
Shifts the bits in an integer to the left by the
number of bits specified and fills the right-most bit with 0.
Answers
and explanations
Question 1: d. An abstract class is defined
using the keyword abstract in front of the
class keyword and almost always defines at least one abstract
method. An abstract class does not have to define an abstract
method. If there are no abstract methods in
an abstract class, then any subclasses of the
abstract
class can be instantiated (as long as they are not, in turn, defined using
the abstract keyword). (See chapter 1.)
Question 2: "Runnable". Only classes that
implement the Runnable interface (and so are of type Runnable) can be targets
of threads.
Question 3: e. Use the final
keyword in front of the class to make the class unable to be subclassed.
(See chapter 1.)
Question 4: b, d. Only "r" and "rw" are
legal modes for a RandomAccessFile.
Question 5: c. This code is perfectly fine.
(See chapter 1.)
Question 6: a, b, d, and e. The other tries
to cast a boolean to an int,
which is illegal.
Quesiton 7: c. Since the superclass is
abstract
and implements Runnable, but does not supply a run()
method, the subclass must supply run() or also
be declared abstract. (See chapter 1.)
Question 8: "InterruptedException" or "IllegalMonitorException".
Question 9: a, c, d, e. superclass,
open,
close,
and
integer are not Java keywords. goto
is a keyword, though it isn’t used as of Java 1.1. (See chapter 2.)
Question 10: c. An octal number in Java
is preceded by a 0.
Question 11: e. Oh, the tangled web we
weave… There are three constructors that come into play. First, the constructor
that thakes an int is invoked. This invokes the constructor that takes
a String. This invokes the no-args constructor, which displays "good-bye."
Then, the constructor that takes a String displays "hello." (See chapter
3.)
Question 12: "s.substring(2, 5)" or "s.substring(2)"
or "s.substring(2, s.length())";
Question 13: a. A new String is created
based on args[0], but args[0]
does not have to be nulled out before first can be garbage collected. As
soon as the line with the label d is executed,
the object that first has referred to is ready to be garbage collected,
because there is no way to recover a reference to this object again. (See
chapter 4.)
Question 14: d. The range of integer types
goes from minus 2(number of bits - 1) to 2(number of bits
- 1) minus 1. (See chapter 5.)
Question 15: c. Even though there is an
instance variable named index defined in the
Car class, the local or automatic variable named index
takes precedence. Since automatic variables do not have a default value,
this code will not compile because it is uninitialized when we attempt
to access the element in the args array. (See
chapter 5.)
Question 16: e. There’s nothing wrong with
this code. 100 0's will appear in the standard output. (See chapter 5.)
Question 17: a, b, c. The elements in arrays
are initialized to their default values: 0, 0.0, null, false, or \u0000,
depending on the data type.
Question 18: "getParameter"
Question 19: c. Assigning a subclass type
to a superclass type is perfectly legal and will run fine at runtime.
Question 20: a. You cannot assign an object
to a sibling object reference, even with casting.
Question 21: "c = ‘\u30A0’;" You can set
a char to a Unicode sequence by matching the
template \udddd, where dddd
are four hexadecimal digits representing the Unicode character you want.
(See chapter 5.)
Question 22: b. Only +
and += are overloaded for String objects.
Question 23: b. Changing the break
statement to break middle will break out of
the loop named using the label middle and continue
with the next iteration of the outer loop. The statement continue
outer would also have this effect. (See chapter 7.)
Question 24: c. NumberFormatException will
be handled in the catch clause for Exception.
Then, regardless of the return statements,
the finally clause will be executed before
control returns to the calling method. (See chapter 8.)
Question 25: a. An explicit cast is needed
to assign a superclass type to a subclass type.
Question 26: b. If the object contained
in a is not actually a Subclass1 object, the assignment will cause Java
to throw a CastClassException. That would be the case in the code in this
example.
Question 27: "IOException" or "java.io.IOException"
Question 28: c. A method in a subclass
cannot add new exception types that it might throw. Since it’s superclass,
Second, does not define any exceptions in its test()
method, Third can’t either. (See chapter 8.)
Question 29: c. The method with all double
parameters is actually the only version of test()
that the Java Virtual Machine can legally coerce the numbers to. The reason
the other versions of test() are not invoked
is that at least one of the parameters would have to be automatically coerced
from a type with greater accuracy to a type with less accuracy, which is
illegal. (See chapter 9.)
Question 30: c. The Math method floor()
finds the integer closest to but less than the parameter to floor().
The methods round() and ceil()
would both result in 91, and min() and max()
both require two parameters. (See chapter 10.)
Question 31: "disable"
Question 32: a. Both ceil()
and round() will produce 15 from 14.9. The
floor()
method yields 14. (See chapter 10.)
Question 33: a, b, c, d. The methods Java
defines for trig operations include sin(),
asin(),
cos(),
and tan(). (See chapter 10.)
Question 34: d. The method equalsIgnoreCase()
would return true for the two Strings a and
b
in the question. (See chapter 10.)
Question 35: d. The method substring()
starts at the first index, inclusive, with 0 being the first character),
and ends at the end index - 1 (that is, exclusive of the end index). (See
chapter 10.)
Question 36: d. This is the default access
control for methods and member variables.
Question 37: b. The
concat() method appends the characters passed to it to the characters
in the String responding to the method call. The concat()
method creates a new String, since Strings cannot be changed once they
are created. (See chapter 10.)
Question 38: a. The first line creates
a File object that represents the file. By creating a FileOutputStream,
you create the file if it does not yet exist, and open that file for reading
and writing. (See chapter 11.)
Question 39: d, c, e. You cannot assign
an integer to a boolean—not even with casting.
Also, the default type for a floating-point literal is double,
and you cannot assign a double to a float
without casting.
Question 40: c, e. thisis
a reserved word, so it cannot be used as an identifier (such as a label).
2to1odds
starts with a number, so it is also invalid as an identifier.
Question 41: a. In order to write a primitive
data type such as an int, you need to use a
FilterInputStream subclass such as DataInputStream. This class defines
writeInt(),
which is perfect for our needs. (See chapter 11.)
Question 42: "abstract"
Question 43: c. Only the "c" from finally
gets written out. The exception thrown doesn’t match the exception caught,
so the catch block is not executed. Control
returns to the caller after finally to see
if there is a catch block there to handle this
unchecked exception. If there is not (as is the case here), execution comes
to an end.
Question 44: e. This code compiles and
runs fine. RandomAccessFile implements the DataOutput interface, so it
does implement writeBytes(), among others.
RandomAccessFile creates the file if it does not yet exist. (See chapter
11.)
Question 45: c. The absolute path includes
the drive name and the top-level directories, as well as the file name
itself. (See chapter 11.)
Question 46: d. The target object for a
Thread must implement Runnable, which means it will pass the test:
targetObject instanceof Runnable
(See chapter 12.)
Question 47: a. The thread's with start()
method is never invoked. (This is not an applet, so init()
is
not automatically invoked.) (See chapter 12.)
Question 48: b. The AWT invokes update()
for the Component, which in invokes paint()
in its default behavior. (See chapter 13.)
Question 49: "ref instanceof MyInterface"
Question 50: e. TextField defines a constructor
that takes the number of columns, as shown in the example. TextField objects
can have their text updated at any time, including long after they're created.
(See chapter 13.)
Question 51: b. The first yields false,
and the others are not legal Java expressions (this is a wrapper type we’re
using here…)
Question 52: b. A FlowLayout arranges components
in this way. (See chapter 13.)
Question 53: a. North and South only can
resize a component horizontally, to the width of the Container. (See chapter
13.)
Question 54: c. Complicated as it might
seem, this is the best way to accomplish this goal. First, you set the
Container's LayoutManager to be a BorderLayout. Then, you create an intermediate
Container and add each Component to this new Container that uses a FlowLayout.
Finally, you add that Container to the "South" of the original Container.
(See chapter 13.)
Question 55: b. The program will compile
fine. However, the Java interpreter specifically looks for a main()
method declared as public and static,
that returns no value, and that takes an array of String objects as its
parameter.
Question 56:
c. The <applet> tag requires three keywords:
code, width, and
height.
Question 57: "p.setBackground(Color.red);"
Question 58: a. Use the getParameter()
method, passing it the name of the value you want to retrieve. This method
retrieves a String representing that value. (You can convert the String
to a primitive data type using a wrapper class if you need to.)
Question 59: c. The StringBuffer class
does not override equals(). Hence, this class
returns false when passed two different objects.
(See chapter 6.)
Question 60: d. The four parameters are
the left, top, width, height, start angle (0 is the 3:00 position), and
the arc angle (the arc ends at start angle plus the arc angle), drawn counter-clockwise.
Question 61: b. The drawLine()
method takes four parameters: the starting point and the ending point of
the line to draw.
Question 62: a, c. You can use drawRect()
to draw a rectangle outline given its upper left point and width and height,
and drawPolygon() to draw each of the four
points of the square, plus an end point that is the same as the first point.
Question 63: b. The hide()
method is more-or-less the opposite of show() and
removes a Component from the display.
Question 64: a. Returning false
indicates that method did not handle the event, which means AWT passes
the event up the container hierarchy looking for someone who does want
to handle it.
Question 65: b, c, e. You cannot directly
free the memory allocated by an object, though you can set an object reference
to null. Also, The garbage collector only runs in low-memory situations,
and so does not always reclaim an object’s memory as soon as it becomes
a candidate for garbage collection.
Question 66: d. setSize()
is specific to Java 1.1.
Question 67: c. These are Component methods.
(The setColor() method is defined in the Graphics
class.)
Question 68: "16"
Question 69: d. The &&
operator combines two boolean expressions.
Question 70: d. The <<
operator shifts the bits the given number of places to the left.
|