You are on page 1of 6

Lecture 6

T/F

1 One if can have more than one else clause. F


2 In if statement; the condition must be an expression of type T
Boolean.

1. How many choices are possible when using a single if-else statement?
a. 1
b. 2
c. 3
d. 4
2. What does the following code fragment write to the monitor?
int sum = 14;
if ( sum < 20 )
System.out.print("Below ");
else
System.out.print("Over ");
System.out.println("the limit.");
a. Below
b. Over
c. Below the limit.
d. Over the limit.
3. What does the following code fragment write to the monitor?
int sum = 14;
if ( sum < 20 )
System.out.print("Below ");
else
{
System.out.print("Over ");
System.out.println("the limit.");
}
(Notice that the program has changed from the previous question!)
a. Below
b. Over
c. Below the limit.
d. Over the limit.

Page 1 of 6
4. What does the following code fragment write to the monitor?
int sum = 94;
if ( sum < 20 )
{
System.out.print("Below ");
System.out.println("the limit.");
}
else
{
System.out.print("Over ");
System.out.println("the limit.");
}
(Notice that the program has changed from the previous question!)
a. Below
b. Over
c. Below the limit.
d. Over the limit.

5. What does the following code fragment write to the monitor?


int sum = 7;
if ( sum > 20 )
{
System.out.print("You win ");
}
else
{
System.out.print("You lose ");
}
System.out.println("the prize.");
(Notice that the program has changed from the previous question!)
a. You win
b. You lose
c. You win the prize.
d. You lose the prize.

Page 2 of 6
6. What does the following code fragment write to the monitor?
int sum = 21;
if ( sum == 20 )
{
System.out.print("You win ");
}
else
{
System.out.print("You lose ");
}
System.out.println("the prize.");
(Notice that the program has changed from the previous question!)
a. You win
b. You lose
c. You win the prize.
d. You lose the prize.

7. What does the following code fragment write to the monitor?


int sum = 21;
if ( sum != 20 )
System.out.print("You win ");
else
System.out.print("You lose ");
System.out.println("the prize.");
(Notice that the program has changed from the previous question!)
a. You win
b. You lose
c. You win the prize.
d. You lose the prize.

8. A sequence of statements contained within a pair of braces ("{" and "}") is


called a:
a. block
b. blob
c. branch
d. brick

Page 3 of 6
9. Evaluate (to true or false) each of the following expressions:
14 <= 14 14 == 14
a. true true
b. false true
c. true false
d. false false
10.Evaluate (to true or false) each of the following expressions:
14 >= 14 14 < 14
a. true true
b. false true
c. true false
d. false false
11.Evaluate (to true or false) each of the following expressions:
-25 > -9 -9 > -25
a. true true
b. false true
c. true false
d. false false
12.Evaluate (to true or false) each of the following expressions:
14 > 14 -25 > -9
a. true true
b. false true
c. true false
d. false false
13.Say that value has a 19 stored in it, and that extra has a 25 stored in it.
Evaluate (to true or false) each of the following expressions:
value <= extra extra == value
a. true true
b. false true
c. true false
d. false false
14.Say that value has a 19 stored in it, and that extra has a 25 stored in it.
Evaluate (to true or false) each of the following expressions:
value >= extra extra > value
a. true true
b. false true
c. true false
d. false false

Page 4 of 6
15.Say that value has a 19 stored in it, and that extra has a 25 stored in it.
Evaluate (to true or false) each of the following expressions:
value != extra extra > value
a. true true
b. false true
c. true false
d. false false
16.Say that value has a 19 stored in it, and that extra has a 25 stored in it.
Evaluate (to true or false) each of the following expressions:
value >= extra extra == value
a. true true
b. false true
c. true false
d. false false
17.The Java if statement:
a. Evaluates whether an expression is equal or not
b. Evaluates whether an expression is true or false
c. Evaluates whether an expression is less than or more than a number
d. None of the above
18.Which of the following is a decision-making statement?
a. if-else
b. do-while
c. for
d. while
19.Which of these is a selection statement in Java?
a. for
b. while
c. if-else
d. do-while
20.The conditional statement; ........... can evaluate any type of Boolean
expression
a. for
b. while
c. do-while
d. if-else

Page 5 of 6
21.What will be the output of the following code fragment?
int a=25;
int b=15;
if((a15)
System.out.println(a);
else
System.out.println(b);
a. Error
b. 1525
c. 15
d. 25

22.What will be the output of the following code fragment?


int a=25;
int b=15;
if(a==17)
System.out.println(a);
else
System.out.println(b);
(Notice that the program has changed from the previous question!)
a. Error
b. 17
c. 15
d. 25

Page 6 of 6

You might also like