• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 2 out of 11 pages
Exam (elaborations)

ECE220 – Programming for Electrical Engineering: ECE 220 Midterm (A)| questions and answers latest spring 2026 - University of Alberta.

Document preview thumbnail
Preview 2 out of 11 pages

ECE220 – Programming for Electrical Engineering page 1 of 11 ECE 220 Midterm (A) March 11th, 2026, 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 11 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 = 4, y = 7; int *p; p = &x; *p = y + 1; printf("%d %dn", x, y); a) 4 7 b) 5 7 c) 7 8 d) 8 8 e) 8 7 2. What is the value of *(arr+2) of the following array? int arr[4] = {5, 10, 15, 20}; a) 5 b) 10 c) 15 d) 20 e) address of arr[2] 3. Which of the following correctly declares (is a prototype) a function that takes a pointer to integer and returns an integer? a) int fun(int); b) int * fun(int *); c) void fun(int *); d) int fun(int *); e) char = fun(int);ECE220 – Programming for Electrical Engineering page 3 of 11 4. What is the output of the following code? int a[3] = {2, 4, 6}; int *p = a; printf("%d ", *p); p++; printf("%dn", *p); a) 2 4 b) 2 6 c) 4 6 d) 4 4 e) undefined behavior 5. What is wrong with the following code? char word[10] = "hello"; word = "abc"; a) nothing is wrong b) array index out of bounds c) string too long d) you cannot assign to an array name e) "abc" is not a stringECE220 – Programming for Electrical Engineering page 4 of 11 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 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"); } default: printf("F"); } a) BEF b) BC c) BE d) BD 7. What is the problem with this function? int *fun(void) { int x = 5; return &x; } a) it returns too many values b) x should be float c) it returns the address of a local variable d) functions cannot return pointers e) no problemECE220 – Programming for Electrical Engineering page 5 of 11 8. What is printed? int x = 7; int *p = &x; (*p)++; printf("%dn", x); a) 7 b) 8 c) address of x d) undefined behavior e) compilation error 9. What is printed? int *p; p = malloc(sizeof(int)); *p = 5; free(p); printf("%dn", *p); a) 0 b) 5 c) address of p d) undefined behavior e) compilation errorECE220 – Programming for Electrical Engineering page 6 of 11 10. What is printed? void fun(int *p) { *(p + 1) = 20; } int main() { int a[3] = {5,10,15}; fun(&a[-1]); printf("%d %dn", a[0], a[1]); return 0; } a) 5 10 b) 20 10 c) 5 20 d) 20 5 e) index out of rangeECE220 – Programming for Electrical Engineering page 7 of 11 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 is the value of count after this code runs? int arr[6] = {2, 5, 8, 1, 6, 4}; int *p = arr; int count = 0; for (int i = 0; i 6; i++) { if (*(p + i) 4) count++; } a) 6 b) 5 c) 4 d) 3 e) 2 12. What is the final value of count? int count = 0; int i = 1; while (i = 10) { if (i % 3 == 0 || i % 4 == 0) { count++; } else { count += 2; } i++; } a) 5 b) 7 c) 10 d) 12 e) 15ECE220 – Programming for Electrical Engineering page 8 of 11 13. What is wrong with the following code? #include stdio.h #include stdlib.h int main() { char str[] = "Hello"; char *copy; int i = 0; copy = (char *)malloc(6 * sizeof(char)); while (str[i] != '0') { copy[i] = str[i]; i++; } copy[i] = '0'; free(copy); printf("%sn", copy); return 0; } a) malloc() cannot be used for strings b) the null terminator is missing c) memory is freed and then used d) free(copy) should be before copy[i] = '0' e) nothing is wrong 14 e). F What will this code print? int arr[5] = {1, 2, 3, 4, 5}; int *p = &arr[4]; int sum = 0; for (int i = 4; i; i--) { sum += *(p - i); if (i % 2 == 0) continue; sum += 3; } printf("%d", sum); a) 10 b) 13 c) 14 d) 16 e) 17ECE220 – Programming for Electrical Engineering page 9 of 11 15. Analyze the function below. void mystery(char *str) { char *start = str; char *end = str; char temp; while(*end && *end != '#') end++; end--; while(start end) { temp = *start; *start = *end; *end = temp; start++; end--; } } What will the following code print? char array[] = "March#11.2026"; mystery(array); printf("%s" , array); a) March#11.2026 b) 2026.11#March c) hcraM#11.2026 d) #11.2026March e) #11.2026hcraMECE220 – Programming for Electrical Engineering page 10 of 11 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 that takes a string as input and returns the number of lowercase letters in the string. For example: if the input string is "AbcDxy1", the function returns 4. Prototype: int count_lowercase(char *str); Notes: count only characters from 'a' to 'z' /* space for your function code */ int count_lowercase(char *str) { int i = 0; int count = 0; while(str[i++]!=0) { if (str[i] = 'a' && str[i] = 'z') { count ++; } } return count; }ECE220 – Programming for Electrical Engineering page 11 of 11 17. Write a function char* str_copy_until(char* str, char symbol) that takes a string and a character symbol, and returns a new dynamically allocated string only with the part before the first occurrence of symbol (inclusive), while the rest of the string is not copied. For example, when the string "This is nice" and a character 's'are put into the function, the function returns the following string "This". Do not modify the original string. Handle memory allocation properly, it means allocate only as much as is needed. char* str_copy_until(char* str, char symbol) { char *a = NULL; //pointer (use it in memory allocation) // your code here int i = 0; int size = 0; while(str[i]!= xyz) i++; size = i+1; a = (char *)malloc(size); if (a == NULL){ printf("no memoryn"); exit (1); } for (i=0; isize; i++) a[i] = str[i]; a[i] = 0; return a; } END OF EXAM

Content preview

ECE220 – Programming for Electrical Engineering


ECE 220
Midterm (A)
March 11th, 2026, 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 11

, 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 = 4, y = 7;
int *p;
p = &x;
*p = y + 1;
printf("%d %d\n", x, y);

a) 4 7
b) 5 7
c) 7 8
d) 8 8
e) 8 7




2. What is the value of *(arr+2) of the following array?
int arr[4] = {5, 10, 15, 20};

a) 5
b) 10
c) 15
d) 20
e) address of arr[2]




3. Which of the following correctly declares (is a prototype) a function that takes a pointer to
integer and returns an integer?

a) int fun(int);
b) int * fun(int *);
c) void fun(int *);
d) int fun(int *);
e) char = fun(int);



page 2 of 11

Document information

Study
Uploaded on
April 13, 2026
Number of pages
11
Written in
2025/2026
Type
Exam (elaborations)
Contains
Questions & answers
$13.91

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
elam17799
3.0
(4)
Sold
25
Followers
1
Items
222
Last sold
3 weeks ago



Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions