This page contains questions and answers for the fifth quiz in CS100: Principles of Computer Science, a course based on the book The Most Comples Machine. The answers given here are sample answers only. There is always some variation in the answers that would receive full credit. The Notes contain any extra comments I happen to have; they are not parts of the answers.
Question 1: Consider the statement: X := Y + 1.
(a) Suppose that I refer to the "syntax" of this statemtent. What, exactly, would I be talking about? That is, what sorts of things about this statement constitute its "syntax"?
Answer: Syntax refers to the grammar or valid structure of a statement. In this case, the statement is an example of a legal "assignment statement", which has been legally constructed according to the BNF template
<assignment_statement> :== <variable> := <value>
The X plays the role of the <variable> while the formula Y+1 plays the role of the <value>.
(b) Suppose that I refer to the "semantics" of this statemtent. What, exactly, would I be talking about? That is, what sorts of things about this statement constitute its "semantics"?
Answer: The semantics is the meaning of the statement, which can be interpreted as the effect of executing the statement. In this case, "X := Y+1" means that the number 1 is to be added to the value of the variable Y, and the result is to be stored as the new value of the variable X.
Note: The syntax of a whole language refers to the whole set of rules determining what strings of symbols are legally part of that language. The syntax of a particular statement usually refers to the particular rules that that statement follows. For example, in English the sytax of the sentence "John runs" includes such facts as that it consists of a noun followed by a verb and that the verb agrees with the noun. The term "semantics" is used similarly.
Question 2: Draw the figure that would be produced by the following xTurtle program:
declare s s := 5 LOOP forward(s) turn (-90) s := s - 1 exit if s = 0 END LOOP
Answer: The picture looks like this, where the longest line is five units long, and the shortest is one unit long:
Question 3: Fill in the box in the following program so that it will draw the picture shown, and explain your reasoning in terms of a precondition. (Note that the command "forward(1)" draws the short horizontal line segment between two vertical lines.)
declare count count := 0 loop turn(90) forward(5) count := count + 1 exit if count = 11 ________________________________ | | | | |______________________________| forward(1) end loop
Answer: The box need only contain the two commands:
Another posssibility would be "turn(180) forward(5) turn(90)". Explanation: In order for the command "forward(1)" to draw the line segment in the correct position and orientation, the turtle must be back down at the bottom of the picture, facing to the right. These conditions are "preconditions" for the forward(1) command. The statements in the box set up these preconditions.