Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. A while loop might never execute; likewise, it is possible for a while loop t

ID: 3677495 • Letter: 1

Question

1.

A while loop might never execute; likewise, it is possible for a while loop to never terminate.

A) True
B) False

2.

Every while loop can be re-written (perhaps cleverly) as a for loop.

Note: For the purposes of this question, please ignore the very special - and unusual - case of a loop designed to be an infinite loop that is then "broken" out of with the break statement. An example of that would be:

for k = 1:inf
    (statements)
if (some condition)
  break;
end
end

This is not a programming practice that you generally should make use of in practice. If you are using the textbook as a reference during this quiz, please keep this in mind while reading the author's commentary.

A) True
B) False

3.

Reasons to avoid using loops in MATLAB include: (select up to the 3 best possible correct answers)

A) Low performance (in terms of clock times)
B) An assortment of built-in array operations
C) Built-in tools such as the find command
D) The ability to use matrices as indices for for loops

4.

Which of the following are good strategies for improving performance of one's MATLAB code? Up to 2 of the selections are correct.

A) Ignoring feedback from MATLAB, since computers can't think for themselves
B) Always allowing MATLAB to automatically printing variables to the screen without using print commands
C) Using the profiler to observe/correct bottlenecks
D) Pre-allocating the size of a vector

5.

Consider the following code fragment:

x = 3;
while rem(x,2) == 0 || x > 3
x = x + 1;
end
fprintf('Final value of x: %i ', x)

What will be displayed in the Command Window after this code fragment is run?

A) Final value of x: 4
B) The loop will never finish terminating, so the print statement on the last line will never be executed.
C) Final value of x: 3
D) Final value of x: 10

6.

Which of the following best describes a sentinel loop?

Note: This isn't covered in the textbook, but is covered in the Lecture 16 notes. Read carefully!

A) A loop that will only execute if a special condition occurs
B) A loop that executes a fixed number of times.
C) A loop that continues over and over until a special condition occurs.
D) A loop that has be coerced into behaving like a do while loop

7.

Which of the following is the most appropriate way to measure wall time?

A) tic/toc
B) clock/etime
C) Writing a while loop that executes once every second, and counting the number of iterations of the loop.
D) cputime

Explanation / Answer

1.True. (It actually depends on the constraints of the loop and the iterations.If the iterations are infinite the loop dosen't end if the case isn't satisfied.

2.True. (The while loop can be re-written as a for loop only in some cases and only for some finite small amount of iterations.If more iterations are needed it is better to use "while" instead of "for".

3. A. Low performance (in terms of clock times).
B. An assortment of built-in array operations.
D. The ability to use matrices as indices for for loops.

4. B. Always allowing MATLAB to automatically printing variables to the screen without using print commands
C. Using the profiler to observe/correct bottlenecks
D. Pre-allocating the size of a vector

5. The loop will never finish terminating, so the print statement on the last line will never be executed. (since the x value has no end. it was said only x>3.

6. A loop that has be coerced into behaving like a do while loop. (But it only works with some sentinal value)

7.Writing a while loop that executes once every second, and counting the number of iterations of the loop. (since the execution time depends on the compiler and not on the original clock.)