APCS Final Exam Rev.
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?
!a || !b || c
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++) {
"A" is printed m more times than "B"
What, if anything, is returned by the method call rearrange ("apple")?
"lppa"
Consider the following method.public static String abMethod(String a, String b){int x = a.indexOf(b);while (x >= 0){a = a.substring(0, x) + a.substring(x + b.length());x = a.indexOf(b);}return a;}
"si the so"
What is printed as a result of the method call stars(5)?
** *** **** *****
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 neqative values found in stores the count in counter ?
1
numList.add(3); numList.add(2); numList.add(1); numList.add(1, 0); // Inserts 0 at index 1, shifting the existing elements numList.set(0, 2);
2,0,2,1
Consider the following code segment, what will be printed?
20.0
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
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());
30
public static sint caloMethod int num Sf (num == 0) return 10: return num + calcMethod num/ 21. What value is returned by the method call calcethod(16)
41
how many times will it print?
6 times
Public class Hero{private String name;private int power;public Hero (String n, int p){name = n;power = p;}pubic void powerUp (int p){power += p;}public int showPower ( ){return power;}} //ends the Hero classpublic class Superhero extends Hero{public SuperHero (String n, int p){Super (n, p);}public void powerUp (int p){super.powerUp (p * 2);}} // ends the SuperHero classThe following code segment appears in a class other that hero and Superhero.Hero j = new SuperHero ("JavaHero", 50);j.powerUp (10);System.out.print (j.showPower ( ) );What is printed as a result of executing the code segment?
70
int a /* value not shown */; int b = a + (int) (Math.random() * a);
A random integer between 0 and a - 1, inclusive
public class ClassA{public String getValue(){return "A";}public void showValue(){System.out.print(getValue());}} // ends ClassA classpublic class ClassB extends ClassA{public String getValue(){return "B";}}
B
public class Points{private double num1;private double num2;public Points(int n1, int n2) // Line 6{num1 = n1; // Line 8num2 = n2; // Line 9}public void incrementPoints(int value) // Line 12{n1 += value; // Line 14n2 += value; // Line 15}}The class does not compile. Which of the following identifies the error in the class definition?
B. In lines 8 and 9, int values cannot be assigned to double variables.
Consider the following code segment. 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] + " "); }
F J G K H L
Which of the following code segments can replace /* missing code */ so the getTotal method works as intended?
I and II
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.public static String getCategory(int density)
I and III only
Which of the following expressions evaluate to 3.5
I only
Which of the following can replace/* missing condition */ so that the printDetails method CANNOT cause a run-time error in the Person class?
II only
The following method is intended to remove all elements of an Arravlist of integers that are divisible by key and add the removed elements to a new Arraybist public static Arraybist<Integer> match(ArrayList<Integer> numlist, int kev AlLay Isteinteger> refurntat new ArrayList<integers/i while (1 < numis t.•S12e(0D Mint num numuist. get (6). Sif Schum numbist. remove(1 ): returnuist.add(num): +++A return returnist: 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 numbist should contain r2. 161 atithe end of the method and an containina5, 10, 201 should bereturned Which of the following best explains why the method does not alwavs work as intended?
The method skips some elements of numList during the traversal.
Consider the following method.public static int doWhat(int num){int var = 0;for (int loop = 1; loop <= num; loop = loop + 2){var += loop;return var;}Which of the following best describes the value returned from a call to doWhat ?
The sum of all odd integers between 1 and num, inclusive
Consider the following statement. Assume that a and b are properlv declared and initialized boolean variables boolean c = (a 86 b)(!a 6a b): Under which of the following conditions will c be assigned the value false ?
When b has the value false
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.
Public static void message (int a, int b, int c) what will be printed with this call message (5, 15, 5)?
YY
int num = /*initial value not shown*/; boolean b1 = true; if (num > 0) { if (num >= 100) { b1 = false; } } else { if (num >= -100) { b1 = false;
boolean b2 = (num > -100) || (num < 100);
modeCount
condition 1 arr[ j ] == arr[ k] condition 2 valCount > nodeCount
public static double getHours (int marker1, int marker2) { return hours; }
double hours = Math.abs(marker1 -marker2) / 60.0;
Which of the following method calls will return true for recursive method?
edcba
Which of the following code segments can replace /* missing code */ so that the rowMajor method works as intended?
for intri is arml for ant kail Svstem.out.print(k + " ") } }
int maxPages = bookArr[0].getPages(); // Get initial value for (Book b : bookArr) { int currentPages = b.getPages(); if (currentPages > maxPages) { maxPages = currentPages; } }
if (currentPages > maxPages) { maxPages = currentPages; } }
Which of the following code segments produces the output "987654321"?
int num = 10;while (num > 1){System.out.print(num);num --;}
numPeaks method
int p = 1; p < data.length-1; p++;
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)
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;}This constructor has the same signature as the existing constructor (String, String, boolean).
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 ?
super(x);
class definitions
the outcome will be unchanged
Which of the following best describes the conditions under which methodone and methodTwo return the same value?
when a % b is equal to one
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?
{"of", "of", "of", "spring"}