Skip to main content
Logo image

Exercises πŸ€”πŸ’­ Conceptual Questions

1.

(a) Logical Data Types.

    In MATLAB, true and 1 are the same.
  • True.

  • Incorrect. While true and 1 are represented by the same numeric value, they have different data types: true is of type logical, while 1 is of type double.
  • False.

  • Incorrect. While true and 1 are represented by the same numeric value, they have different data types: true is of type logical, while 1 is of type double.

(b) Relational Operators: Inequality.

Which of the following commands test that x is not equal to y?
  • ~(x = y)
  • ~(x == y)
  • ~x == y
  • x ~= y
  • x ~== y

(c) Translating Relational Operators.

(d) Basic Logical Operators.

Select the value contained in ans after evaluating the following expression:
ans = (3 >= 2) & (3+4 == 6)
  • ans = 1 (logical)
  • Incorrect. While 3 >= 2 is true, check the second part: is \(3+4\) really equal to \(6\text{?}\)
  • ans = 0 (logical)
  • Correct! Even though 3 >= 2 is true, the expression 3+4 == 6 is false (since \(3+4=7\)). For an AND operation to be true, both sides must be true.
  • ans = 1
  • Incorrect. The result of a logical operation is always of type logical, not double.
  • ans = 0
  • Incorrect. The result of a logical operation is always of type logical, not double.
  • None of the above. This expression will produce an error.

(e) Understanding AND.

    An AND operation (&) returns true if at least one of the conditions is true.
  • True.

  • Incorrect. An AND operation requires both conditions to be true. You’re thinking of OR (|), which returns true if at least one condition is true.
  • False.

  • Incorrect. An AND operation requires both conditions to be true. You’re thinking of OR (|), which returns true if at least one condition is true.

(f) Is p positive?

(g) Is p equal to 4?

(h) Combining with AND.

Select the command that is equivalent to asking the question:
Is p a negative number greater than -4?
  • p < 0 & p >= -4
  • p <= 0 | p > -4
  • -4 < p < 0
  • p < 0 | p > -4
  • p < 0 & p > -4

(i) Negating with NOT.

Which logical statements are equivalent to asking the question:
Is p NOT positive?
  • ~(p > 0)
  • p <= 0
  • ~p > 0
  • p ~> 0
  • p < 0

(j) Combining with OR.

(k) Operator Precedence.

Select the logical statement that is equivalent to the following:
~A & B | C
  • ((~A) & B) | C
  • ~(A & B | C)
  • ~A & (B | C)
  • ~(A & B) | C