0% found this document useful (0 votes)
86 views

apcsa_mock_exam_.pdf

The document contains a series of programming questions and code segments related to Java, covering topics such as variable assignments, control structures, class definitions, and method implementations. Each question is followed by a specific answer or explanation regarding the expected output or behavior of the code. The content appears to be part of a mock exam for an Advanced Placement Computer Science course.

Uploaded by

braamseaaron
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)
86 views

apcsa_mock_exam_.pdf

The document contains a series of programming questions and code segments related to Java, covering topics such as variable assignments, control structures, class definitions, and method implementations. Each question is followed by a specific answer or explanation regarding the expected output or behavior of the code. The content appears to be part of a mock exam for an Advanced Placement Computer Science course.

Uploaded by

braamseaaron
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/ 8

1. Consider the following code segment.

int a = 3 + 2 * 3;
int b = 4 + 3 / 2;
int c = 7 % 4 + 3;
double d = a + b + c;
What is the value of d after the code segment is executed? 20.0

2. Consider the following code segment. Assume num is a properly declared and initialized int variable.
if (num > 0) {
if (num % 2 == 0) {
System.out.println("A"); }
Else {
System.out.println("B"); } }
Which of the following best describes the result of executing the code segment? When num is a positive even
integer, "A" is printed; when num is a positive odd integer, "B" is printed; otherwise, nothing is printed.

3. Consider the following implementation of getHours.


public static double getHours(int marker1, int marker2) {
/* missing statement */
return hours; }
Which of the following statements can replace /* missing statement */ so getHours works as intended?
double hours = Math.abs(marker1 - marker2) / 60.0;

4. Consider the following method.


public static void message(int a, int b, int c) {
if (a < 10) {
if (b < 10) {
System.out.print("X"); }
System.out.print("Y"); }
if (c < 10) {
if (b > 10) {
System.out.print("Y"); }
Else {
System.out.print("Z"); } } }
What is printed as a result of the call message(5, 15, 5)? YY

5. Consider the following class definition.


public class Bird {
private String species;
private String color;
private boolean canFly;
public Bird(String str, String col, boolean cf){
species = str;
color = col;
canFly = cf; } }
Which of the following constructors, if added to the Bird class, will cause a compilation error?
public Bird(String col, String str, boolean cf){
species = str;
color = col;
canFly = cf; }

6. Which of the following expressions evaluate 3.5?


I. (double) 2 / 4 + 3

This study source was downloaded by 100000806476756 from CourseHero.com on 04-23-2025 12:33:21 GMT -05:00

https://www.coursehero.com/file/161954582/apcsa-mock-exam-pdf/
7. Consider the following code segment.
int num = /* initial value not shown */;
boolean b1 = true;
if (num > 0){
if (num >= 100){
b1 = false;}}
Else {
if (num >= -100){
b1 = false; }}
Which of the following statements assigns the same value to b2 as the code segment assigns to b1 for all
values of num ?
boolean b2 = (num < -100) || (num > 0 && num < 100);

