Deck 4: Loops and Files

Full screen (f)
exit full mode
Question
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) The boolean condition can never be true
Use Space or
up arrow
down arrow
to flip the card.
Question
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
Question
A loop that repeats a specific number of times is known as a(n)

A) sentinel loop
B) conditional loop
C) counter-controlled loop
D) infinite loop
Question
What will be the value of x after the following code is executed? int x = 10;
Do
{
X *= 20;
}
While (x > 5);

A) 10
B) 200
C) This is an infinite loop.
D) The loop will not be executed,the initial value of x > 5.
Question
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 = 25,y = 8
B) x = 33,y = 8
C) x = 33,y = 9
D) x = 34,y = 9
Question
Before entering a loop to compute a running total,the program should first do this.

A) Read all the values into main memory
B) Set the accumulator where the total will be kept to an initial value,usually zero
C) Know exactly how many values there are to total
D) Set all variables to zero
Question
This type of loop is ideal in situations where the exact number of iterations is known.

A) while loop
B) do-while loop
C) for loop
D) if statement
Question
Which of the following will open a file named MyFile.txt and allow you to append data to its existing contents?

A) FileWriter fwriter = new FileWriter("MyFile.txt",true);
PrintWriter outFile = new PrintWriter(fwriter);
B) FileWriter fwriter = new FileWriter("MyFile.txt");
PrintWriter outFile = new PrintWriter(fwriter);
C) PrintWriter outfile = new PrintWriter("MyFile.txt",true);
D) PrintWriter outfile = new PrintWriter(true,"MyFile.txt");
Question
If a loop does not contain within itself a way to terminate,it is called a(n)

A) while loop
B) do-while loop
C) for loop
D) infinite loop
Question
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
Question
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;

A) 40
B) 25
C) 30
D) Invalid for statement
Question
This is a value that signals when the end of a list of values has been reached.

A) Terminal value
B) Final value
C) End value
D) Sentinel
Question
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?

A) while (inputFile != null)
{ … }
B) while (!inputFile.EOF)
{ … }
C) while (inputFile.hasNext())
{ … }
D) while (inputFile.nextLine == " ")
{ … }
Question
How many times will the following for loop be executed? for (int count = 10;count < = 21;count++)
System.out.println("Java is great!!!");

A) 1
B) 10
C) 11
D) 0
Question
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) 4
D) 5
Question
What will be the values of x and y as a result of the following code? 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
Question
Given the following statement,which statement will write "Calvin" to the file DiskFile.txt? PrintWriter diskOut = new PrintWriter("DiskFile.txt");

A) System.out.println(diskOut,"Calvin");
B) DiskFile.println("Calvin");
C) PrintWriter.println("Calvin");
D) diskOut.println("Calvin");
Question
This type of loop will always be executed at least once.

A) pre-test loop
B) post-test loop
C) sentinel loop
D) for loop
Question
What will be the value of x after the following code is executed? int x = 10;
While (x < 100)
{
X += 10;
}

A) 90
B) 100
C) 110
D) This is an infinite loop
Question
When using the PrintWriter class,which of the following import statements would you write near the top of your program?

A) import javax.swing.*;
B) import java.io.*;
C) import PrintWriter;
D) import java.file.*;
Question
Which of the following will open a file named MyFile.txt and allow you to read data from it?

A) File file = new File("MyFile.txt");
B) Scanner inputFile = new Scanner("MyFile.txt");
C) File file = new File("MyFile.txt");
Scanner inputFile = new Scanner(file);
D) PrintWriter inputFile = new PrintWriter("MyFile.txt");
Question
You can use this method to determine whether a file exists.

A) The Scanner class's exists method
B) The File class's exists method
C) The File class's canOpen method
D) The PrintWriter class's fileExists method
Question
What will be the value of x after the following code is executed? int x = 10;
While (x < 100);
{
X += 10;
}

A) 90
B) 100
C) 110
D) This is an infinite loop
Question
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
Question
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.nextInt();
B) int number = inputFile.next();
C) int number = inputFile.readInt();
D) int number = inputFile.integer();
Question
What will be the value of x after the following code is executed? int x = 10;
Do
{
X *= 20;
}
While (x < 5);

