Create program flow control constructs including if/else, switch statements and expressions, loops, and break and continue statements.
1. The correct answer is A.
Explanation:
x is between 5 and 20
x
is 10, which satisfies both conditions in the nested if
statements (x > 5
and x < 20
). Therefore, the program prints "x is between 5 and 20"
.x is 5 or less
x
is 10, which does not satisfy the condition x <= 5
in the else
block. Therefore, this message will not be printed.x is greater than 20
x
is 10, which does not satisfy the condition x > 20
. Therefore, this message will not be printed.x
satisfies the conditions within the nested if
statements, leading to the output "x is between 5 and 20"
.2. The correct answer is C.
Explanation:
y
is declared inside the first if
statement and is not accessible outside its block. Therefore, trying to print y
outside its scope results in a compilation error.z
is declared inside the second if
statement and is not accessible outside its block. Therefore, attempting to use z
outside its scope results in a compilation error.a
is declared outside the if
statement, so it is accessible both inside and outside the if
block. Reassigning a inside the if
block is allowed.3. The correct answer is C.
Explanation:
Weekend
dayOfWeek
is 3, which does not match cases 1 or 7, so it does not print "Weekend"
.Invalid day
dayOfWeek
matches one of the specific cases (2, 3, 4, 5, or 6).Weekday
dayOfWeek
is 3, which matches case 3. Therefore, the variable dayType
is set to "Weekday"
, and this value is printed."Weekday"
based on the given dayOfWeek
value.4. The correct answer is B.
Explanation:
A
score
is 85, which does not match the cases for 90 or 100. Therefore, it does not print "A"
.F
score
doesn’t match any of the other case
statements.switch
expression correctly, compiling without errors.B
score
is 85, which doesn’t match the case for 80 or 89."F"
based on the given score
value.5. The correct answer is B.
Explanation:
2
count
is incremented until it reaches 3. The labeled break
statement breaks out of the outer loop when count
equals 3.3
count
is incremented inside the inner while
loop. When count
reaches 3, the labeled break
statement (break outerLoop
) is executed, causing the control to exit the outer loop. Therefore, count
is 3 when printed.4
count
to 4 because the labeled break
statement exits the loop when count
is 3.5
count
to 5 because the labeled break
statement exits the loop when count
is 3.6. The correct answer is C.
Explanation:
5
10
15
i
to sum
. The calculations are as follows: 1 + 2 + 3 + 4 + 5 = 15.20
7. The correct answer is A.
Explanation:
9
continue
statement skips the current iteration when the number is even (num % 2 == 0
). The odd numbers in the array are 1, 3, and 5. Their sum is 1 + 3 + 5 = 9.10
12
15
continue
statement causes the loop to skip adding the even numbers.Do you like what you read? Would you consider?
Do you have a problem or something to say?