Assertions
For the following method, identify each of the three assertions in the table below as being either ALWAYS true, NEVER true or SOMETIMES true / sometimes false at each labeled point in the code. (can abbreviate as A/N/S)
public static int threeHeads() { // Counts coin tosses till we get heads 3x in a row.
Random rand = new Random();
int flip = 1;
int heads = 0;
int count = 0;
// Point A
while (heads < 3) {
// Point B
flip = rand.nextInt(2); // flip coin
if (flip == 0) { // heads
heads++;
// Point C
} else { // tails
// Point D
heads = 0;
}
count++;
}
// Point E
return count;
}
Correct Answer:
Verified
Q1: Programming (15 points)
In this question, we'll address
Q2: Expressions
For each expression in the left-hand
Q3: Parameter Mystery
At the bottom of the page,
Q4: While Loop Simulation
For each call below
Q5: Programming
Write a static method named consecutiveDigits
Q7: Programming
Write a static method named printTwoDigit
Q8: If/Else Simulation
For each call below to
Unlock this Answer For Free Now!
View this answer and more for free by performing one of the following actions
Scan the QR code to install the App and get 2 free unlocks
Unlock quizzes for free by uploading documents