100% found this document useful (1 vote)
347 views21 pages

FOA Practice Exam Attempt 1

This practice exam has 22 multiple choice and short answer questions worth 40 total points. Students have 135 minutes to complete the exam and are allowed unlimited attempts. The instructions specify that students may only use the textbook, lecture slides, and certain programming environments during the exam. Communication devices and online resources are not permitted. All answers must be the student's own unassisted work.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
347 views21 pages

FOA Practice Exam Attempt 1

This practice exam has 22 multiple choice and short answer questions worth 40 total points. Students have 135 minutes to complete the exam and are allowed unlimited attempts. The instructions specify that students may only use the textbook, lecture slides, and certain programming environments during the exam. Communication devices and online resources are not permitted. All answers must be the student's own unassisted work.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Practice Exam

Due No due date Points 40 Questions 22


Time Limit 135 Minutes Allowed Attempts Unlimited

Instructions
This Practice Exam is an accurate reflection of the content, style, and difficulty of the main exam.

In terms of Academic Honesty, you may:

employ grok or a gcc/compilation environment to write and test functions, before you paste
them into the answer boxes;
make use of the textbook and lecture slides in soft- or hard-copy form.

You must not:

make any use of communications devices or channels, such as mobile phones, text messages,
WeChat or WhatsApp, email, or other messaging technologies, while undertaking this
assessment;
make use of any world-wide web or internet based resources, including google and other
search services, wikipedia, and stackoverfow;
make use any soft- or hard-copy documents or resources except for the textbook and lecture
slides;
discuss the questions or their answers with any other person, both during the Exam and after
the completion of the Exam;
engage in any actions that would encourage, permit, or support other enrolled students to
violate the Academic Honesty expectations that apply to this assessment, even if they are
completing this assessment at a different time to you.

All submitted answers must be your own unassisted work.

In the real Exam you will have 15 minutes of reading time, and then 120 minutes of writing time.
This Practice Quiz will allow you 135 minutes of access.

Take the Quiz Again

Attempt History
Attempt Time Score
LATEST Attempt 1 128 minutes 11.67 out of 40 *
* Some questions not yet graded

Submitted Oct 29 at 21:52

Section 1 -- Multiple Choice (8 marks)

Question 1 1 / 1 pts

The four fundamental building blocks of computation are:

calculation, selection, iteration, and mathematics

calculation, simplification, recursion, and mathematics

Correct!
calculation, selection, iteration, and abstraction

calculation, mathematics, selection, and switching

calculation, selection, mathematics, and abstraction

Question 2 0 / 1 pts

If c is a pointer to a structure, which expression is not possible under


any circumstances?

Correct Answer c.c.c.c

*c
c->c->c->c

(*c).a.b.c

You Answered c[0].c

Question 3 1 / 1 pts

Which of the following statements about structs and functions is not


true:

Correct!
Struct variables can only be passed to functions via a pointer

The individual components of a struct variable can be passed to a


function if required, as if they were independent variables of the given
type

Structs can be created inside a function and then their values


returned using the standard return statement

The components of a struct variable passed as an argument to a


function can be individually modified within a function, but the
changes will be lost when the function returns

If a struct variable is passed into a function via a pointer, the function


can use the -> operator to alter individual components within the
original struct variable

Question 4 0 / 1 pts
What is size_t in C?

Correct Answer
A data type for storing the non-negative integers used for memory
management.

A constant that contains information about the maximum memory


available for use in your program while it is running.

You Answered

A data type for storing floating point numbers of arbitrary precision.

The name of a function in one of the standard C libraries.

A data type that Alistair invented and used in one of the example
programs shown in the subject.

Question 5 0 / 1 pts

Suppose that 8-bit two's complement numbers are being employed.


What decimal integer would give rise to the eight-bit sequence
11011101:

Correct Answer -35

-38

+93

+38
You Answered +221

Question 6 1 / 1 pts