A) 10
B) 200
C) This is an infinite loop.
D) The loop will not be executed,the initial value of x > 5.
Question
This is an item that separates other items.

A) Controller
B) Partition
C) Doorway
D) Delimiter
Question
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
Question
A sentinel value _________ and signals that there are no more values to be entered.

A) is a different data type than the values being processed
B) is a special value that cannot be mistaken as a member of the list
C) indicates the start of a list
D) guards the list
Question
How many times will the following do-while loop be executed? int x = 11;
Do
{
X += 20;
}
While (x < = 100);

A) 1
B) 3
C) 4
D) 5
Question
This type of loop is ideal in situations where you always want the loop to iterate at least once.

A) while loop
B) do-while loop
C) for loop
D) if statement
Question
This is a sum of numbers that accumulates with each iteration of a loop.

A) Running total
B) Final total
C) Grand finale
D) Galloping total
Question
A loop that executes as long as a particular condition exists is called a(n)

A) sentinel loop
B) conditional loop
C) count-controlled loop
D) infinite loop
Question
In all but rare cases,loops must contain within themselves

A) arithmetic statements
B) if statements
C) a way to terminate
D) nested loops
Question
In the for loop,the control variable cannot be initialized to a constant value and tested against a constant value.
Question
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) 90
B) 110
C) 130
D) 210
Question
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
Question
What will be the value of x after the following code is executed? int x,y = 15,z = 3;
X = (y--)/ (++z);

A) 3
B) 4
C) 5
D) 6
Question
The do-while loop is a pre-test loop.
Question
Which of the following are pre-test loops?

A) while,for,do-while
B) while,do-while
C) while,for
D) for,do-while
Question
A file must always be opened before using it and closed when the program is finished using it.
Question
When you open a file with the PrintWriter class,the class can potentially throw an IOException.
Question
In a for statement,the control variable can only be incremented.
Question
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.
Question
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.
Question
The do-while loop must be terminated with a semicolon.
Question
You can use the PrintWriter class to open a file for writing and write data to it.
Question
When you pass the name of a fi le 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.
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/48
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 4: Loops and Files
1
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) The boolean condition can never be true
D
2
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
3
A loop that repeats a specific number of times is known as a(n)

A) sentinel loop
B) conditional loop
C) counter-controlled loop
D) infinite loop
C
4
What will be the value of x after the following code is executed? int x = 10;
Do
{
X *= 20;
}
While (x > 5);

A) 10
B) 200
C) This is an infinite loop.
D) The loop will not be executed,the initial value of x > 5.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
5
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 = 25,y = 8
B) x = 33,y = 8
C) x = 33,y = 9
D) x = 34,y = 9
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
6
Before entering a loop to compute a running total,the program should first do this.

A) Read all the values into main memory
B) Set the accumulator where the total will be kept to an initial value,usually zero
C) Know exactly how many values there are to total
D) Set all variables to zero
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
7
This type of loop is ideal in situations where the exact number of iterations is known.

A) while loop
B) do-while loop
C) for loop
D) if statement
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
8
Which of the following will open a file named MyFile.txt and allow you to append data to its existing contents?

A) FileWriter fwriter = new FileWriter("MyFile.txt",true);
PrintWriter outFile = new PrintWriter(fwriter);
B) FileWriter fwriter = new FileWriter("MyFile.txt");
PrintWriter outFile = new PrintWriter(fwriter);
C) PrintWriter outfile = new PrintWriter("MyFile.txt",true);
D) PrintWriter outfile = new PrintWriter(true,"MyFile.txt");
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
9
If a loop does not contain within itself a way to terminate,it is called a(n)

A) while loop
B) do-while loop
C) for loop
D) infinite loop
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
10
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
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
11
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;

A) 40
B) 25
C) 30
D) Invalid for statement
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
12
This is a value that signals when the end of a list of values has been reached.

A) Terminal value
B) Final value
C) End value
D) Sentinel
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
13
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?

A) while (inputFile != null)
{ … }
B) while (!inputFile.EOF)
{ … }
C) while (inputFile.hasNext())
{ … }
D) while (inputFile.nextLine == " ")
{ … }
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
14
How many times will the following for loop be executed? for (int count = 10;count < = 21;count++)
System.out.println("Java is great!!!");

