comp110-spring18

Introductory Programming Tutorial / Quiz

This is a short introductory programming tutorial, with built-in quiz questions along the way. The tutorial builds upon itself, so it should be completed in order. The tutorial introduces you to a language that doesn’t exist in reality, but covers a broad number of topics we will discuss in this class. There are several reasons why this tutorial/quiz is being assigned:

  1. To give you a sense of the sort of things you will encounter in this class
  2. To allow you to assess yourself on how much you may already know (or not know)
  3. To allow me to identify topics which may need additional instruction

To be clear, you will only be graded on the completion of the quiz, not on your actual answers. That said, please give the quiz an honest effort; don’t simply write random answers.

This tutorial is very fast-paced relative to the rest of the class, and touches on topics spanning the next eight weeks. With this in mind, if you’re struggling on this tutorial, don’t panic: you’ll have much more time to grasp these concepts.

Class Tips

This class may require a significant time commitment, especially if you’ve never programmed before. The most important thing you can do is to try to keep pace with the class: attend frequently, and submit all assignments on-time. Even if you miss an assignment, don’t skip it: it’s best to complete all assignments, even if you can’t get credit for them. Concepts from assignments build on top of each other, so outright skipping an assignment will leave you with a blind spot of knowledge.

On my end, whether or not assignments are skipped is the biggest predictor of class outcome. To see this in action, consider the following historical data correlating the number of assignments skipped with overall performance in the class:

Note that missing even one assignment is associated with nearly a 20 point drop in both the average and the median. Considering that no assignment is worth more than around 1.5 points, the loss of points of missing the assignment does not account for most of the difference. This instead is because students get behind in the class if an assignment is outright skipped, leading to lower scores on subsequent assignments and exams.

Begin Tutorial and Questions

Question 1

Code is centered around two basic concepts: expressions and values. Both these concepts are rooted in mathematics. Expressions evaluate to values. For example, 1 is an expression that evaluates to the value 1. A more complex expression is 1 + 1, which evaluates to the value 2.

Consider the following expression:

1 + 2 + 3

What does the above expression evaluate to?

Question 2

Expressions are evaluated using the same rules you’re used to from arithmetic, though some of the notation is a little different. For example, * denotes multiplication, as opposed to the usual x. With all this in mind, what does the following expression evaluate to?

2 + 4 * 3 - 1

Question 3

Values and expressions are both associated with a particular type. The type refers to the kind of value it is, or the kind of value the expression produces. For example, the value 1 is of type Integer, because 1 is an integer. Similarly, the expression 1 + 1 is of type Integer, since 1 + 1 produces a value of type Integer (namely 2).

With this in mind, consider the following expression:

2 * 1 + 4 - 0

What is the type of the above expression?

Question 4

Boolean is a type that contains only two values: true and false. These concepts are based in mathematical logic. For example, the expression 1 < 3 is of type Boolean and produces the value true, since 1 is less than 3.

With this in mind, consider the following expression:

(1 + 1) <= (2 - 2)

This question has two parts:

  1. What is the type of the above expression?
  2. What value does it produce?

Separate your answers by a semicolon, like so:

foo;bar

Question 5

not logically inverts (gets the opposite of) a given value of type Boolean. For example:

not(true) = false
not(false) = true

With that in mind, consider the following expression:

not(1 < 3)

What value does the above expression produce?

Question 6

Values of type String refer to a “string” of characters, and can be used to refer to individual words or even entire sentences. For example, consider the following code:

"apple"

The above expression evaluates to the value "apple", which is of type String. This simply means the word “apple”, as if it were written on a page.

Values of type String can be concatenated together, using the ++ operator. For example, consider the following code:

"apple" ++ "pear"

The above code evaluates to a value of type String, namely "applepear".

Consider the following code:

"hel" ++ "lo" ++ "world"

What value does the above code evaluate to?

Question 7

Program variables are based on mathematical variables. Like mathematical variables, program variables hold values. However, unlike mathematical variables, program variables can be reassigned new values. Reassignment is performed with the := operator. For example, consider the following code:

x := 1

After the above code executes, program variable x will hold the value 1. Consider the next bit of code:

