Comp Sci II Chapter 5 Quiz

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

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

set the accumulator variable to an initial value, often zero.

In a for loop, the control variable is always incremented.

False

Which of the following statements opens a file named MyFile.txt and allows you to read data from it? Scanner inputFile = new Scanner("MyFile.txt"); PrintWriter inputFile = new PrintWriter("MyFile.txt"); File Scanner = new File("MyFile.txt"); File file = new File("MyFile.txt"); Scanner inputFile = new Scanner(file);

File file = new File("MyFile.txt"); Scanner inputFile = new Scanner(file);

Which of the following statements opens a file named MyFile.txt and allows you to append data to its existing contents? FileWriter fwriter = new FileWriter("MyFile.txt", true); PrintWriter outFile = new PrintWriter(fwriter); PrintWriter outfile = new PrintWriter("MyFile.txt", true); FileWriter fwriter = new FileWriter("MyFile.txt"); PrintWriter outFile = new PrintWriter(fwriter); PrintWriter outfile = new PrintWriter(true, "MyFile.txt");

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

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

conditional

Which statement makes all the statements in the body of the loop that appear after it are ignored, and the loop prepares for the next iteration when encountered in a loop?

continue statement

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

count-controlled

The ________ loop is ideal in situations where the exact number of iterations is known.

for

When working with the PrintWriter class, which of the following import statements should you have near the top of your program?

import java.io.*;

Select all that apply. Which of the following steps is normally performed by a for loop? initialize the control variable to a starting value update the control variable during each iteration terminate when the control variable reaches its maximum or minimum value test the control variable by comparing it to a maximum or minimum value

ALL OF THEM

The do-while loop is ideal in situations where you always want the loop to iterate at least once.

True

The for loop is a posttest loop that has built-in expressions for initializing, testing, and updating.

True

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

True

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

You can use the PrintWriter class to open a file and write data to it.

True

In all but very rare cases, loops must contain, within themselves

a way to terminate.

The variable used to keep a running total in a loop is called a(n)

accumulator

An item that separates other items is known as a

delimiter

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

diskOut.println("Calvin");

The ________ loop is ideal in situations where you always want the loop to iterate at least once.

do-while

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? int number = inputFile.integer(); int number = inputFile.readInt(); int number = inputFile.next(); int number = inputFile.nextInt();

int number = inputFile.nextInt();

A ________ loop will always be executed at least once.

posttest

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

sentinel

A(n) ________ is a special value that cannot be mistaken as a member of a list of data items and signals that there are no more data items to be processed.

sentinel

Which of the following is the method you can use to determine whether a file exists?

the File class's exists method

Assume that inputFile references a Scanner object that was used to open a file. Which of the following while loops is the correct way to read data from the file until the end of the file is reached? while (!inputFile.EOF) while (inputFile != null) while (inputFile.nextLine == " ") while (inputFile.hasNext())

while (inputFile.hasNext())


Kaugnay na mga set ng pag-aaral

Adult Health Chapter 40: Nursing Assessment: Musculoskeletal Function

View Set

CTP - Chapter 14 - Cash Flow Forecasting

View Set

Biology - Biochemical Pathways & Enzymes_ch 6

View Set

Human Sexuality Chapter #7, 9. 10, 11 & 12

View Set

Geology 1610 Paul Hudak Exam 1 (Chapters 1-4)

View Set

Fundamentals Nursing Prep U Chapter 39 Fluid, Electrolyte, and Acid-Base Balance

View Set