if else Flashcards

1
Q

Correct the following:
if ( grade >= 60 ) ;

A

logic error, in if-else statement you must not include a semicolon after “if” nor after “else”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What makes a one way selection in an if-else statement/program?

A

it includes only the if statement without the else
ex:
if (grade >= 90 )
System.out.println( “pass”) ;

System.out.println(“end.”) ;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

if you want to add two messages to be printed after the if statement what do you?

A

you put them in a block via ( { ), ex:
if (grade >= 90 ){
System.out.println( “pass”) ;
System.out.println(“congrats.”) ;
}
System.out.println(“done.”) ;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Correct the following:
if ( grade >= 60) ;
System.out.println(“bark bark.”) ;
else
System.out.println(“meow.”) ;

A

compile error, when we put a semicolon after an if statement and there is an else statement after it then it is a compile error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

T/F: writing [ default : ] in a switch code is optional.

A

TRUE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

T/F: you can use a switch code to write an if-else code.

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to write a switch code

A

switch ( — )
{
case —— :
System.out.println(“”) ;
break ;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

You are not allowed to use a switch code on ________ or _________ variable.

A

boolean, double

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

T/F: You are allowed to use int, char, and string variable for a switch code.

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

T/F: you have to write a break for a switch code.

A

false, you dont have to.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

T/F: Order doesn’t matter in a switch code as long as they’re numbered.

A

false, order of the cases does matter, and the numbering doesn’t.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is th purpose of Math.random() ?

A

it gives you a random number within the range of (0 - one digit less than the number you multiplied it by)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

guess the range:
Math.random() * 100

A

0 - 99

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

correct the following:
int num1 = Math.random() * 100 ;

A

int num1 = (int) (Math.random() * 100 ) ;

we must always cast the math random outcome

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

when you have && you skip the second statement if ……

A

the first is false.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

T/F: when you have &
you skip the first statement.

A

false, you don’t skip the first or the second statement.

17
Q

rewrite this code using the condition operator:
If( result == answer )
System.out.println( “ Correct “ ) ;
else
System.out.println( “ Error “ ) ;

A

System.out.println( result == answer ? “Correct” : “Error” ) ;

18
Q

in the condition operator, the semicolon is the equivalent of an __________ in an if-else statement.

A

else statement

19
Q

write the code for a char type variable input

A

char ch = input.next().charAt(0) ;

20
Q

print the outcome:
int x=1; int y=2;
if ((x=0)==0)
System.out.println(“111”);
else if (x==1)
System.out.println(“222”);
else
System.out.println(“333”);

A

111