Booleans
For Loops
While Loops
If Statements
Key Events/Random Numbers
100

What is the last thing printed by the following program?


var start = 30;
var stop = 10;
for(var i = start; i >= stop; i-=5){
    if(i % 2 == 0){
        println(i * 2);
    } else {
        println(i);
    }
}

20

100

We want to simulate flipping a coin 3 times. What kind of loop should we use?

For Loop

100

We want to simulate constantly flipping a coin until we get 3 heads in a row. What kind of loop should we use?

While loop

100

What will be the output of this program?

var number = 5;
var greater_than_zero = number > 0;

if (greater_than_zero){
    println(number);
}

5

100

How many possible values can Randomizer.nextBoolean() return?

2

200

What is the value of the boolean variable canVote at the end of this program?


var age = 17;
var isCitizen = true;
var canVote = age >= 18 && isCitizen;

False

200

What will the following program print when run?

for (var j = 0; j < 2; j++) {
    for (var i = 6; i > 4; i--){
        println (i);
    }
}

6
5
6
5

200

What is the term for the value that signals the end of user input?

Sentinel

200

What will be the output of this program?

var number = 5;
var greater_than_zero = number > 0;

if (greater_than_zero){
    if (number > 5){
        println(number);
    }
}

Nothing will print

200

Write the code to get a random number between 1 and 99

Randomizer.nextInt(1, 99)

300

What is printed by the following program?


var isRaining = false;
var isCloudy = false;
var isSunny = !isRaining && !isCloudy;

var isSummer = false;
var isWarm = isSunny || isSummer;

println("Is it warm: " + isWarm);

Is it warm: true

300

What is the output of the following program?

var result = 0;
var max = 5;
for(var i = 0; i < max; i++){
    result += i;
}
println(result);

10

300

How many times will the following program print "hello"?


var i = 0;
while(i < 10){
    println("hello");
}

This code will loop infinitely

300

What is printed by the following program?

var numApples = 10;
var numOranges = 5;

if(numApples < 20 || numOranges == numApples){
    println("Hello, we are open!");
} else {
    println("Sorry, we are closed!");
}

println("Sincerely, the grocery store");

Hello, we are open!
Sincerely, the grocery store

300

Consider the following program. What is the range of possible outputs when this runs?

function start(){
    var mysteryNum = 5 * Randomizer.nextInt(2,10);
    println(mysteryNum);
}

5 - 50

400

What will be the output when the following code runs?

function start(){
    var loggedIn = false;
    println("User logged in?: " + !loggedIn);
}

User logged in?: true

400

In the following code, what will be the last number to print to the screen before the program finishes?


for (var i = 0; i < 100; i++) {
    if (i % 2 == 0) {
        println(i);
    } else {
        println(2 * i);
    }
}

198

400

The following code continually asks the user for a password until they guess the correct password, then ends. But there is one problem.

var SECRET_PASSWORD = "karel";


function start() {

    while (true) {

        var password = readLine("Enter your password: ");

        if (password == SECRET_PASSWORD) {

            println("You got it!");

        }

        println("Incorrect password, please try again.");

    }

}

Add a break; statement after line 7 so that the program doesn’t loop infinitely

400

What will the following program print when run?


var above16 = true;
var hasPermit = true;
var passedTest = false;

if (above16 && hasPermit && passedTest){
    println("Issue Driver's License");
} else {
    if (above16 || hasPermit || passedTest) {
        println("Almost eligible for Driver's License");
    } else {
        println("No requirements met.");
    }
}

Almost eligible for Driver’s License

400

Write the code to make a circle move up every time a key is pressed

var circle = new Circle(30);

circle.setPosition(getWidth() / 2, getHeight() / 2);

add(circle);

var MOVE_AMOUNT = 10;

function moveUp() {

    circle.move(0, -MOVE_AMOUNT);

}

keyDownMethod(moveUp);

500

Write a program that gets three variables from the user: their study hours per week, their completed assignments per month, and their participation score out of 10, in that order.

We need to figure out if the student is an outstanding student. They are considered outstanding if they study at least 30 hours per week, OR they study at least 20 hours per week, complete at least 10 assignments per month, and have a participation score of at least 8.

var studyHours = readInt("Enter your study hours per week: ");

var assignmentsCompleted = readInt("Enter your completed assignments per month: ");

var participationScore = readInt("Enter your participation score out of 10: ");


// Check if the student is outstanding

if (studyHours >= 30 || (studyHours >= 20 && assignmentsCompleted >= 10 && participationScore >= 8)) {

    println("You are an outstanding student!");

} else {

    println("Keep working to improve.");

}

500

Write a program that prints the numbers from 1 to 100. But for multiples of 3, print “Fizz” instead of the number, and for the multiples of 5, print “Buzz”. For numbers that are multiples of both 3 and 5, print “FizzBuzz”. If a number is not a multiple of either, print the number itself.

for (var i = 1; i <= 100; i++) {

    if (i % 3 == 0 && i % 5 == 0) {

        println("FizzBuzz");

    } else if (i % 3 == 0) {

        println("Fizz");

    } else if (i % 5 == 0) {

        println("Buzz");

    } else {

        println(i);

    }

}

500

Write a program that uses a loop and a half to simulate flipping a coin, You should keep flipping until they get heads 3 times in a row and then break out of the loop. Print out the result of each flip.

// Function to simulate flipping a coin

function flipCoin() {

    return Randomizer.nextBoolean() ? "Heads" : "Tails";

}


// Variable to keep track of consecutive heads count

var consecutiveHeads = 0;


while (true) { // Loop and a half (infinite loop with break condition)

    var result = flipCoin();

    println(result);


    if (result === "Heads") {

        consecutiveHeads++;

    } else {

        consecutiveHeads = 0; // Reset if it's not heads

    }


    if (consecutiveHeads === 3) {

        break; // Exit loop when heads has appeared 3 times in a row

    }

}

500

What will the following program print when run?

var numberOne = 5;
var numberTwo = 10;

if (numberOne == 5) {
    println(1);
}
if (numberOne > 5) {
    println(2);
}
if (numberTwo < 5) {
    println(3);
}
if (numberOne < numberTwo) {
    println(4);
}
if (numberOne != numberTwo) {
    println(5);
}

1
4
5

500

write the code to make a square change colors every time spacebar is pressed

var square = new Rectangle(50, 50);

square.setPosition(getWidth() / 2 - 25, getHeight() / 2 - 25);

add(square);

keyDownMethod(function(e) {

    if (e.keyCode === Keyboard.SPACE) {

        square.setColor(Randomizer.nextColor());

    }

});