When you take the actual Advanced Placement examination, you will have to work with paper and pencil only, without the possibility of running any code with the help of a Java compiler. To prepare yourself for the exam, you should work on this test using only paper and pencil. Complete each question carefully, and press its Submit button to have your answer graded.
Value:1 point.
Which of the following statements best justifies using a for-each loop in place of a for loop?
A for-each loop is more readable and concise and its use will likely lead to fewer programming errors.
Afor loop can't be used on arrays.
A for-each loop makes it easier to control the order in which elements of an array are referenced.
A for loop generally takes a longer time to execute.
A for-each loop compiles to smaller code than a for loop.
Test 9 (1)
ABCDE CorrectExcellent !!!
Score: 1 / 1
Submitted: Monday, November 4, 2013 9:01am
Answer Key
The following model answer has been provided to you by the grader. Carefully compare your answer with the one provided here.
(A) A for-each loop is more readable and concise and its use will likely lead to fewer programming errors.
Consider the following code fragment:
int[] r = { 9, 2, 4, 3, 7, 5, 0, 1 };
for ( int t : r )
{
t = t + 1;
}
Value: 1 point.
Which of the following is a true statement?
The code will execute and the elements of the array r will be unchanged.
The code will execute and each element of the array r will be incremented.
The code will execute and each element of the array r will be the sum of the previous elements.
The code will execute and each element t of the array r will be the sum of the first t integers.
The code will generate an error.
Test 9 (2)
ABCDE CorrectExcellent !!!
Score: 1 / 1
Submitted: Monday, November 4, 2013 9:08am
Answer Key
The following model answer has been provided to you by the grader. Carefully compare your answer with the one provided here.
(A) The code will execute and the elements of the array r will be unchanged.
The for-loop assigns the value of each element of r in turn to the loop variable, t. The body of the loop then increments t. This has no effect whatsoever on r. No new values are assigned to its elements.
Value: 1 point.
What is output by the following code fragment? (You may assume that the for-each loop references the array elements in index order.)
String[] p = { "A", "B", "C", "D" };
String b = "";
for ( String q : p )
b = q + b;
System.out.println( b );
"ABCD"
"DCBA"
"qqqq"
"bbbb"
"" (the empty string)
Test 9 (3)
ABCDE CorrectExcellent !!!
Score: 1 / 1
Submitted: Monday, November 4, 2013 9:10am
Answer Key
The following model answer has been provided to you by the grader. Carefully compare your answer with the one provided here.
(B) "DCBA".
Consider the following code fragment:
String[] cities = new String[ 10 ];
// missing code that inserts a String in each element of this array
for ( String city : cities )
{
if ( city.substring( 0, 1 ).equals( "C" ) )
System.out.println( city );
}
Value: 1 point.
Assuming that the above for-each loop references the array elements in index order, which of the following code fragments result in the same output as the fragment above?
String[] cities = new String[ 10 ];
// missing code that inserts a String in each element of this array
for ( int i = 0 ; i < cities.length ; i++ )
{
String city = cities[ i ];
if ( city.substring( 0, 1 ).equals( "C" ) )
System.out.println( city );
}
String[] cities = new String[ 10 ];
// missing code that inserts a String in each element of this array
for ( int i = 0 ; i < cities.length ; i++ )
{
if ( cities[ i ].substring( 0, 1 ).equals( "C" ) )
System.out.println( cities[ i ] );
}
String[] cities = new String[ 10 ];
// missing code that inserts a String in each element of this array
int i = 0;
while ( i < cities.length )
{
String city = cities[ i ];
if ( city.substring( 0, 1 ).equals( "C" ) )
System.out.println( city );
i++;
}
I only
II only
I and II only
I, II, and III
None of them
Test 9 (4)
ABCDE CorrectExcellent !!!
Score: 1 / 1
Submitted: Monday, November 4, 2013 9:02am
Answer Key
The following model answer has been provided to you by the grader. Carefully compare your answer with the one provided here.
(D) I, II, and III.
Value: 1 point.
Which of the following statements are true?
for loops cannot be used for iterating over arrays.
If the elements of an array of Strings are not in alphabetical order, then it is always better to use a for loop when iterating over the elements of the array.
All for loops should be replaced by for-each loops.
I only
II only
III only
I, II, and III
None of them
Test 9 (5)
ABCDE CorrectExcellent !!!
Score: 1 / 1
Submitted: Monday, November 4, 2013 9:04am
Answer Key
The following model answer has been provided to you by the grader. Carefully compare your answer with the one provided here.
(E) None of them.
Value: 1 point.
What is the value of t after execution of the following code has finished?
double[] a = { 3.5, -1.0, 7.8 };
int t = 0;
for ( double b : a )
{
t += (int)b;
}
9
10
11
10.3
None of the above — a possible loss of precision error occurs.
Test 9 (6)
ABCDE CorrectExcellent !!!
Score: 1 / 1
Submitted: Monday, November 4, 2013 9:14am
Answer Key
The following model answer has been provided to you by the grader. Carefully compare your answer with the one provided here.
(A) 9.
The final value of t is the sum of the results of casting each element of the array a to an int, that is, the sum of 3, −1, and 7.
Value: 1 point.
What can be said about the value of d after the comment is replaced by an actual initialization statement and the resulting code is executed?
String [] a;
// code to initialize a
boolean d = false;
for ( String b : a )
{
for ( String c : a )
{
if ( b.equals( c ) )
d = true;
}
}
The value is true, no matter what Strings (if any) the array a contains.
The value is false, no matter what Strings (if any) the array a contains.
The value is true if a contains a duplicate element; otherwise it is false.
The value is false if a contains a duplicate element; otherwise it is true.
The value is true if a contains at least one element; otherwise it is false.
Test 9 (7)
ABCDE CorrectExcellent !!!
Score: 1 / 1
Submitted: Monday, November 4, 2013 9:07am
Answer Key
The following model answer has been provided to you by the grader. Carefully compare your answer with the one provided here.
(E) The value is true if a contains at least one element; otherwise it is false.
If a contains at least one element, then at some point the outer loop variable b and the inner loop variable c will both reference the same element of a. At that point, the value of b.equals( c ) will be true and so the value true will be assigned to d. Since there is nothing in the code that will ever set this value back to false, the final value of d in such a case will be true.
On the other hand, if a contains no elements, then the nested for-each loops result in no activity at all, and the value of d remains in its initial state, that is, false.
Value: 1 point.
Assuming that the for-each loops reference arrays in index order, what is the output from the following code?
int[][] b = { {1,2}, {3,4} };
for ( int[] c : b )
for ( int y : c )
System.out.print( y );
1234
4321
3412
2143
There is no output; an error occurs.
Test 9 (8)
ABCDE CorrectExcellent !!!
Score: 1 / 1
Submitted: Monday, November 4, 2013 4:50pm
Answer Key
The following model answer has been provided to you by the grader. Carefully compare your answer with the one provided here.
(A) 1234.
Value: 1 point.
What is the value of a after the following code executes?
Main.java:13: error: ';' expected
Small logo Program Control
^
Main.java:13: error: ';' expected
Small logo Program Control
^
Main.java:14: error: not a statement
Test 9
^
Main.java:14: error: ';' expected
Test 9
^
Main.java:15: error: ';' expected
When you take the actual Advanced Placement examination, you will have to work with paper and pencil only, without the possibility of running any code with the help of a Java compiler. To prepare yourself for the exam, you should work on this test using only paper and pencil. Complete each question carefully, and press its Submit button to have your answer graded.
^
Main.java:15: error: ';' expected
When you take the actual Advanced Placement examination, you will have to work with paper and pencil only, without the possibility of running any code with the help of a Java compiler. To prepare yourself for the exam, you should work on this test using only paper and pencil. Complete each question carefully, and press its Submit button to have your answer graded.
^
Main.java:15: error: ';' expected
When you take the actual Advanced Placement examination, you will have to work with paper and pencil only, without the possibility of running any code with the help of a Java compiler. To prepare yourself for the exam, you should work on this test using only paper and pencil. Complete each question carefully, and press its Submit button to have your answer graded.
^
Main.java:15: error: ';' expected
When you take the actual Advanced Placement examination, you will have to work with paper and pencil only, without the possibility of running any code with the help of a Java compiler. To prepare yourself for the exam, you should work on this test using only paper and pencil. Complete each question carefully, and press its Submit button to have your answer graded.
^
Main.java:15: error: ';' expected
When you take the actual Advanced Placement examination, you will have to work with paper and pencil only, without the possibility of running any code with the help of a Java compiler. To prepare yourself for the exam, you should work on this test using only paper and pencil. Complete each question carefully, and press its Submit button to have your answer graded.
^
Main.java:15: error: ';' expected
When you take the actual Advanced Placement examination, you will have to work with paper and pencil only, without the possibility of running any code with the help of a Java compiler. To prepare yourself for the exam, you should work on this test using only paper and pencil. Complete each question carefully, and press its Submit button to have your answer graded.
^
Main.java:15: error: ';' expected
When you take the actual Advanced Placement examination, you will have to work with paper and pencil only, without the possibility of running any code with the help of a Java compiler. To prepare yourself for the exam, you should work on this test using only paper and pencil. Complete each question carefully, and press its Submit button to have your answer graded.
^
Main.java:15: error: ';' expected
When you take the actual Advanced Placement examination, you will have to work with paper and pencil only, without the possibility of running any code with the help of a Java compiler. To prepare yourself for the exam, you should work on this test using only paper and pencil. Complete each question carefully, and press its Submit button to have your answer graded.
^
Main.java:15: error: not a statement
When you take the actual Advanced Placement examination, you will have to work with paper and pencil only, without the possibility of running any code with the help of a Java compiler. To prepare yourself for the exam, you should work on this test using only paper and pencil. Complete each question carefully, and press its Submit button to have your answer graded.
^
Main.java:15: error: ';' expected
When you take the actual Advanced Placement examination, you will have to work with paper and pencil only, without the possibility of running any code with the help of a Java compiler. To prepare yourself for the exam, you should work on this test using only paper and pencil. Complete each question carefully, and press its Submit button to have your answer graded.
^
Main.java:15: error: ';' expected
When you take the actual Advanced Placement examination, you will have to work with paper and pencil only, without the possibility of running any code with the help of a Java compiler. To prepare yourself for the exam, you should work on this test using only paper and pencil. Complete each question carefully, and press its Submit button to have your answer graded.
^
Main.java:15: error: ';' expected
When you take the actual Advanced Placement examination, you will have to work with paper and pencil only, without the possibility of running any code with the help of a Java compiler. To prepare yourself for the exam, you should work on this test using only paper and pencil. Complete each question carefully, and press its Submit button to have your answer graded.
^
Main.java:15: error: ';' expected
When you take the actual Advanced Placement examination, you will have to work with paper and pencil only, without the possibility of running any code with the help of a Java compiler. To prepare yourself for the exam, you should work on this test using only paper and pencil. Complete each question carefully, and press its Submit button to have your answer graded.
^
Main.java:15: error: ';' expected
When you take the actual Advanced Placement examination, you will have to work with paper and pencil only, without the possibility of running any code with the help of a Java compiler. To prepare yourself for the exam, you should work on this test using only paper and pencil. Complete each question carefully, and press its Submit button to have your answer graded.
^
Main.java:15: error: ';' expected
When you take the actual Advanced Placement examination, you will have to work with paper and pencil only, without the possibility of running any code with the help of a Java compiler. To prepare yourself for the exam, you should work on this test using only paper and pencil. Complete each question carefully, and press its Submit button to have your answer graded.
^
Main.java:15: error: ';' expected
When you take the actual Advanced Placement examination, you will have to work with paper and pencil only, without the possibility of running any code with the help of a Java compiler. To prepare yourself for the exam, you should work on this test using only paper and pencil. Complete each question carefully, and press its Submit button to have your answer graded.
^
Main.java:15: error: ';' expected
When you take the actual Advanced Placement examination, you will have to work with paper and pencil only, without the possibility of running any code with the help of a Java compiler. To prepare yourself for the exam, you should work on this test using only paper and pencil. Complete each question carefully, and press its Submit button to have your answer graded.
^
Main.java:15: error: ';' expected
When you take the actual Advanced Placement examination, you will have to work with paper and pencil only, without the possibility of running any code with the help of a Java compiler. To prepare yourself for the exam, you should work on this test using only paper and pencil. Complete each question carefully, and press its Submit button to have your answer graded.
^
Main.java:15: error: not a statement
When you take the actual Advanced Placement examination, you will have to work with paper and pencil only, without the possibility of running any code with the help of a Java compiler. To prepare yourself for the exam, you should work on this test using only paper and pencil. Complete each question carefully, and press its Submit button to have your answer graded.
^
Main.java:15: error: ';' expected
When you take the actual Advanced Placement examination, you will have to work with paper and pencil only, without the possibility of running any code with the help of a Java compiler. To prepare yourself for the exam, you should work on this test using only paper and pencil. Complete each question carefully, and press its Submit button to have your answer graded.
^
Main.java:15: error: '(' expected
When you take the actual Advanced Placement examination, you will have to work with paper and pencil only, without the possibility of running any code with the help of a Java compiler. To prepare yourself for the exam, you should work on this test using only paper and pencil. Complete each question carefully, and press its Submit button to have your answer graded.
^
Main.java:15: error: ';' expected
When you take the actual Advanced Placement examination, you will have to work with paper and pencil only, without the possibility of running any code with the help of a Java compiler. To prepare yourself for the exam, you should work on this test using only paper and pencil. Complete each question carefully, and press its Submit button to have your answer graded.
^
Main.java:15: error: ';' expected
When you take the actual Advanced Placement examination, you will have to work with paper and pencil only, without the possibility of running any code with the help of a Java compiler. To prepare yourself for the exam, you should work on this test using only paper and pencil. Complete each question carefully, and press its Submit button to have your answer graded.
^
Main.java:15: error: not a statement
When you take the actual Advanced Placement examination, you will have to work with paper and pencil only, without the possibility of running any code with the help of a Java compiler. To prepare yourself for the exam, you should work on this test using only paper and pencil. Complete each question carefully, and press its Submit button to have your answer graded.
^
Main.java:15: error: ')' expected
When you take the actual Advanced Placement examination, you will have to work with paper and pencil only, without the possibility of running any code with the help of a Java compiler. To prepare yourself for the exam, you should work on this test using only paper and pencil. Complete each question carefully, and press its Submit button to have your answer graded.
^
Main.java:15: error: not a statement
When you take the actual Advanced Placement examination, you will have to work with paper and pencil only, without the possibility of running any code with the help of a Java compiler. To prepare yourself for the exam, you should work on this test using only paper and pencil. Complete each question carefully, and press its Submit button to have your answer graded.
^
Main.java:15: error: ';' expected
When you take the actual Advanced Placement examination, you will have to work with paper and pencil only, without the possibility of running any code with the help of a Java compiler. To prepare yourself for the exam, you should work on this test using only paper and pencil. Complete each question carefully, and press its Submit button to have your answer graded.
^
Main.java:15: error: ';' expected
When you take the actual Advanced Placement examination, you will have to work with paper and pencil only, without the possibility of running any code with the help of a Java compiler. To prepare yourself for the exam, you should work on this test using only paper and pencil. Complete each question carefully, and press its Submit button to have your answer graded.
^
Main.java:15: error: ';' expected
When you take the actual Advanced Placement examination, you will have to work with paper and pencil only, without the possibility of running any code with the help of a Java compiler. To prepare yourself for the exam, you should work on this test using only paper and pencil. Complete each question carefully, and press its Submit button to have your answer graded.
^
Main.java:15: error: ';' expected
When you take the actual Advanced Placement examination, you will have to work with paper and pencil only, without the possibility of running any code with the help of a Java compiler. To prepare yourself for the exam, you should work on this test using only paper and pencil. Complete each question carefully, and press its Submit button to have your answer graded.
^
Main.java:15: error: ';' expected
When you take the actual Advanced Placement examination, you will have to work with paper and pencil only, without the possibility of running any code with the help of a Java compiler. To prepare yourself for the exam, you should work on this test using only paper and pencil. Complete each question carefully, and press its Submit button to have your answer graded.
^
Main.java:15: error: ';' expected
When you take the actual Advanced Placement examination, you will have to work with paper and pencil only, without the possibility of running any code with the help of a Java compiler. To prepare yourself for the exam, you should work on this test using only paper and pencil. Complete each question carefully, and press its Submit button to have your answer graded.
^
Main.java:15: error: ';' expected
When you take the actual Advanced Placement examination, you will have to work with paper and pencil only, without the possibility of running any code with the help of a Java compiler. To prepare yourself for the exam, you should work on this test using only paper and pencil. Complete each question carefully, and press its Submit button to have your answer graded.
^
Main.java:15: error: ';' expected
When you take the actual Advanced Placement examination, you will have to work with paper and pencil only, without the possibility of running any code with the help of a Java compiler. To prepare yourself for the exam, you should work on this test using only paper and pencil. Complete each question carefully, and press its Submit button to have your answer graded.
^
Main.java:15: error: ';' expected
When you take the actual Advanced Placement examination, you will have to work with paper and pencil only, without the possibility of running any code with the help of a Java compiler. To prepare yourself for the exam, you should work on this test using only paper and pencil. Complete each question carefully, and press its Submit button to have your answer graded.
^
Main.java:15: error: ';' expected
When you take the actual Advanced Placement examination, you will have to work with paper and pencil only, without the possibility of running any code with the help of a Java compiler. To prepare yourself for the exam, you should work on this test using only paper and pencil. Complete each question carefully, and press its Submit button to have your answer graded.
^
Main.java:15: error: not a statement
When you take the actual Advanced Placement examination, you will have to work with paper and pencil only, without the possibility of running any code with the help of a Java compiler. To prepare yourself for the exam, you should work on this test using only paper and pencil. Complete each question carefully, and press its Submit button to have your answer graded.
^
Main.java:16: error: ';' expected
Value: 1 point.
^
Main.java:17: error: ';' expected
Which of the following statements best justifies using a for-each loop in place of a for loop?
^
Main.java:17: error: ';' expected
Which of the following statements best justifies using a for-each loop in place of a for loop?
^
Main.java:17: error: ';' expected
Which of the following statements best justifies using a for-each loop in place of a for loop?
^
Main.java:17: error: ';' expected
Which of the following statements best justifies using a for-each loop in place of a for loop?
^
Main.java:17: error: not a statement
Which of the following statements best justifies using a for-each loop in place of a for loop?
^
Main.java:17: error: ';' expected
Which of the following statements best justifies using a for-each loop in place of a for loop?
^
Main.java:17: error: '(' expected
Which of the following statements best justifies using a for-each loop in place of a for loop?
^
Main.java:17: error: ';' expected
Which of the following statements best justifies using a for-each loop in place of a for loop?
^
Main.java:17: error: ';' expected
Which of the following statements best justifies using a for-each loop in place of a for loop?
^
Main.java:17: error: not a statement
Which of the following statements best justifies using a for-each loop in place of a for loop?
^
Main.java:17: error: ')' expected
Which of the following statements best justifies using a for-each loop in place of a for loop?
^
Main.java:17: error: not a statement
Which of the following statements best justifies using a for-each loop in place of a for loop?
^
Main.java:17: error: ';' expected
Which of the following statements best justifies using a for-each loop in place of a for loop?
^
Main.java:17: error: ';' expected
Which of the following statements best justifies using a for-each loop in place of a for loop?
^
Main.java:17: error: '(' expected
Which of the following statements best justifies using a for-each loop in place of a for loop?
^
Main.java:19: error: : expected
A for-each loop is more readable and concise and its use will likely lead to fewer programming errors.
^
Main.java:19: error: illegal start of expression
A for-each loop is more readable and concise and its use will likely lead to fewer programming errors.
^
Main.java:17: error: not a statement
Which of the following statements best justifies using a for-each loop in place of a for loop?
^
Main.java:19: error: ';' expected
A for-each loop is more readable and concise and its use will likely lead to fewer programming errors.
^
Main.java:19: error: ';' expected
A for-each loop is more readable and concise and its use will likely lead to fewer programming errors.
^
Main.java:19: error: not a statement
A for-each loop is more readable and concise and its use will likely lead to fewer programming errors.
^
Main.java:19: error: ')' expected
A for-each loop is more readable and concise and its use will likely lead to fewer programming errors.
^
Main.java:19: error: not a statement
A for-each loop is more readable and concise and its use will likely lead to fewer programming errors.
^
Main.java:19: error: ';' expected
A for-each loop is more readable and concise and its use will likely lead to fewer programming errors.
^
Main.java:19: error: ';' expected
A for-each loop is more readable and concise and its use will likely lead to fewer programming errors.
^
Main.java:19: error: ';' expected
A for-each loop is more readable and concise and its use will likely lead to fewer programming errors.
^
Main.java:19: error: ';' expected
A for-each loop is more readable and concise and its use will likely lead to fewer programming errors.
^
Main.java:19: error: ';' expected
A for-each loop is more readable and concise and its use will likely lead to fewer programming errors.
^
Main.java:19: error: ';' expected
A for-each loop is more readable and concise and its use will likely lead to fewer programming errors.
^
Main.java:19: error: ';' expected
A for-each loop is more readable and concise and its use will likely lead to fewer programming errors.
^
Main.java:19: error: not a statement
A for-each loop is more readable and concise and its use will likely lead to fewer programming errors.
^
Main.java:21: error: ';' expected
A for loop can't be used on arrays.
^
Main.java:21: error: '(' expected
A for loop can't be used on arrays.
^
Main.java:21: error: unclosed character literal
A for loop can't be used on arrays.
^
Main.java:21: error: ';' expected
A for loop can't be used on arrays.
^
Main.java:21: error: not a statement
A for loop can't be used on arrays.
^
Main.java:21: error: ')' expected
A for loop can't be used on arrays.
^
Main.java:21: error: not a statement
A for loop can't be used on arrays.
^
Main.java:21: error: ';' expected
A for loop can't be used on arrays.
^
Main.java:21: error: not a statement
A for loop can't be used on arrays.
^
Main.java:23: error: ';' expected
A for-each loop makes it easier to control the order in which elements of an array are referenced.
^
Main.java:23: error: '(' expected
A for-each loop makes it easier to control the order in which elements of an array are referenced.
^
Main.java:23: error: ';' expected
A for-each loop makes it easier to control the order in which elements of an array are referenced.
^
Main.java:23: error: ';' expected
A for-each loop makes it easier to control the order in which elements of an array are referenced.
^
Main.java:23: error: not a statement
A for-each loop makes it easier to control the order in which elements of an array are referenced.
^
Main.java:23: error: ')' expected
A for-each loop makes it easier to control the order in which elements of an array are referenced.
^
Main.java:23: error: not a statement
A for-each loop makes it easier to control the order in which elements of an array are referenced.
^
Main.java:23: error: ';' expected
A for-each loop makes it easier to control the order in which elements of an array are referenced.
^
Main.java:23: error: ';' expected
A for-each loop makes it easier to control the order in which elements of an array are referenced.
^
Main.java:23: error: ';' expected
A for-each loop makes it easier to control the order in which elements of an array are referenced.
^
Main.java:23: error: ';' expected
A for-each loop makes it easier to control the order in which elements of an array are referenced.
^
Main.java:23: error: ';' expected
A for-each loop makes it easier to control the order in which elements of an array are referenced.
^
Main.java:23: error: ';' expected
A for-each loop makes it easier to control the order in which elements of an array are referenced.
^
Main.java:23: error: ';' expected
A for-each loop makes it easier to control the order in which elements of an array are referenced.
^
Main.java:23: error: not a statement
A for-each loop makes it easier to control the order in which elements of an array are referenced.
^
Main.java:25: error: ';' expected
A for loop generally takes a longer time to execute.
^
Main.java:25: error: '(' expected
A for loop generally takes a longer time to execute.
^
Main.java:25: error: ';' expected
A for loop generally takes a longer time to execute.
^
100 errors