This is a closed notes, closed book exam.
THERE ARE PROBLEMS ON BOTH SIDES OF THE PAPER
binary is more compact
readInt() from the class
DataInputStream can be used to read integers from the keyboard by
constructing a DataInputStream from System.in. Circle
the correct answer.
False. readInt() from DataInputStream reads binary data not text. It will read for bytes and then interpret those 4 bytes as a single int value.
java.io includes four abstract classes,
InputStream, OutputStream,
Reader, and Writer, that are all derived directly from
java.lang.Object. Which two are the superclasses of the I/O classes
designed for text (character) based I/O? Circle the correct answer.
java.io.Reader is for reading text. java.io.Writer is for writing text.
hasMoreElements used by tio.ReadInput.
Describe the other two mechanisms for detecting EOF and explain when
each is appropriate.
One method is to return a special value. This only works when there is some distinguished value. For example, null can be returned from readLine() because null doesn't correspond to any legal string.
Another method is to use exceptions. This is appropriate when there is no reserved value that can be used as with the previous approach. For example, when reading an int, all return values are legal, so EOF is indicated by throwing a java.io.EOFException.
writeInt() from DataOutputStream, how many
bytes will be written for the value 123456789 (put answer here)?
How many bytes would be written if the same value was written using
println() from PrintWriter (put answer here)?
Using DataOutputStream all ints use exactly 4 bytes, regardless of the value.
Using a PrintWriter, there will be either 1 or 2 bytes for each character. It depends whether the system supports Unicode or just ASCII. Either 9 or 18 will be accepted as correct answers.
throws clause, and those that do not require a
throws clause? It is not necessary to name specific classes in
your answer. A general description of the approach is fine.
Any exception that is derived (directly or indirectly) from RuntimeException or Error, does not need to be declared using a throws. All others require a throws. So Java distinguishes those that require a throws by looking at the super classes of the exception.
class Quiz5
{
public static void main(String[] args)
{
int x;
try {
x = foo(10);
}
catch (MyException e)
{
System.out.println("Caught an exception: " + e);
x = 99;
}
System.out.println(x);
}
static int foo(int x)
{
System.out.println("foo started with " + x);
int temp = bar(x);
System.out.println("foo returning " + temp);
return temp;
}
static int bar(int y)
{
System.out.println("bar started with " + y);
if(y > 0)
throw new MyException("just a test");
System.out.println("when is this executed?");
return y;
}
}
class MyException extends RuntimeException
{
public MyException(String message)
{
super(message);
}
}
OUTPUT:
foo started with 10
bar started with 10
Caught an exception: MyException: just a test
99
The exact wording of the third line is not important, so long as
it includes ``just a test''.