Practice Exam 1A MCQ AP Computer Science A

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/29

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

30 Terms

1
New cards

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?

C) 20.0

2
New cards

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?

D) 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
New cards

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?

C) double hours = Math.abs(marker1 - marker2) / 60.0;

4
New cards

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?

E) public Bird(String col, String str, boolean cf)

{

species = str;

color = col;

canFly = cf;

}

5
New cards

Which of the following expressions evaluate to 3.5 ?

I. (double) 2 / 4 + 3

II. (double) (2 / 4) + 3

III. (double) (2 / 4 + 3)

A) I only

6
New cards

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 ?

E) boolean b2 = (num < -100) || (num > 0 && num < 100);

7
New cards

Consider the following class definition.

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?

D) In lines 14 and 15, the variables n1 and n2 are not defined.

8
New cards

Consider the following code segment.

ArrayList numList = new ArrayList();

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?

B) [2, 0, 2, 1]

9
New cards

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?

D) printSome(20, 5)

10
New cards

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) !a || !b || c

11
New cards

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

12
New cards

Consider the following recursive method.

public static void stars(int num)

{

if (num == 1)

{

return;

}

stars(num - 1);

for (int i = 0; i < num; i++)

{

System.out.print("*");

}

System.out.println();

}

What is printed as a result of the method call stars(5) ?

B)

**

***

****

*****

13
New cards

Consider the following class definitions.

public class Hero

{

private String name;

private int power;

public Hero(String n, int p)

{

name = n;

power = p;

}

public void powerUp(int p)

{

power += p;

}

public int showPower()

{ return power; }

}

public class SuperHero extends Hero

{

public SuperHero(String n, int p)

{

super(n, p);

}

public void powerUp(int p)

{

super.powerUp(p * 2);

}

}

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

14
New cards

Consider the following method, which is intended to return the number of local maximum values in an array. Local maximum values are array elements that are greater than both adjacent array elements. The first and last elements of an array have only a single adjacent element, so neither the first nor the last array element is counted by this method. For example, an array containing the values {3, 9, 7, 4, 10, 12, 3, 8} has two local maximum values: 9 and 12.

public static int countPeaks(int[] data)

{

int numPeaks = 0;

for ( / missing loop header / )

{

if (data[p - 1] < data[p] && data[p] > data[p + 1])

{

numPeaks++;

}

}

return numPeaks;

}

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++

15
New cards

Consider the following code segment.

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

16
New cards

Consider the following class definition.

public class Book

{

private int pages;

public int getPages()

{

return pages;

}

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

}

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();

}

17
New cards

Consider the following method, which is intended to print the values in its two-dimensional integer array parameter in row-major order.

public static void rowMajor(int[][] arr)

{

/ missing code /

}

As an example, consider the following code segment.

int[][] theArray = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};

rowMajor(theArray);

When executed, the code segment should produce the following output.

1 2 3 4 5 6 7 8

Which of the following code segments can replace / missing code / so that the rowMajor method works as intended?

D)

for (int[] j : arr){ for (int k : j) { System.out.print(k + " "); }}

18
New cards

Consider the following class definition.

public class SomeClass

{

private int x = 0;

private static int y = 0;

public SomeClass(int pX)

{

x = pX;

y++;

}

public void incrementY()

{ y++; }

public void incrementY(int inc)

{ y += inc; }

public int getY()

{ return y; }

}

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

19
New cards

Consider the following method.

public static String rearrange(String str)

{

String temp = "";

for (int i = str.length() - 1; i > 0; i--)

{

temp += str.substring(i - 1, i);

}

return temp;

}

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

D) "lppa"

20
New cards

Consider the following two code segments. Assume that the int variables m and n have been properly declared and initialized and are both greater than 0.

for (int i = 0; i < m * n; i++){ System.out.print("A");}

for (int j = 1; j <= m; j++){ for (int k = 1; k < n; k++) { System.out.print("B"); }}

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

21
New cards

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

22
New cards

Consider the following method.

public static int calcMethod(int num)

{

if (num == 0)

{

return 10;

}

return num + calcMethod(num / 2);

}

What value is returned by the method call calcMethod(16) ?

E) 41

23
New cards

Consider the following class definitions.

public class Rectangle

{

private int height;

private int width;

public Rectangle()

{

height = 1;

width = 1;

}

public Rectangle(int x)

{

height = x;

width = x;

}

public Rectangle(int h, int w)

{

height = h;

width = w;

}

// There may be methods that are not shown.

}

public class Square extends Rectangle

{

public Square(int x)

{

/ missing code /

}

}

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);

24
New cards

Consider an integer array nums, which has been properly declared and initialized with one or more values. Which of the following code segments counts the number of negative values found in nums and stores the count in counter ?

int counter = 0;int i = -1;while (i <= nums.length - 2){ i++; if (nums[i] < 0) { counter++; }}

int counter = 0;for (int i = 1; i < nums.length; i++){ if (nums[i] < 0) { counter++; }}

int counter = 0;for (int i : nums){ if (nums[i] < 0) { counter++; }}

A) I only

25
New cards

The following method is intended to remove all elements of an ArrayList of integers that are divisible by key and add the removed elements to a new ArrayList, which the method returns.

public static ArrayList match(ArrayList numList, int key)

{

ArrayList returnList = new ArrayList();

int i = 0;

while (i < numList.size())

{

int num = numList.get(i);

if (num % key == 0)

{

numList.remove(i);

returnList.add(num);

}

i++;

}

return returnList;

}

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.

26
New cards

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.

/* Precondition: arr.length >= 1 /

public static int mode(int[] arr)

{

int modeCount = 1;

int mode = arr[0];

for (int j = 0; j < arr.length; j++)

{

int valCount = 0;

for (int k = 0; k < arr.length; k++)

{

if ( / missing condition 1 / )

{

valCount++;

}

}

if ( / missing condition 2 / )

{

modeCount = valCount;

mode = arr[j];

}

}

return mode;

}

Which of the following can replace / missing condition 1 / and / missing condition 2 / so the code segment works as intended?

A)

27
New cards

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?

E) The sum of all integers from num2 to num3 - 1, inclusive

28
New cards

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 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?

A) I only

29
New cards

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 ?

D) recurMethod("edcba")

30
New cards

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.