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.
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)- A2% (1)
- B2% (1)
- C90% (38)
- D5% (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
Community Discussion
No community discussion yet for this question.