AP Comp Science MCQ Practice 1
AP Comp Science MCQ Practice 1
return x;
}
Assume that the array nums has been declared and initialized as follows.
3. Which of the following changes to SomeClass will allow other classes to access but not modify the value
of myC ?
(A) Make myC public.
(B) Include the method:
public int getC()
{ return myC; }
(C) Include the method:
private int getC()
{ return myC; }
(D) Include the method:
public void getC(int x)
{ x = myC; }
(E) Include the method:
private void getC(int x)
{ x = myC; }
int x = 7;
int y = 3;
return seq;
}
Which of the following should replace /* missing code */ so that almostEqual will work as
intended?
(A) return (d1 - d2) <= tolerance;
(B) return ((d1 + d2) / 2) <= tolerance;
(C) return (d1 - d2) >= tolerance;
(D) return ((d1 + d2) / 2) >= tolerance;
(E) return Math.abs(d1 - d2) <= tolerance;
// There may be instance variables, constructors, and methods that are not shown.
}
Which of the following statements is the most appropriate for changing the name of student from
"Thomas" to "Tom" ?
(A) student = new Person("Tom", 1995);
(B) student.myName = "Tom";
(C) student.getName("Tom");
(D) student.setName("Tom");
(E) Person.setName("Tom");
public Student()
{ /* implementation not shown */ }
// No other constructors
}
return sum;
}
Which of the following statements should be used to replace /* missing code */ so that sumArray
will work as intended?
(A) sum = key[i];
(B) sum += key[i - 1];
(C) sum += key[i];
(D) sum += sum + key[i - 1];
(E) sum += sum + key[i];
Consider the following instance variable and methods. You may assume that data has been initialized with
length > 0. The methods are intended to return the index of an array element equal to target, or -1 if no such
element exists.
if (data[last] == target)
return last;
else
return seqSearchRecHelper(target, last - 1);
}
10. For which of the following test cases will the call seqSearchRec(5) always result in an error?
(A) I only
(B) II only
(C) III only
(D) I and II only
(E) I, II, and III
11. Which of the following should be used to replace // Line 1 in seqSearchRecHelper so that
seqSearchRec will work as intended?
(A) if (last <= 0)
return -1;
(B) if (last < 0)
return -1;
(C) if (last < data.length)
return -1;
(D) while (last < data.length)
(E) while (last >= 0)
return output;
}
return sum;
}
// There may be instance variables, constructors, and methods that are not shown.
}
Which of the following can be used to replace /* expression */ so that getTotalMileage returns
the total of the miles traveled for all vehicles in the fleet?
(A) getMileage(v)
(B) myVehicles[v].getMileage()
(C) Vehicle.get(v).getMileage()
(D) myVehicles.get(v).getMileage()
(E) v.getMileage()
Which of the following can be used to replace /* missing code */ so that isSorted will work
as intended?
(A) I only
(B) II only
(C) III only
(D) I and II only
(E) I and III only
return result;
}
Which of the following expressions can be used to replace /* index */ so that append will work as
intended?
(A) j
(B) k
(C) k + a1.length - 1
(D) k + a1.length
(E) k + a1.length + 1
Which of the following represents the contents of arr as a result of executing the code segment?
(A) {1, 2, 3, 4, 5, 6, 7}
(B) {1, 2, 3, 5, 6, 7}
(C) {1, 2, 3, 5, 6, 7, 7}
(D) {1, 2, 3, 5, 6, 7, 8}
(E) {2, 3, 4, 5, 6, 7, 7}
while (j < k)
{
int x = nums[j];
nums[j] = nums[k];
nums[k] = x;
j++;
k--;
}
}
Which of the following describes what the method arrayMethod() does to the array nums?
(A) The array nums is unchanged.
(B) The first value in nums is copied to every location in the array.
(C) The last value in nums is copied to every location in the array.
(D) The method generates an ArrayIndexOutOfBoundsException.
(E) The contents of the array nums are reversed.
/** @return the element of 2-dimensional array mat whose value is closest to val */
public double findClosest(double[][] mat, double val)
{
double answer = mat[0][0];
double minDiff = Math.abs(answer - val);
for (double[] row : mat)
{
for (double num : row)
{
if ( /* missing code */ )
{
answer = num;
minDiff = Math.abs(num - val);
}
}
}
return answer;
}
Which of the following could be used to replace /* missing code */ so that findClosest will work as
intended?
(A) val - row[num] < minDiff
(B) Math.abs(num - minDiff) < minDiff
(C) val - num < 0.0
(D) Math.abs(num - val) < minDiff
(E) Math.abs(row[num] - val) < minDiff
Which of the following best explains why the code segment will not compile?
(A) Line 2 will not compile because variables of type Book may not refer to variables of type AudioBook.
(B) Line 4 will not compile because variables of type Book may only call methods in the Book class.
(C) Line 5 will not compile because the AudioBook class does not have a method named toString
declared or implemented.
(D) Line 6 will not compile because the statement is ambiguous. The compiler cannot determine which
length method should be called.
(E) Line 7 will not compile because the element at index 1 in the array named books may not have been
initialized.
Assume that animals has been instantiated and initialized with the following contents.
int row = 0;
int col = 0;
for (int value : oldArray)
{
newArray[row][col] = value;
row++;
if ((row % 3) == 0)
{
col++;
row = 0;
}
}
System.out.println(newArray[0][2]);
/** @return true if the width of this RBox is less than the width of other;
* false otherwise
*/
boolean smallerWidth(RBox other);
/** @return true if the depth of this RBox is less than the depth of other;
* false otherwise
*/
boolean smallerDepth(RBox other);
}
Which of the interfaces, if correctly implemented by a Box class, would be sufficient functionality for a user of
the Box class to determine if one Box can fit inside another?
(A) I only
(B) II only
(C) III only
(D) I and II only
(E) I, II, and III
Which of the following will correctly print all of the odd integers contained in arr but none of the even
integers contained in arr ?
(A) for (int x : arr)
if (x % 2 == 1)
System.out.println(x);
(B) for (int k = 1; k < arr.length; k++)
if (arr[k] % 2 == 1)
System.out.println(arr[k]);
(C) for (int x : arr)
if (x % 2 == 1)
System.out.println(arr[x]);
(D) for (int k = 0; k < arr.length; k++)
if (arr[k] % 2 == 1)
System.out.println(k);
(E) for (int x : arr)
if (arr[x] % 2 == 1)
System.out.println(arr[x]);
// Point A
while (n > 2)
{
x = x + y;
// Point B
y = x – y;
n--;
}
// Point C
return x;
}
Which of the following code segments will produce the same output as the code segment above?
(A) for (int k = 1; k <= 25; k++)
System.out.println(k);
(B) for (int k = 1; k <= 100; k = k + 4)
System.out.println(k);
(C) for (int k = 1; k <= 100; k++)
System.out.println(k % 4);
(D) for (int k = 4; k <= 25; k = 4 * k)
System.out.println(k);
(E) for (int k = 4; k <= 100; k = k + 4)
System.out.println(k);
The following code segment appears in another method in the same class.
return answer;
}
Which of the following represents the value returned as a result of the call compute(n, k) ?
(A) n * k
(B) n!
(C) nk
(D) 2k
(E) k n
int sum = 0;
int k = 1;
while (sum < 12 || k < 4)
sum += k;
System.out.println(sum);
public Point()
{
x = 0;
y = 0;
}
// There may be instance variables, constructors, and methods that are not shown.
}
/** Constructs a circle where (a, b) is the center and r is the radius.
*/
public Circle(double a, double b, double r)
{
/* missing code */
}
}
Which of the following replacements for /* missing code */ will correctly implement the Circle
constructor?
if (x % 2 == 0)
y = 3;
else if (x > 9)
y = 5;
else
y = 1;
Which of the following test data sets would test each possible output for the method?
(A) 8, 9, 12
(B) 7, 9, 11
(C) 8, 9, 11
(D) 8, 11, 13
(E) 7, 9, 10
int x = 1;
while ( /* missing code */ )
{
System.out.print(x + " ");
x = x + 2;
}
I. x < 6
II. x != 6
III. x < 7
Which of the proposed replacements for /* missing code */ will cause the code segment to print only
the values 1 3 5 ?
(A) I only
(B) II only
(C) I and II only
(D) I and III only
(E) I, II, and III
END OF SECTION I
40