Select All Right Answers. A. Class Animal (: Abstract Void Growl ) Abstract Animal (Abstract Void Growl )
Select All Right Answers. A. Class Animal (: Abstract Void Growl ) Abstract Animal (Abstract Void Growl )
a. class Animal { b)
c) d)
Question 2: For an object to be a target for a Thread, that object must be of type:
_________________
Question 3: What is the proper way of defining a class named Key so that it cannot be
subclassed?
a) class Key { }
d) class Key {
final;
}
Question 4: What modes are legal for creating a new RandomAccessFile object?
a. "w"
b. "r"
c. "x"
d. "rw"
e. "xrw"
class Tester {
public static void main(String[] args) {
CellPhone cell = new CellPhone();
cell.emergency();
}
}
class Phone {
void emegency() {
dial911();
}
}
What will happen when you try to compile and run the Tester class?
a. The code will not compile because Phone is not also declared as final.
b. The code will not compile because you cannot invoke a final method from a subclass.
c. The code will compile and run fine.
d. The code will compile but will throw a NoSuchMethodException when Tester is run.
e. 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.
System.out.println("Undo");
}
}
a. This code will not compile because the parameter i is not used in undo().
b. This code will not compile because there is no main() method.
c. This code will not compile because Deposit must be an abstract class.
d. This code will not compile because Deposit is not declared public.
e. Everything will compile fine.
a. superclass e. integer
b. goto f. goto, import
c. open g. goto, superclass, open, close
d. close h. they are all valid keywords
a. 0x12 d. (octal)2
b. 32O e. 1
c. 032
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");
}
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)
d: first = null;
f: second = null;
g: System.out.println(riddle);
h: args[0] = null;
i: args[1] = null;
j:
}
}
a. d: d. i:
b. e: e. j:
c. h:
Question 14: What are the range of values for a variable of type byte?
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() {
int index;
if (args.length > 0)
if (args[index].equals("Hiway"))
milesPerGallon*= 2; System.out.println("mpg: " +
milesPerGallon);
}
}
a. 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".
b. 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.
c. The code does not compile because the automatic variable named index has not been
initialized.
d. The code does not compile because milesPerGallon has not been initialized.
e. 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;
System.out.println(d[index]); }
}
a. The code will not compile because the int[] array is not declared correctly.
b. The code will compile but will throw an IndexArrayOutOfBoundsException when it
runs and nothing will appear in the standard output.
c. The code will display the numbers 0 through 99 in the standard output, and then
throw an IndexOutOfBoundsException.
d. The code will compile but the println() method will throw a NoSuchMethodException.
e. 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];
System.out.println(i); }
}
a. 0 c. 2
b. 1 d. 3
e. 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.)
_____________________________________
class Superclass { }
a = b;
class Superclass { }
b = (Subclass1)c;
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?
class Superclass { }
b = (Subclass1)c;
a. - c. >>
b. += d. &
e. 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?
a. break inner;
b. break middle;
c. break outer;
d. continue;
e. continue middle;
try {
tryThis();
return;
return;
return;
} finally {
System.out.println("finally");
}
a. Nothing
b. "exception 1", followed by "finally"
c. "exception 2", followed by "finally"
d. "exception 1"
e. "exception 2"
class Superclass { }
b = a;
class Superclass { }
b = (Subclass1)a;
Question 27: To invoke read() from an InputStream subclass, you must handle what type of
exception? ________________
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?
a. Exception1
b. Exception2
c. no checked exceptions
d. any exceptions declared in the throws clause of the Third’s test() method.
class Test {
public static void main(String[] args) {
Test t = new Test();
System.out.println("1");
}
System.out.println("2");
}
System.out.println("3");
}
System.out.println("4");
}
System.out.println("5");
}
}
a. 1 d. 4
b. 2 e. 5
c. 3
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?
a. abs() d. round()
b. min() e. ceil()
c. floor()
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.)
___________________________________
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?
Question 33: What methods does Java define in the Math class specifically for trigonometric
calculations?
a. cos() d. sin()
b. asin() e. angle()
c. tan()
Question 34: What String instance method would return true when invoked like this:
a.method(b);
a. equals() c. toUpperCase()
b. toLowerCase() d. equalsIgnoreCase()
e. none of the above
Question 35: At the end of these two lines of code:
String s = "hypertext";
a. "yper" d. "per"
b. "ype" e. "perte"
c. "pert"
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?
a. public
b. protected
c. private
d. do not supply an access control keyword
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.
a. Create a new file named "hello.test" if it does not yet exist. The file is not opened.
b. 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.
c. Open a file named "hello.test" so that you can write to it but cannot read from it.
d. 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.
a. here: d. that:
b. _there: e. 2to1odds:
c. this:
import java.io.*;
class Write {
}
}
How can you replace the comment at the end of main() with code that will write the integers
0 through 9?
a)
filter.writeInt(i);
b)
for (int i = 0; i < 10; i++)
file.writeInt(i);
c)
d)
filter.write(i);
e)
Question 42: What keyword, when used in front of a method, must also appear in front of
the class?
___________________________
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) {
try {
wrench();
System.out.println("a");
} catch (ArithmeticException e) {
System.out.println("b");
} finally {
System.out.println("c");
}
System.out.println("d");
a. "a" d. "d"
b. "b" e. none of these
c. "c"
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 {
out.writeBytes("Ninotchka");
}
}
a. The code does not compile because RandomAccessFile is not created correctly.
b. The code does not compile because RandomAccessFile does not implement the
writeBytes() method.
c. The code compiles and runs but throws an IOException because "Ran.test" does not
yet exist.
d. The code compiles and runs but nothing appears in the file "Ran.test" that it creates.
e. 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 {
System.out.println(file.getAbsolutePath());
}
}
a. Ran.test d. c:\source
b. source\Ran.test e. null
c. c:\source\Ran.test
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?
Question 47: What appears in the standard output when you run the Dots class?
t.start();
t = new DashThread().start();
}
}
Question 48: When you invoke repaint() for a Component, the AWT package calls which
Component method?
a. repaint() d. draw()
b. update() e. show()
c. paint()
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:
a. b1 == b2 d. b1 | b2
b. b1.equals(b2) e. b1 && b2
c. b1 & b2 f. b1 || b2
Question 52: Which LayoutManager arranges components left to right, then top to bottom,
centering each row as it moves to the next?
a. BorderLayout d. CardLayout
b. FlowLayout e. GridBagLayout
c. GridLayout
Question 53: A component can be resized horizontally, but not vertically, when it is placed in
which region of a BorderLayout?
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");
}
}
a. The program does not compile because main() is not defined correctly.
b. The program compiles but when you try to run the interpreter complains that it cannot
find the main() method it needs to run.
c. The program compiles but you cannot run it because the class is not declared as
public.
d. The program compiles and runs without an error but does not display anything in the
standard output.
e. 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?
a)
</applet>
b)
</applet>
c)
</applet>
d)
e)
</applet>
Question 57: How would you make the background color red for a Panel referenced by the
variable p?
Question 58: How can you retrieve a circle’s radius value that’s passed to an applet?
a)
doSomethingWithRadius(s);
b)
DoSomethingWithRadius(s);
c)
DoSomethingWithRadius(s);
d)
doSomethingWithRadius(radius);
e)
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");
if (s1.equals(s2))
System.out.println("equal");
else
System.out.println("not equal");
}
}
class E {
if (sb1.equals(sb2))
System.out.println("equal");
else
System.out.println("not equal");
}
}
a. D: equal; E: equal
b. D: not equal; E: not equal
c. D: equal; E: not equal
d. D: not equal; E: not equal
e. nothing appears in the standard output for either class
a. 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.
b. 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.
c. 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.
d. 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.
e. 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 62: What Graphics methods will draw the outline of a square?
a. drawRect() c. drawPolygon()
b. fillRect() d. fillPolygon()
Question 63: What method from Java 1.0.2 can you use to remove a Component from a user
interface display?
Question 64: Returning a value of false in Java 1.0.2 from an event handler:
Question 66: If you’d like to change the size of a Component, you can use the Java 1.1-
specific method:
Question 67: The setForeground() and setBackground() methods are defined in class:
Question 68: How many bits are used to maintain a char data type? ___________
Question 69: The && operator works with which data types?
Question 70: To place a 1 in the high-bit of an int named ref that’s set to 0x00000001, you
can write:
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 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 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 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 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 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 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 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 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:
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 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 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 67: c. These are Component methods. (The setColor() method is defined in the
Graphics class.)
Question 70: d. The << operator shifts the bits the given number of places to the left.