ECE220 – Programming for Electrical Engineering
page 1 of 12
ECE 220
Midterm (A)
March 12th, 2025, 1:00PM
Signature:
Instructions
• PRINT YOUR NAME and Student ID into the boxes above.
• Sign in the Signature box.
• Write your name and your Student ID on the General Purpose Answer Sheet. Your
Student ID should be left-aligned, e.g.
• Fill in the bubbles on the General Purpose Answer Sheet to code in your name and ID.
• Write 100000 in Special Code on the General Purpose Answer Sheet.
• This is a CLOSED BOOK exam. Time allowed: 50 minutes.
• Calculators, cell phones, or computers of any kind are not permitted.
• This exam, worth 90 points, counts 15% toward your final grade in this course.
• Write your answers properly on the answer sheet with an HB pencil. If the optical reader
cannot clearly read your answer, the mark on that question will be 0.
• All the C code provided in this exam is ANSI standard and uses the ASCII character set.
• For questions regarding code fragments, assume that all the necessary header files
and surrounding code are provided.
• Questions are weighted differently in each part: Points
o Part 1 - (5 questions at 3 point each) 15
o Part 2 - (5 questions at 5 points each) 25
o Part 3 - (5 questions at 6 points each) 30
o Part 4 - (2 questions at 10 points each) 20
• This question booklet must be returned with your General Purpose Answer Sheet for
you to get credit for this exam.
• IMPORTANT: Answer Parts 1, 2 and 3 in the General Purpose Answer Sheet. For
Part 4 use the space provided in this booklet.
Name:
Student ID:ECE220 – Programming for Electrical Engineering
page 2 of 12
Part 1: [15 points total – 5 Questions – 3 points each] – Multiple Choice Questions
Answer the following questions starting at question number 1 on the front of your General
Purpose Answer Sheet]
1. What is the output produced by the following code?
int x = 5, y = 10;
int *p1, *p2;
p1 = &x;
p2 = &y;
*p1 = *p2 + 2;
printf("%d %dn", x, y);
a) 5 10
b) 12 10
c) 10 12
d) 7 10
e) 5 12
2. What is wrong with the following code?
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
ptr = ptr + 2;
arr = ptr;
a) You cannot modify (reassign) array names
b) Array index out of bounds
c) Incorrect pointer arithmetic
d) Incorrect array initialization
e) No error, the code is fineECE220 – Programming for Electrical Engineering
page 3 of 12
3. Which of the following correctly declares (is a prototype) a function that takes an integer and
returns a pointer to a character?
a) function char fun(int);
b) char * fun(int);
c) void fun(int, char);
d) char fun(int);
e) char = fun(int);
4. What is the output of the following program?
#include stdio.h
int main() {
int arr[] = {10, 20, 30, 40, 50};
int *p = arr;
printf("%d ", *p++);
printf("%d ", (*p)++);
printf("%d ", *p);
return 0;
}
a) 10 20 20
b) 10 20 21
c) 10 21 21
d) 20 20 21
e) 20 21 21ECE220 – Programming for Electrical Engineering
page 4 of 12
5. What is the value of x after this code executes?
int x = 10;
int *ptr = &x;
*ptr += 5;
ptr = NULL;
a) 0
b) 5
c) 10
d) 15
e) undefined behaviourECE220 – Programming for Electrical Engineering
page 5 of 12
Part 2. [25 points total – 5 Questions – 5 points each] – Multiple Choice Questions
[Answer the following questions starting at question number 6 on the front of your General
Purpose Answer Sheet.]
6. What will this code print?
int arr[5] = {1, 2, 3, 4, 5};
int *p = arr;
int sum = 0;
for (int i = 0; i 5; i++) {
sum += *(p + i);
if (i % 2 == 0)
continue;
sum += i;
}
printf("%d", sum);
a) 15
b) 19
c) 18
d) 20
e) 25
7. What will be the value of results after the following code runs
(Hint: the least significant byte is stored first)?
int a = 0x;
char *p = (char*)&a;
char result = *p;
a) 0x12
b) 0x78
c) 0x56
d) 0x34
e) Cannot be determinedECE220 – Programming for Electrical Engineering
page 6 of 12
8. What will happen if this code is executed?
int* create_array(int size) {
int arr[size];
for (int i = 0; i size; i++)
arr[i] = i;
return arr;
}
int main() {
int* data = create_array(5);
printf("%d", data[2]);
return 0;
}
a) It prints 2
b) It causes a memory leak
c) Causes undefined behavior (returning address of local variable)
d) Compilation error due to variable-length array
e) It crashes (segmentation error)
9. What is the output of this nested loop?
for (int i = 1; i = 3; i++) {
for (int j = 1; j = i; j++) {
printf("%d", j);
}
}
a) 123123
b) 112123
c) 123
d) 111213
e) 121233ECE220 – Programming for Electrical Engineering
page 7 of 12
10. What is the output of the following program?
#include stdio.h
#include string.h
int main() {
char str1[20] = "Hello";
char str2[20] = "World";
char *p1 = str1;
char *p2 = str2;
while(*p1)
p1++;
while(*p2)
*p1++ = *p2++;
printf("%s", str1);
return 0;
}
a) Hello
b) World
c) HelloWorld
d) WorldHello
e) HelloWorldHelloECE220 – Programming for Electrical Engineering
page 8 of 12
Part 3: [30 points total – 5 Questions – 6 points each] – Multiple Choice Questions
[Answer the following questions starting at question number 11 on the front of your General
Purpose Answer Sheet.]
11. What will this for loop print?
for (int i = 1, j = 1; i + j 10; i *= 2, j += i / 2) {
printf("%d ", i + j);
}
a) 2 3 5 9
b) 2 4 7
c) 2 4 8
d) 2 3 6
e) 2 3 6 9
12. What is the value of result after this code runs?
int arr[5] = {1, 2, 3, 4, 5};
int result = 0;
for (int i = 0; i 5; i++) {
if (i % 2 == 0) {
for (int j = 0; j i; j++) {
result += arr[j];
}
} else {
result += arr[i];
}
}
a) 16
b) 19
c) 20
d) 22
e) 28ECE220 – Programming for Electrical Engineering
page 9 of 12
13. What is the final value of count?
int count = 0;
int i = 1;
while (i = 15) {
if (i % 3 == 0 || i % 5 == 0) {
count++;
}
i++;
}
a) 5
b) 7
c) 8
d) 10
e) 15
14. What is the output of this nested switch statement?
int x = 2, y = 3;
switch (x) {
case 1:
printf("A");
break;
case 2:
printf("B");
switch (y) {
case 1:
printf("C");
break;
case 2:
printf("D");
break;
default:
printf("E");
}
break;
default:
printf("F");
}
a) BEF
b) BC
c) BE
d) BD
e) FECE220 – Programming for Electrical Engineering
page 10 of 12
15. What does the following function do?
void mystery(char *str) {
char *start = str;
char *end = str;
char temp;
while(*end)
end++;
end--;
while(start end) {
temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}
a) Calculates string length
b) Sorts characters in the string
c) Reverses the string
d) Removes duplicates from the string
e) Counts occurrences of charactersECE220 – Programming for Electrical Engineering
page 11 of 12
Part 4: [20 points total – 2 Questions – 10 points each] – Programming Questions
[Use the space in the booklet to provide your answers.]
16. Write a function void print_pattern(int n) that prints a pattern of asterisks in the
shape of a right-angled triangle with n rows. For example, if n = 4, it should print:
*
**
***
****
void print_pattern(int n) {
/* space for your code */
for (int i = 1; i = n; i++) {
for (int j = 1; j = i; j++) {
printf("*");
}
printf("n");
}
}ECE220 – Programming for Electrical Engineering
page 12 of 12
17. Write a function char* str_reverse(char* str) that takes a string as input and
returns a new dynamically allocated string with the characters in reverse order. Do not modify
the original string. Remember to handle memory allocation properly.
char* str_reverse(char* str) {
// your code here
if (str == NULL)
return NULL;
int len = 0;
while (str[len] != '0')
len++;
char* result = (char*)malloc((len + 1) * sizeof(char));
if (result == NULL)
return NULL;
for (int i = 0; i len; i++)
result[i] = str[len - 1 - i];
result[len] = '0';
return result;
}
END OF EXAM
Content preview
ECE220 – Programming for Electrical Engineering
ECE 220
Midterm (A)
March 12th, 2025, 1:00PM
Name:
Student ID:
Signature:
Instructions
• PRINT YOUR NAME and Student ID into the boxes above.
• Sign in the Signature box.
• Write your name and your Student ID on the General Purpose Answer Sheet. Your
Student ID should be left-aligned, e.g.
• Fill in the bubbles on the General Purpose Answer Sheet to code in your name and ID.
• Write 100000 in Special Code on the General Purpose Answer Sheet.
• This is a CLOSED BOOK exam. Time allowed: 50 minutes.
• Calculators, cell phones, or computers of any kind are not permitted.
• This exam, worth 90 points, counts 15% toward your final grade in this course.
• Write your answers properly on the answer sheet with an HB pencil. If the optical reader
cannot clearly read your answer, the mark on that question will be 0.
• All the C code provided in this exam is ANSI standard and uses the ASCII character set.
• For questions regarding code fragments, assume that all the necessary header files
and surrounding code are provided.
• Questions are weighted differently in each part: Points
o Part 1 - (5 questions at 3 point each) 15
o Part 2 - (5 questions at 5 points each) 25
o Part 3 - (5 questions at 6 points each) 30
o Part 4 - (2 questions at 10 points each) 20
• This question booklet must be returned with your General Purpose Answer Sheet for
you to get credit for this exam.
• IMPORTANT: Answer Parts 1, 2 and 3 in the General Purpose Answer Sheet. For
Part 4 use the space provided in this booklet.
page 1 of 12
, ECE220 – Programming for Electrical Engineering
Part 1: [15 points total – 5 Questions – 3 points each] – Multiple Choice Questions
Answer the following questions starting at question number 1 on the front of your General
Purpose Answer Sheet]
1. What is the output produced by the following code?
int x = 5, y = 10;
int *p1, *p2;
p1 = &x;
p2 = &y;
*p1 = *p2 + 2;
printf("%d %d\n", x, y);
a) 5 10
b) 12 10
c) 10 12
d) 7 10
e) 5 12
2. What is wrong with the following code?
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
ptr = ptr + 2;
arr = ptr;
a) You cannot modify (reassign) array names
b) Array index out of bounds
c) Incorrect pointer arithmetic
d) Incorrect array initialization
e) No error, the code is fine
page 2 of 12