AP Computer Science A - Boolean Expressions and If Statements (Unit 3) Test Review
e is the correct answer since in the first if statement, low is set to c and in the second one, it is set again to a, causing a to be the lowest value that is substracted, but this isn't correct since a was the largest value
*Question from AP Classroom:* Assume that the int variables a, b, c, and low have been properly declared and initialized. The code segment below is intended to print the sum of the greatest two of the three values but does not work in some cases. if (a > b && b > c) { low = c; } if (a > b && c > b) { low = b; } else { low = a; } System.out.println(a + b + c - low); For which of the following values of a, b, and c does the code segment NOT print the correct value? a) a = 1, b = 1, c = 2 b) a = 1, b = 2, c = 1 c) a = 1, b = 2, c = 3 d) a = 2, b = 2, c = 2 e) a = 3, b = 2, c = 1
d is the correct answer because when num, min, and max are the same value, isSmaller is set to false and isBigger is set to false, so when it is checked if isSmaller is equal to isBigger, we get a true value, so inRange is set to true, when it shouldn't have been because num is equal to min and max and it is NOT between them
*Question from AP Classroom:* Consider the following code segment, which is intended to set the Boolean variable inRange to true if the integer value num is greater than min value and less than max value. Otherwise inRange is set to false. Assume that inRange, num, min, and max have been properly declared and initialized. boolean isBigger; boolean isSmaller; boolean inRange; if (num < max) { isSmaller = true; } else { isSmaller = false; } if (num > min) { isBigger = true; } else { isBigger = false; } if (isBigger == isSmaller) { inRange = true; } else { inRange = false; } Which of the following values of num, min, and max can be used to show that the code does NOT work as intended, and why? a) num = 20, min = 30, and max = 50 b) num = 30, min = 20, and max = 40 c) num = 40, min = 10, and max = 40 d) num = 50, min = 50, and max = 50 e) num = 60, min = 40, and max = 50
-b, because the expression (a == !b) evaluates to true because true is equal to not false. The value true is printed because true is not equal to false. -note: e would not be the right answer since boolean expressions ARE VALID parameters to print statements
*Question from AP Classroom:* Consider the following code segment. boolean a = true; boolean b = false; System.out.print((a == !b) != false); What is printed as a result of executing this code segment, and why? a) false b) true c) 0 d) 1 e) Nothing is printed because the expression (a == !b) != false is an invalid parameter to the System.out.print method.
-c is the correct answer since quant is greater than 10, but since it isn't greater than 20, the second if statement isn't initiated, and so unitPrice is set to 3, and ship's value remains at 8 -hence, total = 20 times 3 + 8 = 68 -note: the second if statement is different and not a part of the first one since everytime we write an "if" (not an else if) statement on a new line, we initiate a completely new if statement that is different from the previous one (this doesn't apply to else if statements)
*Question from AP Classroom:* Consider the following code segment. int quant = 20; int unitPrice = 4; int ship = 8; int total; if (quant > 10) { unitPrice = 3; } if (quant > 20) { ship = 0; } total = quant * unitPrice + ship; What is the value of total after this code segment has been executed, and why? a) 20 b) 60 c) 68 d) 80 e) 88
d is the correct answer because the expressions a < b and c < b both evaluate to true, and therefore, the larger expression becomes true != true, which evaluates to false
*Question from AP Classroom:* Consider the following variable declarations and initializations. int a = 2; int b = 6; int c = 3; Which of the following expressions evaluates to false, and why?: a) a < b == c < b b) a > b == b < c c) a < b != b < c d) a < b != c < b e) a > b != b > c
De Morgan's Law
-a method of simplifying boolean logic using the not (!) logical operator -for example, !(A || B) is the same as !A && !B when the logical operator ! is distributed
Short circuit evaluation
-when a boolean expression is evaluated the evaluation starts at the left hand expression and proceeds to the right, stopping when it is no longer necessary to evaluate any further to determine the final outcome -it is essentially a "short-cut" used by Java -this is useful for doing division and trying to check if something is not being divided by 0 before proceeding to the division calculation -for example, if you suppose x = 0, then the following statement will not cause an error because Java first checks if x is not equal to 0, and Java won't bother doing the division since when using the && logical operator, if one thing is false, then the whole statement is false, and since the first part already evaluated to the false, regardless of what the second part is, the whole statement will always evaluate to false (hence, Java doesn't evaluate division part, and NO ERROR IS CAUSED): if (x != 0 && y/x = 10)
on equality and/or comparisons
Decisions in if statements are generally based on what?
no it does not
Does the if(condition) part of the if statement have a semicolon after it?
no, it doesn't work for primitive data types
Does using the .equals() method work when comparing primitive data types?
it checks to see if the radii of the two Circle objects are the same
For the Circle class, what does the .equals() method check?
it checks if both Rectangle objects have the same length and width
For the Rectangle class, what does the .equals() method check?
it checks if both RectanglePolygon objects have the same number of sides and side length
For the RegularPolygon class, what does the .equals() method check?
-it checks whether both String objects have the same exact characters (case sensitive) in the same order -for example: String a = "a" if (a.equals("A") -the code above would return false, and so the code in the if statement wouldn't be performed since "a" is not the EXACT same as "A"
For the String class, what does the .equals() method check?
-you distribute the ! to stuff inside the parentheses and also properly switch the logical and relational operators that were inside the parentheses -ex: !(x > 5 || x == 2) after applying De Morgan's Law would be x <= 5 && x != 2
How do we use De Morgan's Law?
1. Write out all combinations of the terms 2. Evaluate the logic statements for each combination of terms (you can use T/F or 1/0, where 1 is for T and 0 is for F)
How do you build a truth table?
they are two way selections
If - else statements are which type of selection?
A || B = 1, which is the exact same as saying A || B = true
If A = 1 and B = 0, then what does A || B equal?
A && B is equal to false, which is the same as saying A && B = 0
If A = false and B = false, then what does A && B equal?
A || B = false, which is the same as saying A || B = 0
If A = false and B = false, then what does A || B equal?
A && B equals false, which is the same as saying A && B = 0
If A = false and B = true, then what does A && B equal?
A || B is equal to true, which is the same as saying A || B = 1
If A = false and B = true, then what does A || B equal?
!A = true, which is the same as saying !A = 1
If A = false, then what does !A equal?
A && B = false, which is the same as saying A && B = 0
If A = true and B = false, then what does A && B equal?
A && B = true, which is the same as saying A && B = 1
If A = true and B = true, then what does A && B equal?
A || B = true, which is the same as saying A || B = 1
If A = true and B = true, then what does A || B equal?
!A = false, which is the same as saying !A = 0
If A = true, then what does !A equal?
a false boolean value would be returned since they don't have the same reference
If two objects that didn't have the same reference, were to be compared by the == operator, then what would happen?
no, because Java thinks that the if statement ends at the semicolon, and anything after that semicolon is outside of the if statement
In an if statement without curly brackets, can you have semicolon after one of the lines of code inside the if statement? Why?
we use the .equals() method, because using this we don't just compare the object's reference, and we can compare the value of the objects
Instead of using the == operator, what do we use to compare objects, specifically? Why?
it depends on the class of the object
The way the .equals() method when comparing objects of a certain class data type, depends on what?
True
True or False: Every class data type has the .equals() method built in?
using the logical operators (||, &&, !)
We can build compound expressions inside if statements using what?
to compare data and expressions and these operators return a boolean value such as true or false
We can use the equality and comparison operators to compare what? What type of value do these operators return?
boolean operators
What are logical operators also known as?
!=, ==, <, <=, >, >=
What are the 6 relational operators?
-one of the situations is when we are checking if an object has the null value set to it -the second situation is when checking if two object are aliases (meaning that both have the same reference and point to the same object, and this really only applies when one object is set equal to another one) -for example, the following is a situation where two objects are aliases and using the == operator actually works as it is supposed to, and returns the true boolean value: String x = "letter" String y = x //in this line, y is set equal to x, so both String objects have the same reference if (x == y)
What are the only two times that using the == or != operators works with objects (class data types)?
||, &&, !
What are the three logical operators?
to choose which statements in a program will be executed under specific conditions
What do conditionals give us the power to do?
!A || !B
What does !(A && B) simplify to, according to De Morgan's Law?
!A && !B
What does !(A || B) simplify to, according to De Morgan's Law?
-it checks if one thing is not equal to another thing -for example, in the following code, we check if a is not equal to b: if (a != b)
What does the != relational operator do?
-it checks if one thing is less than another thing -for example, in the following code, we check if a is less than b: if (a < b)
What does the < relational operator do?
-it checks if one thing is less than or equal to another thing -for example, in the following code, we check if a is less than or equal to b: if (a <= b)
What does the <= relational operator do?
-it checks if one thing is equal to another thing -for example, in the following code, we check if a is equal to b: if (a == b)
What does the == relational operator do?
-it checks if one thing is greater than another thing -for example, in the following code, we check if a is greater than b: if (a > b)
What does the > relational operator do?
-it checks if one thing is greater than or equal to another thing -for example, in the following code, we check if a is greater than or equal to b: if (a >= b)
What does the >= relational operator do?
it means "not"
What does the logical operator ! mean?
-it means "and" -for example, in the following code, we only execute the stuff inside the if statement, if a = 1 and b = 2: if (a == 1 && b == 2)
What does the logical operator && mean?
-it means "or" -for example, in the following code, we only execute the stuff inside the if statement if a = 1 or b = 2: if (a == 1 || b == 2)
What does the logical operator || mean?
An if statement is like a gate and the code can only happen if the question/condition is true
What is an analogy that can be used to describe if statements and how they function?
for doing division and trying to check if something is not being divided by 0 before proceeding to the division calculation
What is an important thing that short circuit evaluation can be used for?
||
What is the logical opposite of (&&)?
&&
What is the logical opposite of (||)?
it has higher precedence than the other relational and logical operators, meaning that it is evaluated before the all the other relational and logical operators
What is the precedence of the ! logical operator in comparison to the the other logical operators (&& and ||) and all of the relational operators (!=, ==, <, <=, >, >=)?
-they have lower precedence than the relational operators, meaning that they are evaluated after the relational operators -for example, in the following code, we would first check if a > 1 and if b = 1 and then check if both those conditions are true at the same time, and only then would java execute the stuff inside the if statement: if (a > 1 && b == 1)
What is the precedence of the && and || logical operators in comparison to all of the relational operators (!=, ==, <, <=, >, >=)?
==
What is the relational opposite of (!=)?
>=
What is the relational opposite of (<)?
>
What is the relational opposite of (<=)?
!=
What is the relational opposite of (==)?
<=
What is the relational opposite of (>)?
<
What is the relational opposite of (>=)?
if (condition) { //statements } //other code -in the above syntax, //statements is where your code would be written for when the if statement's condition is true, and the //other code is just stuff outside the if statement -in the above syntax, the condition such as a == 1 would go inside the parentheses -note that in the above syntax, the curly brackets can either come right after the parentheses or where they are currently situated and also the spaces inside the parentheses or after the "if" part don't matter -another thing to note is that you don't need to have the curly brackets but it's a good habit to have them because if you don't then you need to make sure that you don't have any semicolons ; for the code that goes inside the if statement -a final thing to note is that the code inside the if statement (which is represented by //statements) will only be executed when the condition returns a true boolean value
What is the syntax for an if statement?
if (condition1) { //statements (only executes when condition1 is true) } else if (condition2) { //statements (only executes when condition1 is false and condition2 is true) } else { //statements (only executes when both condition1 and condition2 are false) } //other code -in the above code, the code inside the else if statement is only executed when condition1 turns out to be false and condition2 turns out to be true -in the above code, the code inside the else statement is only executed when conditions 1 and 2 are both false -the //other code is stuff outside of the if and else if statements -note that there can be as many else if statements as you want, but there can only be a single if statement and else statement
What is the syntax of an if - else if - else statement?
if (condition1) { //statements (only executes if condition1 is true) } else if (condition2) { //statements (only executes if condition1 is false and condition2 is true) } //other code -in the above code, the code inside the else if statement is only executed when condition1 turns out to be false and condition2 turns out to be true -note that there can be multiply else if statements with their own conditions but only a single if statement -the //other code is stuff outside of the if and else if statements
What is the syntax of an if - else if statement?
if (condition) { //statements (only executes if condition is true) } else { //statements(only executes if condition is false) } //other code -in the above code, the else statement is only executed if the condition inside the if statement turns out to be false -the //other code is stuff outside of the if statement and the else statement
What is the syntax of an if - else statement?
an if statement is a control structure and it controls the sequence of execution in our code
What type of structure is an if statement? What does it control?
-we are comparing the the value stored in the variables for the two primitive data type variables being compared, or for class data types, we are comparing the references for the objects -hence, when the references don't match when comparing objects, we end up getting returned a false boolean value even if the objects themselves store the same value
When using the == operator, what are we comparing?
because when using the == operator, we compare the actual memory reference of the object, and so even though two objects might have the same value, they can different references, so we will be returned a false value regardless
Why don't we use the == operator when comparing objects (class data types)?