Preview

17 - Boolean Expressions and Ifs

 1. Boolean expressions are often used to _____ numbers, boolean values, String values, other objects and data types

  contradict

  compare

  deete

  complement

 2. Given the following code, the following boolean expression would evaluate to 'true'. varThree > varTwo
boolean varOne = false;
int varTwo  = 2;
int varThree = 20;

  True

  False

 3. Refer to the code above. The following expression would evaluate to true. varTwo > varThree

  FALSE

  TRUE

 4. Once again, refer to the code in question 2, which statement is correct about the following boolean expression: varThree + varTwo > varOne

  It is incorrect because you cannot compare boolean and int values using '<'.

  It is correct and evaluates to False

  It is incorrect because both variables start with 'var' which is a reserved word.

  It is correct and evaluates to True.

 5. It is possible to use the logical complement(!) on an int value. E.g. !varTwo

  TRUE

  FALSE

 6. Boolean variables are variables that can be either 'true' or 'false'.

  TRUE

  FALSE

 7. Consider the code below. What is the output?
int a = 14;

int b = -15;

int c = 22;

int d = 11;

if(a > 12 && b < -14 && c < d) {

        System.out.println("This is option 1");

} else if (d - 20 < b) {

        System.out.println("GO DO MORE PROGRAMMING!");

} else if(a + 12 >= c) {

        System.out.println("A moose is on the loose");

} else {

        System.out.println("This is very fun!");

}}

  This is option 1

  This is very fun

  A moose is on the loose

  GO DO MORE PROGRAMMING

 8. Consider the code below. What is the output?
int i = 45, j = -12;

String s = "This is great!";

String s2 = "Where are you?";

if(s2.charAt(4) == ' ' && (i < 4 || j > -43) && !s2.equals("Wonderful")) {

     System.out.println("Amazing");

} else if (s2.charAt(5) == 's') {

     System.out.println("Perfect");

} else if (j + 12 != 0) {

     System.out.println("Great");

} else if(i - 20 < 100) {

     System.out.println("I'm hungry");

} else {

     System.out.println("I'm tired");

}

  Where are you?

  Amazing

  This is great

  I'm hungry

 9. What does the expression (7 != 14) evaluate to?

  FALSE

  TRUE

 10. What is printed?
boolean x;
int y;
x = false;
y = 10;
x = !(y < 10 || y > 10);

System.out.println ( x );

  10

  FALSE

  x

  TRUE

 11. Which is correct, given the following context.
Which is correct? 
String a = "2"
String b = "2" 

Is a == b correct? Or is (a)equals(b) correct?

  a==b is correct

  Both answers give the same result

  (a)equals(b) is correct

  Neither is correct

 12. Select the correct answer
Which is equivalent?

int a = 5

int b = 10

int c = 25

int d = 15

1 - (a + b) == c
2- (a * b) == d
3 - (a + b) == d
4 - (a * c) == b

  3

  4

  2

  1

 13. This code snippet returns an error. True or False?
String s1 = "foo";

String s2 = "foo";

return s1 == s2;

  True

  False

 14. Always use == when when comparing strings, not equals.

  FALSE

  TRUE

 15. What is the following code demonstrating?
if 
 return true;
 else
 return false; 

  That you can use true and false as condition constant variables instead of boolean expressions

  That you can use the return statement to act as a method in itself

  That you can use an if statement as a function

  That you can return the value of a boolean expression