nerdexam
Oracle

1Z0-803 · Question #271

Given: public class TestLoop { public static void main(String[] args) { int array[] = {0, 1, 2, 3, 4}; int key = 3; for (int pos = 0; pos < array.length; ++pos) { if (array[pos] == key) { break; } } S

The correct answer is C. Compilation fails. The following line does not compile: System.out.print("Found " + key + "at " + pos); The variable pos is undefined at this line, as its scope is only valid in the for loop. Any variables created inside of a loop are LOCAL TO THE LOOP. Exam Questions, Study Guides, Practice Tests.

Using Loop Constructs

Question

Given:

public class TestLoop { public static void main(String[] args) { int array[] = {0, 1, 2, 3, 4}; int key = 3; for (int pos = 0; pos < array.length; ++pos) { if (array[pos] == key) { break; } } System.out.print("Found " + key + "at " + pos); } } What is the result?

Options

  • AFound 3 at 2
  • BFound 3 at 3
  • CCompilation fails
  • DAn exception is thrown at runtime

How the community answered

(42 responses)
  • A
    2% (1)
  • B
    2% (1)
  • C
    90% (38)
  • D
    5% (2)

Explanation

The following line does not compile: System.out.print("Found " + key + "at " + pos); The variable pos is undefined at this line, as its scope is only valid in the for loop. Any variables created inside of a loop are LOCAL TO THE LOOP. Exam Questions, Study Guides, Practice Tests. Lead the way to help you pass any IT Certification exams, 100% Pass Guaranteed or Full Refund. Especially Cisco, CompTIA, Citrix, EMC, HP, Oracle, VMware, Juniper, Check Point, LPI, Nortel, EXIN and so on. Our Slogan: First Test, First Pass. Help you to pass any IT Certification exams at the first try. You can reach us at any of the email addresses listed below. Any problems about IT certification or our products, you could rely upon us, we will give you satisfactory answers in 24 hours.

Topics

#For loop#Variable scope#Compilation error#Loop variables

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice