0% found this document useful (0 votes)
46 views11 pages

SYSC 2006 AB - Mock Midterm

sysc 2006 fall 2024 pass mock midterm

Uploaded by

diaansiddiky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views11 pages

SYSC 2006 AB - Mock Midterm

sysc 2006 fall 2024 pass mock midterm

Uploaded by

diaansiddiky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Peer Assisted Study Sessions (PASS)

Facilitator: Teghbir Chadha

SYSC 2006 AB
It is most beneficial to you to write this mock midterm UNDER EXAM CONDITIONS. This
means:
 Complete the midterm in 2 hours, including:
o 10 minutes to set up
 Work on your own and attempt every question.
 Keep your notes and textbook closed.
 Practice with comas if possible.
After the time limit, go back over your work with a different colour or on a separate piece of
paper and try to do the questions you are unsure of. Record your ideas in the margins to remind
yourself of what you were thinking when you take it up at PASS.
The purpose of this mock exam is to give you practice answering questions in a timed setting
and to help you to gauge which aspects of the course content you know well, and which are in
need of further development and review. Use this mock exam as a learning tool in preparing for
the actual exam.
Please note:
 Complete the mock exam before attending the take-up session. During the session you
can work with other students to review your work.

 Often, there is not enough time to review the entire exam in the PASS workshop. Decide
which questions you want to review the most – the Facilitator may ask students to vote
on which questions they want to discuss in detail.

 Facilitators will not distribute an answer key for mock exams. The Facilitator’s role is to
help students work together to compare and assess the answers they have. If you are
not able to attend the PASS workshop, you can work alone or with others in the class.

 PASS worksheets and mock exams are designed as a study aid only for use in PASS
workshops. Worksheets and mock exams may contain errors, intentional or otherwise. It
is up to the student to verify the information contained within by attending the PASS
workshop.
Good Luck writing the Mock Exam!

Take-up Session #1: Wednesday, October 16th, 5:05-6:55pm (LA B249)

Take-up Session #2: Friday, October 18th, 9:35-11:25am (LA B249)

Office Hour:

Contact Information: [email protected]

Part A: Multiple Choice


Peer Assisted Study Sessions (PASS)
Facilitator: Teghbir Chadha

This section is to be completed without the use of outside notes or an IDE.


In the following questions, highlight or underline. Only select one option unless stated otherwise,
or else the whole question will be marked as incorrect.
Each question is worth 1 point.
As bonus study material outside of the allotted time, attempt to explain why.

1) What does the add function do?

#include <stdio.h>
int add(int *a, int *b) {
return *a + *b;
}
int main() {
int x = 5, y = 10;
printf("%d\n", add(&x, &y));
return 0;
}

a) Returns the sum of the values pointed to by a and b


b) Returns the difference of the memory addresses of a and b
c) Returns the memory addresses of a and b
d) Returns the sum of the memory addresses of a and b

2) What is the output of the following?

#include <stdio.h>
int sum(int n) {
if (n <= 0) {
return 0;
} else {
Peer Assisted Study Sessions (PASS)
Facilitator: Teghbir Chadha

return n + sum(n - 1);


}
}
int main(void) {
int result = sum(5);
printf("%d", result);
return 0;
}

a) 15
b) 5
c) 10
d) 20
e) None of the above

3) double func(double x) {
return 2 * x;
}
int main(void) {
int a = 10;
double b;

b = func(a);
return 0;
}

Select the correct statement:


a) b = 20
b) b = 20.0
Peer Assisted Study Sessions (PASS)
Facilitator: Teghbir Chadha

c) Function func returns a different value


d) This program causes a compilation error
e) This program causes a run-time error

4) int main(void) {
int a = 6;
int b = 2;
int *p1;
p1 = &a;
int *p2 = &b;
*p2 = (*p1) * 2;
p1 = &(*p2);
*p2 = a;
return 0;
}

What is the value of b at the end of execution?

a) 0
b) 2
c) 4
d) 6
e) 12

5) void swap(int *px, int *py) {


int *temp;
*temp = *px;
*px = *py;
*py = *temp;
Peer Assisted Study Sessions (PASS)
Facilitator: Teghbir Chadha

}
int main(void) {
int a = 2;
int b = 3;
swap(&a, &b);
}

What is the value of b after execution?

a) 2
b) 3
c) Something else
d) This program causes a run-time error
e) This program causes a compilation error

6) typedef struct person {


int age;
double height;
int shoeSize;
} person_t;

int main(void) {
person_t Paige, Jane, Johnna;
person_t *pa = &Jane, *pb = &Johnna;
Paige.age = 19; // Line A
*pa.shoeSize = 7; // Line B
pb->height = 152; // Line C
struct person Becca = {21, 160, 6}; // Line D
}
Peer Assisted Study Sessions (PASS)
Facilitator: Teghbir Chadha

If there is an error, which line does it occur?

