Preview

17 - Boolean Expressions and Ifs

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

  compare

  contradict

  complement

  deete

 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 False

  It is correct and evaluates to True.

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

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

 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'.

  FALSE

  TRUE

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

}

  This is great

  Where are you?

  I'm hungry

  Amazing

 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

  TRUE

  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==b is correct

  a.equals(b) is correct

  Both answers give the same result

  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

  2

  1

  4

  3

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

String s2 = "foo";

return s1 == s2;

  False

  True

 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 use an if statement as a function

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

  That you can return the value of a boolean expression