1.
In reference to for-each loops, which of the following statements is true? (4 points)
1. for-each loops can be rewritten as for loops
2. an index variable is NOT explicitly coded
3. an ArrayIndexOutOfBoundsException will never be thrown
- I and II only
- I and III only
- II and III only
- I, II, and III
- None of the statements are true
2.
In reference to for-each loops, which of the following statements is true? (4 points)
1. for-each loops can never be rewritten as for loops
2. an index variable is NOT explicitly coded
3. an ArrayIndexOutOfBoundsException will never be thrown
- I and II only
- I and III only
- II and III only
- I, II, and III
- None of the statements are true
3.
In reference to for-each loops, which of the following statements is true? (4 points)
1. for-each loops can be rewritten as for loops
2. an index variable is NOT explicitly coded
3. an ArrayIndexOutOfBoundsException will always be thrown
- I and II only
- I and III only
- II and III only
- I, II, and III
- None of the statements are true
4.
Which is an inappropriate use of a for-each loop? (4 points)
1. when all values in an array need to be traversed
2. when values need to be assigned to an array
3. when an index variable is required to complete a task
- I only
- II only
- III only
- II and III only
- I, II, and III
5.
Which is an inappropriate use of a for-each loop? (4 points)
1. when an array needs to be traversed in reverse order
2. when values need to be assigned to an array
3. when an index variable is required to complete a task
- I only
- II only
- III only
- II and III only
- I, II, and III
6.
Consider the following code segment. What is the value of x after execution of the code? (4 points)
double [] numList = {4.17, 3.11, 2.46, 1.57};
int x = 0;
for(double n : numList)
{
x += (int)n − 1;
}
- 6
- 7
- 7.31
- 8
- 14
- 15
7.
What is the output after the following code segment is executed? (4 points)
String[] mascots = { "Knights", "Seminoles", "Bulls", "Gators" };
int n = −1;
String str = "";
for (String m : mascots)
{
n = m.indexOf("i") + 2;
str += m.substring(n);
}
System.out.println(str);
- houa
- tslesllstors
- htsolesullsators
- ghtsnolesBullsGators
- StringIndexOutOfBoundsException occurs
8.
What is the output after the following code segment is executed? (4 points)
String[] mascots = { "Knights", "Seminoles", "Bulls", "Gators" };
int n = −1;
String str = "";
for (String m : mascots)
{
n = m.indexOf("l") + 2;
str += m.substring(n);
}
System.out.println(str);
- nssa
- ightstors
- nightsssators
- KnightseslsGators
- StringIndexOutOfBoundsException occurs
9.
What is the output after the following code segment is executed? (4 points)
String[] mascots = { "Knights", "Seminoles", "Bulls", "Gators" };
int n = −1;
String str = "";
for (String m : mascots)
{
n = m.indexOf("o") + 2;
str += m.substring(n);
}
System.out.println(str);
- KlBr
- nightsesullss
- nightsssators
- KnightslesBullsrs
- StringIndexOutOfBoundsException occurs
10.
Assume an array of integers named arrNum has been properly declared and initialized. Which of these code segments will result in displaying the same output? (4 points)
1.
int i = 0;
while (i < arrNum.length)
{
System.out.println(arrNum[i]);
i++;
}
2.
for (int i = 0; i <= arrNum.length; i++)
{
System.out.println(arrNum[i]);
}
3.
for (int a : arrNum)
{
System.out.println(a);
}
- I and II only
- II and III only
- I and III only
- All three print the same results.
- All three print different results.