Download Test3a

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
Transcript
Computer Science 1 DDS, Test 3
Computer Science 1, 1998
Data and Data Structures, Test 3
Department of Computer Science
University of the Witwatersrand, Johannesburg
Instructions
You may bring into the examination room one A4 sheet of self-prepared notes and refer to it
while answering the questions on this test. Otherwise, this is a closed book test. No calculator
of any type may be used. Answer all questions. You have a maximum time of 45 minutes to
complete the test. There are 100 marks in total. 100 marks = 100%.
Fill in your name, student number, etc. on the special mark sense card. Encode your student
number and mark your answers to all multiple choice questions on that card. Please read all
instructions on the mark sense card carefully. Use a soft pencil (e.g. with HB lead) when
marking the multiple choice answer sheet.
Most of the questions below are of the multiple choice type. Select the best answer to each
among the choices offered. If more than one answer applies, select that one which is the best
or the most comprehensive or which best expresses the most important and essential aspects
of the issue in question. Read each question completely and carefully before answering it.
Please note that the guessing factor will be applied to all multiple choice questions. I.e., for
each correct answer you will receive 10 points. For each incorrect answer, 2.5 points will be
subtracted from your score. If you do not answer a question, 0 points will be counted for that
question. Thus, on average and statistically, you will not gain points by guessing. If you can
eliminate one or more clearly incorrect answers to a question, guessing among the remaining
ones may, on average, improve your score, but you still run the risk of losing 2.5 points should
your answer turn out to be wrong.
This test consists of two parts. Mark your answers to the multiple choice questions (1-8) on
the mark sense cards provided. Answer the remaining, non-multiple choice questions on the
red answer sheet provided.
Multiple choice part, questions 1-8
Questions
1. Consider an algorithm to search a linear linked list for the first element containing a data
value equal to the value of the variable “searchkey”. The list is not necessarily sorted. When
the search algorithm ends, the variable f should point to the list element satisfying the search
criterion (data = searchkey), if any. In addition, the variable prev should point to the predecessor of the list element satisfying the search criterion. Which of the following goals (postconditions) for this search algorithm captures the above requirements best and most completely?
(Informal, explanatory comments are enclosed in {}.)
(f  –1 and data(f)=searchkey
{searchkey found}
or f = –1)
{searchkey not present}
and in every element in the list before the element to which f points (if any) the data
value is different from the value of searchkey
and (prev = –1 and start = f
{no predecessor}
or prev  –1 and next(prev) = f)
{prev points to predecessor}
1 of 3
Computer Science 1 DDS, Test 3
(f  –1 and data(f)=searchkey
{searchkey found}
or f = –1)
{searchkey not present}
and in every element in the list before the element to which f points (if any) the data
value is different from the value of searchkey
(f  –1 and data(f)=searchkey
or f = –1)
and (prev = –1 and start = f
or prev  –1 and next(prev) = f)
{searchkey found}
{searchkey not present}
{no predecessor}
{prev points to predecessor}
(f  –1 and data(f)=searchkey
{searchkey found}
or f = –1 and data(f)searchkey)
{searchkey not present}
and in every element in the list before the element to which f points (if any) the data
value is different from the value of searchkey
and start  –1
{list not empty}
f  –1 and data(f)=searchkey
{searchkey found}
and in every element in the list before the element to which f points (if any) the data
value is different from the value of searchkey
2. A new list element is to be inserted into a linear linked list. The place where the new element is to be inserted is known. The insertion algorithm must distinguish between
(1) an initially empty list and (2) an initially non-empty list.
inserting (1) at the beginning of the list and (2) after an existing element of the list.
inserting (1) at the beginning of the list, (2) between two elements of the list and (3) at
the end of the list.
(1) an initially empty list, (2) inserting at the beginning of an existing list, (3) inserting
between two elements of an existing list and (4) inserting at the end of an existing
list.
No distinction must be made; the same algorithm will correctly handle all cases.
3. Consider an implementation of a pipeline as a circular array. I.e. the array is declared with
index values ranging from 0 to N–1 inclusive, where N is some sufficiently large integer. The
value of the variable “out” is the index of the oldest data value in the pipeline and the value of
the variable “in” is the index of the array element into which the next data value is to be “put”.
The operation  represents addition modulo N. By convention, the pipeline is empty when
in=out. Which of the following conditions is a precondition for executing a put operation, i.e.
which condition must be true before a put operation is performed?
in  out.
in  1  out
in  out  1
in  1 = out
There is no restriction on performing a put operation; it may be executed at any time.
4. Consider an implementation of a pipeline as a circular array as in question 3 above. Which
of the following algorithms will get the next item from the pipeline?
valretrieved := cirpipeline(out); out := out + 1
cirpipeline(out) := valretrieved; out := out  1
valretrieved := cirpipeline(out); out := out  1
out := out  1; valretrieved := cirpipeline(out)
2 of 3
Computer Science 1 DDS, Test 3
none of the above.
5. Consider a linear linked list implementation of a pipeline. Only one set of pointers is
maintained (i.e. the list is not doubly linked). Which of the following strategies is both correct
and the simplest to implement?
Put operates on the beginning of the list, get operates on the beginning of the list.
Put operates on the beginning of the list, get operates on the end of the list.
Put operates on the end of the list, get operates on the beginning of the list.
Put operates on the end of the list, get operates on the end of the list.
Some of the above are correct implementations of a pipeline and some are incorrect.
Those which are correct result in algorithms which are structurally essentially the
same.
6. Consider a linear linked list implementation of a stack. The time required to push one new
item onto a stack already containing n items
is proportional to log n.
is proportional to n.
is proportional to n log n.
is proportional to n2.
does not vary with n.
7. The operations get (pop) and put (push) are performed on
the same end of a stack and on the same end of a pipeline.
the same end of a stack and on different ends of a pipeline.
different ends of a stack and on the same end of a pipeline.
different ends of a stack and on different ends of a pipeline.
the same or different ends of a stack or a pipeline depending upon how the stack or pipeline is implemented.
8. Regarding preorder, inorder and postorder traversals of a tree, which of the following
statements is true?
An extra initialization (preprocessing) step may be added to a preorder traversal, but not
to the other types.
An extra postprocessing step may be added to a postorder traversal, but not to the other
types.
Extra processing steps between processing the children nodes may be added to an inorder traversal, but not to the other types.
In a preorder traversal, each parent node is processed before its children nodes are processed. In a postorder traversal, each parent node is processed after its children
nodes. In an inorder traversal, the parent node is processed between processing of
the children nodes.
The different types of traversal process the children nodes in a different order.
This is the end of the multiple choice part of this test.
Proceed to the next part.
3 of 3