(4-5) Flashcards
(80 cards)
In the for loop, the control variable cannot be initialized to a constant value and tested against a constant value
False
What will the value of x after the following code is executed?
int x = 10;
while (x < 100)
{
x += 10;
}
100 (check answer)
A for loop normally performs which of these steps
update the control variable during each iteration
test the control variable by comparing it to a maximum/minimum value and terminate when it reaches that value
Answer - all of the above
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
This a control statement that causes a statement or a group of statements to repeat
Loop
In all but rare cases, loops must contain within themselves:
a way to terminate
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?
while (inputFile.hasNext())
How many times will the following for loop be executed?
for (int count = 10; count <= 21; count ++)
System.out.println(“Java is great”);
12 (check answer)
What will be the value of x after the following code is executed?
int x, y = 15, z = 3;
x = (y - -) / (++z);
3
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
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;
}
210
In the following code, what values could be read into number to terminate the while loop?
Scanner keyboard = new Scanner(System.in);
System.out.println(“Enter a number “);
int number = keyboard.nextInt();
while (number < 100 && number > 500)
{
System.out.print(“Enter another number “);
number = keyboard.nextInt();
}
The boolean condition can never be true
A file must always be opened before using it and closed when the program is finished using it
True
This type of loop is ideal in situations where the exact number of iterations is known
for loop
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 (check answer)
Which of the following are pre-test loops?
While and for
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 of the next iteration
False
__________ is the process of inspecting data given to the program by the user and determining if it is valid
Input validation
What will be the value of x after the following code is executed?
int x = 10;
while (x < 100);
{
x +=10;
}
This is an infinite loop
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
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();
The while loop has two important parts: (1) the boolean expression that is tested for a true or false value, and (2) a statement or a block of statements that is repeated as long as the expression is true
True
Each repetition of a loop is known as what?
Iteration
In the following code, what values could be read into number to terminate the while loop?
Scanner keyboard = new Scanner(System.in);
System.out.println(“Enter a number “);
int number = keyboard.nextInt();
while (number < 100 || number > 500)
{
System.out.println(“Enter another number: “);
keyboard.nextInt();
}
Numbers in the range 100 - 500