Module 03: Boolean Expressions and if Statements Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

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

A

-1

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

What will this method call output given yesOrNo(true)?

A

“Yes”

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

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

A

89

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

Which expression is true?

  1. true && !true
  2. !false || !true
  3. true && false
  4. false || false || !true
A
  1. !false || !true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What will this code output?

  1. Hello Karel
  2. Hello Karel
    Second if statement!
  3. Second if statement!
  4. This program will print nothing
A
  1. Second if statement!
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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

A

C

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

What will the values of x and y be after this code segment runs?

  1. x = 100
    y = 200
  2. x = 99
    y = 100
  3. x = 101
    y = 201
  4. x = 99
    y = 199
A
  1. x = 99
    ​y = 199
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

A company uses the following table to determine pay rate based on hours worked:

Which of the following test cases can be used to show that the code does NOT work as intended?

  1. calculateRate(35);
  2. calculateRate(40);
  3. calculateRate(45);
  4. calculateRate(55);
A
  1. calculateRate(40);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Assuming a and b are properly initialized boolean values, which expression would be equivalent to the following:

!(a && b)

  1. a || b
  2. !(a || b)
  3. !a && !b
  4. !a || !b
A
  1. !a || !b
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

A student is trying to determine if the following two expressions are equivalent.
A. x && (!x || y)
B. x && !(x || !y)
What values of x and y would prove that the expressions are NOT equivalent?

x = true
y = true
x = true
y = false
x = false
y = true
x = false
y = false
A
x = true
y = true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Given a, b, and c are properly initialized boolean values, what values would make the following expression false?

(a || b) || (b || c) || (!a || b);

  1. a and b must be different values
  2. a must be false
  3. b and c must have the same values
  4. Nothing. The expression will always be true.
A
  1. Nothing. The expression will always be true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

In order to ride the zip line, you need to be at least 12 years old and weigh at least 75 pounds, but no more than 300 pounds.

Which of the following code segments would correctly determine if you can ride the zip line?

I and II only

I only

II only

III only

I, II, III

A

II only

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

What will the following code output when executed?

A

BC

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

The following code is intended to return only even numbers. If an even number is passed to the method, it should return that number. If an odd number or zero is passed, it should return the next highest even number.

Does the code work as intended?

  1. Yes.
  2. No, the mod function on line 3 should read number % 2 == 1 to find even numbers.
  3. No, the else if on line 7 and the else on line 11 should just be if statements.
  4. No. Zero will get returned on line 5 and not make it to line 7.
A
  1. No. Zero will get returned on line 5 and not make it to line 7
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Given the following statements, which options will print true?

  • String ursa = new String(“2849”);*
  • String major = new String(“2849”);*

I. System.out.println(ursa.equals(major));
II. System.out.println(ursa == major);
III. System.out.println(ursa.equals(“2849”));
IV. System.out.println(major == “2849”);

I, II, III, and IV

IV only

I, III, and IV only

II and IV only

I and III only

A

I and III only

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

What will the final values of x and y be if their initial values are:

x = 12;

y = 5;

A
x = 24
y = 20
17
Q

Assuming weekday and holiday are properly initialized booleans, which expression would be equivalent to the following:

!(!weekday || holiday)

  1. weekday || !holiday
  2. weekday && !holiday
  3. !weekday && !holiday
  4. !(weekday || !holiday)
A
  1. weekday && !holiday
18
Q

The following method is designed to return true if the passed phrase contains either the word cat or dog.

Which of the following test cases can be used to show the code does NOT work as intended?

  1. containsPet(“I have a dog.”);
  2. containsPet(“I don’t have pets.”);
  3. containsPet(“I can catch fish.”);
  4. containsPet(“My dog caught my cat”);
A
  1. containsPet(“I can catch fish.”);
19
Q

What will the following code output when executed?

A

Long Word

Medium Word

20
Q

