Practice Exam 1 for the AP CS A Exam¶

The following problems are similar to what you might see on the AP CS A exam. Please answer each to the best of your ability.

14-1-1: Given the following method. What is the output when m1(5) is called?

public int m1 (int a)
{
if (a == 1)
return 10;
else
return 10 + m1 (a – 1);
}


14-1-2: What will the following code print when it is executed?

for (int j = 1; j <= 3; j++) {
for (int k = 1; k < 4; k++)
System.out.print(j * k + " ");
}


14-1-3: What value is returned as the result of mystery(7)?

public int mystery(int n)
{
if (n == 0) return 1;
else return 2 * mystery (n - 1);
}


14-1-4: Given the following code. Which of the following would return true?

Integer int1 = new Integer(3);
Integer int2 = new Integer(3);
Integer int3 = int2;

I. (int3.equals(int2))
II. (int1.equals(int2))
III. (int3 == int2)
IV. (int1 == int2)
V. (int2 == int3)


14-1-5: Under which of these conditions will a sequential search be faster than a binary search?

14-1-6: There is a method called checkString that determines whether a string is the same forwards and backwards. The following data sets can be used for testing the method. Which is a best set of test cases?

Data set 1: "aba", "abba", "aBa", "z"
Data set 2: "bcb", "bcd", "c"


14-1-7: Which of the following code will produce this output:

1
22
333
4444
55555

I.

for (int i = 0; i < 5; i++) {
for (int j = 0; j < i; j++) {
System.out.print(i);
}
System.out.println();
}

II.

for (int i = 1; i < 5; i++) {
for (int j = i; j > 0; j--) {
System.out.print(i);
}
System.out.println();
}

III.

for (int i  = 1; i <= 5; i++) {
for (int j = i; j > 0; j--) {
System.out.print(i);
}
System.out.println();
}

IV.

for (int i = 1; i < 6; i++) {
for (int j = 0; j < i; j++) {
System.out.println(i);
}
}

V.

for (int i = 0; i < 5; i++) {
for (int j = 0; j < i; j++) {
System.out.print(i+1);
}
System.out.println();
}


14-1-8: What will the following print?

List<Integer> list1 = new ArrayList<Integer>();
list1.set(2, new Integer(2));
System.out.println(list1);


14-1-9: Consider the following declaration for a class that will be used to represent points in the xy-coordinate plane. Which of these constructors would be legal for the NamedPoint class?

 public class Point
{
private int myX; // coordinates
private int myY;

public Point( )
{
myX = 0;
myY = 0;
}

public Point(int a, int b)
{
myX = a;
myY = b;
}

// ... other methods not shown

}

// The following incomplete class declaration is intended to extend the
// above class so that two-dimensional points can be named.

public class NamedPoint extends Point
{
private String myName;
// constructors go here
// ... other methods not shown
}

Proposed Constructors:

I.   public NamedPoint()
{
myName = "";
}
II.  public NamedPoint(int d1, int d2, String name)
{
myX = d1;
myY = d2;
myName = name;
}
III. public NamedPoint(int d1, int d2, String name)
{
super(d1, d2);
myName = name;
}


14-1-10: Given the following incomplete class declaration:

 public class TimeRecord
{
private int hours;
private int minutes; // 0<=minutes<60

public TimeRecord(int h, int m)
{
hours = h;
minutes = m;
}

// postcondition: returns the number of hours
public int getHours()
{ /* implementation not shown */ }

// postcondition: returns the number
// of minutes; 0 <= minutes < 60
public int getMinutes()
{ /* implementation not shown */ }

// precondition: h >= 0; m >= 0
// postcondition: adds h hours and
// m minutes to this TimeRecord
public void advance(int h, int m)
{
hours = hours + h;
minutes = minutes + m;
/* missing code */
}

// ... other methods not shown
}

// Consider the following declaration that appears in a client program:

TimeRecord[] timeCards = new TimeRecord;

