wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Scanner & If Statements

Total questions: 18

Worksheet time: 18mins

Name
Class
Date
1.
 Evaluate (to true or false) each of the following expressions:
14 >= 14
15 > 15
16 != 16
18 >= 19
a)
T, F, F, F
b)
T, T, T, T
c)
F, T, F, F
d)
F, T, T, F
2.
Evaluate the following expression:
2 + 2 == 4
a)
true
b)
false
c)
sometimes true
d)
only true on sundays
3.
Evaluate the following expression
Math.pow(2, 3) >= 9;
a)
true
b)
false
c)
compile error
d)
only true when the Patriots win
4.

True/False: The body of an "if" statement will only be executed if the condition is true.

a)

True

b)

False

5.

What happens to the code in the body of an "if" statement if the condition is false?

a)

The computer determines if it is essential to the program and if it is, then the code is executed anyway.

b)

The code moves to a coastal city, attempting to find love and start a family. However, low waged and crushing mortgage rates lead to it moving back home where it sits, hoping that one day it will be executed.

c)

The code is skipped.

d)

The code stages a revolution and attempts to take over the program, ultimately unsuccessfully, which leads to it not being executed.

6.

Which of these can be paired with an "if" statement to represent other outcomes of a scenario?

a)

or

b)

else

c)

but

d)

sheboygan

7.

Which symbol means "not equal to"?

a)

==

b)

<=

c)

>=

d)

!=

8.

Which of the following is the correct way to instantiate a Scanner that receives input from the user?

a)

Scanner in = new Scanner(System.in);

b)

Scanner in = System.in();

c)

scanner in = Scanner(System.in);

d)

import java.util.Scanner;

9.

Which of the following scanner methods reads the next whole number?

a)

nextLine()

b)

next()

c)

nextInt()

d)

nextNumber()

10.

If both a and b are true, what is the outcome of the following statement?

(!a&&b) || a

a)

true

b)

false

11.

What is the value of num after this statement is executed?


int num = 23;

if(num>20) { num = 20; }

else { num = 30; }

a)

20

b)

30

c)

23

d)

true

12.

Which of the following should replace the <1> to correctly receive any string received from the user?

Scanner s = new Scanner(System.in);

String sent = <1>;

System.out.println(sent);

a)

nextLine();

b)

s.nextInt();

c)

s.nextDouble();

d)

s.nextLine();

13.

Which of the following scanner methods receives the next decimal?

a)

Scanner.nextInt();

b)

Scanner.nextDouble();

c)

Scanner.next();

d)

Scanner.nextLine();

14.

which of the following has the boolean value of true?

a)

3 = 3

b)

!(3 == 3)

c)

"foo".isEqualTo("foo")

d)

true || false

15.

which is the correct way to write an if statement?

a)

if foo: bar

b)

if(foo)

{ bar }

c)

if(foo): bar

d)

if foo then bar

16.

If the user inputs:

Karl is 17 he has a 93.45 in spanish

and the code reads:

Scanner s = new Scanner(System.in);

String answer = s.next();


what is the value of answer?

a)

17.0

b)

"is 17 he has a 93.45 in spanish"

c)

"Karl"

d)

93.45

17.

what does this peice of code evaluate to?

boolean raining = true;

String answer;

if(!(raining)) {answer = "Yay!"}

Else {answer = "Boo!"}

a)

Yay!

b)

Boo!

c)

true

d)

false

18.

What would be the value of num?

int num = 0;

if(num>3) { num = 2; }

if(num<3) {num = 3; }

if (num == 0) { num = 12; }

a)

2

b)

3

c)

12

d)

error