Sample Midterm Exam Paper of CSC3100
Sample Midterm Exam Paper of CSC3100
2023 Summer
Note:
a. No notes or calculators are allowed in the exam.
b. This exam paper has five pages, in double-sided printing.
c. Answer all questions within 100 minutes in an answer book.
d. Write your name and student ID on both the exam paper and answer book.
1. (10×2 points) Choose ONE solution that best suits each question. (2 points each)
(1). An array is declared as a[m][n] where m is the number of rows while n is the
number of columns. Each element of the array occupies b bytes in memory. Denote the
address of the first element a[0][0] by “BA”. What is the address of an element a[i][j]
(0 ≤ i < m, 0 ≤ j < n) if the array is stored row by row (i.e. row-major)?
A. BA + (i × n + j) × b
B. BA + (j × m + i) × b
C. BA + i × j × b + m × n
D. BA + m × n × b + i × j.
(2). What is the BEST and WORST complexity of quick sort?
A. O(n) and O(n2 )
B. O(log(n)) and O(n2 )
C. O(n log(n)) and O(n2 )
D. O(n) and O(n log(n))
(3). Which statement below is WRONG concerning to stack data structure?
A. push() and pop() are two operations defined in stack.
B. A stack contains a sequence of zero or more items of the same type.
C. List-based stack has no limit on total number of items of the stack.
D. Stack is a First-in-first-out (FIFO) structure.
(4). How many comparisons does selection sort make when the input array with size
n is already sorted?
A. O(log n)
B. O(n)
C. O(n log n)
D. O(n2 )
(5). How many comparisons does insertion sort make when the input array with size
n is already sorted?
CUHK(SZ)-CSC3100 Midterm Exam - Page 2 of 6 July 2023
A. O(log n)
B. O(n)
C. O(n log n)
D. O(n2 )
(6). One difference between a queue and a stack is:
A. Queues require linked lists, but stacks do not.
B. Stacks require linked lists, but queues do not.
C. Queues use two ends of the structure; stacks use only one.
D. Stacks use two ends of the structure, queues use only one.
(7). Consider the following pseudo-code:
declare a stack of characters
while ( there are more characters in the word to read )
{
read a character
push the character on the stack
}
while ( the stack is not empty )
{
pop a character off the stack
write the character to the screen
}
A. 321-
B. 321-123
CUHK(SZ)-CSC3100 Midterm Exam - Page 3 of 6 July 2023
C. -123
D. run-time error
(9). What does the function f unc do in general?
public static void func(Queue Q)
{
Stack S = new Stack();
while (!isEmpty(Q))
{
S.push(Q.dequeue());
}
while (!isEmpty(S))
{
Q.enqueue(S.pop());
}
}
2. (5×5 points) Give concise answers to the following short questions (5 points each).
5. (5 points) In the method F below, q1 and q2 are two queues containing integer items.
What should method F print on the screen?
public static void F(){
Queue q1 = new Queue();
Queue q2 = new Queue();
for (int i=1; i<10; i=i+2){
q1.enqueue(i);
q2.enqueue(i+1);
}
while(!q1.isEmpty()){
System.out.print(q1.dequeue()+" ");
System.out.print(q2.dequeue()+" ");
}
}
CUHK(SZ)-CSC3100 Midterm Exam - Page 6 of 6 July 2023
6. (10 points) Given an array of random integers, Push all the zero’s of a given array to
the end of the array. For example, an array before and after reorganization is like:
Before: arr[] = 1, 2, 0, 4, 3, 0, 5, 0;
After: arr[] = 1, 2, 4, 3, 5, 0, 0, 0;
Write in Java or pseudo-code. You are recommended to use the following pseudo-code
style.
pushZero(int arr[]){
... // add your operations
}
7. (10 points) Given a queue Q, you are asked to design an algorithm to reverse Q using
another queue. You can use standard operations defined in a queue ADT. Write in Java
or pseudo-code. You are recommended to use the following pseudo-code style.
reverse(queue Q, queue Q_1){ //Q is to be reversed using Q_1.
...
// return either Q or Q_1 that consists of the reversed sequence.
}
8. (20 points) Given two queues Q1 and Q2 with their standard operations (enqueue,
dequeue, isempty, size), implement a stack ADT S with its standard operations (pop,
push, isempty, size). Suppose that once S is created, you are given two empty queues
Q1 and Q2 by default. Write in Java or pseudo-code. You are recommended to use the
following pseudo-code style.
class S{
queue Q_1 //one queue instance
queue Q_2 //another queue instance
pop(){
... // use Q_1, Q_2 here
}
push(x){
... // use Q_1, Q_2 here
}
...
}