Given:
Which loop incurs a compile time error?
A. the loop starting line 11
B. the loop starting line 7
C. the loop starting line 14
D. the loop starting line 3
Given:
Which loop incurs a compile time error?
A. the loop starting line 11
B. the loop starting line 7
C. the loop starting line 14
D. the loop starting line 3
I didn’t meant declared, but what I meant was that iter2 och iter don’t apply because they are in another scope..then “iter” cannot be found at line 14…sorry my English
Definitly C, try the code I pasted. You have to focus on the scopes see the braceles “{“. The first 2 loops are in an own scope. The third and fourth are in another scope, then iter.hasnext at line 14 has already been declared, earlier
public void foo(Object obj) {}
public void testAll()
{
{
Iterator iter = List.of(1,2,3).iterator();
while (iter.hasNext())
{
foo(iter.next());
}
Iterator iter2 = List.of(1,2,3).iterator();
while (iter.hasNext())
{
foo(iter2.next());
}
}
//C
for (Iterator iter = List.of(1,2,3).iterator(); iter.hasNext();)
{
foo(iter.next());
}
for (Iterator iter2 = List.of(1,2,3).iterator(); iter.hasNext();)
{
foo(iter2.next());
}
}
A – is the right answer, because its outside the method and will not run.
B- variable iter not exists
C.
iter cannot be resolved