8. public class Points {


private double num1;
private double num2;
public Points(int n1, int n2) // Line 6
{
num1 = n1; // Line 8
num2 = n2; // Line 9
}
public void incrementPoints(int value) // Line 12
{
n1 += value; // Line 14
n2 += value; // Line 15 } }
The class does not compile. Which of the following identifies the error in the class definition?
In lines 14 and 15, the variables n1 and n2 are not defined.

9. Consider the following code segment.


ArrayList<Integer> numList = new ArrayList<Integer>();
numList.add(3);
numList.add(2);
numList.add(1);
numList.add(1, 0);
numList.set(0, 2);
System.out.print(numList);
What is printed by the code segment?
[2, 0, 2, 1]

10. Consider the following method.


public static void printSome(int num1, int num2)
{
for (int i = 0; i < num1; i++)
{
if (i % num2 == 0 && i % 2 == 0)
{
System.out.print(i + " ");
}
}
}
Which of the following method calls will cause "0 10 " to be printed?
printSome(20, 5)

This study source was downloaded by 100000806476756 from CourseHero.com on 04-23-2025 12:33:21 GMT -05:00

https://www.coursehero.com/file/161954582/apcsa-mock-exam-pdf/
11. Which of the following code segments produces the output "987654321"?
C.
int num = 10;
while (num > 1)
{
System.out.print(num);
num --;
}
12. Consider the following class definitions.
public class Person
{
private String name;
public String getName()
{ return name; }
}
public class Book
{
private String author;
private String title;
private Person borrower;
public Book(String a, String t)
{
author = a;
title = t;
borrower = null;
}
public void printDetails()
{
System.out.print("Author: " + author + " Title: " + title);
if ( /* missing condition */ )
{
System.out.println(" Borrower: " + borrower.getName());
}
}
public void setBorrower(Person b)
{ borrower = b; }
}
Which of the following can replace /* missing condition */ so that the printDetails method CANNOT cause a
run-time error?
2. borrower != null

13. Assume that a, b, and c are boolean variables that have been properly declared and initialized. Which of the
following boolean expressions is equivalent to !(a && b) || c ? E.

14. The following categories are used by some researchers to categorize zip codes as urban, suburban, or rural
based on population density.
● An urban zip code is a zip code with more than 3,000 people per square mile.
● A suburban zip code is a zip code with between 1,000 and 3,000 people, inclusive, per square mile.
● A rural zip code is a zip code with fewer than 1,000 people per square mile.
Consider the following method, which is intended to categorize a zip code as urban, suburban, or rural based
on the population density of the area included in the zip code. I & III only

This study source was downloaded by 100000806476756 from CourseHero.com on 04-23-2025 12:33:21 GMT -05:00

https://www.coursehero.com/file/161954582/apcsa-mock-exam-pdf/
15. Consider the following code segment. Assume that a is greater than zero.
int a = /* value not shown */;
int b = a + (int) (Math.random() * a);
Which of the following best describes the value assigned to b when the code segment is executed? E. A
random integer between a and 2 * a - 1, inclusive

16. What is printed as a result of the method call stars(5)? B. **


***
****
*****
17. The following code segment appears in a class other than Hero and SuperHero.
Hero j = new SuperHero("JavaHero", 50);
j.powerUp(10);
System.out.println(j.showPower());
What is printed as a result of executing the code segment? D. 70

18. Which of the following can replace /* missing loop header */ so the method countPeaks works as
intended? E. int p =1; p< data.length -1; p++

19. int[][] values = {{1, 2, 3}, {4, 5, 6}};


int x = 0;
for (int j = 0; j < values.length; j++)
{
for (int k = 0; k < values[0].length; k++)
{
if (k == 0)
{
values[j][k] *= 2;
}
x += values[j][k];
}
}
What is the value of x after the code segment is executed? D. 26

20. The following code segment is intended to store in maxPages the greatest number of pages found in any
Book object in the array bookArr.
Book[] bookArr = { /* initial values not shown */ };
int maxPages = bookArr[0].getPages();
for (Book b : bookArr)
{
/* missing code */
}
Which of the following can replace /* missing code */ so the code segment works as intended?
B. if (b.getPages() > maxPages)
{
maxPages = b.getPages();
}

21. Consider the following code segment.


String[] testOne = {"first", "day", "of", "spring"};
String[] resultOne = strArrMethod(testOne);
What are the contents of resultOne when the code segment has been executed?
D. {"of", "of", "of", "spring"}

This study source was downloaded by 100000806476756 from CourseHero.com on 04-23-2025 12:33:21 GMT -05:00

https://www.coursehero.com/file/161954582/apcsa-mock-exam-pdf/
22. Consider the following code segment.
String[] testTwo = {"last", "day", "of", "the", "school", "year"};
String[] resultTwo = strArrMethod(testTwo);
How many times is the line labeled // Line 12 in the strArrMethod executed as a result of executing the code
segment? A. 4 times

23. When executed, the code segment should produce the following output.
12345678
Which of the following code segments can replace /* missing code */ so that the rowMajor method works as
intended?

24. The following code segment appears in a class other than SomeClass.
SomeClass first = new SomeClass(10);
SomeClass second = new SomeClass(20);
SomeClass third = new SomeClass(30);
first.incrementY();
second.incrementY(10);
System.out.println(third.getY());
What is printed as a result of executing the code segment if the code segment is the first use of a SomeClass
object? D. 14

25. What, if anything, is returned by the method call rearrange("apple")? D. "lppa"

26. Assume that the initial values of m and n are the same in code segment I as they are in code segment II.
Which of the following correctly compares the number of times that "A" and "B" are printed when each code
segment is executed? C. "A" is printed m more times than "B".

27. Consider the following statement. Assume that a and b are properly declared and initialized boolean
variables.
boolean c = (a && b) || (!a && b);
Under which of the following conditions will c be assigned the value false? E. When b has the value false

28. What, if anything, is returned by the method call abMethod("sing the song", "ng")?
B. "si the so"

29. What value is returned by the method call calcMethod(16)? E. 41

30. Which of the following code segments can replace /* missing code */ so that the Square class constructor
initializes the Rectangle class instance variables height and width to x?
B. super(x);

31. Which of the following code segments counts the number of negative values found in nums and stores the
count in counter? A. 1 Only

32. The following code segment appears in a class other than ClassA or ClassB.
ClassA obj = new ClassB();
obj.showValue();

This study source was downloaded by 100000806476756 from CourseHero.com on 04-23-2025 12:33:21 GMT -05:00

https://www.coursehero.com/file/161954582/apcsa-mock-exam-pdf/
What, if anything, is printed when the code segment is executed? B. B

33. String[][] letters = {{"A", "B", "C", "D"},


{"E", "F", "G", "H"},
{"I", "J", "K", "L"}};
for (int col = 1; col < letters[0].length; col++)
{
for (int row = 1; row < letters.length; row++)
{
System.out.print(letters[row][col] + " ");
}
System.out.println();
}
E. F J
GK
HL

34. As an example, if the method is called with an ArrayList containing the values [5, 2, 10, 20, 16] and the
parameter key has the value 5, then numList should contain [2, 16] at the end of the method and an ArrayList
containing [5, 10, 20] should be returned.
Which of the following best explains why the method does not always work as intended? E. The method skips
some elements of numList during the traversal.

35. Consider the mode method, which is intended to return the most frequently occurring value (mode) in its
int[] parameter arr. For example, if the parameter of the mode method has the contents {6, 5, 1, 5, 2, 6, 5}, then
the method is intended to return 5.
A. Arr[j] == arr[k] & valCount > modeCount

36. Which of the following best describes the conditions under which methodOne and methodTwo return the
same value? D. When a % b is equal to zero.

37. Consider the following code segment. Assume that num3 > num2 > 0. Which of the following best
describes the contents of num1 as a result of executing the code segment? E. The sum of all integers from
num2 to num3 - 1, inclusive

38. Which of the following code segments can replace /* missing code */ so the getTotal method works as
intended? D. I and II

39. 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;
}
}

