Assignment 5 Questions

Ace your homework & exams now with Quizwiz!

How many times will the following do-while loop be executed? int x = 11; do{ x += 20; } while (x>100) Select one: A. 5 B. 4 C. 1 D. 0

1

In a for loop, the control variable is always incremented. Select one: True False

False

The for loop is a posttest loop that has built-in expressions for initializing, testing, and updating. Select one: True False

False

What does the following code do? Scanner keyboard = new Scanner (system.in); String filename; Syetem.out.print("Enter the filename: "); filename = keyboard.readString(); PrintWriter outFile = new PrintWriter(filename); Select one: A. It allows the user to enter the name of the file that data will be written to. B. It writes to a file named filename. C. It establishes a connection with a file named filename. D. Nothing; the code contains a syntax error.

It allows the user to enter the name of the file that data will be written to.

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

True

When you open a file with the PrintWriter class, the class can potentially throw an IOException. Select one: True False

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. Select one: True False

True

In all but very rare cases, loops must contain, within themselves Select one: A. nested decision structures. B. a way to terminate. C. nested loops. D. arithmetic operators.

a way to terminate

The variable used to keep a running total in a loop is called a(n) Select one: A. integer. B. sentinel. C. accumulator. D. summation.

accumulator

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

all do

A loop that executes as long as a particular condition exists is called a(n) ________ loop. Select one: A. conditional B. infinite C. relational D. count-controlled

conditional

A loop that repeats a specific number of times is known as a(s) Select one: A. count-controlled. B. pretest. C. infinite. D. conditional.

count-controlled

An item that separates other items is known as a Select one: A. delimiter. B. partition. C. sentinel. D. terminator.

delimiter

Given the following statement, which statement will write the string "Calvin" to the file DiskFile.txt? PrintWriter diskOut = new PrintWriter("DiskFile.txt"); Select one: A. DiskFile.println("Calvin"); B. System.out.println(diskOut, "Calvin"); C. diskOut.println("Calvin"); D. PrintWriter.println("Calvin");

diskOut.println("Calvin");

The ________ loop is ideal in situations where you always want the loop to iterate at least once. Select one: A. posttest B. for C. while D. do-while

do-while

The ________ loop is ideal in situations where the exact number of iterations is known. Select one: A. for B. do-while C. while D. posttest

for

You can use the PrintWriter class to open a file and write data to it. Select one: True False

True

What will be the value of x after the following code is executed? int x = 10; while (x<100){ x += 100; } Select one: A. 10 B. 110 C. 90 D. 100

100

How many times will the following for loop be executed? for (int count = 10; count <= 21; count++) System.out.println("Java is great!") Select one: A. 12 B. 0 C. 11 D. 10

12

What will be the value of x after the following code is executed? int x, y = 15; x = y-- Select one: A. 14 B. 15 C. 16 D. 0

15

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; } Select one: A. 110 B. 210 C. 130 D. 90

210

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. Select one: True False

True

The while loop is always the best choice in situations where the exact number of iterations is known. Select one: True False

False

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

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

When working with the PrintWriter class, which of the following import statements should you have near the top of your program? Select one: A. import java.file.*; B. import javac.io.*; C. import java.io.*; D. import javax.swing.*;

import java.io.*;

If a loop does not contain, within itself, a valid way to terminate, it is called a(n) ________ loop Select one: A. do-while B. for C. infinite D. while

infinite

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? Select one: A. int number = inputFile.nextInt(); B. int number = inputFile.readInt(); C. int number = inputFile.next(); D. int number = inputFile.integer();

int number = inputFile.nextInt();

A ________ is a value that signals when the end of a list of values has been reached. A. sentinel B. terminal C. token D. delimiter

sentinel

Before entering a loop to compute a running total, the program should first select one A. read all the values into main memory. B. know exactly how many values there are to total. C. set all variables to zero. D. set the accumulator variable to an initial value, often zero.

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

In general, there are two types of files which are Select one: A. static and dynamic. B. encrypted and unencrypted. C. text and binary. D. delimited and unlimited.

text and binary

Which of the following is the method you can use to determine whether a file exists? Select one: A. the File class's canOpen method B. the PrintWriter class's fileExists method C. the File class's exists method D. the Scanner class's exists method

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? Select one: A. while (inputFile.hasNext()) B. while (inputFile.nextLine == " ") C. while (inputFile != null) D. while (!inputFile.EOF)

while (inputFile.hasNext())

A ________ loop will always be executed at least once Select one: A. pretest B. posttest C. user-controlled D. conditional

posttest

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. Select one: A. terminator B. accumulator C. sentinel D. delimiter

sentinel

What will be the value of x after the following statements are executed? int x = 10; for (int y=5; y<20; y+=5) x += y; Select one: A. 30 B. 40 C. 50 D. 25

40

How many times will the following do-while loop be executed? int x = 11; do { x += 20; } while x <= 100); Select one: A. 5 B. 4 C. 1 D. 3

5

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. Select one: True False

False

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

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

What will be the values of x and y as a result of the following code? int x = 25, y= 8; x += y++; Select one: A. x = 33, y = 9 B. x = 25, y = 8 C. x = 34, y = 9 D. x = 33, y = 8

x = 33, y = 9


Related study sets

Unit 21: NC Statutes and Regulations

View Set

Programming exam (chapters 4 & 5)

View Set

Block 9: Peds Module 1-5 practice questions

View Set

Geology Final: Practice Questions

View Set