a) Line A
b) Line B
c) Line C
d) Line D
e) There are no errors.

7) What is the correct way to read a string of characters (up to 50 characters) using scanf in C?

a) scanf("%s", str);
b) scanf("%50s", &str);
c) scanf("%49s", str);
d) scanf("%50c", str);
e) None of the options work

8) Given the following code snippet:


int a = 6;
int b = 3;
int *pb = &b;
What does the following statement do?
int result = a / *pb;

a) It assigns 2 to the result.


b) It assigns 3 to the result.
c) It assigns 0 to the result.
d) It causes a compilation error.
Peer Assisted Study Sessions (PASS)
Facilitator: Teghbir Chadha

9) Consider the following function that is designed to find the maximum value among the first
size integers in an array:
int find_max_v1(int arr[], int size) {
int max = arr[0];
for (int i = 1; i <= size; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}

What is the outcome of using this function?


a) This function returns the correct result
b) This function returns an incorrect result
c) This function causes a compilation error
d) This function may cause the program to terminate with a run-time error

10) The statement:


int *p;
What does this statement represent?

a) declares a variable named *p with type int


b) declares a variable named p with type "pointer-to-int"
c) declares a variable that stores an integer
d) declares an array of integers

11) Consider this:


int multiply(int x) {
int result = x * 2;
Peer Assisted Study Sessions (PASS)
Facilitator: Teghbir Chadha

return result;
}
int sum(int a, int b) {
int sum_value = multiply(a) + b; // Line B
return sum_value;
}
int main(void) {
int a = 4, b = 6;
printf("%d\n", multiply(a));
printf("%d\n", sum(a, b));
return 0;
}

Immediately after int sum_value = multiply(a) + b; (Line B) is executed, but before return
sum_value; is executed, how many variables named result exist in memory?

a) 0
b) 1
c) 2
d) 3

12) What will be the output:


int main(void) {
int arr[6] = {2, 3, 5, 6, 8, 10};
for (int i = 0; i < 6; i += 2) {
if (arr[i] % 2 != 0)
printf("%d,", arr[i]);
}
return 0;
}
Peer Assisted Study Sessions (PASS)
Facilitator: Teghbir Chadha

a) 2,6,10
b) 5
c) 3,5,10
d) 8,10
e) Nothing is printed
f) Something else is printed

Part B: Memory Diagrams


This section is to be completed without the use of outside notes or an IDE.
Draw the memory diagram of what is asked in the question.

1. #include <stdio.h>

void adjust(int *x, int *y) {


*x += *y;
*y += 3; // Point B
}

int modify(int *x, int *y) {


*x += 2;
*x += *y; //Point A
adjust(x, y);
return *x - *y;
}

int main(void) {
int a = 5;
int b = 10;
Peer Assisted Study Sessions (PASS)
Facilitator: Teghbir Chadha

int result = modify(&a, &b);


printf("Result: %d\n", result);
return 0;
}

(a) Draw the memory diagram similar to one that would be produced by the C Tutor immediately
after the statement at Point A has been executed. (5 points)

(b) Draw the memory diagram similar to one that would be produced by the C Tutor immediately
after the statement at Point B has been executed. (5 points)

2. #include <stdio.h>

typedef struct {
int x;
int y;
} point_t;

void updatePoints(point_t *arr, int size) {


for (int i = 0; i < size; i++) {
arr[i].x += i; // Point A
arr[i].y += 2;
}
}

int main(void) {
point_t arr[2] = {{1, 2}, {3, 4}};
updatePoints(arr, 2);
printf("Point 1: (%d, %d)\n", arr[0].x, arr[0].y);
Peer Assisted Study Sessions (PASS)
Facilitator: Teghbir Chadha

printf("Point 2: (%d, %d)\n", arr[1].x, arr[1].y);


return 0;
}

(a) Draw the memory diagram similar to one that would be produced by the C Tutor immediately
after the statement at Point A has been executed for the first time. (5 points)

(b) Draw the memory diagram similar to one that would be produced by the C Tutor immediately
after the statement at Point A has been executed for the second time, that is, immediately after
the statement arr[i].x += i; is executed for the second time, but before the function returns. (5
points)

Part C: Coding
This section must be completed on paper to reflect midterm conditions. It can be checked with
an IDE and C Tutor

1. Write a program that takes a string input from the user and counts the number of vowels
using a recursive function. (10 points)

2. Define a structure to hold employee information (name, ID, hourly wage, hours worked).
Write a function that takes a pointer to an employee structure, updates the hours
worked, and calculates the annual salary. What are the advantages of passing the
structure by pointer rather than by value? (13 points)

OPTIONAL (EXTRA PRACTICE):

3. Write a function that takes an integer array and its size, doubles each element using
pointer arithmetic, and then prints the modified array. (10 points)

4. Write a C function that swaps the elements at two given indices in an array. Use pointers
to perform the swap. (10 points)

You might also like