A) 1
B) 10
C) 11
D) 0
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
15
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) 4
D) 5
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
16
What will be the values of x and y as a result of the following code? 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
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
17
Given the following statement,which statement will write "Calvin" to the file DiskFile.txt? PrintWriter diskOut = new PrintWriter("DiskFile.txt");

A) System.out.println(diskOut,"Calvin");
B) DiskFile.println("Calvin");
C) PrintWriter.println("Calvin");
D) diskOut.println("Calvin");
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
18
This type of loop will always be executed at least once.

A) pre-test loop
B) post-test loop
C) sentinel loop
D) for loop
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
19
What will be the value of x after the following code is executed? int x = 10;
While (x < 100)
{
X += 10;
}

A) 90
B) 100
C) 110
D) This is an infinite loop
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
20
When using the PrintWriter class,which of the following import statements would you write near the top of your program?

A) import javax.swing.*;
B) import java.io.*;
C) import PrintWriter;
D) import java.file.*;
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
21
Which of the following will open a file named MyFile.txt and allow you to read data from it?

A) File file = new File("MyFile.txt");
B) Scanner inputFile = new Scanner("MyFile.txt");
C) File file = new File("MyFile.txt");
Scanner inputFile = new Scanner(file);
D) PrintWriter inputFile = new PrintWriter("MyFile.txt");
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
22
You can use this method to determine whether a file exists.

A) The Scanner class's exists method
B) The File class's exists method
C) The File class's canOpen method
D) The PrintWriter class's fileExists method
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
23
What will be the value of x after the following code is executed? int x = 10;
While (x < 100);
{
X += 10;
}

A) 90
B) 100
C) 110
D) This is an infinite loop
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
24
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
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
25
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.nextInt();
B) int number = inputFile.next();
C) int number = inputFile.readInt();
D) int number = inputFile.integer();
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
26
What will be the value of x after the following code is executed? int x = 10;
Do
{
X *= 20;
}
While (x < 5);

A) 10
B) 200
C) This is an infinite loop.
D) The loop will not be executed,the initial value of x > 5.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
27
This is an item that separates other items.

A) Controller
B) Partition
C) Doorway
D) Delimiter
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
28
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
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
29
A sentinel value _________ and signals that there are no more values to be entered.

A) is a different data type than the values being processed
B) is a special value that cannot be mistaken as a member of the list
C) indicates the start of a list
D) guards the list
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
30
How many times will the following do-while loop be executed? int x = 11;
Do
{
X += 20;
}
While (x < = 100);

A) 1
B) 3
C) 4
D) 5
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
31
This type of loop is ideal in situations where you always want the loop to iterate at least once.

A) while loop
B) do-while loop
C) for loop
D) if statement
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
32
This is a sum of numbers that accumulates with each iteration of a loop.

A) Running total
B) Final total
C) Grand finale
D) Galloping total
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
33
A loop that executes as long as a particular condition exists is called a(n)

A) sentinel loop
B) conditional loop
C) count-controlled loop
D) infinite loop
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
34
In all but rare cases,loops must contain within themselves

A) arithmetic statements
B) if statements
C) a way to terminate
D) nested loops
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
35
In the for loop,the control variable cannot be initialized to a constant value and tested against a constant value.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
36
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) 90
B) 110
C) 130
D) 210
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
37
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
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
38
What will be the value of x after the following code is executed? int x,y = 15,z = 3;
X = (y--)/ (++z);

A) 3
B) 4
C) 5
D) 6
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
39
The do-while loop is a pre-test loop.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
40
Which of the following are pre-test loops?

A) while,for,do-while
B) while,do-while
C) while,for
D) for,do-while
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
41
A file must always be opened before using it and closed when the program is finished using it.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
42
When you open a file with the PrintWriter class,the class can potentially throw an IOException.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
43
In a for statement,the control variable can only be incremented.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
44
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.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
45
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.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
46
The do-while loop must be terminated with a semicolon.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
47
You can use the PrintWriter class to open a file for writing and write data to it.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
48
When you pass the name of a fi le 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.
Unlock Deck
Unlock for access to all 48 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 48 flashcards in this deck.