1/19
Flashcards based on the provided AP Computer Science A practice exam pages.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What is the value of d after the code segment int a = 3 + 2 * 3; int b = 4 + 3 / 2; int c = 7 % 4 + 3; double d = a + b + c; is executed?
20.0
What best describes the result of executing the code segment if (num > 0) { if (num % 2 == 0) { System.out.println('A'); } else { System.out.println('B'); } }
When num is a positive even integer, 'A' is printed; when num is a positive odd integer, 'B' is printed; otherwise, nothing is printed.
Which statement can replace /* missing statement / so getHours works as intended in the method public static double getHours(int marker1, int marker2) { / missing statement */ return hours; }
double hours = Math.abs(marker1 - marker2) / 60.0;
What is printed as a result of the call message(5, 15, 5) given the 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'); } } }
YY
Which of the following constructors, if added to the Bird class, will cause a compilation error? 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; } }
public Bird(String col, String str, boolean cf) { species = str; color = col; canFly = cf; }
Which of the following expressions evaluate to 3.5 ? (I) (double)2/4+3 (II) (double) (2 / 4) + 3 (III) (double) (2 / 4 + 3)
I only
Which of the following statements assigns the same value to b2 as the code segment assigns to b1 for all values of num ? int num = /* initial value not shown */; boolean b1 = true; if (num > 0) { if (num >= 100) { b1 = false; } } else { if (num >= -100) { b1 = false; } }
(num < -100) | | (num > 0 && num < 100)
Which of the following identifies the error in the 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 } }
In lines 14 and 15, the variables n1 and n2 are not defined.
What is printed by the code segment? ArrayList
[2, 0, 2, 1]
Which of the following method calls will cause '0 10 ' to be printed given 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 + ' '); } } }
printSome(20, 5)
Which of the following code segments produces the output '987654321' ?
int num = 10; while (num > 1) { num--; System.out.print(num); }
Which of the following can replace /* missing condition / so that the printDetails method CANNOT cause a run-time error? 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; } }
borrower != null
Which of the following boolean expressions is equivalent to !(a && b) | | c ?
!a | | !b | | c
Which of the following code segments can replace /* missing code / so the getCategory method works as intended? public static String getCategory(int density) { / missing code */ }
I and III only
Which of the following best describes the value assigned to b when the code segment is executed? int a = /* value not shown */; int b = a + (int) (Math.random() * a);
A random integer between a and 2*a-1, inclusive
What is printed as a result of the method call stars(5) ? 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 executing the code segment? 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); } } Hero j = new SuperHero('JavaHero', 50); j.powerUp(10); System.out.println(j.showPower());
70
Which of the following can replace /* missing loop header / so the method countPeaks works as intended? 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; }
int p = 1; p < data.length - 1; p++
What is the value of x after the code segment is executed? 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]; } }
26
Which of the following can replace /* missing code / so the code segment works as intended? Book[] bookArr = { / initial values not shown / }; int maxPages = bookArr[0].getPages(); for (Book b : bookArr) { / missing code */ }
if (b.getPages() > maxPages) { maxPages = b.getPages(); }