Name:     ID: 
 
Email: 

Java CH4

True/False
Indicate whether the statement is true or false.
 

 1. 

The statement a += 3; is equivalent to the statement a = a + 3;.
 

 2. 

A program that uses the Random class first must import java.util.Random.
 

 3. 

Braces always occur in pairs and no semicolon immediately follows a closing brace.
 

 4. 

You cannot write an if or if-else statement without braces.
 

 5. 

In general, it is better to under-use braces than to overuse them.
 

 6. 

If integer variables a, b, and c all have a value of 5, the statement a == b == c; would yield a value of true.
 

 7. 

if-else statements are commonly used to check user inputs before processing them.
 

 8. 

The statements in a while loop will always execute at least once.
 

 9. 

In a for loop, the counter is updated at the beginning (top) of the loop before any statements are executed.
 

 10. 

The for loop allows the programmer to declare the loop control variable inside of the loop header.
 

 11. 

A for loop can never be used in place of a while loop.
 

 12. 

Control statements can be nested inside each other in any combination that proves useful.
 

 13. 

A while loop terminates immediately when a break statement is encountered, but a for loop does not.
 

 14. 

A sentinel-controlled loop can be used to read all of the data from a file.
 

 15. 

Failure to close an output file may result in no data being saved to it.
 

Multiple Choice
Identify the choice that best completes the statement or answers the question.
 

 16. 

Java’s increment operator is represented by the symbol ____.
a.
+=
c.
+
b.
++
d.
=+
 

 17. 

The ____ class contains a number of methods that can be used to perform common mathematical operations, such as calculate a square root.
a.
Mathematical
c.
Random
b.
Arithmetic
d.
Math
 

 18. 

The condition in an if statement must be a(n) ____ expression.
a.
Integer
c.
Arithmetic
b.
String
d.
Boolean
 

 19. 

There are ____ relational operators.
a.
4
c.
6
b.
5
d.
7
 

 20. 

The equal-to (comparison) operator is represented by the symbol ____.
a.
=
c.
!=
b.
==
d.
===
 

 21. 

The process of executing all of the contained statements within a loop is called a(n) ____ of the loop.
a.
iteration
c.
execution
b.
implementation
d.
processing
 

 22. 

Which of the following code snippets would display the square roots of 25, 20, 15, and 10?
a.
int number = 25;
while (number > 10){
     System.out.println ("The square root of " +
          number + " is " + Math.sqrt (number));
     number -= 5;
}
b.
int number = 25;
while (number <= 10){
     System.out.println ("The square root of " +
          number + " is " + Math.sqrt (number));
     number--;
}
c.
int number = 25;
while (number >= 10){
     System.out.println ("The square root of " +
          number + " is " + Math.sqrt (number));
     number -= 5;
}
d.
int number = 25;
while (number >= 10){
     System.out.println ("The square root of " +
          number + " is " + Math.sqrt (number));
     number--;
}
 

 23. 

Which of the following represents the common structure for all while loops?
a.
initialize variables
change variables involved in the condition
while (condition){
     perform calculations
}
b.
initialize variables
while (condition){
     perform calculations
     change variables involved in the condition
}
c.
while (condition){
     initialize variables
     perform calculations
     change variables involved in the condition
}
d.
initialize variables
while (condition){
     perform calculations
}
change variables involved in the condition
 

 24. 

Which of the following represents the proper structure of a for loop?
a.
for (initialize counter; update counter) {
    statement;
    statement;
     . . .;
}
b.
for (initialize counter; test counter; update counter) {
    statement;
    statement;
     . . .;
}
c.
for (update counter; initialize counter; test counter ) {
    statement;
    statement;
     . . .;
}
d.
for (test counter; initialize counter; update counter) {
    statement;
    statement;
     . . .;
}
 

 25. 

Consider the following code fragment:

System.out.print("Enter a positive integer: ");
int n = reader.nextInt();

int limit = n / 2;

for (int d = 2; d <= limit; d++){
    if (n % d == 0)
        System.out.print (d + " ");
}

If the user enters the number 12 when prompted, the output displayed to the console will be ____.
a.
2
c.
2 3 4 6
b.
2 3 4 5 6
d.
no output
 

 26. 

Which of the following is an advantage of taking input data from a text file rather than a human user typing on the keyboard?
a.
The data set can be much larger.
b.
The data can be input much more quickly and with less chance of error.
c.
The data can be used repeatedly with the same program or with different programs.
d.
All of the above are correct.
 

 27. 

File objects are instances of the class ____.
a.
File
c.
Scanner
b.
IO
d.
Stream
 

 28. 

I/O exceptions are generally so serious that they belong to a category called ____ exceptions, meaning that a program must at least acknowledge they might be thrown, if not do something to recover from them.
a.
unchecked
c.
fatal
b.
acknowledged
d.
checked
 

 29. 

Data can be output to a text file using the class ____.
a.
Writer
c.
Scanner
b.
PrintWriter
d.
FileWriter
 

 30. 

The class JOptionPane in the package javax.swing includes several versions of the method ____ for input dialog boxes.
a.
showMessageDialog
c.
showInputDialog
b.
inputDialog
d.
showDialog
 

Completion
Complete each statement.
 

 31. 

The assignment operator can be combined with the arithmetic and concatenation operators to provide ____________________ assignment operators.
 

 

 32. 

Using the same name for two different methods is called ____________________.
 

 

 33. 

Java’s random number generator is implemented in the ____________________ class.
 

 

 34. 

The while and if-else statements are known as ____________________ statements.
 

 

 35. 

A(n) ____________________ expression returns a value of true or false.
 

 

 36. 

A(n) ____________________ can be used to graphically depict the behavior of an if-else construct or a loop construct.
 

 

 37. 

The ____________________ statement(s) provides a looping mechanism that executes statements repeatedly for as long as some condition remains true.
 

 

 38. 

In a(n) ____________________ loop, a loop control variable is used to keep track of the number of times that the loop has executed.
 

 

 39. 

____________________ loops are structured so that they continue to execute until some task is accomplished.
 

 

 40. 

The ____________________ statement combines counter initialization, condition test, and update into a single expression.
 

 

 41. 

Both for loops and while loops are ____________________ loops, meaning that a continuation condition is tested at the top of the loop on each pass before the loop’s body of code is executed.
 

 

 42. 

To get out of a loop prematurely (before the loop condition is false), you can use a(n) ____________________ statement.
 

 

 43. 

When implementing ____________________ input, you read input continuously until a specific value is read in.
 

 

 44. 

Any text file, including the special case of an empty file, must contain at least one special character that marks the ____________________.
 

 

 45. 

When a Java program encounters an operation that it cannot perform at run-time, the JVM throws a(n) ____________________.
 

 

 46. 

Suppose you have declared, instantiated, and used an instance of the PrintWriter class named writer to write data to a file. When you are done using it to write to a file, you must close the file with the statement ____________________.
 

 

 47. 

____________________ statements are usually executed at the bottom of the loop and change the values of the variables tested in the terminating condition.
 

 

 48. 

A(n) ____________________ error occurs whenever a loop goes around one too many or one too few times.
 

 

 49. 

A(n) ____________________ loop is a loop that repeats forever (or until the program is artificially halted).
 

 

 50. 

A typical ____________________ is a small window that contains a message asking for information, a text field for entering it, and command buttons labeled OK and Cancel.
 

 



 
         Start Over