Note: If you missed the test (e.g. with a medical note), I STRONGLY RECOMMEND that you not look at these solutions at first. I suggest you prepare to take the test, then print the PDF file of the test as actually given, then time yourself and write it in less than 45 minutes with no aids allowed (and don't answer the telephone, etc -- go to a library if necessary). Then look at these solutions, and bring me your completed test if you're unsure what your grade would have been for any particular question.
CSC 104 mid-term test #1
17 February 2012, 9:10 AM
No aids permitted.
Total: 20 marks.
Time allotted: 45 minutes.
Since time may be short, be careful not to get stuck on one question to the exclusion of others. The amount of marks or answer-space allotted does not indicate how long it will take you to complete the question, nor does the size of the answer-space indicate the size of the correct answer.
Answer all questions. Answer questions in the space provided.
Do not open this booklet until you are instructed to.
1. [4 marks] Suppose you are telling a fellow student how to use the CDF computers. You say, "Go to room 2210 and sit down at any free computer." This is a fine instruction to a person, but:
a) What disqualifies it from being called an "algorithm"?
(The definition of an algorithm presented in class was: A finite sequence of unambiguous, executable steps which ultimately terminates when followed.)
b) How could we modify the instruction to fix this problem?
b) "Go to room 2210 and sit down at the first free computer."
Or "go to room 2210 and sit down at the first computer (whether anyone
is using it or not)."
Notes: This would-be algorithm might be able to be criticized on other bases too (for example: it's not executable if there are no free computers). This could be fine if it answers the question. However, the fact that (for example) it doesn't say what to do at the computer is not an answer to the question; perhaps "you" said that at a later time, or earlier -- the question asks "what disqualifies it from being called an algorithm"; just any cricitism of the instruction is not necessarily an answer to this question.
It's also not probably a defect that it doesn't say that room 2210 is in the Bahen building, or that this is at the University of Toronto, or that sitting involves bending your legs, or any other kind of thing you could add to specify more precisely what the person is supposed to do. There is always the issue as to which operations exist and which you have to specify; in a programming language we make that clear; I think that the only reasonable interpretation of this test question is that the words in the instruction are intelligible to the listener.
2. [3 marks]
Programs can also be treated as data. Give an example of a program which
can have another program as input, and give an example of using it in
this manner.
It's common for programs to process other programs, one way or another. When discussing the idea that software is a kind of data, I gave a somewhat technical example: that when the operating system is instructed to run a machine-language program, it has to examine the program to determine its memory and other requirements. But there are all sorts of more familiar examples.
The commonest student answer which will get full marks on this question was to specify an editor of some kind, when used to edit the text of a computer program. For example, if you open your story.py file in nedit, that's nedit (a program) treating the text of story.py (another program) as input.
The python interpreter (e.g. the "python" program when you type "python story.py") is an example of a program which processes other programs. It reads the python program and does what it says.
And Wing-101 combines both of the above.
Grading note: Unfortunately, many students gave answers about programs and input data where that input data wasn't a program. Well, it's very common for there to be programs and for programs to have input; that's really not what this question was about; it's far more specific. Sometimes students pointed out that that input data is the output of another program. Well, that's also not very special -- almost all data we have in the computer is the output of some program, even if only of the editor which a user originally typed it in to. This test question quite specifically asks for cases where the program is the input.
3. [5 marks]
After successfully executing the command "cd /u/eem/104",
a CDF computer user successfully executes the command
"cat file1"
and then the command "cat ../file2".
a) What is an absolute path name for file1?
b) What is an absolute path name for file2?
c) Some time later, the user executes a cd command, and then
"cat test/files/greeting". The file which is displayed
is also known by the absolute path name
"/u/csc104h/winter/test/files/greeting".
What was the cd command?
4. [4 marks]
In the URL
http://www.dgp.toronto.edu/~ajr/104/courseinfo.html
, what is the function of each of the following components?
(What does it tell the computer(s) to do?)
a) http
b) www.dgp.toronto.edu
c) /~ajr/104/courseinfo.html
b) This string is translated to an IP address and identifies the computer from which the data should be requested.
c) This path name is sent to the web server, and serves to identify which of the many files it serves is sent back to the client (web browser). The web server can be configured to use this information in a variety of possible ways, but most commonly there is a simple rule for mapping this to a file path name, and the contents of that file is then transmitted.
5. [4 marks]
The following Python statement outputs 14:
print 2 + 3 * 4However, the following two Python programs do not both output 14.
x = 3 * 4 print 2 + xWhat does each one output, and why do they differ?
x = 2 + 3 print x * 4
The original program does the multiplication "first" because of the operator precedence rules. That is to say, "2+3*4" is equivalent to "2+(3*4)".
However, the operator precedence rules only operate within a single numeric expression; they don't affect the sequence of execution of separate Python statements. In the last program, "x=2+3" is executed first, since it is in a separate Python statement from the multiplication; so the addition is executed before the multiplication in this program, yielding a different result.
Grading note: Some students probably didn't have any reason to think that the programs would output the same value -- they understood what each program does, explained it, and identified the correct output. This is fine for full marks.