0% found this document useful (0 votes)
10 views4 pages

AP Computer Science Exam Practice 11

The document presents a series of programming questions related to Java, focusing on code segments involving loops, class definitions, recursion, and method overriding. It asks for the best descriptions of outcomes from executing specific code snippets and the implications of modifying class methods. Each question provides multiple-choice answers to assess understanding of Java concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

AP Computer Science Exam Practice 11

The document presents a series of programming questions related to Java, focusing on code segments involving loops, class definitions, recursion, and method overriding. It asks for the best descriptions of outcomes from executing specific code snippets and the implications of modifying class methods. Each question provides multiple-choice answers to assess understanding of Java concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

37. Consider the following code segment. Assume that num3 > num2 > 0.

int num1 = 0;
int num2 = /* initial value not shown */;
int num3 = /* initial value not shown */;

while (num2 < num3)


{
num1 += num2;
num2++;
}
Which of the following best describes the contents of num1 as a result of executing the code segment?

(A) The product of num2 and num3


(B) The product of num2 and num3 - 1
(C) The sum of num2 and num3
(D) The sum of all integers from num2 to num3, inclusive
(E) The sum of all integers from num2 to num3 - 1, inclusive

GO ON TO THE NEXT PAGE.

46 AP Computer Science A Practice Exam


38. Consider the following class definition.
public class Value
{
private int num;

public int getNum()


{
return num;
}

// There may be instance variables, constructors, and methods not shown.


}
The following method appears in a class other than Value. It is intended to sum all the num instance
variables of the Value objects in its ArrayList parameter.
/** Precondition: valueList is not null */
public static int getTotal(ArrayList<Value> valueList)
{
int total = 0;
/* missing code */
return total;
}
Which of the following code segments can replace /* missing code */ so the getTotal method
works as intended?
I. for (int x = 0; x < valueList.size(); x++)
{
total += valueList.get(x).getNum();
}

II. for (Value v : valueList)


{
total += v.getNum();
}

III. for (Value v : valueList)


{
total += getNum(v);
}

(A) I only
(B) II only
(C) III only
(D) I and II
(E) I and III

GO ON TO THE NEXT PAGE.

AP Computer Science A Practice Exam 47


39. Consider the following recursive method.
public static boolean recurMethod(String str)
{
if (str.length() <= 1)
{
return true;
}
else if (str.substring(0, 1).compareTo(str.substring(1, 2)) > 0)
{
return recurMethod(str.substring(1));
}
else
{
return false;
}
}
Which of the following method calls will return true ?

(A) recurMethod("abcba")
(B) recurMethod("abcde")
(C) recurMethod("bcdab")
(D) recurMethod("edcba")
(E) recurMethod("edcde")

GO ON TO THE NEXT PAGE.

48 AP Computer Science A Practice Exam


40. Consider the following class definitions.
public class A
{
public String message(int i)
{
return "A" + i;
}
}

public class B extends A


{
public String message(int i)
{
return "B" + i;
}
}
The following code segment appears in a class other than A or B.
A obj1 = new B(); // Line 1
B obj2 = new B(); // Line 2
System.out.println(obj1.message(3)); // Line 3
System.out.println(obj2.message(2)); // Line 4
Which of the following best explains the difference, if any, in the behavior of the code segment that will result
from removing the message method from class A ?

(A) The statement in line 3 will cause a compiler error because the message method for obj1 cannot
be found.
(B) The statement in line 4 will cause a compiler error because the message method for obj2 cannot
be found.
(C) As a result of the method call in line 3, the message method in class B will be executed instead of
the message method in class A.
(D) As a result of the method call in line 4, the message method in class B will be executed instead of
the message method in class A.
(E) The behavior of the code segment will remain unchanged.

GO ON TO THE NEXT PAGE.

AP Computer Science A Practice Exam 49

You might also like