// Assume that timeCards has been initialized with TimeRecord
// objects. Consider the following code segment that is intended to compute
// the total of all the times stored in timeCards.

TimeRecord total = new TimeRecord(0,0);
for (int k = 0; k < timeCards.length; k++)
{
/* missing expression */
}

Which of the following can be used to replace /* missing expression */ so that the code segment will work as intended?

I.

II.

III.

IV.

V.


14-1-11: Consider the following class definitions, which of the following can replace the missing code?

public class ContactInfo
{
private String name;
private String phoneNumber;

public ContactInfo(String theName, String thePhoneNumber)
{
this.name = theName;
this.phoneNumber = thePhoneNumber;
}

public String getName() { return name; }

public String getPhoneNumber() { return phoneNumber; }
}

public class ExtendedContactInfo extends ContactInfo
{
private String nickname;

public ExtendedContactInfo (String theNickname,
String theName,
String thePhoneNumber)
{
// missing code
}
}

I.
super(theNickname, theName, thePhoneNumber);

II.
this.name = theName;
this.phoneNumber = thePhoneNumber;
this.nickname = theNickname;

III.
this.nickname = theNickname;
this.name = theName;
this.phoneNumber = thePhoneNumber;

IV.
this.nickname = theNickname;
this.name = theName;
this.phoneNumber = thePhoneNumber;

V.
super(theName,thePhoneNumber);
this.nickname = theNickname;


14-1-12: Which will cause the longest execution of a sequential search looking for a value in an array of 10 integers?

14-1-13: Which of the following reasons for using an inheritance hierarchy are valid?

I.   Methods from a superclass can be used in a subclass without rewriting
or copying code.
II.  Objects from subclasses can be passed as arguments to a method  designed
for the superclass
III. Objects from subclasses can be stored in the same array
IV.  All of the above
V.   None of the above


14-1-14: Consider the following method and if int[] a = {8, 3, 1}, what is the value in a after m1(a); is run?

public int m1(int[] a)
{
a--;
return (a * 2);
}


14-1-15: What are the values of a and b after the for loop finishes?

int a = 10, b = 3, t;
for (int i=1; i<=6; i++)
{
t = a;
a = i + b;
b = t - i;
}


14-1-16: Consider the following code. What string is referenced by s1 after the code executes?

String s1 = "Hi There";
String s2 = s1;
String s3 = s2;
String s4 = s1;
s2 = s2.toLowerCase();
s3 = s3.toUpperCase();
s4 = null;


14-1-17: Consider the following code segment. What value is in sum after this code executes?

int[][] matrix = {{1,1,2,2},{1,2,2,4},{1,2,3,4},{1,4,1,2}};

int sum = 0;
int col = matrix.length - 2;
for (int row = 0; row < 4; row++)
{
sum = sum + matrix[row][col];
}


14-1-18: Consider the following code segment, what are the contents of mat after the code segment has executed?

int [][] mat = new int ;
for (int row = 0; row < mat.length; row++)
{
for (int col = 0; col < mat.length; col++)
{
if (row < col)
mat[row][col] = 3;
else if (row == col)
mat[row][col] = 2;
else
mat[row][col] = 1;
}
}


14-1-19: Assume that temp is an int variable initialized to be greater than zero and that a is an array of type int. Also, consider the following code segment. What of the following will cause an infinite loop?

for ( int k = 0; k < a.length; k++ )
{
while ( a[ k ] < temp )
{
a[ k ] *= 2;
}
}


14-1-20: Consider the following method. What is the output from conditionTest(3,-2);?

 public static void conditionTest(int num1, int num2)
{
if ((num1 > 0) && (num2 > 0)) {
if (num1 > num2)
System.out.println("A");
else
System.out.println("B");
}
else if ((num2 < 0) || (num1 < 0)) {
System.out.println("C");
}
else if (num2 < 0) {
System.out.println("D");
}
else {
System.out.println("E");
}
}