14 min readβ’february 21, 2023
Peter Cao
Peter Cao
Unit Number | Unit Name | Percent | Number of Questions |
1 | Primitive Types | 2.5-5% | 1-2 |
2 | Using Objects | 5-7.5% | 2-3 |
3 | Boolean Expressions and If Statements | 15-17.5% | 6-7 |
4 | Iteration | 17.5-22.5% | 7-9 |
5 | Writing Classes | 5-7.5% | 2-3 |
6 | Array | 10-15% | 4-6 |
7 | ArrayList | 2.5-7.5% | 1-3 |
8 | 2D Array | 7.5-10% | 3-4 |
9 | Inheritance | 5-10% | 2-4 |
10 | Recursion | 5-7.5% | 2-3 |
Trace and Annotate
Process of Elimination
Answer
Answer
Line 1: gets the first 2 letters of word1 and saves it as str1
Line 2: gets the last letter of word2 and saves it as str2
Line 3: makes a new string result by putting str2 at the beginning and str1 after
Line 4: Finds where str2 is in result, because it is at the beginning, the integer that is printed isΒ
0
A is the correct answer
Answer
Answer
Answer
Value of a | Value of b | Value of (b or a) | Value of !(b or a) | Value of (a and !(b or a)) |
false | false | false | true | false |
false | true | true | false | false |
true | false | true | false | false |
true | true | true | false | false |
Answer
val = 48, div = 6, 48 % 2 == 0, 6 > 0, 48 % 6 == 0
"48 " is printed
val = 24, div = 5, 24 % 2 == 0, 5 > 0, 24 % 5 != 0
val = 12, div = 4, 12 % 2 == 0, 4 > 0, 12 % 4 == 0
"48 12 " is printed
val = 6, div = 3, 6 % 2 == 0, 3 > 0, 6 % 3 == 0
**"48 12 6 " is printed**
val = 3, div = 2, 3 % 2 != 0
Code Ends
**A is the correct answer**
Answer
Answer
Answer
Answer
After Line 2: ["one"]
After Line 3: ["one", "two"]
After Line 4: ["three", "one", "two"]
After Line 5: ["three", "one", "four"]
After Line 6: ["three", "one", "four", "five"]
After Line 7: ["three", "four", "five"]
The correct answer is D.
Answer
// Suppose we have an ArrayList myList = [1, 2, 3, 5] and let's call
// removeEvens on this ArrayList.
// When i = 0, the element myList.get(0) = 1 is not even, so the array is
// untouched.
// When i = 1, the element myList.get(1) = 2 is even, so the 2 is removed
// and all the elements will shift to the left, so myList is now [1, 3, 5]
// When i = 2, the element myList.get(2) = 5, but we have skipped 3 because
// it is now at index 1 but i is looking for the new element at index 2!
Answer
points.length = 4, points[0].length = 5;
row = 0
col = 4, 4 >= 0, "15 " printed
col = 3, 3 >= 0, "15 14 " printed
col = 2, 2 >= 0, "15 14 13 " printed
col = 1, 1 >= 0, "15 14 13 12 " printed
col = 0, 1 >= 0, "15 14 13 12 11 " printed
col = -1, -1 < 0, NEW LINE
row = 1
col = 4, 4 >= 1, "25 " printed
col = 3, 3 >= 1, "25 24 " printed
col = 2, 2 >= 1, "25 24 23 " printed
col = 1, 1 >= 1, "25 24 23 22 " printed
col = 0, 0 < 1, NEW LINE
By process of elimination, D is correct
Answer
sum = 0
x = {1, 2, 3, 4} (x.length - 1 will always be 3)
y = 0, sum = 1, y = 1, sum = 3, y = 2, sum = 6, y = 3, 3 !< 3
We now move to a new row and repeat.
The method adds every number in the 2D array except from the last
column, which will turn out to be 54, Answer: B
Answer
IN THING2 CALC:
n = 2 -> 4
super.calc() -> use the calc method in the parent class, which is Thing1
THING1 CALC IS CALLED, IN THING1 CALC:
n = 4 -> 12, "12" is printed
BACK IN THING2:
n = 4, "124" is printed
The correct answer is D.
Answer
Answer to 1a
/** Returns the number of leap years between year1 and year 2 inclusive
*
Precondition
: 0 <= year1 <= year2
*/
public static int numberOfLeapYears(int year1, int year2) {
/*
Pseudocode:
start looping from year 1 to year 2 inclusive, set a counter and add
to the counter if isLeapYear(year) is true
*/
int counter = 0;
for (int i = year1; i <= year2; i++) {
if isLeapYear(i) {
counter++;
}
}
return counter;
}
Answer to 1b
/** Returns the value representing the day of the week for the given date
* (month, day, year), where 0 denotes Sunday, 1 denotes Monday, ..., and 6 denotes Saturday
*
Precondition
: The date is a valid date.
*/
public static int dayOfWeek(int month, int day, int year) {
/*
Pseudocode first attempt
: Use dayOfYear to figure out how many days of the year it has been
in the year, and then modulo 7 to figure out how many days it has been,
then subtract 1 because January 1st is 1 and not 0 and add firstDayOfYear()
However, this may return a value larger than 6, which is outside our
desired range for the output so we should add firstDayOfYear()
and subtract 1 first before doing modulo to get the correct answer
*/
return (dayOfYear(month, day, year) - 1 + firstDayOfYear(year)) % 7;
}
Answer to 2
/** Tracks steps walked
*/
public class StepTracker {
private int totalDays;
private final int ACTIVESTEPS;
private int activeDayCount;
private int totalSteps;
/** Creates a StepTracker with activeSteps specified and other necessary instance
* variables, including total steps, active days, and total days.
*/
public StepTracker(int activeSteps) {
ACTIVESTEPS = activeSteps;
totalDays = 0;
activeDayCount = 0;
totalSteps = 0;
}
/** Adds a day of steps to total steps and also checks if there is an active
* day. Also increments the total number of days
*/
public void addDailySteps(int stepsInDay) {
totalDays++;
totalSteps += stepsInDay;
if (stepsInDay >= ACTIVESTEPS) {
activeDayCount++;
}
}
/** Returns the number of active days
*/
public int activeDays() {
return activeDayCount;
}
/** Returns the average steps a day
*/
public double averageSteps() {
return (double) totalSteps / totalDays;
}
Answer to 3a
/** Returns an ArrayList of delimiters from the array tokens
*/
public ArrayList<String> getDelimitersList(String[] tokens) {
/* Traverse through the array, if a token is a delimiter, add it to the ArrayList
*/
ArrayList<String> delimiterList = new ArrayList<String>()
for (String token: tokens) {
if (token.equals(openDel) || token.equals(closeDel)) {
delimiterList.add(token);
}
}
return delimiterList;
}
Answer to 3b
/** Returns true if the delimiters are balanced and false otherwise
Precondition
: delimiters contains only valid open and close delimiters.
*/
public boolean isBalanced(ArrayList<String> delimiters) {
/*
Pseudocode
: Set a counter, which represents the number of unpaired
delimiters to 0 and for every open delimiter, add 1,
for every close delimiter, subtract 1, representing an open
delimiter has been paired return false if there is a close
delimiter when the counter is 0 (no open to pair with) or the counter
is not 0 in the end (not equal), else, return true
*/
int openCount = 0;
for (String delimiter: delimiters) {
if (delimiter.equals(openDel)) {
openCount++;
} else {
if (openCount == 0) {
return false;
} else {
openCount--;
}
}
}
return openCount == 0;
}
Answer to 4a
/** Constructs a LightBoard object having numRows rows and numCols columns.
*
Precondition
: numRows > 0, numCols > 0
*
Postcondition
: each light has a 40% probability of being set to on.
*/
public LightBoard(int numRows, int numCols) {
/* Traverse through each cell in the 2D array, for each cell, generate a random
integer from 0 to 9, if this integer < 4, set to true, else set to false
*/
import java.util.Random;
Random rand = new Random();
lights = new boolean[numRows][numCols];
for (int i = 0; i < numRows, i++) {
for (int j = 0; j < numCols; j++) {
if (rand.nextInt(10) < 4) {
lights[i][j] = true;
}
}
}
}
Answer to 4b
/** Evaluates a light in row index row and column index col and returns a status
*
Precondition
: row and col are valid indexes in lights.
*/
public boolean evaluateLight(int row, int col) {
/* First count the ons on the column, then check the status of the light in question
and proceed from there as in the instructions
*/
int columnCount = 0;
for (boolean[] row: lights) {
if (row[col]) {
columnCount++;
}
if (lights[row][col] && columnCount % 2 == 0) {
return false;
} else if (!lights[row][col] && columnCount % 3 == 0) {
return true;
}
return lights[row][col];
}
Β© 2023 Fiveable Inc. All rights reserved.