Preview

17 - Boolean Expressions and Ifs

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

  contradict

  deete

  complement

  compare

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

  FALSE

  TRUE

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

  TRUE

  FALSE

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

  It is correct and evaluates to True.

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

  It is correct and evaluates to False

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

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

  FALSE

  TRUE

 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

  GO DO MORE PROGRAMMING

  This is very fun

  A moose is on the loose

 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");

}

  This is great

  Where are you?

  I'm hungry

  Amazing

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

  TRUE

  FALSE

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

System.out.println ( x );

  TRUE

  10

  FALSE

  x

 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.equals(b) is correct

  Neither is correct

  Both answers give the same result

  a==b 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

  2

  3

  4

  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 the return statement to act as a method in itself

  That you can return the value of a boolean expression

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

  That you can use an if statement as a function