Vous êtes sur la page 1sur 6

16-17 FJ Ch4 Test

True/False
Indicate whether the sentence or 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 letter of the choice that best completes the statement or answers the question.

____ 16. Javas 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. The equal-to (comparison) operator is represented by the symbol ____.
a. = c. !=
b. == d. ===
____ 20. 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
____ 21. 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--;
}
____ 22. 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
____ 23. 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;
. . .;
}
____ 24. 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
____ 25. 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.
____ 26. File objects are instances of the class ____.
a. File c. Scanner
b. IO d. Stream
____ 27. 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
____ 28. Data can be output to a text file using the class ____.
a. Writer c. Scanner
b. PrintWriter d. FileWriter
Matching

a. Flowchart g. (AC) Counter-controlled Loop


b. Iteration h. (AD) Infinite Loop
c. Counter i. (AE) Control Statements
d. Entry-controlled Loop j. (BC) Sentinel Node
e. Sentinel Value k. (BD) Off-by-one Error
f. (AB) Overloading l. (BE) Task-Controlled Loop

____ 29. A loop that stops when a counter variable reaches a specific limit.
____ 30. A diagram that displays the flow of control of a program.
____ 31. A type of statement that chooses alternate course of action based on the truth or falsity of a given condition.
____ 32. A loop in which the controlling condition is not changed in such a manner to allow the loop to terminate.
____ 33. A variable used to count the number of times some process is completed.
____ 34. A loop in which the control condition is tested before the loop is executed.
____ 35. Usually seen with loops, this error shows up as a result that is one less or one greater than the expected value.
____ 36. The process of using the same operator symbol or identifier to refer to many different functions.
____ 37. A special node in a linked structure that contains no data but instead makes the beginning or end of the
structure.
____ 38. Program statements that cause a process to be repeated.
____ 39. A special value that indicates the end of a set of data or of a process.
____ 40. A type of loop that terminates when it is finished performing some task.
16-17 FJ Ch4 Test
Answer Section

TRUE/FALSE

1. ANS: T REF: 106


2. ANS: T REF: 109
3. ANS: T REF: 112
4. ANS: F REF: 112
5. ANS: F REF: 112
6. ANS: F REF: 114
7. ANS: T REF: 114
8. ANS: F REF: 116
9. ANS: F REF: 121
10. ANS: T REF: 122
11. ANS: F REF: 123
12. ANS: T REF: 124
13. ANS: F REF: 125
14. ANS: T REF: 128
15. ANS: T REF: 129

MULTIPLE CHOICE

16. ANS: B REF: 106


17. ANS: D REF: 107
18. ANS: D REF: 112
19. ANS: B REF: 114
20. ANS: A REF: 117
21. ANS: C REF: 118
22. ANS: B REF: 119
23. ANS: B REF: 120
24. ANS: C REF: 124
25. ANS: D REF: 127
26. ANS: A REF: 128
27. ANS: D REF: 128
28. ANS: B REF: 129

MATCHING

29. ANS: G
30. ANS: A
31. ANS: I
32. ANS: H
33. ANS: C
34. ANS: D
35. ANS: K
36. ANS: F
37. ANS: J
38. ANS: B
39. ANS: E
40. ANS: L

Vous aimerez peut-être aussi