Search Header Logo
JavaScript Control Structures

JavaScript Control Structures

Assessment

Presentation

Computers

9th Grade

Practice Problem

Medium

Created by

DeLaina Pruitt

Used 12+ times

FREE Resource

1 Slide • 14 Questions

1

JavaScript Control Structures

by DeLaina Pruitt

2

Multiple Choice

What will the following program print when run?

for (var j = 0; j < 2; j++) {

for (var i = 6; i > 4; i--){

println (i);

1

4

5

4

5

2

6

5

4

6

5

4

3

6

5

6

5

4

0

2

6

4

3

Multiple Choice

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.");

}

}

1

Change the true in line 4 to be password != SECRET_PASSWORD so that the program doesn’t loop infinitely

2

Change the true in line 4 to be password == SECRET_PASSWORD so that the program doesn’t loop infinitely

3

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

4

Put line 9 inside of an else statement

4

Multiple Choice

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

var age = 17;

var isCitizen = true;

var canVote = age >= 18 && isCitizen;

1

True

2

False

3

Yes

4

none, there is a syntax error in this code

5

Multiple Choice

What will be the output of this program?

var number = 5;

var greater_than_zero = number > 0;

if (greater_than_zero){

println(number);

}

1

0

2

5

3

True

4

Nothing with print

6

Multiple Choice

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);

}

}

1

0

2

5

3

True

4

Nothing will print

7

Multiple Choice

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);

1

Is it warm: true

2

Is it warm: false

3

Is it warm: yes

4

Is it warm: no

8

Multiple Choice

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");

1

Hello, we are open! Sincerely, the grocery store

2

Sorry, we are closed!

Sincerely, the grocery store

3

Hello, we are open!

4

Sorry, we are closed!

5

Sincerely, the grocery store

9

Multiple Choice

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.");

}

}

1

Issue Driver’s License

2

Almost eligible for Driver’s License

3

No requirements met.

4

Nothing will print

10

Multiple Choice

We want to print the phrase “CodeHS is the best” exactly 25 times. What kind of control structure should we use?

1

If statement

2

While loop

3

Break statement

4

For loop

11

Multiple Choice

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);

1

15

2

10

3

0

4

12345

12

Multiple Choice

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

1

2

1

3

5

3

1

4

4

1

4

5

13

Multiple Choice

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);

}

}

1

10

2

20

3

30

4

60

14

Multiple Choice

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

1

While loop

2

For loop

3

Variable loop

4

Infinite loop

15

Multiple Choice

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

var i = 0;

while(i < 10){

println("hello");

}

1

9

2

11

3

This code will loop infinitely

4

10

JavaScript Control Structures

by DeLaina Pruitt

Show answer

Auto Play

Slide 1 / 15

SLIDE