Jump to content

Introduction to C programming/Quizes/BooleanLogic/Answers

From Wikiversity

Question 1

[edit | edit source]

Assume that w=6, x=10, y=6 and z=3. What are the truth values of the following statements?

  • w<9
    • True. 6<9
  • w!=y
    • False. 6 is equal to 6
  • z>x
    • False. 3 is less than 10
  • z<=y
    • True. 3 is less than 6
  • x-6>y*2
    • False. x-6 is 4. y*2 is 12. 4 is less than 12, not greater.

Question 2

[edit | edit source]

With the same values of w,x,y and z, what are the values of the following combinations?

  • w<9 && w!=y
    • False. w<9 is true, but w!=y is false. If either term of an AND is false, the total is false.
  • x+7>2 || x==6
    • True. x+7>2 is true. x==6 is false. If either part of an OR is true, the total is true. In fact, the compiler won't even check the second term, it already knows the total is true.
  • !(x-9>1)
    • True. x-9>1 is false (its equal). The NOT of a false statement is true.
  • !((x>y) && (x<z))
    • True. x>y is true. x<z is false. So their AND is false. And the NOT of a fasle statement is true.
  • (x>7) && !(x>7)
    • False. x>7 is true.  !(x>7) is false. If either part of an AND is false, the total is false. This is actually a trick question- x && !x is always false.
  • (x>7) || !(x>7)
    • True. x>7 is true.  !(x>7) is false. If either part of an OR is true, the total is true. This is another trick question- x || !x is always true.

Question 3

[edit | edit source]

Under what values of x, y, and z do the following numbered statements get executed? Give the answers in forms like x>y>z.

if(x==y){
  statement 1;
  statement 2;
}
else if(y>z){
  statement 3;
  if(x==z){
    statement 4;
    if(y<z){
      statement 5;
    }
  }
}
else{
  statment 6;
}
  • statement 1: this occurs only if x==y. We get this from the if its in
  • statement 2: this occurs only if x==y. Since it is in the same block as 1, it has the same conditions
  • statement 3: this occurs only if x!=y and y>z. We get the first term due to it being in an else branch for if 1, and the second from the second if
  • statement 4: this occurs only if x!=y, y>z and z==x. We get this from the 3 ifs/elses it's in.
  • statement 5: never. This statement would require y>z and y<z. This cannot be true, so the line never executes. A compiler may warn you of this.
  • statement 6: this occurs only if x!=y and y<=z. We get this as the else from the 2 above ifs.

Question 4

[edit | edit source]

Under what values of x do the following numbered statements get executed?

switch (x){
case 0:
  statement 1;
  break;
case 1:
  statement 2;
case 2:
  statement 3;
case 4: 
  statement 4;
  break;
case 5:
case 6:
  statement 5;
default:
  statement 6;
  break;
}
  • statement 1: only if x=0
  • statement 2: only if x=1
  • statement 3: only if x=1 or x=2 (no break after statement 2, so a 1 would fall through)
  • statement 4: only if x=1, x=2, or x=4. No breaks for either case 1 or 2.
  • statement 5: only if x=5 or x=6.
  • statement 6: any value of x except 0,1,2, and 4. Notice that it does happen for 5 and 6 due to not having a break.