Deck 4: Conditionals and Loops
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/40
Play
Full screen (f)
Deck 4: Conditionals and Loops
1
The ___________________ statement causes execution of a loop to stop, and the statement following the loop to be subsequently executed.
A) stop
B) halt
C) continue
D) break
E) switch
A) stop
B) halt
C) continue
D) break
E) switch
D
Explanation: The break statement causes the execution of a loop to stop, and the statements to be subsequently executed. Therefore choice d is correct. The continue statement causes current iteration of a loop to stop and the condition to be evaluated again, either stopping the loop or causing the next iteration. There are no stop or halt statements in the Java language. A switch statement is a control structure.
Explanation: The break statement causes the execution of a loop to stop, and the statements to be subsequently executed. Therefore choice d is correct. The continue statement causes current iteration of a loop to stop and the condition to be evaluated again, either stopping the loop or causing the next iteration. There are no stop or halt statements in the Java language. A switch statement is a control structure.
2
A _________________ is an object that has methods that allow you to process a collection of items one at a time.
A) iterator
B) loop
C) conditional
D) palindrome
E) nested loop
A) iterator
B) loop
C) conditional
D) palindrome
E) nested loop
A
Explanation: An iterator is an object that has methods that allow you to process a collection of items one at a time. None of the other choices are objects in Java.
Explanation: An iterator is an object that has methods that allow you to process a collection of items one at a time. None of the other choices are objects in Java.
3
Which of the following for loop headers will cause the body of the loop to be executed 10 times?
A) for(int i = 0; i <= 10; i++)
B) for(int i = 1; i < 10; i++)
C) for(int i = 1; i <= 11; i++)
D) for(int i = 0; i < 10; i++)
E) none of these for loops will execute the loop body 10 times
A) for(int i = 0; i <= 10; i++)
B) for(int i = 1; i < 10; i++)
C) for(int i = 1; i <= 11; i++)
D) for(int i = 0; i < 10; i++)
E) none of these for loops will execute the loop body 10 times
D
Explanation: The loop executes when i is equal to 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. Therefore it executes exactly 10 times. Choice a and c execute 11 times, while choice a executes 9 times.
Explanation: The loop executes when i is equal to 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. Therefore it executes exactly 10 times. Choice a and c execute 11 times, while choice a executes 9 times.
4
In a nested if statement an else clause is matched to the closest unmatched if.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
5
Which of the following expressions best represents the condition "if the grade is between 70 and 100"?
A) if (75 < grade && grade < 100)
B) if (grade != 75 && grade != 100)
C) if (75 < grade < 100)
D) if (75 > grade || grade < 100)
E) if (75 < grade || grade < 100)
A) if (75 < grade && grade < 100)
B) if (grade != 75 && grade != 100)
C) if (75 < grade < 100)
D) if (75 > grade || grade < 100)
E) if (75 < grade || grade < 100)
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
6
Let a and b be valid boolean expressions. Which of the following best describes the result of the expression a || b?
A) It will evaluate to true if a evaluates to true and b evaluates to true. It will evaluate to false otherwise.
B) It will evaluate to false if a evaluates to false and b evaluates to false. It will evaluate to true otherwise.
C) It will evaluate to true if a evaluates to false and b evaluates to false. It will evaluate to true otherwise.
D) It will evaluate to true if a evaluates to false or b evaluates to false. It will evaluate to true otherwise.
E) None of the above statements correctly describes the evaluation of the expression.
A) It will evaluate to true if a evaluates to true and b evaluates to true. It will evaluate to false otherwise.
B) It will evaluate to false if a evaluates to false and b evaluates to false. It will evaluate to true otherwise.
C) It will evaluate to true if a evaluates to false and b evaluates to false. It will evaluate to true otherwise.
D) It will evaluate to true if a evaluates to false or b evaluates to false. It will evaluate to true otherwise.
E) None of the above statements correctly describes the evaluation of the expression.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
7
Suppose we wanted to process a text file called "input.txt" using the Scanner object. Which of the following lines of code correctly creates the necessary Scanner object?
A) Scanner inputFile = new Scanner("input.txt");
B) Scanner inputFile = new Scanner(new InputFile("input.txt");
C) Scanner inputFile = new Scanner(new File(input.txt);
D) Scanner inputFile = new Scanner(new InputFile(input.txt);
E) Scanner inputFile = new Scanner(new File("input.txt");
A) Scanner inputFile = new Scanner("input.txt");
B) Scanner inputFile = new Scanner(new InputFile("input.txt");
C) Scanner inputFile = new Scanner(new File(input.txt);
D) Scanner inputFile = new Scanner(new InputFile(input.txt);
E) Scanner inputFile = new Scanner(new File("input.txt");
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
8
A logical expression can be described by a ________________ that lists all possible combinations of values for the variables involved in an expression.
A) palindrome
B) nested loop
C) equality operator
D) switch statement
E) truth table
A) palindrome
B) nested loop
C) equality operator
D) switch statement
E) truth table
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
9
Every if statement requires an associated else statement, but not every else statement requires an associated if statement.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
10
Which of the following logical operators has the highest precedence?
A) !
B) &&
C) ||
D) <
E) >
A) !
B) &&
C) ||
D) <
E) >
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
11
In Java, a boolean expression is limited to having exactly 2 logical operators.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
12
A while statement always executes its loop body at least once.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
13
What happens if a case in a switch statement does not end with a break statement?
A) The program will not compile.
B) The switch statement will never execute.
C) It will cause an infinite loop.
D) The switch statement will execute the next case statement as well.
E) The case will never be executed.
A) The program will not compile.
B) The switch statement will never execute.
C) It will cause an infinite loop.
D) The switch statement will execute the next case statement as well.
E) The case will never be executed.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
14
Suppose we want to condition an if statement on whether two String objects, referenced by stringOne and stringTwo, are the same. Which of the following is the correct way to achieve this?
A) if(stringOne == stringTwo)
B) if(stringOne.compareTo(stringTwo))
C) if(stringOne.equals(stringTwo))
D) if(stringOne != stringTwo)
E) if(stringOne === stringTwo)
A) if(stringOne == stringTwo)
B) if(stringOne.compareTo(stringTwo))
C) if(stringOne.equals(stringTwo))
D) if(stringOne != stringTwo)
E) if(stringOne === stringTwo)
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
15
Which of the following statements best describes the flow of control in the main method of a Java program that has no conditionals or loops?
A) Program statements are all executed at the same time.
B) Program statements are executed according to their priority, which is specified by the programmer.
C) Program statements are executed linearly, with earlier statements being executed first.
D) Program statements are executed linearly, with later statements being executed first.
E) Some program statements are executed at the same time, and others are executed in a linear manner.
A) Program statements are all executed at the same time.
B) Program statements are executed according to their priority, which is specified by the programmer.
C) Program statements are executed linearly, with earlier statements being executed first.
D) Program statements are executed linearly, with later statements being executed first.
E) Some program statements are executed at the same time, and others are executed in a linear manner.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
16
Which of the following is not a valid relational operator in Java
A) <=
B) >
C) ==
D) <>
E) !=
A) <=
B) >
C) ==
D) <>
E) !=
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
17
Which of the following best describes this code snippet? if (count != 400)
System.out.println("Hello World!");
A) If the variable count is exactly equal to 400, "Hello World" will be printed.
B) If the variable count is not equal to 400, "Hello World" will be printed.
C) If the variable count is close to, but not greater than, 400, "Hello World" will be printed.
D) If the variable count is exactly equal to 399 or 401, "Hello World" will be printed.
E) This code will not compile.
System.out.println("Hello World!");
A) If the variable count is exactly equal to 400, "Hello World" will be printed.
B) If the variable count is not equal to 400, "Hello World" will be printed.
C) If the variable count is close to, but not greater than, 400, "Hello World" will be printed.
D) If the variable count is exactly equal to 399 or 401, "Hello World" will be printed.
E) This code will not compile.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
18
The ___________________ statement causes current iteration of a loop to stop and the condition to be evaluated again, either stopping the loop or causing the next iteration.
A) stop
B) halt
C) continue
D) break
E) switch
A) stop
B) halt
C) continue
D) break
E) switch
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
19
The following snippet of code will not compile because the second part of the if statement needs to be on the second line.
if(a <
b) System.out.println("a is less than b");
if(a <
b) System.out.println("a is less than b");
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
20
A _______________ loop always executes its loop body at least once.
A) while
B) for
C) do
D) repeat
E) There are no loop structures with this property in Java.
A) while
B) for
C) do
D) repeat
E) There are no loop structures with this property in Java.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
21
What is wrong with the following snippet of code? Rewrite it so it produces the correct output.
if(a <
b)
if(a ==
c)
System.out.println("a < b and a == c");
else
System.out.println("a is not less than b");
if(a <
b)
if(a ==
c)
System.out.println("a < b and a == c");
else
System.out.println("a is not less than b");
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
22
Write a code fragment that determines how many times the character 'A' appears in a String object called name.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
23
An infinite loop is a compile-time error.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
24
Write a code fragment that allows a user to continue inputting numbers until she enters a sentinel value of 0. Then print the sum of all the numbers she entered. You may assume that a Scanner object called input has already been created.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
25
A Scanner object can use delimiters other than a space.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
26
Rewrite the following code fragment using a for loop instead of a while loop.
int i = 0;
while(i < 50) {
System.out.println(i);
i+=2;
}
int i = 0;
while(i < 50) {
System.out.println(i);
i+=2;
}
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
27
The following code compiled, but while running it the program appears to hang (e.g. nothing happens). This is a clear sign of an infinite loop. What part of this code fragment caused the infinite loop?
while(i < 50); {
System.out.println(i);
i+=2;
}
while(i < 50); {
System.out.println(i);
i+=2;
}
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
28
It is possible to implement a switch statement using if statements.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
29
Write a switch statement that switches on an integer variable named val. If val is 2 or 15, then output "Hello World." For all other values, output "Goodbye World."
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
30
Write a short code fragment that uses a while loop to verify that the user enters a positive integer as input. You may assume that a Scanner object called input has already been created.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
31
Rewrite the following for loop as a while loop.
for(int i = 0; i < MAX; i++)
//loop body
for(int i = 0; i < MAX; i++)
//loop body
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
32
What is output by the following code fragment?
int num = 0;
int max = 10;
while(num < max) {
System.out.println(num);
num += 2;
}
int num = 0;
int max = 10;
while(num < max) {
System.out.println(num);
num += 2;
}
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
33
Write a for loop to print the even numbers from 1 to 100 (inclusive).
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
34
The relational operators should not be used to test the equality of objects.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
35
Write a snippet of code that determines which of two integer variables, intOne and intTwo, contains a larger number, and print out the larger one. If they are equal, the output should say that.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
36
The Scanner object can be used to read text files.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
37
Assume numOne and numTwo are integers. How many times might the following code print out grape? Explain your answer.
if(numOne > numTwo && numOne < numTwo)
System.out.println("grape");
else {
if(numOne == numTwo) {
System.out.println("grape");
if(numOne > numTwo)
System.out.println("grape");
}//end if numOne == numTwo
}//end else
if(numOne > numTwo && numOne < numTwo)
System.out.println("grape");
else {
if(numOne == numTwo) {
System.out.println("grape");
if(numOne > numTwo)
System.out.println("grape");
}//end if numOne == numTwo
}//end else
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
38
Write a short application that takes in a String from the user and prints it out backwards.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
39
Write a do loop that verifies that the user enters an odd value. You may assume that a Scanner object called input has already been created.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
40
Using a while loop, write a code fragment that computes the sum of the first n natural numbers (i.e. 1, 2, 3, 4, ... n), where n is an integer variable already defined in the program.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck