OCJP Encyclopedia Tutorials
Consider
the following application:
1.
class Q6 {
2.
public static void main(String args[]){
3.
Holder h = new Holder();
4.
h.held = 100;
5.
h.bump(h);
6.
System.out.println(h.held);
7.
}
8.
}
9.
10.
class Holder {
11.
public int held;
12.
public void bump(Holder theHolder){theHolder.held++;}
13.
}
What
value is printed out at line 6?
A.
0.
B.
1.
C.
100.
D.
101.
Answer: D
is correct. A holder is constructed on line 6. A reference to that
holder is passed into method bump() on line 5. Within the method
call, the holder's held variable is bumped from 100 to 101.
Question:
Consider the following application:
1.
class Q7 {
2.
public static void main(String args[]){
3.
double d = 12.3;
4.
Decrementer dec = new Decrementer();
5.
dec.decrement(d);
6.
System.out.println(d);
7.
}
8.
}
9.
10.
class Decrementer {
11.
public void decrement(double decMe){decMe = decMe - 1.0;}
12.
}
What
value is printed out at line 6?
A.
0.0
B.
-1.0
C.
12.3
D.
11.3
Answer:C
is correct. The decrement() method is passed a copy of the argument
d; the copy gets decremented, but the original is untouched.
Question:
What does the following code fragment print out at line 10?
1.
try{
2.
FileOutputStream fos = new FileOutputStream("xx");
3.
for (byte b=10; b<50; b++)
4.
fos.write(b);
5.
fos.close();
6.
RandomAccessFile raf = new RandomAccessFile("xx","r");
7.
raf.seek(10);
8.
int i = raf.read();
9.
raf.close();
10.
System.out.println("i=" + i);
8.
}
9.
catch (IOException e) {}
A.
The output is i = 30.
B.
The output is i = 20.
C.
The output is i = 10.
D.
There is no output because the code throws an exception at line 1.
E.
There is no output because the code throws an exception at line 6.
Answer:B
is correct. All the code is perfectly legal, so no exceptions are
thrown. The first byte in the file is 10, the next byte is 11, the
next is 12 and so on. The byte at file position 10 is 20, so the
output is i = 20.
Question:
Which of the following signatures are valid for the main() method
entry point of an application?
A.
public static void main()
B.
public static void main(String arg[])
C.
public void main(String [] arg)
D.
public static void main(String[] args)
E.
public static int main(String [] arg)
Answer:B
and D are both acceptable
Question:
What is the range of values that can be assigned to a variable of
type short?
A.
It depends on the underlying hardware.
B.
0 through 216-1
C.
0 through 232-1
D.
-21515 through 215-1
E.
-231 through 231-1.
Answer:D
is correct. The range for a 16-bit short is -215 through 215-1. This
range is part of the Java specification, regardless of the underlying
hardware.
Question:
A file is created with the following Code:
1.
FileOutputStream fos = new FileOutputStream("datafile");
2.
DataOutputstream dos = new DataOutputstream(fos);
3. for(int i=0;
i<500; i++)
4.
dos.writeInt(i);
You
would like to write code to read back the data from this file. Which
solutions listed below will work? (Choose none, some, or all).
A.
Construct a FileInputStream, passing the name of the file. Onto the
FileInputStream, chain a DataInputStream, and call its readInt()
method.
B.
Construct a FileReader, passing the name of the file. Call the file
reader's readInt() method.
C.
Construct a PipedInputStream, passing the name of the file. Call the
piped input stream's readInt() method.
D.
Construct a RandomAccessFile, passing the name of the file. Call the
random access file's readInt() method.
E.
Construct a FileReader, passing the name of the file. Onto the
FileReader, chain a DataInputStream, and call its readInt() method.
Answer: A
and D are correct. Solution A chains a data input stream onto a file
input stream. Solution D simply uses the RandomAccessFile class. B
fails because the FileReader class has no readInt() method; readers
and writers only handle text. Solution C fails because the
PipedInputStream class has nothing to do with file I/O. (Piped inout
and output streams are used in inter-thread communication). Solution
E fails because you cannot chain a data input stream onto a file
reader. Readers read chars, and input streams handle bytes.
No comments:
Post a Comment