x := 2
x := 1 + 3

This code performs two reassignments, which occur in top-down order. A step-by-step explanation of what this code does follows:

As such, program variable x will ultimately hold the value 4.

Note that the actual reassignment occurs after the expression on the righthand side of the := is evaluated. With this in mind, consider the following code:

x := 1
x := x + x

A step-by-step explanation of what this code does follows:

Consider the following code:

x := 2 + 1
x := x + 4
x := x * 2

What value does x hold when the above code completes?

Question 8

if checks a condition, which is an expression of type Boolean. This condition is given in parentheses (()). If the condition evaluates to true, then the first bit of given code is executed. Conversely, if the condition evaluates to false, then the second bit of given code is executed. For example, consider the following code:

if (true) then:
  x := 8
otherwise:
  x := 9

In the above code, since the Boolean expression true evaluates to the value true, the first bit of code is executed, namely x := 8. As such, when this code completes, program variable x will hold the value 8.

Conversely, consider the following variant of the above code:

if (false) then:
  x := 8
otherwise:
  x := 9

In the above case, since the Boolean expression false evaluates to the value false, the bit of code underneath otherwise is executed, namely x := 9. As such, program variable x ends up holding the value 9.

The following code illustrates a more complex example:

if (1 < 2) then:
  x := 8
otherwise:
  x := 9

In the above case, since the Boolean expression 1 < 2 evaluates to true, the first bit of code is executed, namely x := 8. As such, when this code completes, program variable x will hold the value 8.

Consider the following code:

if (1 < 0) then:
  x := 1 + 1
otherwise:
  x := 2 + 2

What value does program variable x hold when the above code completes?

Question 9

The bits of code in if can contain multiple lines to execute. With this in mind, consider the following code:

if (0 >= 0) then:
  x := 1
  x := x + 1
otherwise:
  x := 2
  x := x + x

What value does program variable x hold when the above code completes?

Question 10

if can be chained along to check multiple conditions. The first condition which evaluates to true ends up being the one picked. If none of the conditions evaluate to true, then the code in the final otherwise is executed. For example, consider the following code:

if (1 > 10) then:
  x := 0
otherwise if (10 > 2) then:
  x := 1
otherwise:
  x := 2

In this case, since 1 > 10 evaluates to false, the first bit of code is skipped. The condition 10 > 2 is then evaluated, producing true. Since this condition evaluates to true, the code underneath this specific condition is executed. This places value 1 into variable x. At this point, there is nothing left to exeucte.

Consider the following code:

if (2 > 2) then:
  x := 0
otherwise if (2 > 4) then:
  x := 1
otherwise if (2 >= 5) then:
  x := 2
otherwise:
  x := 3

What value does program variable x hold when this code is finished executing?

Question 11

Program functions work much like mathematical functions. Program functions allow you to define a bit of code which is executed whenever you want. This bit of code is known as the function body. Additionally, program functions can be given parameters, which allow you to pass different values to program functions. For example, consider the following code:

define function f(x) = x + 1
y := f(7)

The first line defines a function named f, which takes a single parameter bound to the variable x. The f function will then add 1 to the parameter, and ultimately return the result. The second line then calls function f with parameter 7, yielding the value 8 (i.e., 7 + 1). The value 8 is then reassigned into program variable y.

Function calls can be seen as a way of copying the function body to wherever the call is made, substituting the function parameter in the function definition with whatever parameter is passed in the call. In other words, when f(7) is seen above, this can be replaced with the function body x + 1. The x is then substituted with 7, so f(7) overall does the same thing as 7 + 1.

Consider the code below:

define function f(x) = x * x
y := f(3)

What value will program variable y hold when the above code completes?

Question 12

Functions can call other functions. For example, consider the following code:

define function a(x) = x - 1
define function b(y) = a(x * 3)
z := b(2)

The b(2) call is equivalent to writing out a(2 * 3), from the definition of program function b. Simplifying the parameter to program function a, this is equivalent to a(6). From the definition of program function a, a(6) is equivalent to 6 - 1. Simplifying this code down yields 5, ultimately resulting in program variable z holding the value 5.