What values for x and y will cause the program to execute the /* missing code */?

A
x = 12
y = 12
21
Q

Look at the following code and explain what the result is for each print statement and why.

A

System.out.println(s1 == s2);

// True: s2 is refering to the same object as s1, so the “==” operator will return as true since the two strings are referring to the same address.

System.out.println(s1 == s3);

// False: s1 is a string while s3 is an object, so they are not referring to the memory address

System.out.println(s4 == s3);

// False: since s4 is a new string - though it is referring to the value of s3 - it is allocated its own address; hence they are not the same memory location and therefore not equal.

System.out.println(s4 == s5);

// True: since s5 is referring to the memory of s4 so the address comparison is true (compared to the line above where the content was equal but not the address)

System.out.println(s1.equals(s2));

// True: the value is true since s1 and s2 have the same content so they content is equal.

System.out.println(s1.equals(s3));

// True: though one is a string and one is an object, both have the same content, notably “Hello,” so they will be true through a content comparison

22
Q

What are rational operators?

A

==, !=, , <=, >=

Allow for the comparison or primitive type values.

Results are stored as Boolean values

23
Q

What are Boolean operators?

A

Booleans can also use Relational Operators to store true/false information

Relational Operators are used to comparing the values of two expressions

24
Q

What is an equality operator?

A

==

Returns true is both the operators are referring to the same object

25
Q

What are if statements?

A

if Statements are used when there are a set of statements that should be executed only when certain conditions are met.

if (boolean expression)

{

//execute statements if condition is true

}

  • the boolean expression is a condition that is being evaluated in the if statement
  • The condition must be within in the parentheses()
26
Q

What are if-else statements?

A

if (boolean expression)

{

//execute statements if condition is true

}

else

{

//execute statement if condition is false

}

  • Fix inefficiency by using else statements
  • Execute the initial boolean expression evaluates to false
  • simplifies execution steps
27
Q

What are else-if statements?

A

if(booleanExpression)

{

//code to execute if the boolean expression is true!

}

else if(booleanExpressionTwo)

{

//code will execute if else if boolean statement is true!

//Only test is boolean expression one is false

}

else

{

//code will execute if all boolean expressions are false

}

when there are more than two possible conditional scenarios

else if Statements allow us to incorporate additional conditions to our if-else statements!

28
Q

What are logical operators?

A

Can be used to connect boolean expressions to make more complex expressions:

NOT: ! -! evaluates a condition to the opposite boolean value

AND: && - && evaluates whether all conditions are true

OR: || - II evaluate whether one condition is true

29
Q

What are Short Circuit Evalution?

A

Evaluating only the first boolean expression

30
Q

What are Nested if statements?

A

Nesting if statements mean we can place if statements within other if statements

31
Q

What is De Morgan’s Law?

A
  • Show how we can negate “and”s and “or”s in compound Boolean statements:*
  • not(A and B) is the same as (not A) or (not B):

!(A && B) == !A || !B

  • not(A or B) is the same as (not A) and (not B):

!(A || B) == !A && !B

32
Q

What are aliases?

A

Two objects references are considered aliases when they both refer to the same object

33
Q

What is reference equality?

A

Equality operator (==) compares the reference (address in memory) of 2 object

34
Q

What is logical equality?

A

Compares the data of the objects instead of the value of the references.

Uses the .equals() method

35
Q

How does object storage work?

A

In memory, the variable simply stores a location or a reference to where the actual object data is located

Variable points to the object data

36
Q

What is the difference between == and .equals() in java?

A

In general, both equals() and “==” operator in Java are used to compare objects to check equality but here are some of the differences between the two:

  • The main difference between the .equals() method and == operator is that one is a method and the other is the operator.
  • We can use == operators for reference comparison (address comparison) and .equals() method for content comparison. In simple words, == checks if both objects point to the same memory location whereas .equals() evaluates to the comparison of values in the objects.
37
Q
A