Unit 3: Boolean Expressions & if Statements Flashcards

1
Q

Under what conditions does an if statement execute?

A

An if statement executes when the boolean expression within the parentheses is true.

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

How do you compare Strings?

A

You compare strings through the “.equals()” method, which returns a boolean expression.

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

What is the difference between = and == ?

A

= is used to assign values

== is used when checking equality

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

Which of the following symbols is the “not equal” symbol?

a) =!
b) ==
c) !=
d) !!

A

c) !=

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

Under what conditions does an else statement execute?

A

Else statements execute if the initial boolean expression evaluates to false

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

What is the purpose of an else if statement?

A

Else if statements allow us to incorporate additional conditions to if-else statements

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

Will it return true or false?

!(true && false) || (false && false)

A

true

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

What are the 3 logical operators?

A

NOT !
AND &&
OR ||

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

What is the not operator?

A

!(not) evaluates a condition to the opposite boolean value

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

Are both of these equivalent?

p && !q || !p && q
p || q) && (!p || !q

A

yes

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

What is the and operator?

A

The &&(and) evaluates whether all conditions are true

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

What is the or operator?

A

|| (or) evaluates whether one condition is true

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

Do boolean operators also have an order of operations?

A

Yes!

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

What would the output be for the following code

double x = 15.0/11.0;
System.out.println(11.0*x == 15.0);

A

False

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

Is this statement true?

!(a && b) == !a or !b

A

True statement, De Morgan’s law

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

When && and || are combined in one logical expression, which one has a higher rank?

A

&& has a higher rank

17
Q

How do you do the opposite of De Morgan’s law?

!(a || b) == ?????????

A

!a and !b, distribute negation and flip symbol

18
Q

The boolean expression
a[i] == max || !(max != a[i])
can be simplified to:

(A) a[i] == max
(B) a[i] != max
(C) a[i] < max || a[i] > max
(D) true
(E) false
A

(A) a[i] = max

19
Q

When does short circuit evaluation come into play?

A

When there is multiple conditions.

Also, the conditions are run in order:

Not all conditions are ran

20
Q

What will this code return given checkMethod(false, true)?

public int checkMethod(boolean x, boolean y)
{
    if(!x)
    {
        if(y)
        {
            return -1;
        }
        else
        {
            return 1;
        }
    }
    else
    {
        return 0;
    }
}
A

-1

21
Q

Given the following call, what value would be returned for findTheMiddle(2110890125)?

public static String findTheMiddle(int number)
{
String stringNum = “” + number;
int mid = stringNum.length()/2;

    if(stringNum.length() % 2 == 1)
    {
        return stringNum.substring(mid,mid+1);
    } 
    else
    {
        return stringNum.substring(mid-1,mid+1);
    }
}
A

89

22
Q

What will this program print if the value of grade is 80?

if(grade > 90)
{
    System.out.println("A");
}
else if(grade > 80)
{
    System.out.println("B");
}
else if(grade > 70)
{
    System.out.println("C");
}
A

C