In the 32-bit floating point system (for float variables) that was
explored using the floatbits.c program the following bit patterns are
observed for the three given values, expressed as four 8-bit bytes:

2.0 = 01000000 00000000 00000000 00000000


2.5 = 01000000 00100000 00000000 00000000
3.0 = 01000000 01000000 00000000 00000000

The bit pattern corresponding to the decimal number 3.5 is most


likely to be:

Correct! 01000000 01100000 00000000 00000000

01000000 01110000 00000000 00000000

01000000 01101000 00000000 00000000

01000000 11100000 00000000 00000000

01000000 01101100 00000000 00000000

Question 7 1 / 1 pts

Which of the following is not a valid file access mode to use with a
call to fopen():

"a", to append to an existing file


"a+", to both read from and append to an existing file

"w+", to create a new file that will be both written to and also read

"r", to read from an existing file

Correct!
"rw", to both read from and write to an existing file

Question 8 1 / 1 pts

Stacks and queues are two abstract data types that both support
insert and remove operations. One way of implementing these data
types is via linked lists. In regard to a linked list implementation of
stacks and queues, which is the most correct statement:

Correct!

In a stack, insertion is at the head, and removal is also at the head

In a queue, insertion is at the head, and removal is also at the head

In a queue, insertion is at the tail, and removal is also at the tail

The stack or the queue cannot be permitted to contain duplicate


items

To implement stacks and queues efficiently, doubly-linked lists are


required, rather than singly-linked lists
Section 2 -- Short Answer (10 marks)

Question 9 0.67 / 2 pts

Consider the following code fragment, where n is an int variable that


has some positive initial value.

void
mystery(int n) {
int i, j, k;
int sum1=0, sum2=0, sum3=0;
for (i=0; i<n; i++) {
sum1++;
for (j=0; j<i; j++) {
sum2++;
for (k=i*j; k>0; k=k/2) {
sum3++;
}
}
}
return;
}

Each of the three variables sum1, sum2, and sum3 ends up with a value
that is a function of n. Match those variables on the left with the
possible big-Oh expressions on the right.

Correct! sum1 grows as O(n) (linear)

You Answered sum2 grows as O(n log n) (n times the log of n)

Correct Answer O(n^2) (quadratic)

You Answered sum3 grows as O(n^2) (quadratic)


Correct Answer O(n^2 log n) (n-
squared times the
log of n)

Other Incorrect Match Options:


O(n log n) (n times the log of n)
O(n^2.5) (n-squared times the square root of n)
O(n^3) (cubic)
O(n^0.5) (square root of n)
O(n^1.5) (n times the square root of n)

Question 10 0 / 2 pts

Suppose that the pattern

now#or#never

is being used to search the text T given by

0 0 1 1 2 2 3
01234567890123456789012345678901234
not#now#not#never#not#now#nor#never

using the KMP algorithm.

The first alignment of the pattern is at offset 0 in T, and is where the


pattern matching process commences.

Trace the KMP algorithm as it operates from then on, and indicate
the character offsets in T (using the numbers above the string as a
guide) of the third, fifth, and seventh alignments of the pattern.

You Answered The offset associated 2


with the third alignment
is

Correct Answer 3
You Answered The offset associated 4
with the fifth alignment
is

Correct Answer 8

You Answered The offset associated 8


with the seventh
alignment is

Correct Answer 11

Other Incorrect Match Options:


12
6
2
1
5
7
10
4
9

Question 11 2 / 2 pts

Consider the seven-character string "baa#bab", and an eight-element


suffix array S[8] constructed for it. Fill in the values of the specified
elements of S[].

When constructing the suffix array, assume that character '#' is


smaller than any of the alphabetic characters, and that '$' is the
special symbol appended to the string that is smaller than all of the
normal characters.

Correct! The value of S[0] is


7

Correct! The value of S[1] is 3

Correct! The value of S[5] is 6

Other Incorrect Match Options:


1
0
4
2
5

Question 12 2 / 2 pts

