Java Exam II Part A Chapter 4

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

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

False

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

False

The while loop is always the best choice in situations where the exact number of iterations is known.

False

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.

False

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

True

Java provides a set of simple unary operators designed just for incrementing and decrementing variables

True

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.

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

A loop that repeats a specific number of times is known as a(n) __________ loop. a. count-controlled b. infinite c. conditional d. pretest

A

Before entering a loop to compute a running total, the program should first a. set the accumulator variable to an initial value, often zero b. set all variables to zero c. read all the values into main memory d. know exactly how many values there are to total

A

Each repetition of a loop is known as a(n) __________. a. iteration b. cycle c. execution d. lap

A

How many times will the following do-while loop be executed? int x = 11; do { x += 20; } while (x <= 100); a. 5 b. 4 c. 3 d. 1

A

What will be the value of x after the following code is executed? int x = 10, y = 20; while (y < 100) { x += y; } a. 90 b. 110 c. 210 d. this is an infinite loop

D

The variable used to keep a running total in a loop is called a(n) __________. a. accumulator b. sentinel c. summation d. integer

A

What will be the values of x and y after the following code is executed? int x, y = 15, z = 3; x = (y--) / (++z); a. 3 b. 4 c. 5 d. 6

A

A __________ is a value that signals when the end of a list of values has been reached. a. terminal b. sentinel c. token d. delimiter

B

If a loop does not contain, within itself, a valid way to terminate, it is called a(n) __________ loop a. for b. while c. do-while d. infinite

D

What will be the values of x and y as a result of the following code? int x = 25, y = 8; x += y++; a. x = 34, y = 9 b. x = 25, y = 8 c. x = 33, y = 8 d. x = 33, y = 9

D

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

B

In all but very rare cases, loops must contain, within themselves a. nested loops b. a way to terminate c. arithmetic operators d. nested decision strucures

B

The variable that controls the number of times a loop iterates is known as a(n) __________. a. counter variable b. loop control variable c. running total d. decrement variable

B

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; } a. 130 b. 210 c. 110 d. 90

B

What will be the value of x after the following code is executed? int x, y = 4, z = 6; x = (y++) * (++z); a. 24 b. 28 c. 30 d. 35

B

Which is a control structure that causes a statement or group of statements to repeat? a. block b. loop c. prefix mode d. body

B

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

B

The do-while loop must be terminated with a semicolon.

True

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

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

True

An item that separates other items is known as a a. partition b. delimiter c. sentinel d. terminator

B

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(); } a. Numbers less than 100 or greater than 500 b. Numbers in the range 100 - 499 c. Numbers in the range 100 - 500 d. Impossible - the boolean condition can never be true

D

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(); } a. Numbers less than 100 b. Numbers greater than 500 c. Numbers in the range 100 - 499 d. Numbers in the range 100 - 500

D

What will be the value of x after the following code is executed? int x, y = 15; x = y--; a. 14 b. 16 c. 0 d. 15

D

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; a. 25 b. 30 c. 50 d. 40

D

What will be the values of x aand y after the following code is executed? int x = 12, y = 5; x += y--; a. x = 12, y = 5 b. x = 16, y = 4 c. x = 17, y = 5 d. x = 17, y = 4

D

How many times will the following do-while loop be executed? int x = 11; do { x += 20; } while (x > 100); a. 0 b. 1 c. 5 d. 4

B

The __________ loop is ideal in situations where the exact number of iterations is known. a. for b. while c. do-while d. posttest

A

A __________ loop will always be executed at least once. a. pretest b. posttest c. conditional d. user-controlled

B

A random number, created as an object of the Random class, is always a(n) __________. a. object b. integer c. float d. class

B

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. a. accumulator b. sentinel c. summation d. integer

B

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

B

Which of the following statements will create an object from the Random class? a. randomNumbers() = new Random(); b. Random myNumber = new Random(); c. myNumber = new Random(); d. Random = new randomNumbers();

B

A loop that executes as long as a particular condition exists is called a(n) __________ loop. a. infinite b. count-controlled c. conditional d. relational

C

The __________ loop allows the user to decide on the number of iterations. a. counter controlled loop b. dynamically executed loop c. user controlled loop d. infinite loop

C

The __________ loop is ideal in situations where you always want the loop to iterate at least once. a. for b. while c. do-while d. posttest

C

What will be printed after the following code is executed? for (int number = 5; number <= 15; number +=3) System.out.print(number + ", "); a. 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, b. 5, 8, 11, 14, 17, c. 5, 8, 11, 14 d. This is an invalid for statement

C

What will be the value of x after the following code is executed? int x = 10; while (x < 100) { x += 100; } a. 100 b. 90 c. 110 d. 10

C

When working with the PrintWriter class, which of the following import statements should you have near the top of your program? a. import javax.swing.*; b. import javac.io.*; c. import java.io.*; d. import java.file.*;

C

Which of the following are pre-test loops? a. while, for, do-while b. while, do-while c. while, for d. for, do-while

C

Which of the following is the method you can use to determine whether a file exists? a. the File class's canOpen method b. the Scanner class's exists method c. the File class's exists method d. the PrintWriter class's fileExists method

C

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? a. while (inputFile.nextLine == " ") { ... } b. while (inputFile != null) { ... } c. while (!inputFile.EOF) { ... } d. while (inputFile.hasNext()) { ... }

D

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

D

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

D

Which of the following expressions will generate a random number in the range of 1 through 10? a. myNumber = randomNumbers.nextInt(10); b. myNumber = randomNumbers.nextInt(1) + 10; c. myNumber = randomNumbers.nextInt(11) - 1; d. myNumber = randomNumbers.nextInt(10) + 1;

D


Ensembles d'études connexes

8th Grade Spanish Study Guide for Proficiency Exam! (Blue Packet)

View Set

Check your understanding ch. 17 & ch. 20

View Set

Lifespan Final accumulative part

View Set

Chapter 4 - Casualty (Liability) Insurance Basics

View Set