Evaluating Boolean Expressions

Practice Questions

Computer Science › Evaluating Boolean Expressions

Page 1 of 2
10 of 16
1

boolean x;
int y;
x = false;
y = 10;
x = !(y < 10 || y > 10);

System.out.println ( x );

What is printed?

2

Which of the following Boolean expression evalutes to True?

I. False && False && False && True && False

II. (False && False) || (True || False) || (False && (True || False))

III. (True && False) && (True || False || (True && True))

IV. !(True && True && False)

V. False || False || False || True || False

3

Which is true?

a = 4

b = 2

c = 5

d = 6

a) a = c - d

b) a = b - d

c) a = d - b

d) a = c * d

4

Consider the following code:

int\[\] a = {8,4,1,5,1,5,6,2,4};

int b = 1;

boolean\[\] vals = new boolean\[a.length\];

for(int i = 0; i < a.length; i++) {

vals\[i\] = (a\[i\] - 1 > 4);

}

for(int i = 0; i < vals.length; i++) {

String s = "Duns Scotus";

if(!vals\[i\]) {

s = "Mithrandir";

}

System.out.println(s);

}

What is the output for the code above?

5

int currentYear = 2016;

bool leapYear;

``

if (currentYear % 4 == 0) {

leapYear = true;

} else {

leapYear = false;

}

``

febDays = leapYear ? 28 : 29

Based on the code above, what value will febDays have?

6

True or False.

This code snippet returns an error.

String s1 = "foo";

String s2 = "foo";

return s1 == s2;

7

What does this statement return?

int a = 2

int b = 3

a == b

8

Consider the code below:

for(int i = 0; i < 10; i++) {

if(i % 2 == 0) {

System.out.println("This is my favorite case!");

} else if(i + 6 > 12 && i % 3 == 1) {

System.out.println("There once was a pool in the back yard!");

} else {

System.out.println("Well, at least it is something...");

}

}

What is the output for the code above?

9

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
10

What does the following code print?

int i = 3;

int j = 4;

int k = 5;

int l = i * k - j;

int m = i*2 + j + k;

if ( ( l > m ) || ( l * i ) = (m+m+i) )

System.out.println("The statement proves true");

else

System.out.println("The statement proves false");

Page 1 of 2
Return to subject