SG Unit6ProgressCheckMCQ 619f17e634aa49.619f17e71419a5.33413876
SG Unit6ProgressCheckMCQ 619f17e634aa49.619f17e71419a5.33413876
Which of the following code segments would correctly set the first two elements of array arr to 10 so that the
new value of array arr will be {10, 10, 3, 4, 5} ?
arr[0] = 10;
(A) arr[1] = 10;
arr[1] = 10;
(B) arr[2] = 10;
The following code segment appears in a method in the same class as transform.
/* missing code */
arr = transform(arr);
After executing the code segment, the array arr should contain {1, 0, 1, 0}. Which of the following can
be used to replace /* missing code */ so that the code segment works as intended?
3. Consider the following method, which is intended to return the number of strings of length greater than or equal to 3
in an array of String objects.
I. checkString(new String[]);
II. checkString(new String[0]);
III. String[] str = {"cat", "dog"};
checkString(str);
(A) II only
(B) III only
(C) I and III only
(D) II and III only
(E) I, II, and III
Which of the following represents the contents of arr after the code segment has been executed?
(A) {10, 20, 30, 70, 120}
(B) {10, 20, 50, 90, 50}
(B) 1
(C) 0
(D) -4
(E) Nothing is printed because the code segment causes a runtime error.
I.
for (int i = 0; i <= fruits.length; i++)
{
System.out.println(fruits[i]);
}
II.
for (int i = 0; i <= fruits.length - 1; i++)
{
System.out.println(fruits[i]);
}
III.
for (int i = 1; i <= fruits.length; i++)
{
System.out.println(fruits[i - 1]);
}
(A) I only
(B) II only
(C) I and III only
(D) II and III only
(E) I, II, and III
7. The Fibonacci numbers are a sequence of integers. The first two numbers are 1 and 1. Each subsequent number is
equal to the sum of the previous two integers. For example, the first seven Fibonacci numbers are 1, 1, 2, 3, 5, 8,
and 13.
The following code segment is intended to fill the fibs array with the first ten Fibonacci numbers. The code
segment does not work as intended.
Which of the following best identifies why the code segment does not work as intended?
Which of the following for loops produces the same output as the code segment?
for (int x : numbers)
{
(A) System.out.println(numbers[x]);
}
for (int x : numbers)
{
(B) System.out.println(numbers);
}
for (int x : numbers)
{
(C) System.out.println(x);
}
for (numbers : int x)
{
(D) System.out.println(numbers[x]);
}
for (numbers : int x)
{
(E) System.out.println(x);
}
The following code segment, which appears in a class other than Toy, prints the year each Toy object in
toyArray was first sold by its manufacturer. Assume that toyArray is a properly declared and initialized
array of Toy objects.
Which of the following could be used in place of the given code segment to produce the same output?
I.
for (int k = 0; k < toyArray.length; k++)
{
System.out.println(getYearFirstSold(k));
}
II.
for (int k = 0; k < toyArray.length; k++)
{
System.out.println(k.getYearFirstSold());
}
III.
for (int k = 0; k < toyArray.length; k++)
{
System.out.println(toyArray[k].getYearFirstSold());
}
(A) I only
(B) II only
(C) III only
(D) I and II
(E) II and III
I.
int[] arr = {1, 2, 3, 4, 5};
for (int x = 0; x < arr.length; x++)
{
System.out.print(arr[x + 3]);
}
II.
int[] arr = {1, 2, 3, 4, 5};
for (int x : arr)
{
System.out.print(x + 3);
}
Which of the following best describes the behavior of code segment I and code segment II ?
(A) Both code segment I and code segment II will print 45.
(B) Both code segment I and code segment II will print 45678.
Code segment I will cause an ArrayIndexOutOfBoundsException and code segment II will
(C)
print 45.
Code segment I will cause an ArrayIndexOutOfBoundsException and code segment II will
(D)
print 45678.
(E) Both code segment I and code segment II will cause an ArrayIndexOutOfBoundsException.
11. The code segment below is intended to set the boolean variable duplicates to true if the int array
arr contains any pair of duplicate elements. Assume that arr has been properly declared and initialized.
Which of the following can replace /* missing loop header */ so that the code segment works as intended?
12. In the code segment below, assume that the int array numArr has been properly declared and initialized. The
code segment is intended to reverse the order of the elements in numArr. For example, if numArr initially
contains {1, 3, 5, 7, 9}, it should contain {9, 7, 5, 3, 1} after the code segment executes.
Which of the following can be used to replace /* missing loop header */ so that the code segment works as
intended?
(A) for (int k = 0; k < numArr.length / 2; k++)
13. Consider the following code segment, which is intended to print the maximum value in an integer array values.
Assume that the array has been initialized properly and that it contains at least one element.
Which of the following should replace /* missing initial value */ so that the code segment will work as
intended?
(A) 0
(B) values[0]
(C) values[1]
(D) Integer.MIN_VALUE
(E) Integer.MAX_VALUE
14. Consider the following method, which is intended to return the index of the first negative integer in a given array of
integers.
What precondition is needed on the values array so that the method will work as intended?
(A) The array values must contain at least one negative integer.
(B) The array values must contain at least one nonnegative integer.
(C) The array values must contain at least one positive integer.
(D) No precondition is needed. The method will never work as intended.
(E) No precondition is needed. The method will always work as intended.