This assessment begins with a short survey about any CS background you may already have.
Have you previously taken COMP 108?
Outside of COMP 108, do you have any other CS experience (e.g., high school classes, internships, etc.). Explain.
This is a short introductory programming tutorial, with built-in 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 covered in COMP 110/L.
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 bulk of the class. With this in mind, if you’re struggling on this tutorial, don’t panic: you’ll have much more time to grasp these concepts.
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?
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
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 evaluates to.
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
evaluates to 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?
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 evaluates to the value true
, since 1
is less than or equal to 3
.
With this in mind, consider the following expression:
4 >= 5
This question has two parts:
Separate your answers by a semicolon, like so:
Integer;7
Boolean
expressions can be combined with Integer
expressions in mathematically meaningful ways.
For example, the expression (1 + 1) <= (2 - 2)
is a Boolean
expression which evaluates to the value false
.
This is because 1 + 1
evaluates to the value 2
, and 2 - 2
evaluates to the value 0
, so ultimately this asks 2 <= 0
, which is false
.
Consider the following expression:
(3 * 2 + 1) > (4 - 3 - 2)
This question has two parts:
Separate your answers by a semicolon, like so:
Integer;7
not
logically inverts (gets the opposite of) a given value of type Boolean
.
For example:
not(true) is false
not(false) is true
not(1 <= 1) is false
not(3 > 4) is true
With that in mind, consider the following expression:
not(1 < 3)
What value does the above expression evaluate to?
Values of type String
refer to a “string” of characters, and can be used to refer to individual letters, 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?
==
compares two values for equality, evaluating to a Boolean
value.
The two values compared do not need to be of the same type, though comparing two values of different types always produces false
.
For example:
1 == 1 is true
1 == 2 is false
true == true is true
true == false is false
"apple" == "apple" is true
"apple" == "pear" is false
1 == true is false
1 == "apple" is false
Consider the following code:
("hello" == "goodbye") == (3 == (2 + 2))
What value does the above code evaluate to?
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:
x := 2
is executed first, and the value 2
is placed into program variable x
.x := 1 + 3
is then executed.
1 + 3
part evaluates to value 4
.4
is reassigned into program variable x
.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:
1
to x
.x := x + x
is then evaluated.
x + x
is evaluated.
Since x
holds the value 1
at this point, this ends up evaluating 1 + 1
, evaluating to the value 2
.2
is then reassigned into x
, so ultimately x
holds 2
.Consider the following code:
x := 2 + 1
x := x + 4
x := x * 2
What value does x
hold when the above code completes?
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 code under then
is executed.
Conversely, if the condition evaluates to false
, then the code under otherwise
is executed.
Notably, these two cases are mutually exclusive; if
will always execute either the code under then
or the code under otherwise
, but never both.
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 code underneath then
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 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 code underneath then
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?
In an if
, We can put multiple lines of code underneath then
or otherwise
.
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?
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 code underneath then
is skipped.
The condition 10 > 2
is then evaluated, evaluating to true
.
Since this condition evaluates to true
, the code underneath this specific condition is executed.
This reassigns 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?
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)
f
, which takes a single parameter bound to the variable x
.
f
function adds 1
to its parameter (held in x
), and ultimately returns whatever value x + 1
evaluates to.f
with parameter 7
.
f(7)
part is evaluated first.
f
on the first line.x
will hold the value 7
, corresponding to the 7
passed in the f(7)
call.x + 1
is evaluated, where x
is 7
.
This is equivalent to evaluating 7 + 1
, so this evaluates to 8
.8
is returned.y := ...
is evaluated.
From the previous description, f(7)
evaluated to 8
, so y
is reassigned to hold 8
.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 does program variable y
hold when the above code completes?
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 in this manner results in an expression which evaluates to 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?
repeatedly if
works much like if
, with the following caveats:
otherwise
portion.then
is done executing, the if
starts all over again, starting with the condition check.false
, then the repeatedly if
ends execution.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:
x
holds the value 0
, from the x := 0
line.repeatedly if
part is next, which checks the condition x < 2
.x
holds 0
, so this checks 0 < 2
.0 < 2
evaluates to true
, the bit of code underneath then
is executed, namely x := x + 1
.x
holds value 0
at this point, this ultimately performs x := 0 + 1
, so x
holds the value 1
.repeatedly
part of repeatedly if
comes in.
The condition is now checked again, as if we started all over again starting at the repeatedly if
.
However, x
maintains the last value which was reassigned to it, namely 1
from the previous step.
As such, the condition checked is 1 < 2
.
Since 1 < 2
evaluates to true
, this executes the bit of code underneath the repeatedly if
part again.x
holds the value 1
at this point, this ultimately executes x := 1 + 1
, reassigning the value 2
into x
.x
holds the value 2
, so 2 < 2
is evaluated.
Since 2 < 2
evaluates to false
, the repeatedly if
portion is skipped entirely.
However, the fact that x
held 2
is unchanged.
As such, when this code completes, x
will hold the value 2
.Consider the following code:
x := 1
repeatedly if (x < 5) then:
x := x + 2
What value will x
hold when the above code completes?
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
x < 2
evaluates to true
, as x
was initially 0
from the x := 0
line.
As such, the code underneath then
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 then
is executed.x := x - 1
is then executed, ultimately reassigning the value 26
to x
.26 < 2
evaluates to false
, the code underneath then
is skipped over, resulting in 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?
Arrays are special values which hold a collection of other values.
These other values must all be of the same type.
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
.
To be clear, 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 the array holds. 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
.
Consider the following code:
x := make_array("hello", "goodbye")
y := make_array(1 + 2, 3 - 4, 5 * 6)
z := make_array(1, 3, 5)
What are the types of the values held in variables x
, y
, and z
?
Separate your answers by semicolons, e.g.:
Array of Integer;Array of Boolean;Array of String
Individual array elements can be accessed using square brackets ([]
).
The value in the brackets refers to an index of the array.
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)
w := a[0]
x := a[1]
y := a[2]
z := a[2 - 1]
In the above code, once execution completes, w
, x
, y
, and z
will hold the following values:
w
holds 7
x
holds 8
y
holds 9
z
holds 8
This is because:
7
is at index 0
in the array held in a
(w
).8
is at index 1
in the array held in a
(x
).9
is at index 2
in the array held in a
(y
).2 - 1
evaluates to 1
, so 8
is at index 1
in the array held in a
(z
).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 the above code completes?
Separate your answers by semicolons, e.g.:
true;false;true
Just like variables, array elements can be reassigned by putting them on the lefthand side of :=
.
For example, consider the following code:
a := make_array("alpha", "beta", "gamma")
a[1] := "hello"
x := a[0]
y := a[1]
z := a[2]
After executing the above code, variables x
, y
, and z
will hold the following values:
x
holds "alpha"
y
holds "hello"
z
holds "gamma"
As shown above, the value held by variable y
shows that a[1]
has been reassigned to hold "hello"
.
Similarly, as shown by the values held in variables x
and z
, a[0]
and a[2]
have retained their original values from make_array
.
Consider the following code:
a := make_array(7 + 2, 3 - 1, 2 * 3, 2 * 2)
a[1 + 2] := 9
w := a[0]
x := a[1]
y := a[2]
z := a[3]
What values will variables w
, x
, y
, and z
hold after the above code completes?
Separate your answers by semicolons, e.g.:
1;2;3;4
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 (held in variable s
) of the array (specifically with s := s + a[x]
).
All of this 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 completes?
repeatedly if
can be used to iterate over multiple arrays at once.
For example, consider the following code:
a := make_array(8, 2, 5)
b := make_array(-1, -2, -3)
x := 0
repeatedly if (x < 3) then:
b[x] := a[x]
x := x + 1
p := b[0]
q := b[1]
r := b[2]
The repeatedly if
in the above code effectively copies values from the array held in variable a
into the array held in variable b
.
This copying occurs one element at a time.
With this in mind, variables p
, q
, and r
will hold the following values:
p
holds 8
q
holds 2
r
holds 5
Consider the following code:
a := make_array("alpha", "beta", "gamma", "delta")
b := make_array("foo", "bar", "baz", "boo")
x := 0
y := 3
repeatedly if (y >= 0) then:
b[y] := a[x]
x := x + 1
y := y - 1
p := b[0]
q := b[1]
r := b[2]
s := b[3]
What values will variables p
, q
, r
, and s
hold after this code executes?
Separate your answers by semicolons, e.g.:
"hi";"hello";"hey";"yo"