This study source was downloaded by 100000806476756 from CourseHero.com on 04-23-2025 12:33:21 GMT -05:00

https://www.coursehero.com/file/161954582/apcsa-mock-exam-pdf/
Which of the following method calls will return true? D. recurMethod("edcba")

40. 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.

FRQ

1. a) public static int hailstoneLength(int n) {


int count = 1;
while (n > 1) {
If (n % 2 == 0) {
n /= 2;
}
else {
n = 3*n + 1;
}
count++;
}
return count;
}
b) public static boolean isLongSeq(int n) {
Return hailstoneLength(n) > n; }

c) public static double propLong(int n) {


int count = 0;
For (int i = 1; i <= n +1; i++) {
if (isLongSeq(i)){
count++;
}
}
return(double) count / n;

2. Public class GameSpinner {


private int sector;
private int previousSpin = 0;
Private int currentLength = 0;

Public GameSpinner(int g) { sector = s; }


Public int spin() {
Int newSpin = (int) (Math.random() * sectors) + 1;
If (newSpin == previousSpin) {
current Length++; }
return NewSpin;
}
Public int currentRun() {
Return currentLength; } }

This study source was downloaded by 100000806476756 from CourseHero.com on 04-23-2025 12:33:21 GMT -05:00

https://www.coursehero.com/file/161954582/apcsa-mock-exam-pdf/
3. a) Public void addReview(Product Review prodReview) {
reviewList.add(prodReview);

String theProdName = prdReview.getName();


Boolean found = false;
for (String n : productList) {
if (n.equals(prodName)) {
Found = true; }
}
If (!found) {
productList.add(prodName): }
}
b) public int getNumGoodReviews (String prodName) {
int numGoodReviews = 0;
for (ProductReview prodReview : reviewList) {
If (prodName.equals(prodreivew.getName())) {
String review = prodReview.getReview();
if (review.indexOf(“best”) >= 0) {
numGoodReviews++; } } }
Return numGoodReviews; }

4. a) public Theater (int seatsPerRow, int tier1Rows, int tier2Rows) {


theaterSeats = new Seat[tier1Rows + tier2Rows][seatsPerRow];
for (int r = 0; r < tier1Rows + tier2Rows; r++) {
For (int c = 0; c < seatsPerRow; c++) {
If ( r < tier1Rows) {
theaterSeats[r][c] = new Seat(true, 1);
}
else {
theater Seats[r][c] = new Seat(true,2); } } } }
b) public boolean reassignSeat(int fromRow, int fromCol, int toRow, int toCol) {
Seat toS = theaterSeats[towRow][toCol];
if (!toSo.isAvailable()) {
Return false; }
Seat fromS = theaterSeats[fromRow][fromCol];
if (toS.getTier() < fromS.getTier()) {
Return false; }
toS.setAvailability(false);
fromS.setAvailability(true);
return true; }

This study source was downloaded by 100000806476756 from CourseHero.com on 04-23-2025 12:33:21 GMT -05:00

https://www.coursehero.com/file/161954582/apcsa-mock-exam-pdf/
Powered by TCPDF (www.tcpdf.org)

You might also like