The following declarations are exactly the same as the ones used in
the file lists.h that was discussed in class.

typedef struct node node_t;

struct node {
data_t data;
node_t *next;
};

typedef struct {
node_t *head;
node_t *foot;
} list_t;

A student wrote the following function to reverse the nodes in a list by


reassigning all the pointers, so that the node (and data item) that
used to be at the head of the list is now at the foot, and the node (and
data item) that used to be at the foot of the list is now at the head.
But four of the student's assignment statements have been lost.

list_t
*reverse(list_t *list) {
node_t *curr, *prev, *next;
assert(list);
prev = NULL;
curr = list->head;
while (curr) {
// line A
// line B
// line C
curr = next;
}
list->foot = list->head;
// line D
return list;
}

Match the line locations on the left with the correct assignment
statements on the right.

Correct! line A next = curr->next

Correct! line B curr->next = prev

Correct! line C prev = curr

Correct! line D list->head = prev

Other Incorrect Match Options:


curr = next
next = next->next
list->head = list->head->next
curr->next = curr
list->head = list->foot
curr = prev

Question 13 2 / 2 pts

The first phase of heapsort converts the input array to an initial heap.

Suppose that an eight-element input array is given by

A[] = { 10, 15, 16, 14, 20, 17, 22, 29 }


and gets converted to a heap by the first phase of heapsort.

Match the initial heap array locations on the left with the elements
that they contain on the right.

Correct! A[0] has the value 29

Correct! A[1] has the value 20

Correct! A[2] has the value 22

Correct! A[6] has the value 16

Other Incorrect Match Options:


17
14
15
10

Section 3 -- Programming (14 marks)

Question 14 Not yet graded / 3 pts

A program is being written to manipulate data about books in


libraries. Each book has a unique integer book number assigned to it,
an author, a title, a publisher, a year of publication, a count of the
total number of times it has been borrowed, and an array storing the
integer library card numbers of the (up to) ten people who borrowed it
most recently. The book title, author, and publisher strings may
contain up to forty characters.

The library itself might contain as many as 100,000 books.

Give suitable #define, typedef, and then variable declarations to


represent the books being held by the library.

Be sure to select "Preformatted" for the LMS text box before you
enter each answer, and (if necessary) again after you have pasted
text into it. If you choose to develop your function using grok or
another external tool, be sure to paste your answer regularly into the
text box, so your progress is recorded; and be sure to use spaces to
indent your code, and not tabs. Tabs cannot be typed into the LMS
text box.

Your Answer:
#define MAX_CHAR 40;
#define MAX_BOOK 100000;
#define MAX_RECENT_BORROW 10;

typedef struct {
int book_num;
char title[MAX_CHAR+1];
char author[MAX_CHAR+1];
char publisher[MAX_CHAR+1];
int year;
int borrow_times;
int library_cards[MAX_RECENT_BORROW+1];
} book_t;

typedef struct {
book_t library[MAX_BOOK];
} library_t;

Question 15 Not yet graded / 3 pts

Suppose that an integer array is being used to store sequences of


values that are strictly positive, for example:

{1,1,1,2,2,2,2,2,5,4,4,4,1,1,1,1,1,1,3,3,3,0}

The last value in the array is always zero, and provides a sentinel,
meaning that a buddy variable is not required.
A student notices that there are often repeated values straight after
each other, and suggests that the data could be restructured into a
packed form, with negative numbers introduced to indicate repetitions
of the previous (positive) value. For example, the same data would
be represented in this packed form as:

{1,-2,2,-4,5,4,-2,1,-5,3,-2,0}

In this packed representation each negative value –n means that the


immediately preceding value from the array should be duplicated
another n times. For example, the first three values of the original
array, {1,1,1}, are represented by the first two values of the packed
array, {1,-2}.

Write a function

void unpack(int A[])

that reverses that transformation, expanding the positive and