With all this in mind, consider the following code:

define function f(x) = x + 2
define function g(y) = f(y) + f(y + 1)
z := g(1)

What value will program variable z hold when the above code completes?

Question 13

repeatedly if works much like if, with the following caveats:

For example, consider the following code:

x := 0
repeatedly if (x < 2) then:
  x := x + 1

A step-by-step breakdown of the above code’s execution follows:

Consider the following code:

x := 1
repeatedly if (x < 5) then:
  x := x + 2

What value will x hold when the above code completes?

Question 14

Note that the condition check in repeatedly if is only performed after all the code underneath the repeatedly if part is executed. For example, consider the following code:

x := 0
repeatedly if (x < 2) then:
  x := 27
  x := x - 1

Initially, x < 2 evaluates to true, as x was initially 0 from the x := 0 line. As such, the bit of code underneath repeatedly if is executed. x is then reassigned to hold the value 27. While 27 is not less than 2 (from the condition x < 2), execution still proceeds, because the condition check is performed only after all the code underneath the repeatedly if is executed. As such, x := x - 1 is then executed, ultimately reassigning the value 26 to x. From here, the condition is checked. Since 26 < 2 evaluates to false, the repeatedly if is skipped over, yielding program variable x holding the value 26.

Consider the following code:

x := 1
repeatedly if (x >= 0) then:
  x := x - 2
  x := x + 1

What value will x hold when the above code completes?

Question 15

Arrays are special values which refer to a collection of other values. Because arrays hold other values, the type of array values is a little different, since the array type includes the type of the value held. For example, if an array holds values of type Integer, then we say the type of the array is Array of Integer. Similarly, if an array holds values of type Boolean, then the type of the array is Array of Boolean. All the values held in an array must be of the same type; we cannot, for instance, have an array that holds both Integer and Boolean values.

When arrays are created, we must specify the values to place into the array. For example, consider the following code:

a := make_array(5, 6, 7)

The above code makes an array which holds the values 5, 6, and 7, in that specific order. The variable a is assigned this array value, so a holds a value of type Array of Integer.

Individual array elements can be accessed using square brackets ([]). The value in the brackets refers to an index of the array. Indices work just like line numbers in a list, except that array indices start from 0 as opposed to the usual 1. For example, consider the following code:

a := make_array(7, 8, 9, 10)
x := a[0]
y := a[1]
z := a[2]

In the above code, once execution completes, x will hold the value 7, y will hold the value 8, and z will hold the value 9. This is because 7 is at index 0 in the array held in a, 8 is at index 1 in the array held in a, and 9 is at index 2 in the array held in a.

Consider the following code:

a := make_array(true, false, true)
x := a[1 - 0]
y := a[1 + 1]
z := a[2 - 2]

What values will program variables x, y, and z hold once this code completes executing? Separate your answers by semicolons, e.g.:

1;2;3

Question 16

Arrays are commonly used in conjunction with repeatedly if, in order to iterate over the array. Iteration allows us to access all the elements in the array one-by-one, and manipulate them as needed. For example, consider the following code:

a := make_array(4, 5, 6)
x := 0
s := 0
repeatedly if (x < 3) then:
  s := s + a[x]
  x := x + 1

Variable x is used as an index into the array held in a, which initially starts at 0 (specifically with x := 0). The condition of the repeatedly if checks to see that x is less than 3. This is significant, as the largest valid index for the array in a is 2, and 2 is the largest integer is that less than 3. Lastly, each time the code underneath the repeatedly if is executed, the value in x is incremented by 1 (specifically with x := x + 1). With this in mind, the above code will iterate through every element of the array in a, starting from the value at index 0 and ending with the value at index 2. Upon each iteration, the value of the current element of the array is added to a rolling sum of the array (specifically with s := s + a[x]). All of this ultimately means that s will hold the sum of the elements in the array once this code completes, so s will ultimately hold the value 15 once this code is done executing.

Consider the following related code:

a := make_array(5, 2, 1, 2)
x := 0
r := 1
repeatedly if (x < 4) then:
  r := r * a[x]
  x := x + 1

What value will r hold once the above code finishes executing?