Deck 5: Loops and Files

ملء الشاشة (f)
exit full mode
سؤال
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");
استخدم زر المسافة أو
up arrow
down arrow
لقلب البطاقة.
سؤال
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
سؤال
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
سؤال
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) pretest
سؤال
The ________ is ideal in situations where the exact number of iterations is known.

A) for loop
B) while loop
C) posttest loop
D) do-while loop
سؤال
A loop that repeats a specific number of times is known as a(n) ________ loop.

A) count-controlled
B) infinite
C) conditional
D) pretest
سؤال
This type of loop will always be executed at least once.

A) pretest
B) posttest
C) infinite
D) conditional
سؤال
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.*;
سؤال
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
سؤال
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.nextLine == " ")
B) while (inputFile != null)
C) while (!inputFile.EOF)
D) while (inputFile.hasNext())
سؤال
What does the following code do?
Scanner keyboard = new Scanner(System.in);
String filename;
System.out.print("Enter the filename: ");
Filename = keyboard.readString();
PrintWriter outFile = new PrintWriter(filename);

A) It writes to a file named filename.
B) It allows the user to enter the name of the file that data will to be written to.
C) It establishes a connection with a file named filename.
D) Nothing. The code contains a syntax error.
سؤال
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
سؤال
A for loop normally performs which of these steps?

A) update the control variable during each iteration
B) test the control variable by comparing it to a maximum/minimum value and terminate when the variable reaches that value
C) initialize a control variable to a starting value
D) All of the above
سؤال
Before entering a loop to compute a running total, the program should first

A) set the accumulator variable to an initial value, usually 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.
سؤال
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
سؤال
What will be the value of x after the following code is executed?
Int x = 10;
While (x < 100)
{
X += 10;
}

A) 100
B) 90
C) 110
D) 10
سؤال
This is an item that separates other items.

A) partition
B) delimiter
C) sentinel
D) terminator
سؤال
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) 25
B) 30
C) 50
D) 40
سؤال
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
سؤال
You can use this method 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
سؤال
The for loop is a posttest loop that has built-in expressions for initializing, testing, and updating.
سؤال
The variable used to keep the running total is called a(n)

A) sentinel.
B) integer.
C) summation.
D) accumulator.
سؤال
When you open a file with the PrintWriter class, the class can potentially throw an IOException.
سؤال
You can use the PrintWriter class to open a file and write data to it.
سؤال
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();
سؤال
The while loop is ideal in situations where the exact number of iterations is known.
سؤال
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
سؤال
In all but rare cases, loops must contain within themselves

A) nested loops.
B) a way to terminate.
C) arithmetic operators.
D) if statements.
سؤال
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.
سؤال
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");
سؤال
In a for loop, the control variable can only be incremented.
سؤال
In general, there are two types of files:

A) text and binary.
B) encrypted and unencrypted.
C) delimited and unlimited.
D) static and dynamic.
سؤال
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
سؤال
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.
سؤال
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 file = new File("MyFile.txt");
D) PrintWriter inputFile = new PrintWriter("MyFile.txt");
سؤال
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 from the list to be processed.

A) accumulator
B) sentinel
C) delimiter
D) terminator
سؤال
A file must always be opened before using it and closed when the program is finished using it.
سؤال
The do-while loop is ideal in situations where you always want the loop to iterate at least once.
سؤال
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.
سؤال
If a loop does not contain within itself a way to terminate, it is called

A) a for loop.
B) an infinite loop.
C) a while loop.
D) a do-while loop.
فتح الحزمة
قم بالتسجيل لفتح البطاقات في هذه المجموعة!
Unlock Deck
Unlock Deck
1/40
auto play flashcards
العب
simple tutorial
ملء الشاشة (f)
exit full mode
Deck 5: Loops and Files
1
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
2
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
3
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
4
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) pretest
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
5
The ________ is ideal in situations where the exact number of iterations is known.

A) for loop
B) while loop
C) posttest loop
D) do-while loop
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
6
A loop that repeats a specific number of times is known as a(n) ________ loop.

A) count-controlled
B) infinite
C) conditional
D) pretest
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
7
This type of loop will always be executed at least once.

A) pretest
B) posttest
C) infinite
D) conditional
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
8
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.*;
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
9
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
10
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.nextLine == " ")
B) while (inputFile != null)
C) while (!inputFile.EOF)
D) while (inputFile.hasNext())
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
11
What does the following code do?
Scanner keyboard = new Scanner(System.in);
String filename;
System.out.print("Enter the filename: ");
Filename = keyboard.readString();
PrintWriter outFile = new PrintWriter(filename);

A) It writes to a file named filename.
B) It allows the user to enter the name of the file that data will to be written to.
C) It establishes a connection with a file named filename.
D) Nothing. The code contains a syntax error.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
12
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
13
A for loop normally performs which of these steps?

A) update the control variable during each iteration
B) test the control variable by comparing it to a maximum/minimum value and terminate when the variable reaches that value
C) initialize a control variable to a starting value
D) All of the above
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
14
Before entering a loop to compute a running total, the program should first

A) set the accumulator variable to an initial value, usually 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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
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) 5
B) 4
C) 3
D) 1
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
16
What will be the value of x after the following code is executed?
Int x = 10;
While (x < 100)
{
X += 10;
}

A) 100
B) 90
C) 110
D) 10
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
17
This is an item that separates other items.

A) partition
B) delimiter
C) sentinel
D) terminator
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
18
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) 25
B) 30
C) 50
D) 40
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
19
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
20
You can use this method 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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
21
The for loop is a posttest loop that has built-in expressions for initializing, testing, and updating.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
22
The variable used to keep the running total is called a(n)

A) sentinel.
B) integer.
C) summation.
D) accumulator.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
23
When you open a file with the PrintWriter class, the class can potentially throw an IOException.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
24
You can use the PrintWriter class to open a file and write data to it.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
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.next();
B) int number = inputFile.integer();
C) int number = inputFile.readInt();
D) int number = inputFile.nextInt();
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
26
The while loop is ideal in situations where the exact number of iterations is known.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
27
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
28
In all but rare cases, loops must contain within themselves

A) nested loops.
B) a way to terminate.
C) arithmetic operators.
D) if statements.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
29
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
30
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");
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
31
In a for loop, the control variable can only be incremented.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
32
In general, there are two types of files:

A) text and binary.
B) encrypted and unencrypted.
C) delimited and unlimited.
D) static and dynamic.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
33
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
34
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
35
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 file = new File("MyFile.txt");
D) PrintWriter inputFile = new PrintWriter("MyFile.txt");
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
36
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 from the list to be processed.

A) accumulator
B) sentinel
C) delimiter
D) terminator
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
37
A file must always be opened before using it and closed when the program is finished using it.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
38
The do-while loop is ideal in situations where you always want the loop to iterate at least once.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
39
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
40
If a loop does not contain within itself a way to terminate, it is called

A) a for loop.
B) an infinite loop.
C) a while loop.
D) a do-while loop.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.
فتح الحزمة
k this deck
locked card icon
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 40 في هذه المجموعة.