negative values in A[] back to the original set of all positive values.
Your function must take an array of packed data in A[0..n-1] where
A[n-1] is the sentinel zero value, and unpack it to form A[0..m-1]
where now A[m-1] is the sentinel zero.

You may assume that A[] is large enough to contain the fully
unpacked sequence. You may not declare any arrays within your
function.

Be sure to select "Preformatted" for the LMS text box before you
enter each answer, and (if necessary) again after you have pasted
text into it. If you choose to develop your function using grok or
another external tool, be sure to paste your answer regularly into the
text box, so your progress is recorded; and be sure to use spaces to
indent your code, and not tabs. Tabs cannot be typed into the LMS
text box.

Your Answer:
void
unpack(int A[]) {
int i = 0, j;

while (A[i]!=0) {
if (A[i] < 0) {
for (j=i+1; j!=0; j++) {
A[j]
}
}

i++
}

The next several questions relate to the following declarations. Study


them carefully and the other information provided here, and then
move on to the questions.

A set of simplified declarations is being used to represent binary


search trees in which each stored element is of type data_t:

typedef struct tree tree_t;

struct tree {
data_t data; // the data stored at this node
tree_t *left; // left subtree of node
tree_t *rght; // right subtree of node
};

Two objects of type data_t can be compared in the usual manner by


calling the function:

int cmp_data(data_t *d1, data_t *d2)

A tree_t can store duplicate elements by simply inserting the second


instance of any value as if it was very slightly greater than the first
instance of that value. For example, the tree constructed by inserting
the strings

"fat", "cat", "fat", "bat", "eat", "rat", "fat", "cat"

would be:

fat
/ \
cat fat
/ \ \
bat eat rat
/ /
cat fat

The "handle" to access a tree is of type tree_t *, and an empty tree


is represented by the value NULL.
Be sure to select "Preformatted" for the LMS text box before you
enter each answer, and (if necessary) again after you have pasted
text into it. If you choose to develop your function using grok or
another external tool, be sure to paste your answer regularly into the
text box, so your progress is recorded; and be sure to use spaces to
indent your code, and not tabs. Tabs cannot be typed into the LMS
text box.

You only need to submit the required function, but may include other
functions too if you decide to break the process into smaller parts. Do
not submit a main program.

Question 16 Not yet graded / 3 pts

In the context of the declarations that have been provided, write a


function

int sum_tree(tree_t *t);

that applies another function

int get_int(data_t *d)

to each data item stored in the tree, and returns the sum of those
integer values, added up over all of the data_t elements stored in the
tree. If t is empty then sum_tree(t) should return zero. You do not
need to write get_int(), and may call it without knowing anything
about its operation.

Include a comment prior to each main block of code to indicate your


intentions.

Be sure to select "Preformatted" for the LMS text box before you
enter each answer, and (if necessary) again after you have pasted
text into it. If you choose to develop your function using grok or
another external tool, be sure to paste your answer regularly into the
text box, so your progress is recorded; and be sure to use spaces to
indent your code, and not tabs. Tabs cannot be typed into the LMS
text box.
Your Answer:
int
sum_trees(tree_t *t) {
int right_side, left_side, sum=0;

if (t!= NULL) {
right_side = sum_trees(t -> right);
left_side = sum_trees(t -> left);
sum = (t -> data) + right_side + left_side;
}
}

Unanswered
Question 17 Not yet graded / 3 pts

In the context of the declarations that have been provided, write a


function

tree_t *bst_insert(tree_t *t, data_t *d);

that creates a new tree node that stores the data value indicated by
*d, and inserts it into the correct place in t, returning the new address
of the root of the tree.

For example, a typical calling sequence for this function might be:

tree_t *t=NULL;
data_t d;
while (get_value(&d)) {
// now insert d into t
t = bst_insert(t, &d);
}
// t now contains all of the data

Include a comment prior to each main block of code to indicate your


intentions.

Before starting to answer this question, you should read the next
question too. You may find it convenient to develop a single helper
function that can assist with both bst_insert() and the bst_merge()
function required by the next question.

Be sure to select "Preformatted" for the LMS text box before you
enter each answer, and (if necessary) again after you have pasted
text into it. If you choose to develop your function using grok or
another external tool, be sure to paste your answer regularly into the
text box, so your progress is recorded; and be sure to use spaces to
indent your code, and not tabs. Tabs cannot be typed into the LMS
text box.

Your Answer:

Unanswered
Question 18 Not yet graded / 2 pts

In the context of the declarations that have been provided, write a


function

tree_t *bst_merge(tree_t *t1, tree_t *t2);

that constructs and returns a single tree containing the union (the
merge) of the elements in t1 and t2, by combining them and at the
same time destroying the original trees.

For example, a typical calling sequence for this function might be:

tree_t *t1=NULL, *t2=NULL;


... // construct tree t1
... // construct tree t2
t1 = bst_merge(t1, t2);
t2 = NULL; // this tree not required now

Include a comment prior to each main block of code to indicate your


intentions.

Be sure to select "Preformatted" for the LMS text box before you
enter each answer, and (if necessary) again after you have pasted
text into it. If you choose to develop your function using grok or
another external tool, be sure to paste your answer regularly into the
text box, so your progress is recorded; and be sure to use spaces to
indent your code, and not tabs. Tabs cannot be typed into the LMS
text box.

Your Answer:
Section 4 -- Algorithms (8 marks in
total)
The next several questions relate to the following problem
description. Read it carefully, and then move on to the questions.

A group of student are discussing how to compute the k th smallest


value in an input array containing n values, and where 0 ≤ k < n/2.
For example if the input array is:

A[] = { 10, 17, 18, 12, 35, 24, 19 }

then when k=2, the value required is 17, because if A[] was sorted,
then A[2] would be 17.

The input data is provided in an unsorted array, and the only


operation that may be performed on elements of the array is to
compare them against other elements. The elements in the input
array may be permuted by the function doing the computation, and
their order can be changed as the computation proceeds.

The next several questions involve a range of different algorithms


that might be used to solve this problem, given these constraints.

Question 19 Not yet graded / 2 pts

Student A says, "this problem is easy to solve, and a solution can


always be achieved in O(n log n) worst-case time, provided I can use
O(n) extra space".

Use the answer box below to describe in English the approach that
Student A is thinking of.

You must give enough detail in your answer that the marker can be
sure that the approach you are describing will (a) solve the problem,
and (b) have the stated analysis.
Your Answer:

Student A is using merge sort. Using divide and conquer sorting


algorithm, the array is split into two halves, using recursion, call the
merge sort function for the first half and then the second half. then,
merge the two sorted halves created just now.

Question 20 Not yet graded / 2 pts

Student B says, "this problem is easy to solve, and a solution can


always be achieved in O(n + kn) worst-case time, with O(1) extra
space required".

Use the answer box below to describe in English the approach that
Student B is thinking of.

You must give enough detail in your answer that the marker can be
sure that the approach you are describing will (a) solve the problem,
and (b) have the stated analysis.

Your Answer:

Student B is using selection sort.

Question 21 Not yet graded / 2 pts

Student C says, "this problem is easy to solve, and a solution can


always be achieved in O(n + k log n) time, with O(1) extra space
required".

Use the answer box below to describe in English the approach that
Student C is thinking of.

You must give enough detail in your answer that the marker can be
sure that the approach you are describing will (a) solve the problem,
and (b) have the stated analysis.
Your Answer:

Student C is using heap sort

Question 22 Not yet graded / 2 pts

Student D says, "this problem is easy to solve, and a solution can


always be achieved in O(n) time on average, but with a worst case
time of O(n2), and with O(1) extra space required".

Use the answer box below to describe in English the approach that
Student D is thinking of.

You must give enough detail in your answer that the marker can be
sure that the approach you are describing will (a) solve the problem,
and (b) have the stated analysis.

Your Answer:

Student D is using insertion sort

You might also like