Chapter 5 : Loops & Files

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

all of the above

A for loop normally performs which of these steps? a. initializes a control variable to a starting value b. tests the control variable by comparing it to a maximum/minimum value and terminate when the variable reaches that value c. updates the control variable during each iteration d. all of the above e. None of the above

conditional loop

A loop that executes as long as a particular condition exists is called a(n)

counter-controlled loop

A loop that repeats a specific number of times is known as a(n)

is a special value that cannot be mistaken as a member of the list

A sentinel value _________ and signals that there are no more values to be entered.

while (inputFile.hasNext()) { ... }

Assume that inputFile references a Scanner object that was used to open a file. Which of the following while loops shows the correct way to read data from the file until the end of the file is reached?

int number = inputFile.nextInt();

Assuming that inputFile references a Scanner object that was used to open a file, which of the following statements will read an int from the file?

Set the accumulator where the total will be kept to an initial value, usually zero

Before entering a loop to compute a running total, the program should first do this.

An iteration

Each repetition of a loop is known as what?

diskOut.println("Calvin");

Given the following statement, which statement will write "Calvin" to the file DiskFile.txt? PrintWriter diskOut = new PrintWriter("DiskFile.txt");

5

How many times will the following do-while loop be executed? int x = 11; do { x += 20; } while (x <= 100);

1

How many times will the following do-while loop be executed? int x = 11; do { x += 20; } while (x > 100);

11

How many times will the following for loop be executed? for (int count = 10; count <= 21; count++) System.out.println("Java is great!!!");

infinite loop

If a loop does not contain within itself a way to terminate, it is called a(n)

Braces

If you are using a block of statements, don't forget to enclose all of the statements in a set of:

a way to terminate

In all but rare cases, loops must contain within themselves

The boolean condition can never be true

In the following code, what values could be read into number to terminate the while loop? Scanner keyboard = new Scanner(System.in); System.out.print("Enter a number: "); int number = keyboard.nextInt(); while (number < 100 && number > 500) { System.out.print("Enter another number: "); number = keyboard.nextInt(); }

Numbers in the range 100 - 500

In the following code, what values could be read into number to terminate the while loop? Scanner keyboard = new Scanner(System.in); System.out.print("Enter a number: "); int number = keyboard.nextInt(); while (number < 100 || number > 500) { System.out.print("Enter another number: "); number = keyboard.nextInt(); }

True

T/F: A file must always be opened before using it and closed when the program is finished using it.

False

T/F: In a for statement, the control variable can only be incremented.

False

T/F: In the for loop, the control variable cannot be initialized to a constant value and tested against a constant value.

True

T/F: Java provides a set of simple unary operators designed just for incrementing and decrementing variables.

False

T/F: The do-while loop is a pre-test loop.

True

T/F: The do-while loop must be terminated with a semicolon.

True

T/F: The while loop has two important parts: (1) a boolean expression that is tested for a true or false value, and (2) a statement or block of statements that is repeated as long as the expression is true.

False

T/F: When the break statement is encountered in a loop, all the statements in the body of the loop that appear after it are ignored, and the loop prepares for the next iteration.

True

T/F: When the continue statement is encountered in a loop, all the statements in the body of the loop that appear after it are ignored, and the loop prepares for the next iteration.

True

T/F: When you open a file with the PrintWriter class, the class can potentially throw an IOException.

True

T/F: When you pass the name of a file to the PrintWriter constructor, and the file already exists, it will be erased and a new empty file with the same name will be created.

True

T/F: You can use the PrintWriter class to open a file for writing and write data to it.

++

The increment operator is:

Loop

This is a control structure that causes a statement or group of statements to repeat.

Running total

This is a sum of numbers that accumulates with each iteration of a loop.

Sentinel

This is a value that signals when the end of a list of values has been reached.

Delimiter

This is an item that separates other items.

User controlled loop

This type of loop allows the user to decide the number of iterations.

for loop

This type of loop is ideal in situations where the exact number of iterations is known.

do-while loop

This type of loop is ideal in situations where you always want the loop to iterate at least once.

post-test loop

This type of loop will always be executed at least once.

Loop control variable

This variable controls the number of times that the loop iterates.

5, 8, 11, 14,

What will be printed after the following code is executed? for (int number = 5; number <= 15; number +=3) System.out.print(number + ", ");

210

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; }

This is an infinite loop

What will be the value of x after the following code is executed? int x = 10, y = 20; while (y < 100) { x += y; }

200

What will be the value of x after the following code is executed? int x = 10; do { x *= 20; } while (x < 5);

This is an infinite loop.

What will be the value of x after the following code is executed? int x = 10; do { x *= 20; } while (x > 5);

40

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;

100

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

What will be the value of x after the following code is executed? int x = 10; while (x < 100); { x += 10; }

3

What will be the value of x after the following code is executed? int x, y = 15, z = 3; x = (y--) / (++z);

28

What will be the value of x after the following code is executed? int x, y = 4, z = 6; x = (y++) * (++z);

x = 17, y = 4

What will be the values of x and y as a result of the following code? int x = 12, y = 5; x += y--;

x = 33, y = 9

What will be the values of x and y as a result of the following code? int x = 25, y = 8; x += y++;

import java.io.*;

When using the PrintWriter class, which of the following import statements would you write near the top of your program?

while, for

Which of the following are pre-test loops?

FileWriter fwriter = new FileWriter("MyFile.txt", true); PrintWriter outFile = new PrintWriter(fwriter);

Which of the following will open a file named MyFile.txt and allow you to append data to its existing contents?

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 read data from it?

The File class's exists method

You can use this method to determine whether a file exists.

Defensive coding

____________ is the process of inspecting data given to the program by the user and determining if it is valid.


Kaugnay na mga set ng pag-aaral

Organization psychology chapter 7, 8,9, 10, and 12

View Set

Chapter 14 - Closing the Residential Transaction

View Set