1 Sample Exam Style Questions ANSWERS

  1. B
  2. B
  3. C
  4. A
  5. You would use a while loop when the loop should run long as long as a condition is true as opposed to running for a known number of iterations.
  6. (42 - 32) * 5 / 9 + 273.15
  7. A
  8. D
  9. B
  10. B
  11. C
  12. D
  13. B
  14. D
  15. 21
  16. C
  17. a = 5 and b = 10. In Matlab a function cannot cause the value of an input variable to change.
  18. C
  19. B
  20. D
  21. s = [1 2 3 4]
  22. A
  23. The value of i can become equal to n in which case x(i + 1) is an error because i + 1 is greater than n and is not a valid index for x.
  24. B
  25. Removes all of the elements from x and puts them into z in sorted increasing order.
  26. C
  27. A
  28. B
  29. D
  30. B
  31. A
  32. x = [5; x];
  33. D
  34. Use logical indexing: x(x < 0) = 0
  35. for i = 2 : n and for j = 1 : (i - 1)
  36. A
  37. We want to count the number of times that the signal changes from 0 to 1: sum(x(2 : end) - x(1 : end - 1) == 1)
  38. D
  39. C
  40. The height of rectangle i is y(i). The vector of heights is simply y(1 : end - 1). The width of rectangle i is x(i + 1) - x(i). The vector of widths is x(2 : end) - x(1 : end - 1). The total area is sum(y(1 : end-1) .* (x(2 : end) - x(1 : end - 1)))
  41. B
    • A. The loop iterates from the end of x to the front of x; therefore, it finds the first element equal to 1. If no element is equal to 1 then i is 0.
    • C. The loop iterates from the front of x until it finds an element that is equal to 1; therefore, it finds the first element equal to 1. If no element is equal to 1 then an error occurs because the index i will become greater than the number of elements in x.
    • D. The loop iterates from the front of x until it finds an element that is equal to 1; therefore, it finds the first element equal to 1. If no element is equal to 1 then the loop stops and i is incorrectly set to numel(x).
  42. The test cases should include inputs where the year:
    • is a leap year because the year is divisible by 400
    • is a leap year because the year is divisible by 4 and not divisible by 100
    • is not a leap year because the year is not divisible by 4
    • is not a leap year because the year is divisible by 4 and 100 (and not 400)
Year Expected return value Reason
2000 true year is divisible by 400
2004 true year is divisible by 4 and not divisible by 100
2005 false year is not divisible by 4
2100 false year is divisible by 4 and 100 but not 400
      1. A vector where each element is a single non-negative digit
      1. The function would have to do something like:
      1. check if the number of elements in the vector is less than 4 or greater than 8
        • if so then the answer is false, done
      2. check if each element of the vector is less than 0 or greater than 9
        • if so then the answer is false, done
      3. answer is true, done
      1. Many different answers are possible; the tricky part is dealing with leading zeros (such as the 3 leading zeros in 0009). One answer is:
      • a vector of length 2. The first element is the number of leading zeros. The second element is an integer equal to the remaining digits of the pin not including the leading zeros.
      1. We require a linear relationship for line fitting. Take the square root of both sides of the equation: \[ \sqrt{d} = \sqrt{\frac{1}{2}g} t \] which is linear in \(t\). Now fit a straight line to the time x = t and the square root of the measured distances y = sqrt(d) to obtain the slope of the line m. The slope of the line is an estimate of \(m = \sqrt{\frac{1}{2}g}\); therefore, our estimate of \(g\) is equal to 2 * m^2.
      • alternatively, rewrite the relationship as \[ t^2 = \frac{2}{g} d \] which is linear in \(d\). Now fit a straight line to the distances x = d and the squared time y = t .^ 2 to obtain the slope of the line m. The slope of the line is an estimate of \(m = \frac{2}{g}\); therefore, our estimate of \(g\) is equal to 2 / m.
      1. The speed of the ball is equal to the first derivative of the distance as a function of time. We can use a finite difference to estimate the derivative.
      1. Using a forward difference we get: speed = (d(7) - d(6)) / (t(7) - t(6)) = (1.801 - 1.270) / 0.1 = 5.31 Using a backward difference we get: speed = (d(6) - d(5)) / (t(6) - t(5)) = (1.270 - 0.773) / 0.1 = 4.97 Using a central difference we get: speed = (d(7) - d(5)) / (t(7) - t(5)) = (1.801 - 0.773) / 0.2 = 5.14
      1. v(1) is less than v(2) (or v(1) is the smallest element in v)
      1. v(2:end) is sorted from smallest to largest value
      1. Yes.
function tf = isSorted(v)

if numel(v) < 2               % see answer to part c
    tf = true;
elseif v(1) > v(2)            % see answer to part a
    tf = false;
else
    tf = isSorted(v(2:end));  % see answer to part b
end

end 
      1. \(x^0 = 1\)
      1. \(x^n = \frac{1}{x^{-n}}\) if \(n < 0\)
      1. \(x^n = x \times x^{n-1}\) if \(n > 0\)
function y = intpow(x, n)

if n == 0
    y = 1;
elseif n < 0
    y = 1 / intpow(x, -n);
else
    y = x * intpow(x, n - 1);
end

end