Chapter 4 programming
How many times will the following do-while loop be executed? int x = 11; do { x += 20; } while (x > 100);
1
What will be the value of x after the following code is executed? int x, y = 15; x = y--;
15
How many times will the following do-while loop be executed? int x = 11; do { x += 20; } while (x <= 100);
5
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.nextInt();*****
In all but rare cases, loops must contain within themselves?
A way to terminate
A for loop normally performs which of these steps?
All of the above
If a loop does not contain within itself a way to terminate, it is called
An infinite loop
A loop that executes as long as a particular condition exists is called a(n)
Conditional loop
A loop that repeats a specific number of times is known as a(n)
Counter-controlled loop
This is an item that separates other items
Delimiter
In the for loop, the control variable cannot be initialized to a constant value and tested against a constant value
False
The do-while loop is a pre-test loop.
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
Which of the following will open a file named MyFile.txt and allow you to read data from it?
File file = new File("MyFile.txt"); Scanner inputFile = new Scanner(file);
This type of loop is ideal in situations where the exact number of iterations is known.
For Loop
Look at the following code: Scanner keyboard = new Scanner(System.in); int studentGrade = 0; int totalOfStudentGrades = 0; while(studentGrade != -1) { System.out.println("Enter student grade: :"); studentGrade = keyboard.nextInt(); totalOfStudentGrades += studentGrade; } In the loop header: while(studentGrade != -1), what is the purpose of "-1"
It is a sentinel
This type of loop will always be executed at least once
Post-test loop
This is a sum of numbers that accumulates with each iteration of a loop
Running total
This is a value that signals when the end of a list of values has been reached.
Sentinel
Before entering a loop to compute a running total, the program should first
Set the accumulator where the total will be kept to an initial value, usually zero
What will be the value of x after the following code is executed? int x = 10, y = 20; while (y < 100) { x += y; }
This is an infinite loop
In a for loop, the control variable can only be incremented
True
The do-while loop must be terminated with a semicolon.
True
When you open a file with the PrintWriter class, the class can potentially throw an IOException
True
This type of loop is ideal in situations where you always want the loop to iterate at least once
do-while loop
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; ==40
What will be the values of x and y as a result of the following code?
int x = 25, y = 8; x += y++; ==x = 33, y = 9
What will be the values of x and y as a result of the following code? int x = 12, y = 5; x += y- -;
x = 17, y = 4