Programming Fundamentals I (Chapter 4 - Loops)
How many times will the following do-while loop be executed? int x = 11; do { x += 20; } while (x > 100);
1
What will be the value of x after the following code is executed? int x = 10; while (x < 100) { x += 10; }
100
What will be the value of x after the following code is executed? int x = 10, y = 20; while (y < 100) { x += y; y += 20; }
210
What will be the value of x after the following code is executed? int x, y = 15, z = 3; x = (y--) / (++z);
3
What will be the value of x after the following code is executed? int x = 10; for (int y = 5; y < 20; y +=5) x += y;
40
In all but rare cases, loops must contain within themselves
A way to terminate
Before entering a loop to compute a running total, the program should first do this
Set the accumulator where the total will be kept to an initial value, usually zero
A loop that executes as long as a particular condition exists is called a(n):
Conditional loop
In a for statement, the control variable can only be incremented
False
In the for loop, the control variable cannot be initialized to a constant value and tested against a constant value
False
The do-while loop is a pre-test loop
False
Which of the following will open a file named MyFile.txt and allow you to read data from it?
File file = new File("MyFile.txt"); Scanner inputFile = new Scanner(file);
Which of the following will open a file named MyFile.txt and allow you to append data to its existing contents?
FileWriter fwriter = new FileWriter("MyFile.txt", true); PrintWriter outFile = new PrintWriter(fwriter);
If a loop does not contain within itself a way to terminate, it is called a(n):
Infinite loop
This is a control structure that causes a statement or group of statements to repeat
Loop
This variable controls the number of times that the loop iterates
Loop control variable
What will be the value of x after the following code is executed? int x = 10, y = 20; while (y < 100) { x += y; }
This is an infinite loop
What will be the value of x after the following code is executed? int x = 10; while (x < 100); { x += 10; }
This is an infinite loop
Java provides a set of simple unary operators designed just for incrementing and decrementing variables
True
You can use the PrintWriter class to open a file for writing and write data to it
True
This type of loop is ideal in situations where you always want the loop to iterate at least once
do-while loop
This type of loop is ideal in situations where the exact number of iterations is known
for loop
When using the PrintWriter class, which of the following import statements would you write near the top of your program?
import java.io.*;
A sentinel value ________ and signals that there are no more values to be entered
is a special value that cannot be mistaken as a member of the list
What will be the values of x and y as a result of the following code? int x = 12, y = 5; x += y--;
x = 17, y = 4