1Z0-803 · Question #132
Given: public class App { public static void main(String[] args) { char[] arr = {'A', 'e', 'I', 'O', 'u'}; int count = 0; for (int i = 0; i < arr.length; i++) { switch (arr[i]) { case 'A': continue; c
The correct answer is B. Total match found: 2. The code iterates through a character array, using a switch statement to conditionally increment a counter based on character matches, with continue statements affecting loop progression and break statements affecting switch execution.
Question
Given:
public class App { public static void main(String[] args) { char[] arr = {'A', 'e', 'I', 'O', 'u'}; int count = 0; for (int i = 0; i < arr.length; i++) { switch (arr[i]) { case 'A':
continue; case 'a':
count++; break; case 'E':
count++; break; case 'I':
count++; continue; case 'O':
count++; break; case 'U':
count++; } } System.out.print("Total match found: " + count); } } What is the result?
Options
- ATotal match found: 1
- BTotal match found: 2
- CTotal match found: 3
- DTotal match found: 5
How the community answered
(35 responses)- A3% (1)
- B74% (26)
- C6% (2)
- D17% (6)
Why each option
The code iterates through a character array, using a switch statement to conditionally increment a counter based on character matches, with `continue` statements affecting loop progression and `break` statements affecting switch execution.
This result would occur if only one of the matching characters ('I' or 'O') caused an increment, while the other was bypassed or did not match.
The program initializes `count` to 0. When `arr[i]` is 'A', `continue` skips the iteration. When `arr[i]` is 'e', no match occurs. When `arr[i]` is 'I', `count` becomes 1, then `continue` skips the iteration. When `arr[i]` is 'O', `count` becomes 2, then `break` exits the switch. When `arr[i]` is 'u', no match occurs. The final value of `count` is 2.
This result would require three characters to increment `count`, which does not happen due to the `continue` statements for 'A' and 'I' only incrementing for 'I'.
This result implies that all five characters in the array ('A', 'e', 'I', 'O', 'u') would contribute to the count, which is incorrect because 'A', 'e', and 'u' do not increment `count`.
Concept tested: Java switch statement, loop control (break, continue)
Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
Topics
Community Discussion
No community discussion yet for this question.