Quiz Apcspracticemultiplechoicetest2016
Quiz Apcspracticemultiplechoicetest2016
Quiz Apcspracticemultiplechoicetest2016
}
How many times will a ‘!’ be printed?
A 45
B 35
C 40
D 38
E 42
Page 1 of 31
2. Consider the following class definitions.
Page 2 of 31
3. Given the following code:
String s3 = s2.substring(0,1);
I. s1.equals(s3)
II. s1 == s4
III.s1.equals(s4)
A I and III only
C I only
D II only
E III only
B Never true
C a=b
D a<b
E a>b
Page 3 of 31
5. Given the following class declarations:
public Car () {
make = "Ford"; }
make = theMake; }
return make;
super(theMake); }
}
Which of the following will cause a compile time error?
A Car myCar = new Car();
Page 4 of 31
6. Consider the following code segment.
list1.add("a");
list1.add("b");
list1.add(0,"c");
list1.add(1, "d");
list1.set(2, "e");
list1.add("f");
System.out.println(list1);
B [c, d, e, b, f]
C [c, a, e, b, f]
D [c, d, e, a, b, f]
E [c, a, e, d, b, f]
Page 5 of 31
7. Given the following code:
if (str.length() == 1) return 0;
else
if (str.substring(0,1).equals("y")) return 1 +
mystery(str.substring(1));
B 4
C 1
D 3
E 0
Page 6 of 31
8. Given the following class definition. What are the values of x, y, and z just before
method2 returns?
public class Class1
int temp = a;
a = b;
b = temp;
return b;
int x = 3;
int y = 5;
}
A x=5, y=3, z=5
Page 7 of 31
9. Given the following class declarations.
I. v.go();
II. v.switchToElectric();
B II only
Page 8 of 31
10. Consider the following code segment:
(s.substring(0,1).equals(s.substring(1,2)) ||
check(s.substring(1)));
Pick the answer below that best describes all the cases when this method will
return true.
A s contains two or more of the same characters
Which of the following declarations would not cause a compile time error?
A Rat rat = new Mouse();
12. Which of the following correctly shows the iterations of an ascending (from left
to right) insertion sort on an array with the following elements: {7,3,8,5,2}?
A {3,7,8,5,2}, {3,7,8,5,2}, {3,5,7,8,2}, {2,3,5,7,8}
Page 9 of 31
13. Consider the following code segment:
int p = 5;
int q = 2;
int sum = 0;
while (p <= 7)
sum += p % q;
p++;
q++;
B 2
C 3
D 4
E 7
Page 10 of 31
14. Consider the following method. What is the output from conditionTest(3,-2)?
if (num1>num2)
System.out.println("A");
else
System.out.println("B");
System.out.println("C");
System.out.println("D");
else {
System.out.println("E");
}
A A
B B
C C
D D
E E
Page 11 of 31
15. Consider the following code segment
y = y * 2;
What are the values of s and b after the following has executed?
String s = "rain";
int b = 4;
test(s,b);
A s="rainbow"; b=8;
B s="rain"; b=8;
C s="rainbow"; b=4;
D s="rain"; b=4;
E s="bow"; b=4;
System.out.print(str.substring(x, x + 2));
B mmnnooppqqrr
C mnnooppqqr
D mnonopopqpqr
Page 12 of 31
17. Given that count and n are both integer values, which of the following is true?
// Code block I
System.out.println(count);
//Code block II
count = 0;
count = count + 1;
System.out.println(count);
}
A The output from I and II is the same for all values of n.
D The output from I and II is the same for all values of n except when n=0
18. Given the following values for a 2D array m and the following code
1111
1234
2222
2468
int sum = 0;
B 9
C 10
D 4
E 20
Page 13 of 31
19. Consider the following method.
if (temp)
return temp;
return temp;
Which of the below best describes all the cases in which test returns true?
A Whenever the first element in a is equal to val.
if (a == 1)
return 1;
else
B 11
C 51
D 60
E 61
Page 14 of 31
21. Consider the following class definitions.
return "Pizza";
return "Studying";
return "Taco";
super.getInfo();
return "Eating";
System.out.println(s.getInfo());
A Pizza
B Taco
C Studying
D Eating
E Studying
Eating
Page 15 of 31
22. What are the values of a and b after the following loop finishes?
int a = 8, b = 3, temp = 0;
temp = a;
a = i + b;
b = temp - i;
}
A a=6, b=5
B a=11,b=0
C a=13, b=0
D a=9, b=3
E a=0, b=13
II. All methods declared in an interface are abstract methods (can’t have a method
body).
B III only
C I and II only
E I only
Page 16 of 31
24. Consider the following partial class definitions.
return 0;
int x = 2;
int y = -1;
x = (int) Math.pow(x,
Math.abs(y * 2));
}
A 512
B 256
C 32
D 64
E 16
Page 17 of 31
26. What is printed when the following main method is executed?
return -1; }
System.out.println(s.mystery(0,4,3));
}
A -1
B 0
C 1
D 2
E 3
Page 18 of 31
27. Which of the following correctly defines an interface?
}
A I only
B (B) II only
C III only
Page 19 of 31
28. 28. The following incomplete method is intended to sort the array elem in
ascending order.
int minIndex = j;
minIndex = k;
elem[j] = elem[minIndex];
elem[minIndex] = temp;
Which of the following could be used to replace /* missing code */ in the code
above so that the method always sorts the array a in ascending order?
A int k = j - 1; k >= 0; k--
D Interfaces are used in the standard Java classes in package java.util, but not abstract classes.
E Abstract classes can have methods with bodies (code in them), but interfaces
Page 20 of 31
30. 30. Consider the following code segment:
mat[row][col] = 1;
mat[row][col] = 2;
else
mat[row][col] = 3;
What are the contents of mat after the code segment has been executed?
A { { 2, 3, 3, 3 }, { 1, 2, 3, 3 }, { 1, 1, 2, 3 } }
B { { 1, 1, 2, 3 } { 1, 2, 3, 3 }, { 2, 3, 3, 3 } }
C { { 2, 3, 3 }, { 1, 2, 3 }, { 1, 1, 2 }, { 1, 1, 1 } }
D { { 2, 1, 1 }, { 3, 2, 1 }, { 3, 3, 2 }, { 3, 3, 3 } }
E { { 2, 1, 1, 1 }, { 3, 2, 1, 1 }, { 3, 3, 2, 1 } }
Page 21 of 31
31. Given the following method.
if (a[i] == v)
return i;
B 1
C 2
D -1
Page 22 of 31
32. Given the following declarations.
public class A {
A a = new A();
B b = new B();
C c = new C();
B b.test(c,c);
C c.test(b,b);
D a.test(c,b);
E c.test(c,a);
33. Which of the following would be the correct result from the following
expression?
B 133
C 131
D 132
E 136
Page 23 of 31
34. Consider the following class declaration.
public Test(int n)
value = n;
return value;
Test c = a;
a.add(200);
E E)
Page 24 of 31
35. 35. Consider the following partial class declaration.
// . . .
Assume that the Person objects are ordered by last name and then first name. Which
of the
return (last.compareTo(testP.last) +
first.compareTo(testP.first));
if (first.compareTo(testP.first) == 0)
return last.compareTo(testP.last);
Page 25 of 31
else
return first.compareTo(testP.first);
if (last.compareTo(testP.last) == 0)
return first.compareTo(testP.first);
else
return last.compareTo(testP.last);
}
A I only
B II only
D I and II only
E III only
Page 26 of 31
36. Consider the following code that is part of a class declaration.
k >= 0; k--)
return k;
return -1;
Which of the following best describes the contents of myStuff after the following
int m = mystery(n);
A All values in position m+1 through myStuff.length-1 are <= n.
Page 27 of 31
37. Consider the following declaration of the class RandomList, which has a
constructor that
// precondition: n > 0
public RandomList(int n)
/* missing code */
Which of the following could be used to replace /* missing code */ so that the
B II only
C III only
Page 28 of 31
D II and III
if (a.length > 1)
a[1] = a[1] * 2;
y = y * 2;
What are the values of s and b after the following has executed?
int[] s = {3,4};
int b = 4;
test(s,b);
A s={3, 8}; b=4;
Page 29 of 31
39. The following method should sort the contents of the array elements. Which of
the
int pIndex = j;
pIndex--;
elem[pIndex] = temp;
}
A pIndex > 0 && temp < elem[pIndex - 1]
Page 30 of 31
40. What is the output from mystery(4321) when mystery is defined as follows:
//precondition: x >=0
if ((x / 10) != 0) {
mystery(x / 10);
System.out.print(x % 10);
}
A 12344321
B 1234
C 4321
D 43211234
E 32144123
Page 31 of 31