nerdexam
Oracle

1Z0-803 · Question #178

Given: What is the result?

The correct answer is A. 2 4 6 8. The program iterates through numbers from 1 up to (but not including) 10 and conditionally prints only the even numbers encountered within that range.

Using Loop Constructs

Question

Given:

What is the result?

Options

  • A2 4 6 8
  • B2 4 6 8 9
  • C1 3 5 7
  • D1 3 5 7 9

How the community answered

(25 responses)
  • A
    76% (19)
  • B
    8% (2)
  • C
    4% (1)
  • D
    12% (3)

Why each option

The program iterates through numbers from 1 up to (but not including) 10 and conditionally prints only the even numbers encountered within that range.

A2 4 6 8Correct

The loop initializes 'i' to 1 and continues as long as 'i' is less than 10. Inside the loop, an 'if' condition checks if 'i' is an even number using the modulo operator ('i % 2 == 0'), printing only '2', '4', '6', and '8' before the loop terminates.

B2 4 6 8 9

This choice includes '9', which is an odd number and would not be printed by the 'if (i % 2 == 0)' condition.

C1 3 5 7

This choice includes only odd numbers, which are excluded by the 'if (i % 2 == 0)' condition.

D1 3 5 7 9

This choice includes only odd numbers, which are excluded by the 'if (i % 2 == 0)' condition, and also includes '9' which is odd.

Concept tested: Loop iteration with conditional logic

Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

Topics

#looping#conditional logic#output prediction

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice