Lewis/Loftus Java Software Solutions, 4e
TestBank- Chapter 10
Part A
Given the nested if-else structure below, answer questions 1-2.
if (a > 0)
if (b < 0)
x = x + 5;
else
if (a > 5)
x = x + 4;
else
x = x + 3;
else
x = x + 2;
1) If x is currently 0, a = 5 and b = 5, what will x become after the above statement is executed?
a) 0
b) 2
c) 3
d) 4
e) 5
2) If x is currently 0, a = 0 and b = -5, what will x become after the above statement is executed?
a) 0
b) 2
c) 3
d) 4
e) 5
3) The break statement does which of the following?
a) ends the program
b) transfers control out of the current control structure such as a loop or switch statement
c) ends the current line of output, returning the cursor
d) denotes the ending of a switch statement
e) indicates the end of line when using System.out.print
4) If x is an int where x = 0, what will x be after the following loop terminates?
while (x < 100)
x *= 2;
a) 2
a) 64
b) 100
c) 128
d) None of the above, this is an infinite loop
5) How many times will the following loop iterate?
int x = 10;
while (x > 0)
{
System.out.println(x);
x--;
}
a) 0 times
b) 1 time
c) 9 times
d) 10 times
e) 11 times
6) Given that s is a String, what does the following loop do?
for (int j = s.length( ); j > 0; j--)
System.out.print(s.charAt(j-1));
a) it prints s out backwards
b) it prints s out forwards
c) it prints s out backwards after skipping the last character
d) it prints s out backwards but does not print the 0th character
e) it yields a run-time error because there is no character at s.charAt(j-1) for j = 0
7) The following nested loop structure will execute the inner most statement (x++) how many times?
for (int j = 0; j < 100; j++)
for (int k = 100; k > 0; k--)
x++;
a) 100
b) 200
c) 10,000
d) 20,000
e) 1,000,000
Part B
1) A Java program can handle an exception in several different ways. Which of the following is not a way that a Java program could handle an exception?
a) ignore the exception
b) handle the exception where it arose using try and catch statements
c) propagate the exception to another method where it can be handled
d) throw the exception to a pre-defined Exception class to be handled
e) all of the above are ways that a Java program could handle an exception
Answer: d. Explanation: A thrown exception is either caught by the current code if the code is contained inside a try statement and the appropriate catch statement is implemented, or else it is propagated to the method that invoked the method that caused the exception and caught there in an appropriate catch statement, or else it continues to be propagated through the methods in the opposite order that those methods were invoked. This process stops however once the main method is reached. If not caught there, the exception causes termination of the program (this would be answer a, the exception was ignored). However, an exception is not thrown to an Exception class.
Note: this is tricky. Answers a,b,c can be found in your text on page 532. The example on pp.543-544 should help you understand that the exception is not being thrown to the pre-defined class Exception. The exception is actually being handled right there in the decision statement and conclusion "throw problem".
2) An exception can produce a “call stack trace” which lists
a) the active methods in the order that they were invoked
b) the active methods in the opposite order that they were invoked
c) the values of all instance data of the object where the exception was raised
d) the values of all instance data of the object where the exception was raised and all local variables and parameters of the method where the exception was raised
e) the name of the exception thrown
Answer: b. Explanation: The call stack trace provides the names of the methods as stored on the run-time stack. The method names are removed from the stack in the opposite order that they were placed, that is, the earliest method was placed there first, the next method second, and so forth so that the most recently invoked method is the last item on the stack, so it is the first one removed. The stack trace then displays all active methods in the opposite order that they were called (most recent first).
Note: see top p. 534
3) A finally clause will execute
a) only if the try statement that precedes it does not throw an exception
b) only if the try statement that precedes it throws an exception that is caught
c) only if the try statement that precedes it throws an exception that is not caught
d) only if the try statement that precedes it throws an exception, whether it is caught or not
e) in any circumstance
4) Which of the following messages passed to the String str could throw a StringIndexOutOfBoundsException?
a) str.length( )
b) str.charAt(2);
c) str.replace(‘a’, ‘A’);
d) str.equals(str);
e) any of the above could throw a StringIndexOutOfBoundsException
5) Character streams manage
a) byte-sized data
b) binary data
c) Unicode characters
d) ASCII characters
e) compressed data
Answer: c. Explanation: Character streams are used to manage 16-bit Unicode characters. This differs from a byte stream that is used to manage any kind of byte-sized data, including ASCII characters and binary data of other types.
6) System.err is a(n)
a) input stream
b) GUI dialog box that indicates when an error has arisen
c) object
d) Error subclass
e) RuntimeException subclass
Answer: c. Explanation: There are three default streams available in Java, System.in, System.out, and System.err. All of these are objects with System.in being an input stream, System.out being an output stream and System.err being an error stream (which is also an output stream).
7) The Scanner class provides an abstraction for input operations by
a) using try and catch statements to catch any IOException instead of throwing the Exception elsewhere
b) parsing input lines into individual tokens
c) performing conversion operations from String to the appropriate type as specified in the Scanner message
d) inputting from the standard input stream if create is called using System.in
e) all of the above
Answer: e. Explanation: Input into a Java program is difficult because it requires a lot of overhead. The Scanner class implements all of that overhead so that the programmer does not have to see it. Thus, Scanner is an abstraction for performing input operations without the details. These details include importing java.io classes, handling IOExceptions in some way, inputting from the standard input stream, dividing the input into individual tokens and converting each token as needed into the requesting form.
8) A Timer object generates ______ at regular intervals.
a) ActionEvents
b) MouseEvents
c) RuntimeExceptions
d) non-throwable Exception
e) KeyEvents
9) The Timer object should be used to support which of the following types of applications?
a) animation
b) GUI input from the mouse
c) any applet application
d) input from and output to text files
e) any application that waits for user input via the keyboard
10) The difference between a checked and an unchecked exception is:
a) checked exceptions need not be listed in a throws clause
b) unchecked exceptions must be listed in a throws clause
c) neither kind of exception follows the rules of exception propagation
d) an unchecked exception requires no throws clause
e) a checked exception always must be caught by a try block; an unchecked exception does not
Answer: d. Explanation: A checked exception must either be caught or it must be listed in a throws clause. An unchecked exception requires no throws clause. Both kinds of exceptions follow the rules of exception propagation.
public int question1_2(int x, int y)
{
if (x == y) return 0;
else return question1_2(x-1, y) + 1;
}
11) If the method is called as question1_2(8, 3), what is returned?
a) 11
b) 8
c) 5
d) 3
e) 24
12) Calling this method will result in infinite recursion if which condition below is initially true?
a) (x = = y)
b) (x != y)
c) (x > y)
d) (x < y)
e) (x = = 0 && y != 0)
13) The following method should:
return true if the int parameter is even and either positive or 0, and false otherwise.
Which set of code should you use to replace … so that the method works appropriately?
public boolean question3(int x) { … }
a) if (x = = 0) return true;else if (x < 0) return false;else return question3(x – 1);
b) if (x = = 0) return false;else if (x < 0) return true;else return question3(x – 1);
c) if (x = = 0) return true;else if (x < 0) return false;else return question3(x – 2);
d) if (x = = 0) return false;else if (x < 0) return true;else return question3(x – 2);
e) return(x = = 0);
14) What does the following method compute? Assume the method is called initially with i = 0
public int question9(String a, char b, int i)
{
if (i = = a.length( )) return 0;
else if (b = = a.charAt(i)) return question9(a, b, i+1) + 1;
else return question9(a, b, i+1);
}
a) The length of String a
b) The length of String a concatenated with char b
c) The number of times char b appears in String a
d) Returns 1 if char b appears in String a at least once, and 0 otherwise
e) The char which appears at location i in String a
Assume that int[ ] a = {6, 2, 4, 6, 2, 1, 6, 2, 5} and consider the two recursive methods below, foo and bar.
public int foo(int[ ] a, int b, int j)
{
if (j < a.length)
if (a[j] != b) return foo (a, b, j+1);
else return foo (a, b, j+1) + 1;
else return 0;
}
public int bar(int[ ] a, int j)
{
if (j < a.length)
return a[ j ] + bar(a, j+1);
else return 0;
}
15) What is the result of calling foo(a, 2, 0);?
a) 0
b) 1
c) 2
d) 3
e) 4
16) What is the result of calling bar(a, 0);?
a) 0
b) 5
c) 6
d) 12
e) 34
17) What is the result of bar(a, 8);?
a) 0
b0 5
c) 6
d) 12
e) 34
18) What does the following recursive method determine?
public boolean question16(int[ ]a, int[ ] b, int j)
{
if (j = = a.length) return false;
else if (j = = b.length) return true;
else return question16(a, b, j+1);
}
a) Returns true if a and b are equal in size, false otherwise
b) Returns true if a is larger than b, false otherwise
c) Returns true if b is larger than a, false otherwise
d) Returns true if a and b have no elements
e) Returns the length of array a + length of array b
1
Error! Bookmark not defined. © 2005