0% found this document useful (0 votes)
60 views78 pages

231 CO1003 Final

This document contains a final exam for an Introduction to Programming course. The exam consists of 60 multiple choice questions across various learning outcomes related to programming concepts in C. It provides instructions for students to fill out their student ID and name on the question sheet and answer sheet. The exam code and date are also provided.

Uploaded by

Dinh Lâm
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
0% found this document useful (0 votes)
60 views78 pages

231 CO1003 Final

This document contains a final exam for an Introduction to Programming course. The exam consists of 60 multiple choice questions across various learning outcomes related to programming concepts in C. It provides instructions for students to fill out their student ID and name on the question sheet and answer sheet. The exam code and date are also provided.

Uploaded by

Dinh Lâm
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/ 78

Lecturer(s): Approved by:

Head of Department of CS

Semester/Academic year 1 2023-2024


FINAL EXAM
Date 26/12/2023
Course title Introduction to Programming
UNIVERSITY OF TECHNOLOGY Course ID CO1003
FACULTY OF CSE Duration 90 minutes Question sheet code 2311
Notes: - Stu. ID and Stu. Fullname fields at the bottom of the question sheets and on the top of
the answer sheet must be filled in. Students answer on the answer sheets.
- The exam consists of 60 multiple-choice questions, 08 of which are harmonized
with the assignment.

Q 1. (L.O.3.2) What is the purpose of the following function?


void foo(char * s, int m, int n){
while(m < n){
char c = *(s + m);
*(s + m) = *(s + n);
*(s + n) = c;
m++; n--;
}
}
 
A To reverse any part of a string. B Nothing because the string will not be
 changed.
C To reverse a string and then reverse it back. D There will be an error if the function is
called.
Q 2. (L.O.1.1) What will be displayed if printf("C:\blue\red") is run?
   
A bluered B edue C C:bluered D C:\blue\red

Q 3. (L.O.3.1) What will be displayed if the following code snippet is run?


int i = -1; do{printf("Hello!");}while(i++);
 
A None of the other choices is correct. B Hello!
 
C Hello!Hello! D Hello!Hello!Hello!

Q 4. (L.O.1.1) The function main contains the following codes:


int a; char c;
scanf("%d%c", &a, &c);
printf("%d %c", a, c);

If “32␣a” is entered, what will be displayed?


   
A 32a B 32␣a C 32␣␣ D An error

Q 5. (L.O.2.2) In the C programming language, which keyword is used to define a structured data type?
   
A struct B class C type D record

Q 6. (L.O.1.1) Which will be displayed if printf("%5.2d", 10) is run?


   
A 10.00 B ␣␣␣10 C 00010 D 10

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2311 Page 1/12


Q 7. (L.O.3.2) Consider the following function
int fn(int x, int y){
if(x != y) return (x + y);
else return (x - y);
}

If printf("%d", fn(3, fn(4, 5))) is run, the output will be


   
A 21 B 15 C 12 D 27

Q 8. (L.O.1.2) The function main consists of the following codes:


int a = 6; if(a == 5) printf("a = 5");

What will be displayed if the program is run?


   
A a = 6 B a = 7 C a = 8 D a = 5

Q 9. (L.O.3.1) Which will be displayed if the following code snippet is run and the value of m (of type int) is 1?
switch(m){
case 1: printf("%d", m++);
case 2: printf("%d", ++m);
default: printf("%d", m);
}
   
A 2 B 133 C 233 D 1

Q 10. (L.O.1.2) What will be the output if the following code snippet is run?
#include <stdio.h>
int main(){
int x = 5;
if(x = 6) printf("Yes");
else printf("No");
return 0;
}
   
A No B An error C Nothing D Yes

Q 11. (L.O.1.1) Which will be displayed if the following code snippet is run with input “Hello␣world!”?
char c = ’A’;
char s[5];
scanf("%s", s);
printf("%s%c", s, c);
   
A Hello B HelloA C HellA D Hello␣

Q 12. (L.O.1.2) Find the most appropriate condition that can be used to check if the character kept by a variable
c is an english letter?

A (((’a’ <= c) || (c <= ’z’)) || ((’A’ <= c) && (c <= ’Z’)))

B (((’a’ <= c) || (c <= ’z’)) && ((’A’ <= c) || (c <= ’Z’)))

C (((’a’ <= c) && (c <= ’z’)) || ((’A’ <= c) && (c <= ’Z’)))

D (((’a’ <= c) && (c <= ’z’)) && ((’A’ <= c) && (c <= ’Z’)))

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2311 Page 2/12


Q 13. (L.O.2.1) Which will be the output if the following code snippet is run?
#include <stdio.h>
int main(){
int a = 5;
++a;
a += a++;
printf("%d", a);
return 0;
}
   
A 12 B 11 C 13 D 10

Q 14. (L.O.3.2) What will be the output if the following code snippet is run?
#include <stdio.h>
void fun(int * ptr){
*ptr = 30;
}
int main(){
int y = 20;
fun(&y);
printf("%d", y);
return 0;
}
   
A 30 B 20 C Runtime error D Compile-time error

Q 15. (L.O.2.1) In programming language C, which conditon can be used to determine if an integer is a multiple
of both 3 and 5?
 
A (num % 3 == 0) || (num % 5 == 0) B (num % 3 == 0) && (num % 5 != 0)
 
C (num % 3 != 0) && (num % 5 == 0) D (num % 3 == 0) && (num % 5 == 0)

The information below applies to questions 16–17. It is known that the title of the Add command is defined as
follows:

• The title can only contain characters that are one of the following types: lowercase letters, uppercase letters,
digits, or special characters (including spaces, commas, periods, hyphens, colons, | and /).

• The title must not start or end with a space.

The function checkTitle(char * raw_title) has the following output:

• If the title is valid according to the above conditions, return -1;

• If the title is invalid due to violating the maximum length condition, return the current length of the title.

• If the title is invalid due to violating other conditions (besides the maximum length condition), return the
position of the first character that violates the condition.

Q 16. (L.O.1.2) With the input raw_title = "Do homework", what is the output of the checkTitle func-
tion?
   
A 12 B -1 C 11 D 2

Q 17. (L.O.1.2) With the input raw_title = "Buy(Pencil, Eraser) ", what is the output of the
checkTitle function?   
A -1 B 19 C 3 D 20

Q 18. (L.O.1.1) If printf("%d", 0246) is called, it will display what?


   
A 0246 B Other choice C 246 D 6420

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2311 Page 3/12


Q 19. (L.O.2.2) Which declaration is the declaration of a double pointer in C programming language?
   
A int *val; B int &val; C int *&val; D int **val;

The information below applies to questions 20–21. Knowing that the boolean function
inList(char c, char * s) returns True if the character c is in the string s, otherwise it returns
False. Implement the function checkTitle(char * raw_title) as follows:
int checkTitle(char * raw_title){
int len_raw_title = strlen(raw_title);
// violate the string length
if(len_raw_title > 100){
return len_raw_title;
}
// first character is not space
/* (a) */{
return 0;
}
// check validity for middle characters
for(int i = 0; i < len_raw_title; ++i){
if(isLowerLetter(raw_title[i])
|| isUpperLetter(raw_title[i])
|| isDigit(raw_title[i])
|| inList(raw_title[i], " ,.-:|/")
){
/* (c) */
}
else {
return i;
}
}
// last character is not space
if(raw_title[len_raw_title-1] == ’ ’){
return len_raw_title-1;
}
return -1;
}

(L.O.1.2) Which can be /* (a) */?


Q 20. 
A ifraw_title[0] = ’ ’ B IF(raw_title[0] == ’ ’)
 
C if(raw_title[0] = ’ ’) D if(raw_title[0] == ’ ’)

Q 21. (L.O.2.1) The code for /* (c) */ can be


  
A continue; B break; C goto;

D None of the other choices is correct.

The information below applies to questions 22–23. Given the Task structure defined as follows:
struct Task {
int num;
char title[MAX_LENGTH_TITLE + 1];
char description[MAX_LENGTH_DESCRIPTION + 1];
char time[MAX_LENGTH_TIME + 1];
enum Status status;
};

And the addTask function declaration as follows:

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2311 Page 4/12


bool addTask(struct Task * array_tasks, int no_tasks, char * new_title, char *
new_description, char * new_time);

The function adds a new task to the end of the array with member variables corresponding to new_title,
new_description, new_time. The first task added to the application has num equal to 1. Tasks added after
the first task will have num equal to the num of the previously added task plus 1.
For an implementation in C of the function:
bool addTask(struct Task * array_tasks, int no_tasks, char * new_title, char *
new_description, char * new_time) {
if(no_tasks >= MAX_NO_TASKS){
return false;
}
/*(a)*/(array_tasks[/*(b)*/].title, new_title);
/*(a)*/(array_tasks[/*(b)*/].description, new_description);
/*(a)*/(array_tasks[/*(b)*/].time, new_time);
array_tasks[/*(b)*/].status = IN_PROGRESS;
array_tasks[/*(b)*/].num = /*(c)*/;
return true;
}

(L.O.2.1) The code for /*


Q 22.  (a) */ can be  
A strlen B strcpy C strtok D strcat

Q 23. (L.O.2.1) The code for /* (b) */ and /* (c) */ can respectively be
 
A (b) no_tasks + 1, (c) no_tasks + 1 B (b) no_tasks + 1, (c) no_tasks + 2
 
C (b) no_tasks, (c) no_tasks + 1 D (b) no_tasks, (c) no_tasks

Q 24. (L.O.2.2) Given pointer pA2 keeping the address of a pointer pA1 and pA1 keeping the address of variable
A of type int, which expression can be used to get the value of A and to assign that value to a variable x
also of type int?
   
A int x = **pA2; B int x = *pA2; C int x = *(pA2); D All choices are correct.

Q 25. (L.O.2.1) What will be the output if the following code snippet is run?
int a[2][3] = {{1, 2, 3}, {4, 5, 6}};
int *p = a;
printf("%d", *(p+4));
   
A 3 B 5 C 4 D An error

Q 26. (L.O.3.2) To request the operating system for a memory space exactly 64 bits in the heap region which
call can be used?
   
A malloc(8) B calloc(1, 64) C malloc(64) D calloc(8, 8)

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2311 Page 5/12


Q 27. (L.O.2.1) Given that the variable display is a one-dimensional array of characters. display is used to store
strings representing the days of the week. The array display is allocated with a sufficient number of elements
to perform the operations below.
For the following code snippet 8.1:
// code 8.1
printf("%s", display);

After executing the above code on the display array, the desired output (output 1) is:
|MON||TUE||WED||THU||FRI||SAT||SUN|
Assuming that currently, all characters in display are spaces, except for the last character being the null
terminator.
Given the following code snippet to modify the display variable so that when the code snippet 8.1 is
executed, the desired output (output 1) is achieved.
char day_of_weeks[][/*(a)*/] = {"MON", "TUE", "WED", "THU", "FRI", "SAT",
"SUN"};
for(int i = 0; i < 7; ++i){
display[/*(b)*/] = display[/*(c)*/] = ’|’;
/*(d)*/;
}

Choose the valid codes to fill in the comments /* (a) */, /* (b) */, /* (c) */, and /* (d) */ respectively:

A (a) 4, (b) 5 * i, (c) 5 * i + 4, (d) strncpy(display + 5 * i + 1, day_of_weeks[i], 3)

B (a) 4, (b) 5 * i, (c) 5 * i + 4, (d) strcpy(display + 5 * i + 1, day_of_weeks[i])

C (a) 3, (b) i, (c) i + 4, (d) strcpy(display + 5 * i + 1, day_of_weeks[i])

D (a) 4, (b) 5 * i, (c) 5 * i + 4, (d) strncpy(display + 5 * i + 1, day_of_weeks[i], 3)
or strcpy(display + 5 * i + 1, day_of_weeks[i])
Q 28. (L.O.2.2) What will be the output if the following code snippet is run?
struct stu{int x, y;};
struct stu s1 = {1,4};
struct stu s2 = {2,3};
if(s1.y > s2.y)
printf("True");
else
printf("False");
   
A False B True C TrueFalse D FalseTrue

Q 29. (L.O.1.1) What will be the value of x if the following code snippet is run?
#include <stdio.h>
void solve(){
int x = printf("Hello");
printf("%d", x);
}
int main(){
solve();
return 0;
}
   
A 10 B 1 C 5 D 0

Q 30. (L.O.1.1) Which declaration is correct in C programming language?


   
A int my_n = 50; B int my_n = 5,0; C int $my_n = 50; D int my n = 50;

Q 31. (L.O.2.2) Which expression can be used to get the value of the third element of an array a of six elements?
   
A *a + 3 B *(a + 2) C *(*a + 3) D &(a + 2)

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2311 Page 6/12


Q 32. (L.O.1.2) What will be displayed if the following code snippet is run?
int n = 20, i = 1;
while (i < n) {
i *= 2;
n /= 2;
printf("%d ", n);
}
   
A 10 5 2 B 20 10 5 2 C 10 5 2 1 D 10 10 5 2

Q 33. (L.O.1.1) The call printf("%d", 0xB7) will display what if it is run?
   
A 117 B B7 C 0 D 183

Q 34. (L.O.3.1) Which statement is the most correct?


 
A All code statements in the while loop are B All code statements in the for loop are
executed at least once. executed at least once.
C All choices are correct. D All code statements in the do-while loop
are executed at least once.
Q 35. (L.O.3.2) What will be the output if the following program is run?
#include <stdio.h>
int compare(char s[], char r[]){
if (s < r) return 1; else return 0;
}
int main(){
char s[4] = "cat";
char r[4] = "cat";
printf("%d", compare(r, s));
return 0;
}
   
A 0 B An error C 1 D A garbage value

Q 36. (L.O.3.2) What will be the output if the following program is run?
#include <stdio.h>
void TT(int x, int *y){
x = x + *y;
*y = *y + x;
}
int main(){
int a = 6, b = 8;
TT(a, &b);
printf("a = %d, b = %d", a, b);
return 0;
}
   
A a = 6, b = 14 B a = 14, b = 8 C a = 6, b = 8 D a = 6, b = 22

Q 37. (L.O.2.1) Which is the hexadecimal representation of the initial value of variable m in the declaration
int m = 48 | 12;?
   
A 0x4 B 0x3c C 0xc D 0x30

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2311 Page 7/12


Q 38. (L.O.2.2) Consider structure (struct) SV as below, the call sizeof(SV) will return which value? Assume
that size of type int is 4 bytes and size of type char is 1 byte.
typedef struct SV{
char mssv[8];
char name[20];
int dtb;
} SV;
   
A 34 B 40 C 32 D 28

Q 39. (L.O.2.1) Assume p is void pointer whose value is the address of a variable x of type int*. Which way is
correct to get the value 
whose address is kept by x? Choose the most correctanswer.
A (int)(**p) B All choices are correct. C *(*((int **)p))

D **((int **)p)

Q 40. (L.O.3.1) What will be displayed if the following code snippet is run?
#include <stdio.h>
#include <string.h>
int main(){
if (strcmp("HCMUT", "HCM") > 0){
printf("Inside if");
} else if (strcmp("HCMUT", "HCM") == 0){
printf("Inside else if");
} else {
printf("Inside else");
}
return 0;
}
   
A Inside else if B Inside else C An error D Inside if

Q 41. (L.O.3.2) What will be the output if the following program is run?
#include <stdio.h>
int func(int a, int b){
a = a - 1;
return a * b;
}
int main(){
int a = 3, b = 4;
int c = func(a, b);
printf("%d %d %d", a, b, c);
return 0;
}
   
A 3 4 8 B 3 4 12 C 2 4 8 D 2 4 12

Q 42. (L.O.2.2) Which operations on struct are illegal?


 
A Define a struct containing a pointer to a B Dynamic allocation
variable whose type is struct itself 
C Type-casting D All choices are correct.

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2311 Page 8/12


Q 43. (L.O.3.2) Find the recursion depth of the call foo(5) (the number of function calls until the base case is
reached including the first call).
int foo(int n){
if (n == 0 || n == 1) return 1;
else return foo(n - 1) * foo(n - 2);
}
   
A 5 B 11 C 3 D 8

Q 44. (L.O.1.2) What will be the value of variable S if the following code snippet is run?
int S = 0;
for(int i = 1; i < 10; i+=2) S += i;
printf("%d", S);
   
A 55 B 25 C 45 D 10

Q 45. (L.O.1.1) Given the C-string char s[6] = {’h’, ’e’, ’\0’, ’l’, ’l’, ’o’}, what will happen if
printf("%6c", s) is run? 
A String “he” will be displayed. B String “he␣llo” will be displayed.
 
C String “hello” will be displayed. D A warning about incorrect conversion
specification will be displayed.
Q 46. (L.O.1.2) In the C programming language, the else statement is used together with
   
A while B for C if D switch

Q 47. (L.O.2.1) Which way is correct to define a parameter of a function as a 2-dimensional C-array with elements
of type of a 1-dimensional C-array consisting of 10 integers as elements?
   
A int s[10][] B int s[10][10] C int s[][] D int s[][10]

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2311 Page 9/12


Q 48. (L.O.3.1) Given the definition of a struct Date representing a day below. The struct Date consists of
three components: the variable day represents the day, the variable month represents the month, and the
variable year represents the year. It is assumed that even months have 30 days and odd months have 31
days. For example, February and April have 30 days, and September and November have 31 days.
struct Date{
int day;
int month;
int year;
};

Given the following function implemented in C programming language, the function takes the parameter
date of struct Date as input and returns a struct Date representing the previous date of the input
date.
struct Date getPreviousDate(struct Date date){
struct Date pre_date;
pre_date.day = date.day - 1;
if (pre_date.day == 0) {
pre_date.month = /* (a) */;
if (/* (b) */) {
pre_date.year = date.year - 1;
/* (c) */
}
else {
pre_date.year = date.year;
}
pre_date.day = /* (d) */;
} else {
pre_date.month = date.month;
pre_date.year = date.year;
}
return pre_date;
}

For the given example code:


struct Date date = {20, 11, 2023};
struct Date pre_date = getPreviousDate(date);
After execution, pre_date’s components day, month, and year will be 19, 11, and 2023 respectively.
Choose the valid codes to fill in the comments /* (a) */, /* (b) */, /* (c) */, and /* (d) */ respectively.

A (a) date.day - 1, (b) pre_date.month == 1, (c) No code here, (d) 30

B (a) date.day, (b) pre_date.month == 0, (c)
pre_date.month = date.month, (d) 30 + pre_date.month % 2
C (a) date.day, (b) pre_date.month == 1, (c) pre_date.month = 12, (d)
31 - pre_date.month % 2
D (a) date.day - 1, (b) pre_date.month == 0, (c) pre_date.month = 12,
(d) 30 + pre_date.month % 2

Stu. ID: . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . Code: 2311 Page 10/12


Q 49. (L.O.1.2) What will be the output if the following code snippet is run?
#include <stdio.h>
void solve(){
int ch = 2;
switch(ch){
case 1: printf("1 ");
case 2: printf("2 ");
case 3: printf("3 ");
default: printf("None");
}
}
int main(){
solve();
return 0;
}
   
A 1 2 3 None B 2 3 None C 2 D None

Q 50. (L.O.3.1) What does the following function do?


void foo(int* a, int b){
int c = *a;
*a = b;
b = c;
}
 
A To assign value of a variable whose address B To assign value of parameter b to a vari-
is kept by a to parameter b able whose address is kept by a
C To swap the values of parameter a and D To assign value of parameter b to param-
parameter b eter &a
Q 51. (L.O.3.2) What is the purpose of the following function?
int foo(int arr[], int n){
int a = -1;
int b = 0;
for(int i = 0; i < n; i++){
int count = 0;
for(int j = 0; j < n; j++){
if (arr[j] == arr[i]) count++;
}
if(count > b){
b = count;
a = arr[i];
}
}
return a;
}
 
A To count the number of element n in the B To find the sum of the elements of the array.
array. 
C To find the element that appears most fre- D To count the number of distinct elements
quently in the array. of the array.
Q 52. (L.O.2.1) In the C programming language, which operator is used to perform the modulus operation?
   
A - B * C / D %

Stu. ID: . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . Code: 2311 Page 11/12


Q 53. (L.O.2.2) What will be the output if the following code snippet is run?
#include <stdio.h>
int main(){
int x = 10, *ptr;
ptr = &x;
*ptr = 20;
printf("%d", x);
}
   
A 20 B 10 C Error D A garbage value

Q 54. (L.O.2.2) Given a struct with two member row and col and a variable x of that struct data type. Which
of the following calls can be used to display the values of the data members row and col of variable x?
 
A None of the other choices is correct. B printf("x.row, x.col");
 
C printf("%d, %d", x.row, x.col); D printf(x);

Q 55. (L.O.2.2) Which is the size of a struct consisting of one 1-dimensional array with 10 elements of type int
and one variable of type char? The size of int is 4 bytes and the size of char is 1 byte.
   
A 11 bytes B 41 bytes C 5 bytes D 44 bytes

Q 56. (L.O.1.2) Which will be the output if the following code snippet is run?
int a = 6, b = 8;
if(a > 5){
a -= 1; b += 1;
}else{
a += 1; b -= 1;
}
printf("a = %d, b = %d", a, b);
   
A a = 5, b = 9 B a = 6, b = 9 C a = 5, b = 8 D a = 6, b = 8

(L.O.2.1) How many memory


Q 57.  cells that are neededto store string “HELLO␣WORLD!”?

A 12 B 11 C 13 D 15

Q 58. (L.O.2.1) Given an array a of six elements, which expression can be used to get the value of the fourth
element of the array?
   
A a[3] B a[4] C a[1] D a[2]

Q 59. (L.O.3.2) Which statement about what a function results in is true ?


 
A A function must always return a specific B A function can be set to not return any
value (number, string, character,. . . ) value or to return a specific value.
C A function cannot return any value (num- D All choices are correct.
ber, string, character, . . . )
Q 60. (L.O.1.2) In a nested if-else statement, the conditions are checked in which order?
 
A From the condition of the innermost if to B The conditions are checked at the same time.
the condition of the outermost if. 
C From the condition of the outermost if to D In the order that the programmer specifies
the condition of the innermost if. at the running time.

Stu. ID: . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . Code: 2311 Page 12/12


Semester/Academic year 1 2023-2024
FINAL EXAM (Answer Key) Date 26/12/2023
Course title Introduction to Programming
UNIVERSITY OF TECHNOLOGY Course ID CO1003
FACULTY OF CSE Duration 90 minutes Question sheet code 2311

   
Q 1. A Q 16. B Q 30. A Q 46. C
   
Q 2. B Q 17. C Q 31. B Q 47. D
  
Q 3. C  Q 32. A Q 48. D
 Q 18. B 

Q 4. C  Q 33. D Q 49. B
 Q 19. D 
Q 34. D 
Q 5. A Q 50. B
  
Q 35. C 
Q 6. B Q 20. D Q 51. C
  
Q 7. C Q 21. A Q 36. D 
 Q 52. D

Q 8. D
Q 37. B 

 Q 53. A
 Q 22. B Q 38. C
Q 9. B 
  Q 54. A
 Q 23. C Q 39. B
Q 10. D  
 Q 55. D
 Q 24. A Q 40. D
Q 11. A  
 Q 56. A
 Q 25. B Q 41. A
Q 12. C  

 Q 26. A Q 42. C Q 57. C
Q 13. C   
 Q 27. A Q 43. A Q 58. A
Q 14. A   
 Q 28. B Q 44. B Q 59. B
Q 15. D   
Q 29. C Q 45. D Q 60. C
Lecturer(s): Approved by:
Head of Department of CS

Semester/Academic year 1 2023-2024


FINAL EXAM
Date 26/12/2023
Course title Introduction to Programming
UNIVERSITY OF TECHNOLOGY Course ID CO1003
FACULTY OF CSE Duration 90 minutes Question sheet code 2312
Notes: - Stu. ID and Stu. Fullname fields at the bottom of the question sheets and on the top of
the answer sheet must be filled in. Students answer on the answer sheets.
- The exam consists of 60 multiple-choice questions, 08 of which are harmonized
with the assignment.

Q 1. (L.O.3.1) What will be displayed if the following code snippet is run?


int i = -1; do{printf("Hello!");}while(i++);
 
A Hello!Hello!Hello! B None of the other choices is correct.
 
C Hello! D Hello!Hello!

Q 2. (L.O.1.1) The function main contains the following codes:


int a; char c;
scanf("%d%c", &a, &c);
printf("%d %c", a, c);

If “32␣a” is entered, what will be displayed?


   
A An error B 32a C 32␣a D 32␣␣

Q 3. (L.O.1.1) Which will be displayed if the following code snippet is run with input “Hello␣world!”?
char c = ’A’;
char s[5];
scanf("%s", s);
printf("%s%c", s, c);
   
A Hello␣ B Hello C HelloA D HellA

Q 4. (L.O.3.2) What will be the output if the following code snippet is run?
#include <stdio.h>
void fun(int * ptr){
*ptr = 30;
}
int main(){
int y = 20;
fun(&y);
printf("%d", y);
return 0;
}
   
A Compile-time error B 30 C 20 D Runtime error

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2312 Page 1/12


Q 5. (L.O.2.2) What will be the output if the following code snippet is run?
#include <stdio.h>
int main(){
int x = 10, *ptr;
ptr = &x;
*ptr = 20;
printf("%d", x);
}
   
A A garbage value B 20 C 10 D Error

(L.O.2.1) How many memory


Q 6.  cells that are neededto store string “HELLO␣WORLD!”?

A 15 B 12 C 11 D 13

Q 7. (L.O.3.2) What will be the output if the following program is run?


#include <stdio.h>
int func(int a, int b){
a = a - 1;
return a * b;
}
int main(){
int a = 3, b = 4;
int c = func(a, b);
printf("%d %d %d", a, b, c);
return 0;
}
   
A 2 4 12 B 3 4 8 C 3 4 12 D 2 4 8

Q 8. (L.O.2.2) Which is the size of a struct consisting of one 1-dimensional array with 10 elements of type int
and one variable of type char? The size of int is 4 bytes and the size of char is 1 byte.
   
A 44 bytes B 11 bytes C 41 bytes D 5 bytes

Q 9. (L.O.2.2) Which expression can be used to get the value of the third element of an array a of six elements?
   
A &(a + 2) B *a + 3 C *(a + 2) D *(*a + 3)

Q 10. (L.O.1.1) Given the C-string char s[6] = {’h’, ’e’, ’\0’, ’l’, ’l’, ’o’}, what will happen if
printf("%6c", s) is run? 
A A warning about incorrect conversion B String “he” will be displayed.
specification will be displayed. 
C String “he␣llo” will be displayed. D String “hello” will be displayed.

Q 11. (L.O.3.2) What is the purpose of the following function?


void foo(char * s, int m, int n){
while(m < n){
char c = *(s + m);
*(s + m) = *(s + n);
*(s + n) = c;
m++; n--;
}
}
 
A There will be an error if the function is B To reverse any part of a string.
called. 
C Nothing because the string will not be D To reverse a string and then reverse it back.
changed.

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2312 Page 2/12


Q 12. (L.O.3.1) What does the following function do?
void foo(int* a, int b){
int c = *a;
*a = b;
b = c;
}
 
A To assign value of parameter b to param- B To assign value of a variable whose address
eter &a is kept by a to parameter b
C To assign value of parameter b to a vari- D To swap the values of parameter a and
able whose address is kept by a parameter b
Q 13. (L.O.1.2) In the C programming language, the else statement is used together with
   
A switch B while C for D if

Q 14. (L.O.1.2) What will be the output if the following code snippet is run?
#include <stdio.h>
int main(){
int x = 5;
if(x = 6) printf("Yes");
else printf("No");
return 0;
}
   
A Yes B No C An error D Nothing

The information below applies to questions 15–16. Knowing that the boolean function
inList(char c, char * s) returns True if the character c is in the string s, otherwise it returns
False. Implement the function checkTitle(char * raw_title) as follows:
int checkTitle(char * raw_title){
int len_raw_title = strlen(raw_title);
// violate the string length
if(len_raw_title > 100){
return len_raw_title;
}
// first character is not space
/* (a) */{
return 0;
}
// check validity for middle characters
for(int i = 0; i < len_raw_title; ++i){
if(isLowerLetter(raw_title[i])
|| isUpperLetter(raw_title[i])
|| isDigit(raw_title[i])
|| inList(raw_title[i], " ,.-:|/")
){
/* (c) */
}
else {
return i;
}
}
// last character is not space
if(raw_title[len_raw_title-1] == ’ ’){
return len_raw_title-1;
}

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2312 Page 3/12


return -1;
}

(L.O.1.2) Which can be /* (a) */?


Q 15. 
A if(raw_title[0] == ’ ’) B ifraw_title[0] = ’ ’
 
C IF(raw_title[0] == ’ ’) D if(raw_title[0] = ’ ’)

Q 16. (L.O.2.1) The code for /* (c) */ can be


  
A None of the other choices is correct. B continue; C break;

D goto;

Q 17. (L.O.2.1) Assume p is void pointer whose value is the address of a variable x of type int*. Which way is
correct to get the value 
whose address is kept by x?
 Choose the most correct answer.
A **((int **)p) B (int)(**p) C All choices are correct.

D *(*((int **)p))

Q 18. (L.O.2.1) Given that the variable display is a one-dimensional array of characters. display is used to store
strings representing the days of the week. The array display is allocated with a sufficient number of elements
to perform the operations below.
For the following code snippet 8.1:
// code 8.1
printf("%s", display);

After executing the above code on the display array, the desired output (output 1) is:
|MON||TUE||WED||THU||FRI||SAT||SUN|
Assuming that currently, all characters in display are spaces, except for the last character being the null
terminator.
Given the following code snippet to modify the display variable so that when the code snippet 8.1 is
executed, the desired output (output 1) is achieved.
char day_of_weeks[][/*(a)*/] = {"MON", "TUE", "WED", "THU", "FRI", "SAT",
"SUN"};
for(int i = 0; i < 7; ++i){
display[/*(b)*/] = display[/*(c)*/] = ’|’;
/*(d)*/;
}

Choose the valid codes to fill in the comments /* (a) */, /* (b) */, /* (c) */, and /* (d) */ respectively:

A (a) 4, (b) 5 * i, (c) 5 * i + 4, (d) strncpy(display + 5 * i + 1, day_of_weeks[i], 3)
or strcpy(display + 5 * i + 1, day_of_weeks[i])
B (a) 4, (b) 5 * i, (c) 5 * i + 4, (d) strncpy(display + 5 * i + 1, day_of_weeks[i], 3)

C (a) 4, (b) 5 * i, (c) 5 * i + 4, (d) strcpy(display + 5 * i + 1, day_of_weeks[i])

D (a) 3, (b) i, (c) i + 4, (d) strcpy(display + 5 * i + 1, day_of_weeks[i])

Q 19. (L.O.1.1) The call printf("%d", 0xB7) will display what if it is run?
   
A 183 B 117 C B7 D 0

Q 20. (L.O.2.1) Which is the hexadecimal representation of the initial value of variable m in the declaration
int m = 48 | 12;?
   
A 0x30 B 0x4 C 0x3c D 0xc

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2312 Page 4/12


Q 21. (L.O.3.2) Find the recursion depth of the call foo(5) (the number of function calls until the base case is
reached including the first call).
int foo(int n){
if (n == 0 || n == 1) return 1;
else return foo(n - 1) * foo(n - 2);
}
   
A 8 B 5 C 11 D 3

Q 22. (L.O.1.1) If printf("%d", 0246) is called, it will display what?


   
A 6420 B 0246 C Other choice D 246

The information below applies to questions 23–24. It is known that the title of the Add command is defined as
follows:

• The title can only contain characters that are one of the following types: lowercase letters, uppercase letters,
digits, or special characters (including spaces, commas, periods, hyphens, colons, | and /).

• The title must not start or end with a space.

The function checkTitle(char * raw_title) has the following output:

• If the title is valid according to the above conditions, return -1;

• If the title is invalid due to violating the maximum length condition, return the current length of the title.

• If the title is invalid due to violating other conditions (besides the maximum length condition), return the
position of the first character that violates the condition.

Q 23. (L.O.1.2) With the input raw_title = "Do homework", what is the output of the checkTitle func-
tion?
   
A 2 B 12 C -1 D 11

Q 24. (L.O.1.2) With the input raw_title = "Buy(Pencil, Eraser) ", what is the output of the
checkTitle function?   
A 20 B -1 C 19 D 3

Q 25. (L.O.3.1) Which will be displayed if the following code snippet is run and the value of m (of type int) is 1?
switch(m){
case 1: printf("%d", m++);
case 2: printf("%d", ++m);
default: printf("%d", m);
}
   
A 1 B 2 C 133 D 233

Q 26. (L.O.1.2) Which will be the output if the following code snippet is run?
int a = 6, b = 8;
if(a > 5){
a -= 1; b += 1;
}else{
a += 1; b -= 1;
}
printf("a = %d, b = %d", a, b);
   
A a = 6, b = 8 B a = 5, b = 9 C a = 6, b = 9 D a = 5, b = 8

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2312 Page 5/12


Q 27. (L.O.2.2) Which operations on struct are illegal?
 
A All choices are correct. B Define a struct containing a pointer to a
 variable whose type is struct itself
C Dynamic allocation D Type-casting

The information below applies to questions 28–29. Given the Task structure defined as follows:
struct Task {
int num;
char title[MAX_LENGTH_TITLE + 1];
char description[MAX_LENGTH_DESCRIPTION + 1];
char time[MAX_LENGTH_TIME + 1];
enum Status status;
};

And the addTask function declaration as follows:


bool addTask(struct Task * array_tasks, int no_tasks, char * new_title, char *
new_description, char * new_time);

The function adds a new task to the end of the array with member variables corresponding to new_title,
new_description, new_time. The first task added to the application has num equal to 1. Tasks added after
the first task will have num equal to the num of the previously added task plus 1.
For an implementation in C of the function:
bool addTask(struct Task * array_tasks, int no_tasks, char * new_title, char *
new_description, char * new_time) {
if(no_tasks >= MAX_NO_TASKS){
return false;
}
/*(a)*/(array_tasks[/*(b)*/].title, new_title);
/*(a)*/(array_tasks[/*(b)*/].description, new_description);
/*(a)*/(array_tasks[/*(b)*/].time, new_time);
array_tasks[/*(b)*/].status = IN_PROGRESS;
array_tasks[/*(b)*/].num = /*(c)*/;
return true;
}

(L.O.2.1) The code for /*


Q 28.  (a) */ can be  
A strcat B strlen C strcpy D strtok

Q 29. (L.O.2.1) The code for /* (b) */ and /* (c) */ can respectively be
 
A (b) no_tasks, (c) no_tasks B (b) no_tasks + 1, (c) no_tasks + 1
 
C (b) no_tasks + 1, (c) no_tasks + 2 D (b) no_tasks, (c) no_tasks + 1

Q 30. (L.O.3.2) To request the operating system for a memory space exactly 64 bits in the heap region which
call can be used?
   
A calloc(8, 8) B malloc(8) C calloc(1, 64) D malloc(64)

Q 31. (L.O.2.1) In programming language C, which conditon can be used to determine if an integer is a multiple
of both 3 and 5?
 
A (num % 3 == 0) && (num % 5 == 0) B (num % 3 == 0) || (num % 5 == 0)
 
C (num % 3 == 0) && (num % 5 != 0) D (num % 3 != 0) && (num % 5 == 0)

Q 32. (L.O.1.2) In a nested if-else statement, the conditions are checked in which order?
 
A In the order that the programmer specifies B From the condition of the innermost if to
at the running time. the condition of the outermost if.
C The conditions are checked at the same time. D From the condition of the outermost if to
the condition of the innermost if.

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2312 Page 6/12


Q 33. (L.O.2.1) In the C programming language, which operator is used to perform the modulus operation?
   
A % B - C * D /

Q 34. (L.O.2.2) Given pointer pA2 keeping the address of a pointer pA1 and pA1 keeping the address of variable
A of type int, which expression can be used to get the value of A and to assign that value to a variable x
also of type int?
  
A All choices are correct. B int x = **pA2; C int x = *pA2;

D int x = *(pA2);

Q 35. (L.O.2.2) In the C programming language, which keyword is used to define a structured data type?
   
A record B struct C class D type

Q 36. (L.O.3.1) Which statement is the most correct?


 
A All code statements in the do-while loop B All code statements in the while loop are
are executed at least once. executed at least once.
C All code statements in the for loop are D All choices are correct.
executed at least once.
Q 37. (L.O.1.2) The function main consists of the following codes:
int a = 6; if(a == 5) printf("a = 5");

What will be displayed if the program is run?


   
A a = 5 B a = 6 C a = 7 D a = 8

Q 38. (L.O.2.2) Given a struct with two member row and col and a variable x of that struct data type. Which
of the following calls can be used to display the values of the data members row and col of variable x?
 
A printf(x); B None of the other choices is correct.
 
C printf("x.row, x.col"); D printf("%d, %d", x.row, x.col);

Q 39. (L.O.1.2) What will be displayed if the following code snippet is run?
int n = 20, i = 1;
while (i < n) {
i *= 2;
n /= 2;
printf("%d ", n);
}
   
A 10 10 5 2 B 10 5 2 C 20 10 5 2 D 10 5 2 1

Q 40. (L.O.3.2) Consider the following function


int fn(int x, int y){
if(x != y) return (x + y);
else return (x - y);
}

If printf("%d", fn(3, fn(4, 5))) is run, the output will be


   
A 27 B 21 C 15 D 12

Q 41. (L.O.1.1) Which declaration is correct in C programming language?


   
A int my n = 50; B int my_n = 50; C int my_n = 5,0; D int $my_n = 50;

Q 42. (L.O.2.1) Which way is correct to define a parameter of a function as a 2-dimensional C-array with elements
of type of a 1-dimensional C-array consisting of 10 integers as elements?
   
A int s[][10] B int s[10][] C int s[10][10] D int s[][]

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2312 Page 7/12


Q 43. (L.O.1.2) Find the most appropriate condition that can be used to check if the character kept by a variable
c is an english letter?

A (((’a’ <= c) && (c <= ’z’)) && ((’A’ <= c) && (c <= ’Z’)))

B (((’a’ <= c) || (c <= ’z’)) || ((’A’ <= c) && (c <= ’Z’)))

C (((’a’ <= c) || (c <= ’z’)) && ((’A’ <= c) || (c <= ’Z’)))

D (((’a’ <= c) && (c <= ’z’)) || ((’A’ <= c) && (c <= ’Z’)))

Q 44. (L.O.1.1) What will be the value of x if the following code snippet is run?
#include <stdio.h>
void solve(){
int x = printf("Hello");
printf("%d", x);
}
int main(){
solve();
return 0;
}
   
A 0 B 10 C 1 D 5

Q 45. (L.O.1.2) What will be the value of variable S if the following code snippet is run?
int S = 0;
for(int i = 1; i < 10; i+=2) S += i;
printf("%d", S);
   
A 10 B 55 C 25 D 45

Q 46. (L.O.2.1) Given an array a of six elements, which expression can be used to get the value of the fourth
element of the array?
   
A a[2] B a[3] C a[4] D a[1]

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2312 Page 8/12


Q 47. (L.O.3.1) Given the definition of a struct Date representing a day below. The struct Date consists of
three components: the variable day represents the day, the variable month represents the month, and the
variable year represents the year. It is assumed that even months have 30 days and odd months have 31
days. For example, February and April have 30 days, and September and November have 31 days.
struct Date{
int day;
int month;
int year;
};

Given the following function implemented in C programming language, the function takes the parameter
date of struct Date as input and returns a struct Date representing the previous date of the input
date.
struct Date getPreviousDate(struct Date date){
struct Date pre_date;
pre_date.day = date.day - 1;
if (pre_date.day == 0) {
pre_date.month = /* (a) */;
if (/* (b) */) {
pre_date.year = date.year - 1;
/* (c) */
}
else {
pre_date.year = date.year;
}
pre_date.day = /* (d) */;
} else {
pre_date.month = date.month;
pre_date.year = date.year;
}
return pre_date;
}

For the given example code:


struct Date date = {20, 11, 2023};
struct Date pre_date = getPreviousDate(date);
After execution, pre_date’s components day, month, and year will be 19, 11, and 2023 respectively.
Choose the valid codes to fill in the comments /* (a) */, /* (b) */, /* (c) */, and /* (d) */ respectively.

A (a) date.day - 1, (b) pre_date.month == 0, (c) pre_date.month = 12,
(d) 30 + pre_date.month % 2
B (a) date.day - 1, (b) pre_date.month == 1, (c) No code here, (d) 30

C (a) date.day, (b) pre_date.month == 0, (c)
pre_date.month = date.month, (d) 30 + pre_date.month % 2
D (a) date.day, (b) pre_date.month == 1, (c) pre_date.month = 12, (d)
31 - pre_date.month % 2

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2312 Page 9/12


Q 48. (L.O.3.2) What will be the output if the following program is run?
#include <stdio.h>
int compare(char s[], char r[]){
if (s < r) return 1; else return 0;
}
int main(){
char s[4] = "cat";
char r[4] = "cat";
printf("%d", compare(r, s));
return 0;
}
   
A A garbage value B 0 C An error D 1

Q 49. (L.O.3.1) What will be displayed if the following code snippet is run?
#include <stdio.h>
#include <string.h>
int main(){
if (strcmp("HCMUT", "HCM") > 0){
printf("Inside if");
} else if (strcmp("HCMUT", "HCM") == 0){
printf("Inside else if");
} else {
printf("Inside else");
}
return 0;
}
   
A Inside if B Inside else if C Inside else D An error

Q 50. (L.O.1.1) What will be displayed if printf("C:\blue\red") is run?


   
A C:\blue\red B bluered C edue D C:bluered

Q 51. (L.O.2.1) What will be the output if the following code snippet is run?
int a[2][3] = {{1, 2, 3}, {4, 5, 6}};
int *p = a;
printf("%d", *(p+4));
   
A An error B 3 C 5 D 4

Q 52. (L.O.2.2) What will be the output if the following code snippet is run?
struct stu{int x, y;};
struct stu s1 = {1,4};
struct stu s2 = {2,3};
if(s1.y > s2.y)
printf("True");
else
printf("False");
   
A FalseTrue B False C True D TrueFalse

Q 53. (L.O.3.2) Which statement about what a function results in is true ?


 
A All choices are correct. B A function must always return a specific
 value (number, string, character,. . . )
C A function can be set to not return any D A function cannot return any value (num-
value or to return a specific value. ber, string, character, . . . )

Stu. ID: . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . Code: 2312 Page 10/12


Q 54. (L.O.3.2) What will be the output if the following program is run?
#include <stdio.h>
void TT(int x, int *y){
x = x + *y;
*y = *y + x;
}
int main(){
int a = 6, b = 8;
TT(a, &b);
printf("a = %d, b = %d", a, b);
return 0;
}
   
A a = 6, b = 22 B a = 6, b = 14 C a = 14, b = 8 D a = 6, b = 8

Q 55. (L.O.2.2) Which declaration is the declaration of a double pointer in C programming language?
   
A int **val; B int *val; C int &val; D int *&val;

Q 56. (L.O.2.2) Consider structure (struct) SV as below, the call sizeof(SV) will return which value? Assume
that size of type int is 4 bytes and size of type char is 1 byte.
typedef struct SV{
char mssv[8];
char name[20];
int dtb;
} SV;
   
A 28 B 34 C 40 D 32

Q 57. (L.O.3.2) What is the purpose of the following function?


int foo(int arr[], int n){
int a = -1;
int b = 0;
for(int i = 0; i < n; i++){
int count = 0;
for(int j = 0; j < n; j++){
if (arr[j] == arr[i]) count++;
}
if(count > b){
b = count;
a = arr[i];
}
}
return a;
}
 
A To count the number of distinct elements B To count the number of element n in the
of the array. array.
C To find the sum of the elements of the array. D To find the element that appears most fre-
quently in the array.
Q 58. (L.O.1.1) Which will be displayed if printf("%5.2d", 10) is run?
   
A 10 B 10.00 C ␣␣␣10 D 00010

Stu. ID: . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . Code: 2312 Page 11/12


Q 59. (L.O.2.1) Which will be the output if the following code snippet is run?
#include <stdio.h>
int main(){
int a = 5;
++a;
a += a++;
printf("%d", a);
return 0;
}
   
A 10 B 12 C 11 D 13

Q 60. (L.O.1.2) What will be the output if the following code snippet is run?
#include <stdio.h>
void solve(){
int ch = 2;
switch(ch){
case 1: printf("1 ");
case 2: printf("2 ");
case 3: printf("3 ");
default: printf("None");
}
}
int main(){
solve();
return 0;
}
   
A None B 1 2 3 None C 2 3 None D 2

Stu. ID: . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . Code: 2312 Page 12/12


Semester/Academic year 1 2023-2024
FINAL EXAM (Answer Key) Date 26/12/2023
Course title Introduction to Programming
UNIVERSITY OF TECHNOLOGY Course ID CO1003
FACULTY OF CSE Duration 90 minutes Question sheet code 2312

   
Q 1. D Q 16. B Q 30. B Q 46. B
  
Q 2. D  Q 31. A Q 47. A
 Q 17. C  
Q 3. B  Q 32. D Q 48. D

Q 18. B 

Q 4. B  Q 33. A Q 49. A
Q 19. A 
 
Q 5. B  Q 34. B
Q 20. C Q 50. C
 
 Q 35. B 
Q 6. D Q 51. C
Q 21. B 

Q 7. B  Q 36. A 
Q 22. C  Q 52. C

Q 8. A Q 37. A 
  Q 53. C
 Q 23. C Q 38. B
Q 9. C 
  Q 54. A
 Q 24. D Q 39. B
Q 10. A 
 Q 55. A
  Q 40. D
Q 11. B Q 25. C 
 Q 56. D
  Q 41. B
Q 12. C Q 26. B 

 Q 57. D
 Q 42. A
Q 13. D Q 27. D  
 Q 43. D Q 58. C
Q 14. A   
Q 28. C Q 44. D Q 59. D
   
Q 15. A Q 29. D Q 45. C Q 60. C
Lecturer(s): Approved by:
Head of Department of CS

Semester/Academic year 1 2023-2024


FINAL EXAM
Date 26/12/2023
Course title Introduction to Programming
UNIVERSITY OF TECHNOLOGY Course ID CO1003
FACULTY OF CSE Duration 90 minutes Question sheet code 2313
Notes: - Stu. ID and Stu. Fullname fields at the bottom of the question sheets and on the top of
the answer sheet must be filled in. Students answer on the answer sheets.
- The exam consists of 60 multiple-choice questions, 08 of which are harmonized
with the assignment.

Q 1. (L.O.2.2) Which is the size of a struct consisting of one 1-dimensional array with 10 elements of type int
and one variable of type char? The size of int is 4 bytes and the size of char is 1 byte.
   
A 11 bytes B 44 bytes C 41 bytes D 5 bytes

Q 2. (L.O.1.1) Given the C-string char s[6] = {’h’, ’e’, ’\0’, ’l’, ’l’, ’o’}, what will happen if
printf("%6c", s) is run? 
A String “he” will be displayed. B A warning about incorrect conversion
 specification will be displayed.
C String “he␣llo” will be displayed. D String “hello” will be displayed.

Q 3. (L.O.2.1) Which is the hexadecimal representation of the initial value of variable m in the declaration
int m = 48 | 12;?
   
A 0x4 B 0x30 C 0x3c D 0xc

Q 4. (L.O.2.2) Consider structure (struct) SV as below, the call sizeof(SV) will return which value? Assume
that size of type int is 4 bytes and size of type char is 1 byte.
typedef struct SV{
char mssv[8];
char name[20];
int dtb;
} SV;
   
A 34 B 28 C 40 D 32

Q 5. (L.O.2.1) Given an array a of six elements, which expression can be used to get the value of the fourth
element of the array?
   
A a[3] B a[2] C a[4] D a[1]

Q 6. (L.O.1.1) The function main contains the following codes:


int a; char c;
scanf("%d%c", &a, &c);
printf("%d %c", a, c);

If “32␣a” is entered, what will be displayed?


   
A 32a B An error C 32␣a D 32␣␣

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2313 Page 1/12


Q 7. (L.O.3.2) What is the purpose of the following function?
void foo(char * s, int m, int n){
while(m < n){
char c = *(s + m);
*(s + m) = *(s + n);
*(s + n) = c;
m++; n--;
}
}
 
A To reverse any part of a string. B There will be an error if the function is
 called.
C Nothing because the string will not be D To reverse a string and then reverse it back.
changed.
Q 8. (L.O.1.1) The call printf("%d", 0xB7) will display what if it is run?
   
A 117 B 183 C B7 D 0

(L.O.2.1) How many memory


Q 9.  cells that are neededto store string “HELLO␣WORLD!”?

A 12 B 15 C 11 D 13

The information below applies to questions 10–11. Given the Task structure defined as follows:
struct Task {
int num;
char title[MAX_LENGTH_TITLE + 1];
char description[MAX_LENGTH_DESCRIPTION + 1];
char time[MAX_LENGTH_TIME + 1];
enum Status status;
};

And the addTask function declaration as follows:


bool addTask(struct Task * array_tasks, int no_tasks, char * new_title, char *
new_description, char * new_time);

The function adds a new task to the end of the array with member variables corresponding to new_title,
new_description, new_time. The first task added to the application has num equal to 1. Tasks added after
the first task will have num equal to the num of the previously added task plus 1.
For an implementation in C of the function:
bool addTask(struct Task * array_tasks, int no_tasks, char * new_title, char *
new_description, char * new_time) {
if(no_tasks >= MAX_NO_TASKS){
return false;
}
/*(a)*/(array_tasks[/*(b)*/].title, new_title);
/*(a)*/(array_tasks[/*(b)*/].description, new_description);
/*(a)*/(array_tasks[/*(b)*/].time, new_time);
array_tasks[/*(b)*/].status = IN_PROGRESS;
array_tasks[/*(b)*/].num = /*(c)*/;
return true;
}

(L.O.2.1) The code for /*


Q 10.  (a) */ can be  
A strlen B strcat C strcpy D strtok

Q 11. (L.O.2.1) The code for /* (b) */ and /* (c) */ can respectively be
 
A (b) no_tasks + 1, (c) no_tasks + 1 B (b) no_tasks, (c) no_tasks
 
C (b) no_tasks + 1, (c) no_tasks + 2 D (b) no_tasks, (c) no_tasks + 1

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2313 Page 2/12


Q 12. (L.O.3.2) To request the operating system for a memory space exactly 64 bits in the heap region which
call can be used?
   
A malloc(8) B calloc(8, 8) C calloc(1, 64) D malloc(64)

Q 13. (L.O.2.1) What will be the output if the following code snippet is run?
int a[2][3] = {{1, 2, 3}, {4, 5, 6}};
int *p = a;
printf("%d", *(p+4));
   
A 3 B An error C 5 D 4

Q 14. (L.O.1.2) In the C programming language, the else statement is used together with
   
A while B switch C for D if

Q 15. (L.O.3.2) What will be the output if the following program is run?
#include <stdio.h>
void TT(int x, int *y){
x = x + *y;
*y = *y + x;
}
int main(){
int a = 6, b = 8;
TT(a, &b);
printf("a = %d, b = %d", a, b);
return 0;
}
   
A a = 6, b = 14 B a = 6, b = 22 C a = 14, b = 8 D a = 6, b = 8

Q 16. (L.O.3.1) Which statement is the most correct?


 
A All code statements in the while loop are B All code statements in the do-while loop
executed at least once. are executed at least once.
C All code statements in the for loop are D All choices are correct.
executed at least once.
Q 17. (L.O.2.2) In the C programming language, which keyword is used to define a structured data type?
   
A struct B record C class D type

Q 18. (L.O.1.1) What will be displayed if printf("C:\blue\red") is run?


   
A bluered B C:\blue\red C edue D C:bluered

Q 19. (L.O.3.2) Which statement about what a function results in is true ?


 
A A function must always return a specific B All choices are correct.
value (number, string, character,. . . ) 
C A function can be set to not return any D A function cannot return any value (num-
value or to return a specific value. ber, string, character, . . . )
Q 20. (L.O.2.2) Which declaration is the declaration of a double pointer in C programming language?
   
A int *val; B int **val; C int &val; D int *&val;

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2313 Page 3/12


Q 21. (L.O.1.2) What will be the output if the following code snippet is run?
#include <stdio.h>
void solve(){
int ch = 2;
switch(ch){
case 1: printf("1 ");
case 2: printf("2 ");
case 3: printf("3 ");
default: printf("None");
}
}
int main(){
solve();
return 0;
}
   
A 1 2 3 None B None C 2 3 None D 2

Q 22. (L.O.2.1) In programming language C, which conditon can be used to determine if an integer is a multiple
of both 3 and 5?
 
A (num % 3 == 0) || (num % 5 == 0) B (num % 3 == 0) && (num % 5 == 0)
 
C (num % 3 == 0) && (num % 5 != 0) D (num % 3 != 0) && (num % 5 == 0)

Q 23. (L.O.3.1) What will be displayed if the following code snippet is run?
#include <stdio.h>
#include <string.h>
int main(){
if (strcmp("HCMUT", "HCM") > 0){
printf("Inside if");
} else if (strcmp("HCMUT", "HCM") == 0){
printf("Inside else if");
} else {
printf("Inside else");
}
return 0;
}
   
A Inside else if B Inside if C Inside else D An error

Q 24. (L.O.1.2) Which will be the output if the following code snippet is run?
int a = 6, b = 8;
if(a > 5){
a -= 1; b += 1;
}else{
a += 1; b -= 1;
}
printf("a = %d, b = %d", a, b);
   
A a = 5, b = 9 B a = 6, b = 8 C a = 6, b = 9 D a = 5, b = 8

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2313 Page 4/12


Q 25. (L.O.2.2) What will be the output if the following code snippet is run?
struct stu{int x, y;};
struct stu s1 = {1,4};
struct stu s2 = {2,3};
if(s1.y > s2.y)
printf("True");
else
printf("False");
   
A False B FalseTrue C True D TrueFalse

Q 26. (L.O.2.2) Given pointer pA2 keeping the address of a pointer pA1 and pA1 keeping the address of variable
A of type int, which expression can be used to get the value of A and to assign that value to a variable x
also of type int?
  
A int x = **pA2; B All choices are correct. C int x = *pA2;

D int x = *(pA2);

Q 27. (L.O.2.1) In the C programming language, which operator is used to perform the modulus operation?
   
A - B % C * D /

Q 28. (L.O.3.1) Which will be displayed if the following code snippet is run and the value of m (of type int) is 1?
switch(m){
case 1: printf("%d", m++);
case 2: printf("%d", ++m);
default: printf("%d", m);
}
   
A 2 B 1 C 133 D 233

Q 29. (L.O.1.2) What will be the value of variable S if the following code snippet is run?
int S = 0;
for(int i = 1; i < 10; i+=2) S += i;
printf("%d", S);
   
A 55 B 10 C 25 D 45

Q 30. (L.O.3.2) What will be the output if the following code snippet is run?
#include <stdio.h>
void fun(int * ptr){
*ptr = 30;
}
int main(){
int y = 20;
fun(&y);
printf("%d", y);
return 0;
}
   
A 30 B Compile-time error C 20 D Runtime error

Q 31. (L.O.1.1) If printf("%d", 0246) is called, it will display what?


   
A 0246 B 6420 C Other choice D 246

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2313 Page 5/12


Q 32. (L.O.3.2) Consider the following function
int fn(int x, int y){
if(x != y) return (x + y);
else return (x - y);
}

If printf("%d", fn(3, fn(4, 5))) is run, the output will be


   
A 21 B 27 C 15 D 12

Q 33. (L.O.1.2) In a nested if-else statement, the conditions are checked in which order?
 
A From the condition of the innermost if to B In the order that the programmer specifies
the condition of the outermost if. at the running time.
C The conditions are checked at the same time. D From the condition of the outermost if to
the condition of the innermost if.
Q 34. (L.O.3.2) What is the purpose of the following function?
int foo(int arr[], int n){
int a = -1;
int b = 0;
for(int i = 0; i < n; i++){
int count = 0;
for(int j = 0; j < n; j++){
if (arr[j] == arr[i]) count++;
}
if(count > b){
b = count;
a = arr[i];
}
}
return a;
}
 
A To count the number of element n in the B To count the number of distinct elements
array. of the array.
C To find the sum of the elements of the array. D To find the element that appears most fre-
quently in the array.
Q 35. (L.O.1.2) What will be displayed if the following code snippet is run?
int n = 20, i = 1;
while (i < n) {
i *= 2;
n /= 2;
printf("%d ", n);
}
   
A 10 5 2 B 10 10 5 2 C 20 10 5 2 D 10 5 2 1

Q 36. (L.O.2.2) Given a struct with two member row and col and a variable x of that struct data type. Which
of the following calls can be used to display the values of the data members row and col of variable x?
 
A None of the other choices is correct. B printf(x);
 
C printf("x.row, x.col"); D printf("%d, %d", x.row, x.col);

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2313 Page 6/12


Q 37. (L.O.3.1) What does the following function do?
void foo(int* a, int b){
int c = *a;
*a = b;
b = c;
}
 
A To assign value of a variable whose address B To assign value of parameter b to param-
is kept by a to parameter b eter &a
C To assign value of parameter b to a vari- D To swap the values of parameter a and
able whose address is kept by a parameter b
Q 38. (L.O.2.1) Which will be the output if the following code snippet is run?
#include <stdio.h>
int main(){
int a = 5;
++a;
a += a++;
printf("%d", a);
return 0;
}
   
A 12 B 10 C 11 D 13

Q 39. (L.O.3.2) What will be the output if the following program is run?
#include <stdio.h>
int func(int a, int b){
a = a - 1;
return a * b;
}
int main(){
int a = 3, b = 4;
int c = func(a, b);
printf("%d %d %d", a, b, c);
return 0;
}
   
A 3 4 8 B 2 4 12 C 3 4 12 D 2 4 8

The information below applies to questions 40–41. Knowing that the boolean function
inList(char c, char * s) returns True if the character c is in the string s, otherwise it returns
False. Implement the function checkTitle(char * raw_title) as follows:
int checkTitle(char * raw_title){
int len_raw_title = strlen(raw_title);
// violate the string length
if(len_raw_title > 100){
return len_raw_title;
}
// first character is not space
/* (a) */{
return 0;
}
// check validity for middle characters
for(int i = 0; i < len_raw_title; ++i){
if(isLowerLetter(raw_title[i])

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2313 Page 7/12


|| isUpperLetter(raw_title[i])
|| isDigit(raw_title[i])
|| inList(raw_title[i], " ,.-:|/")
){
/* (c) */
}
else {
return i;
}
}
// last character is not space
if(raw_title[len_raw_title-1] == ’ ’){
return len_raw_title-1;
}
return -1;
}

(L.O.1.2) Which can be /* (a) */?


Q 40. 
A ifraw_title[0] = ’ ’ B if(raw_title[0] == ’ ’)
 
C IF(raw_title[0] == ’ ’) D if(raw_title[0] = ’ ’)

Q 41. (L.O.2.1) The code for /* (c) */ can be


  
A continue; B None of the other choices is correct. C break;

D goto;

Q 42. (L.O.1.2) What will be the output if the following code snippet is run?
#include <stdio.h>
int main(){
int x = 5;
if(x = 6) printf("Yes");
else printf("No");
return 0;
}
   
A No B Yes C An error D Nothing

Q 43. (L.O.2.2) Which operations on struct are illegal?


 
A Define a struct containing a pointer to a B All choices are correct.
variable whose type is struct itself 
C Dynamic allocation D Type-casting

Q 44. (L.O.2.2) Which expression can be used to get the value of the third element of an array a of six elements?
   
A *a + 3 B &(a + 2) C *(a + 2) D *(*a + 3)

Q 45. (L.O.3.1) What will be displayed if the following code snippet is run?
int i = -1; do{printf("Hello!");}while(i++);
 
A None of the other choices is correct. B Hello!Hello!Hello!
 
C Hello! D Hello!Hello!

Q 46. (L.O.1.2) The function main consists of the following codes:


int a = 6; if(a == 5) printf("a = 5");

What will be displayed if the program is run?


   
A a = 6 B a = 5 C a = 7 D a = 8

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2313 Page 8/12


Q 47. (L.O.1.1) What will be the value of x if the following code snippet is run?
#include <stdio.h>
void solve(){
int x = printf("Hello");
printf("%d", x);
}
int main(){
solve();
return 0;
}
   
A 10 B 0 C 1 D 5

Q 48. (L.O.3.2) What will be the output if the following program is run?
#include <stdio.h>
int compare(char s[], char r[]){
if (s < r) return 1; else return 0;
}
int main(){
char s[4] = "cat";
char r[4] = "cat";
printf("%d", compare(r, s));
return 0;
}
   
A 0 B A garbage value C An error D 1

Q 49. (L.O.1.1) Which will be displayed if the following code snippet is run with input “Hello␣world!”?
char c = ’A’;
char s[5];
scanf("%s", s);
printf("%s%c", s, c);
   
A Hello B Hello␣ C HelloA D HellA

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2313 Page 9/12


Q 50. (L.O.2.1) Given that the variable display is a one-dimensional array of characters. display is used to store
strings representing the days of the week. The array display is allocated with a sufficient number of elements
to perform the operations below.
For the following code snippet 8.1:
// code 8.1
printf("%s", display);

After executing the above code on the display array, the desired output (output 1) is:
|MON||TUE||WED||THU||FRI||SAT||SUN|
Assuming that currently, all characters in display are spaces, except for the last character being the null
terminator.
Given the following code snippet to modify the display variable so that when the code snippet 8.1 is
executed, the desired output (output 1) is achieved.
char day_of_weeks[][/*(a)*/] = {"MON", "TUE", "WED", "THU", "FRI", "SAT",
"SUN"};
for(int i = 0; i < 7; ++i){
display[/*(b)*/] = display[/*(c)*/] = ’|’;
/*(d)*/;
}

Choose the valid codes to fill in the comments /* (a) */, /* (b) */, /* (c) */, and /* (d) */ respectively:

A (a) 4, (b) 5 * i, (c) 5 * i + 4, (d) strncpy(display + 5 * i + 1, day_of_weeks[i], 3)

B (a) 4, (b) 5 * i, (c) 5 * i + 4, (d) strncpy(display + 5 * i + 1, day_of_weeks[i], 3)
or strcpy(display + 5 * i + 1, day_of_weeks[i])
C (a) 4, (b) 5 * i, (c) 5 * i + 4, (d) strcpy(display + 5 * i + 1, day_of_weeks[i])

D (a) 3, (b) i, (c) i + 4, (d) strcpy(display + 5 * i + 1, day_of_weeks[i])

Q 51. (L.O.2.2) What will be the output if the following code snippet is run?
#include <stdio.h>
int main(){
int x = 10, *ptr;
ptr = &x;
*ptr = 20;
printf("%d", x);
}
   
A 20 B A garbage value C 10 D Error

The information below applies to questions 52–53. It is known that the title of the Add command is defined as
follows:

• The title can only contain characters that are one of the following types: lowercase letters, uppercase letters,
digits, or special characters (including spaces, commas, periods, hyphens, colons, | and /).

• The title must not start or end with a space.

The function checkTitle(char * raw_title) has the following output:

• If the title is valid according to the above conditions, return -1;

• If the title is invalid due to violating the maximum length condition, return the current length of the title.

• If the title is invalid due to violating other conditions (besides the maximum length condition), return the
position of the first character that violates the condition.

Q 52. (L.O.1.2) With the input raw_title = "Do homework", what is the output of the checkTitle func-
tion?
   
A 12 B 2 C -1 D 11

Stu. ID: . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . Code: 2313 Page 10/12


Q 53. (L.O.1.2) With the input raw_title = "Buy(Pencil, Eraser) ", what is the output of the
checkTitle function?   
A -1 B 20 C 19 D 3

Q 54. (L.O.1.1) Which declaration is correct in C programming language?


   
A int my_n = 50; B int my n = 50; C int my_n = 5,0; D int $my_n = 50;

Q 55. (L.O.2.1) Which way is correct to define a parameter of a function as a 2-dimensional C-array with elements
of type of a 1-dimensional C-array consisting of 10 integers as elements?
   
A int s[10][] B int s[][10] C int s[10][10] D int s[][]

Q 56. (L.O.2.1) Assume p is void pointer whose value is the address of a variable x of type int*. Which way is
correct to get the value 
whose address is kept by x?
 Choose the most correct answer.
A (int)(**p) B **((int **)p) C All choices are correct.

D *(*((int **)p))

Q 57. (L.O.1.2) Find the most appropriate condition that can be used to check if the character kept by a variable
c is an english letter?

A (((’a’ <= c) || (c <= ’z’)) || ((’A’ <= c) && (c <= ’Z’)))

B (((’a’ <= c) && (c <= ’z’)) && ((’A’ <= c) && (c <= ’Z’)))

C (((’a’ <= c) || (c <= ’z’)) && ((’A’ <= c) || (c <= ’Z’)))

D (((’a’ <= c) && (c <= ’z’)) || ((’A’ <= c) && (c <= ’Z’)))

Q 58. (L.O.1.1) Which will be displayed if printf("%5.2d", 10) is run?


   
A 10.00 B 10 C ␣␣␣10 D 00010

Stu. ID: . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . Code: 2313 Page 11/12


Q 59. (L.O.3.1) Given the definition of a struct Date representing a day below. The struct Date consists of
three components: the variable day represents the day, the variable month represents the month, and the
variable year represents the year. It is assumed that even months have 30 days and odd months have 31
days. For example, February and April have 30 days, and September and November have 31 days.
struct Date{
int day;
int month;
int year;
};

Given the following function implemented in C programming language, the function takes the parameter
date of struct Date as input and returns a struct Date representing the previous date of the input
date.
struct Date getPreviousDate(struct Date date){
struct Date pre_date;
pre_date.day = date.day - 1;
if (pre_date.day == 0) {
pre_date.month = /* (a) */;
if (/* (b) */) {
pre_date.year = date.year - 1;
/* (c) */
}
else {
pre_date.year = date.year;
}
pre_date.day = /* (d) */;
} else {
pre_date.month = date.month;
pre_date.year = date.year;
}
return pre_date;
}

For the given example code:


struct Date date = {20, 11, 2023};
struct Date pre_date = getPreviousDate(date);
After execution, pre_date’s components day, month, and year will be 19, 11, and 2023 respectively.
Choose the valid codes to fill in the comments /* (a) */, /* (b) */, /* (c) */, and /* (d) */ respectively.

A (a) date.day - 1, (b) pre_date.month == 1, (c) No code here, (d) 30

B (a) date.day - 1, (b) pre_date.month == 0, (c) pre_date.month = 12,
(d) 30 + pre_date.month % 2
C (a) date.day, (b) pre_date.month == 0, (c)
pre_date.month = date.month, (d) 30 + pre_date.month % 2
D (a) date.day, (b) pre_date.month == 1, (c) pre_date.month = 12, (d)
31 - pre_date.month % 2
Q 60. (L.O.3.2) Find the recursion depth of the call foo(5) (the number of function calls until the base case is
reached including the first call).
int foo(int n){
if (n == 0 || n == 1) return 1;
else return foo(n - 1) * foo(n - 2);
}
   
A 5 B 8 C 11 D 3

Stu. ID: . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . Code: 2313 Page 12/12


Semester/Academic year 1 2023-2024
FINAL EXAM (Answer Key) Date 26/12/2023
Course title Introduction to Programming
UNIVERSITY OF TECHNOLOGY Course ID CO1003
FACULTY OF CSE Duration 90 minutes Question sheet code 2313

   
Q 1. B Q 16. B Q 32. D Q 47. D
   
Q 2. B Q 17. A Q 33. D Q 48. D
   
Q 3. C Q 18. C Q 34. D Q 49. A
  
Q 19. C 
Q 4. D Q 35. A Q 50. A
  
Q 20. B Q 36. A 
Q 5. A Q 51. A
  
Q 6. D Q 21. C Q 37. C
  
 Q 52. C
Q 7. A Q 22. B Q 38. D
  

Q 8. B Q 23. B Q 39. A Q 53. D

 Q 24. A
Q 9. D  
 Q 40. B Q 54. A
Q 25. C  
  Q 41. A Q 55. B
Q 10. C Q 26. A
 
  Q 56. C
Q 11. D Q 27. B Q 42. B
 
 
Q 12. A Q 43. D Q 57. D
Q 28. C
   
Q 13. C Q 29. C Q 44. C Q 58. C
   
Q 14. D Q 30. A Q 45. D Q 59. B
   
Q 15. B Q 31. C Q 46. B Q 60. A
Lecturer(s): Approved by:
Head of Department of CS

Semester/Academic year 1 2023-2024


FINAL EXAM
Date 26/12/2023
Course title Introduction to Programming
UNIVERSITY OF TECHNOLOGY Course ID CO1003
FACULTY OF CSE Duration 90 minutes Question sheet code 2314
Notes: - Stu. ID and Stu. Fullname fields at the bottom of the question sheets and on the top of
the answer sheet must be filled in. Students answer on the answer sheets.
- The exam consists of 60 multiple-choice questions, 08 of which are harmonized
with the assignment.

Q 1. (L.O.2.2) Which operations on struct are illegal?


 
A Define a struct containing a pointer to a B Type-casting
variable whose type is struct itself 
C Dynamic allocation D All choices are correct.

Q 2. (L.O.2.1) Which will be the output if the following code snippet is run?
#include <stdio.h>
int main(){
int a = 5;
++a;
a += a++;
printf("%d", a);
return 0;
}
   
A 12 B 13 C 11 D 10

Q 3. (L.O.1.1) If printf("%d", 0246) is called, it will display what?


   
A 0246 B 246 C Other choice D 6420

Q 4. (L.O.2.2) Which declaration is the declaration of a double pointer in C programming language?


   
A int *val; B int *&val; C int &val; D int **val;

Q 5. (L.O.2.2) What will be the output if the following code snippet is run?
#include <stdio.h>
int main(){
int x = 10, *ptr;
ptr = &x;
*ptr = 20;
printf("%d", x);
}
   
A 20 B Error C 10 D A garbage value

Q 6. (L.O.3.2) Consider the following function


int fn(int x, int y){
if(x != y) return (x + y);
else return (x - y);
}

If printf("%d", fn(3, fn(4, 5))) is run, the output will be


   
A 21 B 12 C 15 D 27

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2314 Page 1/12


Q 7. (L.O.3.1) Which will be displayed if the following code snippet is run and the value of m (of type int) is 1?
switch(m){
case 1: printf("%d", m++);
case 2: printf("%d", ++m);
default: printf("%d", m);
}
   
A 2 B 233 C 133 D 1

Q 8. (L.O.3.2) What will be the output if the following code snippet is run?
#include <stdio.h>
void fun(int * ptr){
*ptr = 30;
}
int main(){
int y = 20;
fun(&y);
printf("%d", y);
return 0;
}
   
A 30 B Runtime error C 20 D Compile-time error

Q 9. (L.O.2.1) Given an array a of six elements, which expression can be used to get the value of the fourth
element of the array?
   
A a[3] B a[1] C a[4] D a[2]

Q 10. (L.O.1.1) What will be displayed if printf("C:\blue\red") is run?


   
A bluered B C:bluered C edue D C:\blue\red

Q 11. (L.O.1.2) In a nested if-else statement, the conditions are checked in which order?
 
A From the condition of the innermost if to B From the condition of the outermost if to
the condition of the outermost if. the condition of the innermost if.
C The conditions are checked at the same time. D In the order that the programmer specifies
at the running time.

(L.O.2.1) How many memory


Q 12.  cells that are neededto store string “HELLO␣WORLD!”?

A 12 B 13 C 11 D 15

Q 13. (L.O.1.1) Which will be displayed if the following code snippet is run with input “Hello␣world!”?
char c = ’A’;
char s[5];
scanf("%s", s);
printf("%s%c", s, c);
   
A Hello B HellA C HelloA D Hello␣

Q 14. (L.O.2.2) Consider structure (struct) SV as below, the call sizeof(SV) will return which value? Assume
that size of type int is 4 bytes and size of type char is 1 byte.
typedef struct SV{
char mssv[8];
char name[20];
int dtb;
} SV;
   
A 34 B 32 C 40 D 28

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2314 Page 2/12


Q 15. (L.O.1.2) What will be the value of variable S if the following code snippet is run?
int S = 0;
for(int i = 1; i < 10; i+=2) S += i;
printf("%d", S);
   
A 55 B 45 C 25 D 10

Q 16. (L.O.3.2) What will be the output if the following program is run?
#include <stdio.h>
int func(int a, int b){
a = a - 1;
return a * b;
}
int main(){
int a = 3, b = 4;
int c = func(a, b);
printf("%d %d %d", a, b, c);
return 0;
}
   
A 3 4 8 B 2 4 8 C 3 4 12 D 2 4 12

Q 17. (L.O.1.2) The function main consists of the following codes:


int a = 6; if(a == 5) printf("a = 5");

What will be displayed if the program is run?


   
A a = 6 B a = 8 C a = 7 D a = 5

Q 18. (L.O.2.1) Which is the hexadecimal representation of the initial value of variable m in the declaration
int m = 48 | 12;?
   
A 0x4 B 0xc C 0x3c D 0x30

Q 19. (L.O.2.2) Which is the size of a struct consisting of one 1-dimensional array with 10 elements of type int
and one variable of type char? The size of int is 4 bytes and the size of char is 1 byte.
   
A 11 bytes B 5 bytes C 41 bytes D 44 bytes

Q 20. (L.O.2.1) What will be the output if the following code snippet is run?
int a[2][3] = {{1, 2, 3}, {4, 5, 6}};
int *p = a;
printf("%d", *(p+4));
   
A 3 B 4 C 5 D An error

Q 21. (L.O.2.2) Given a struct with two member row and col and a variable x of that struct data type. Which
of the following calls can be used to display the values of the data members row and col of variable x?
 
A None of the other choices is correct. B printf("%d, %d", x.row, x.col);
 
C printf("x.row, x.col"); D printf(x);

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2314 Page 3/12


Q 22. (L.O.1.2) What will be the output if the following code snippet is run?
#include <stdio.h>
int main(){
int x = 5;
if(x = 6) printf("Yes");
else printf("No");
return 0;
}
   
A No B Nothing C An error D Yes

Q 23. (L.O.2.1) In the C programming language, which operator is used to perform the modulus operation?
   
A - B / C * D %

Q 24. (L.O.1.2) What will be the output if the following code snippet is run?
#include <stdio.h>
void solve(){
int ch = 2;
switch(ch){
case 1: printf("1 ");
case 2: printf("2 ");
case 3: printf("3 ");
default: printf("None");
}
}
int main(){
solve();
return 0;
}
   
A 1 2 3 None B 2 C 2 3 None D None

Q 25. (L.O.3.1) What will be displayed if the following code snippet is run?
#include <stdio.h>
#include <string.h>
int main(){
if (strcmp("HCMUT", "HCM") > 0){
printf("Inside if");
} else if (strcmp("HCMUT", "HCM") == 0){
printf("Inside else if");
} else {
printf("Inside else");
}
return 0;
}
   
A Inside else if B An error C Inside else D Inside if

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2314 Page 4/12


Q 26. (L.O.3.2) What will be the output if the following program is run?
#include <stdio.h>
void TT(int x, int *y){
x = x + *y;
*y = *y + x;
}
int main(){
int a = 6, b = 8;
TT(a, &b);
printf("a = %d, b = %d", a, b);
return 0;
}
   
A a = 6, b = 14 B a = 6, b = 8 C a = 14, b = 8 D a = 6, b = 22

Q 27. (L.O.1.2) Find the most appropriate condition that can be used to check if the character kept by a variable
c is an english letter?

A (((’a’ <= c) || (c <= ’z’)) || ((’A’ <= c) && (c <= ’Z’)))

B (((’a’ <= c) && (c <= ’z’)) || ((’A’ <= c) && (c <= ’Z’)))

C (((’a’ <= c) || (c <= ’z’)) && ((’A’ <= c) || (c <= ’Z’)))

D (((’a’ <= c) && (c <= ’z’)) && ((’A’ <= c) && (c <= ’Z’)))

Q 28. (L.O.1.1) Given the C-string char s[6] = {’h’, ’e’, ’\0’, ’l’, ’l’, ’o’}, what will happen if
printf("%6c", s) is run? 
A String “he” will be displayed. B String “hello” will be displayed.
 
C String “he␣llo” will be displayed. D A warning about incorrect conversion
specification will be displayed.
Q 29. (L.O.2.1) Assume p is void pointer whose value is the address of a variable x of type int*. Which way is
correct to get the value 
whose address is kept by x?
 Choose the most correct answer.
A (int)(**p) B *(*((int **)p)) C All choices are correct.

D **((int **)p)

Q 30. (L.O.3.1) What does the following function do?


void foo(int* a, int b){
int c = *a;
*a = b;
b = c;
}
 
A To assign value of a variable whose address B To swap the values of parameter a and
is kept by a to parameter b parameter b
C To assign value of parameter b to a vari- D To assign value of parameter b to param-
able whose address is kept by a eter &a

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2314 Page 5/12


Q 31. (L.O.3.2) What is the purpose of the following function?
int foo(int arr[], int n){
int a = -1;
int b = 0;
for(int i = 0; i < n; i++){
int count = 0;
for(int j = 0; j < n; j++){
if (arr[j] == arr[i]) count++;
}
if(count > b){
b = count;
a = arr[i];
}
}
return a;
}
 
A To count the number of element n in the B To find the element that appears most fre-
array. quently in the array.
C To find the sum of the elements of the array. D To count the number of distinct elements
of the array.
Q 32. (L.O.1.1) Which will be displayed if printf("%5.2d", 10) is run?
   
A 10.00 B 00010 C ␣␣␣10 D 10

Q 33. (L.O.2.1) In programming language C, which conditon can be used to determine if an integer is a multiple
of both 3 and 5?
 
A (num % 3 == 0) || (num % 5 == 0) B (num % 3 != 0) && (num % 5 == 0)
 
C (num % 3 == 0) && (num % 5 != 0) D (num % 3 == 0) && (num % 5 == 0)

The information below applies to questions 34–35. Given the Task structure defined as follows:
struct Task {
int num;
char title[MAX_LENGTH_TITLE + 1];
char description[MAX_LENGTH_DESCRIPTION + 1];
char time[MAX_LENGTH_TIME + 1];
enum Status status;
};

And the addTask function declaration as follows:


bool addTask(struct Task * array_tasks, int no_tasks, char * new_title, char *
new_description, char * new_time);

The function adds a new task to the end of the array with member variables corresponding to new_title,
new_description, new_time. The first task added to the application has num equal to 1. Tasks added after
the first task will have num equal to the num of the previously added task plus 1.
For an implementation in C of the function:
bool addTask(struct Task * array_tasks, int no_tasks, char * new_title, char *
new_description, char * new_time) {
if(no_tasks >= MAX_NO_TASKS){
return false;
}
/*(a)*/(array_tasks[/*(b)*/].title, new_title);
/*(a)*/(array_tasks[/*(b)*/].description, new_description);
/*(a)*/(array_tasks[/*(b)*/].time, new_time);
array_tasks[/*(b)*/].status = IN_PROGRESS;

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2314 Page 6/12


array_tasks[/*(b)*/].num = /*(c)*/;
return true;
}

(L.O.2.1) The code for /*


Q 34.  (a) */ can be  
A strlen B strtok C strcpy D strcat

Q 35. (L.O.2.1) The code for /* (b) */ and /* (c) */ can respectively be
 
A (b) no_tasks + 1, (c) no_tasks + 1 B (b) no_tasks, (c) no_tasks + 1
 
C (b) no_tasks + 1, (c) no_tasks + 2 D (b) no_tasks, (c) no_tasks

Q 36. (L.O.1.1) The function main contains the following codes:


int a; char c;
scanf("%d%c", &a, &c);
printf("%d %c", a, c);

If “32␣a” is entered, what will be displayed?


   
A 32a B 32␣␣ C 32␣a D An error

Q 37. (L.O.1.2) In the C programming language, the else statement is used together with
   
A while B if C for D switch

The information below applies to questions 38–39. It is known that the title of the Add command is defined as
follows:

• The title can only contain characters that are one of the following types: lowercase letters, uppercase letters,
digits, or special characters (including spaces, commas, periods, hyphens, colons, | and /).

• The title must not start or end with a space.

The function checkTitle(char * raw_title) has the following output:

• If the title is valid according to the above conditions, return -1;

• If the title is invalid due to violating the maximum length condition, return the current length of the title.

• If the title is invalid due to violating other conditions (besides the maximum length condition), return the
position of the first character that violates the condition.

Q 38. (L.O.1.2) With the input raw_title = "Do homework", what is the output of the checkTitle func-
tion?
   
A 12 B 11 C -1 D 2

Q 39. (L.O.1.2) With the input raw_title = "Buy(Pencil, Eraser) ", what is the output of the
checkTitle function?   
A -1 B 3 C 19 D 20

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2314 Page 7/12


Q 40. (L.O.2.1) Given that the variable display is a one-dimensional array of characters. display is used to store
strings representing the days of the week. The array display is allocated with a sufficient number of elements
to perform the operations below.
For the following code snippet 8.1:
// code 8.1
printf("%s", display);

After executing the above code on the display array, the desired output (output 1) is:
|MON||TUE||WED||THU||FRI||SAT||SUN|
Assuming that currently, all characters in display are spaces, except for the last character being the null
terminator.
Given the following code snippet to modify the display variable so that when the code snippet 8.1 is
executed, the desired output (output 1) is achieved.
char day_of_weeks[][/*(a)*/] = {"MON", "TUE", "WED", "THU", "FRI", "SAT",
"SUN"};
for(int i = 0; i < 7; ++i){
display[/*(b)*/] = display[/*(c)*/] = ’|’;
/*(d)*/;
}

Choose the valid codes to fill in the comments /* (a) */, /* (b) */, /* (c) */, and /* (d) */ respectively:

A (a) 4, (b) 5 * i, (c) 5 * i + 4, (d) strncpy(display + 5 * i + 1, day_of_weeks[i], 3)

B (a) 3, (b) i, (c) i + 4, (d) strcpy(display + 5 * i + 1, day_of_weeks[i])

C (a) 4, (b) 5 * i, (c) 5 * i + 4, (d) strcpy(display + 5 * i + 1, day_of_weeks[i])

D (a) 4, (b) 5 * i, (c) 5 * i + 4, (d) strncpy(display + 5 * i + 1, day_of_weeks[i], 3)
or strcpy(display + 5 * i + 1, day_of_weeks[i])
Q 41. (L.O.2.2) What will be the output if the following code snippet is run?
struct stu{int x, y;};
struct stu s1 = {1,4};
struct stu s2 = {2,3};
if(s1.y > s2.y)
printf("True");
else
printf("False");
   
A False B TrueFalse C True D FalseTrue

Q 42. (L.O.3.2) Which statement about what a function results in is true ?


 
A A function must always return a specific B A function cannot return any value (num-
value (number, string, character,. . . ) ber, string, character, . . . )
C A function can be set to not return any D All choices are correct.
value or to return a specific value.
Q 43. (L.O.1.1) What will be the value of x if the following code snippet is run?
#include <stdio.h>
void solve(){
int x = printf("Hello");
printf("%d", x);
}
int main(){
solve();
return 0;
}
   
A 10 B 5 C 1 D 0

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2314 Page 8/12


Q 44. (L.O.3.2) To request the operating system for a memory space exactly 64 bits in the heap region which
call can be used?
   
A malloc(8) B malloc(64) C calloc(1, 64) D calloc(8, 8)

Q 45. (L.O.3.2) What is the purpose of the following function?


void foo(char * s, int m, int n){
while(m < n){
char c = *(s + m);
*(s + m) = *(s + n);
*(s + n) = c;
m++; n--;
}
}
 
A To reverse any part of a string. B To reverse a string and then reverse it back.
 
C Nothing because the string will not be D There will be an error if the function is
changed. called.
Q 46. (L.O.1.1) The call printf("%d", 0xB7) will display what if it is run?
   
A 117 B 0 C B7 D 183

The information below applies to questions 47–48. Knowing that the boolean function
inList(char c, char * s) returns True if the character c is in the string s, otherwise it returns
False. Implement the function checkTitle(char * raw_title) as follows:
int checkTitle(char * raw_title){
int len_raw_title = strlen(raw_title);
// violate the string length
if(len_raw_title > 100){
return len_raw_title;
}
// first character is not space
/* (a) */{
return 0;
}
// check validity for middle characters
for(int i = 0; i < len_raw_title; ++i){
if(isLowerLetter(raw_title[i])
|| isUpperLetter(raw_title[i])
|| isDigit(raw_title[i])
|| inList(raw_title[i], " ,.-:|/")
){
/* (c) */
}
else {
return i;
}
}
// last character is not space
if(raw_title[len_raw_title-1] == ’ ’){
return len_raw_title-1;
}
return -1;
}

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2314 Page 9/12


(L.O.1.2) Which can be /* (a) */?
Q 47. 
A ifraw_title[0] = ’ ’ B if(raw_title[0] = ’ ’)
 
C IF(raw_title[0] == ’ ’) D if(raw_title[0] == ’ ’)

Q 48. (L.O.2.1) The code for /* (c) */ can be


  
A continue; B goto; C break;

D None of the other choices is correct.

Q 49. (L.O.1.2) Which will be the output if the following code snippet is run?
int a = 6, b = 8;
if(a > 5){
a -= 1; b += 1;
}else{
a += 1; b -= 1;
}
printf("a = %d, b = %d", a, b);
   
A a = 5, b = 9 B a = 5, b = 8 C a = 6, b = 9 D a = 6, b = 8

Stu. ID: . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . Code: 2314 Page 10/12


Q 50. (L.O.3.1) Given the definition of a struct Date representing a day below. The struct Date consists of
three components: the variable day represents the day, the variable month represents the month, and the
variable year represents the year. It is assumed that even months have 30 days and odd months have 31
days. For example, February and April have 30 days, and September and November have 31 days.
struct Date{
int day;
int month;
int year;
};

Given the following function implemented in C programming language, the function takes the parameter
date of struct Date as input and returns a struct Date representing the previous date of the input
date.
struct Date getPreviousDate(struct Date date){
struct Date pre_date;
pre_date.day = date.day - 1;
if (pre_date.day == 0) {
pre_date.month = /* (a) */;
if (/* (b) */) {
pre_date.year = date.year - 1;
/* (c) */
}
else {
pre_date.year = date.year;
}
pre_date.day = /* (d) */;
} else {
pre_date.month = date.month;
pre_date.year = date.year;
}
return pre_date;
}

For the given example code:


struct Date date = {20, 11, 2023};
struct Date pre_date = getPreviousDate(date);
After execution, pre_date’s components day, month, and year will be 19, 11, and 2023 respectively.
Choose the valid codes to fill in the comments /* (a) */, /* (b) */, /* (c) */, and /* (d) */ respectively.

A (a) date.day - 1, (b) pre_date.month == 1, (c) No code here, (d) 30

B (a) date.day, (b) pre_date.month == 1, (c) pre_date.month = 12, (d)
31 - pre_date.month % 2
C (a) date.day, (b) pre_date.month == 0, (c)
pre_date.month = date.month, (d) 30 + pre_date.month % 2
D (a) date.day - 1, (b) pre_date.month == 0, (c) pre_date.month = 12,
(d) 30 + pre_date.month % 2
Q 51. (L.O.2.2) Given pointer pA2 keeping the address of a pointer pA1 and pA1 keeping the address of variable
A of type int, which expression can be used to get the value of A and to assign that value to a variable x
also of type int?
   
A int x = **pA2; B int x = *(pA2); C int x = *pA2; D All choices are correct.

Stu. ID: . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . Code: 2314 Page 11/12


Q 52. (L.O.3.2) What will be the output if the following program is run?
#include <stdio.h>
int compare(char s[], char r[]){
if (s < r) return 1; else return 0;
}
int main(){
char s[4] = "cat";
char r[4] = "cat";
printf("%d", compare(r, s));
return 0;
}
   
A 0 B 1 C An error D A garbage value

Q 53. (L.O.1.1) Which declaration is correct in C programming language?


   
A int my_n = 50; B int $my_n = 50; C int my_n = 5,0; D int my n = 50;

Q 54. (L.O.3.1) Which statement is the most correct?


 
A All code statements in the while loop are B All choices are correct.
executed at least once. 
C All code statements in the for loop are D All code statements in the do-while loop
executed at least once. are executed at least once.
Q 55. (L.O.3.2) Find the recursion depth of the call foo(5) (the number of function calls until the base case is
reached including the first call).
int foo(int n){
if (n == 0 || n == 1) return 1;
else return foo(n - 1) * foo(n - 2);
}
   
A 5 B 3 C 11 D 8

Q 56. (L.O.3.1) What will be displayed if the following code snippet is run?
int i = -1; do{printf("Hello!");}while(i++);
 
A None of the other choices is correct. B Hello!Hello!
 
C Hello! D Hello!Hello!Hello!

Q 57. (L.O.2.1) Which way is correct to define a parameter of a function as a 2-dimensional C-array with elements
of type of a 1-dimensional C-array consisting of 10 integers as elements?
   
A int s[10][] B int s[][] C int s[10][10] D int s[][10]

Q 58. (L.O.2.2) Which expression can be used to get the value of the third element of an array a of six elements?
   
A *a + 3 B *(*a + 3) C *(a + 2) D &(a + 2)

Q 59. (L.O.2.2) In the C programming language, which keyword is used to define a structured data type?
   
A struct B type C class D record

Q 60. (L.O.1.2) What will be displayed if the following code snippet is run?
int n = 20, i = 1;
while (i < n) {
i *= 2;
n /= 2;
printf("%d ", n);
}
   
A 10 5 2 B 10 5 2 1 C 20 10 5 2 D 10 10 5 2

Stu. ID: . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . Code: 2314 Page 12/12


Semester/Academic year 1 2023-2024
FINAL EXAM (Answer Key) Date 26/12/2023
Course title Introduction to Programming
UNIVERSITY OF TECHNOLOGY Course ID CO1003
FACULTY OF CSE Duration 90 minutes Question sheet code 2314

   
Q 1. B Q 17. D Q 33. D Q 47. D
  
Q 2. B Q 18. C  Q 48. A
  Q 34. C
Q 3. C Q 19. D  
  Q 35. B Q 49. A
Q 4. D Q 20. C 
  Q 36. B 
Q 50. D
Q 5. A Q 21. A 
  Q 37. B 
Q 6. B Q 22. D Q 51. A
   
Q 7. C Q 23. D Q 38. C Q 52. B
   
Q 8. A Q 24. C Q 39. B Q 53. A
 
Q 9. A Q 25. D  
Q 40. A Q 54. D
 
Q 10. C Q 26. D  
  Q 41. C Q 55. A
Q 11. B Q 27. B  
  Q 42. C Q 56. B
Q 12. B Q 28. D  
  Q 43. B Q 57. D
Q 13. A Q 29. C 
Q 44. A 
  Q 58. C
Q 14. B Q 30. C 
  Q 45. A 
Q 15. C Q 31. B  Q 59. A
  Q 46. D 
Q 16. A Q 32. C Q 60. A
Lecturer(s): Approved by:
Head of Department of CS

Semester/Academic year 1 2023-2024


FINAL EXAM
Date 26/12/2023
Course title Introduction to Programming
UNIVERSITY OF TECHNOLOGY Course ID CO1003
FACULTY OF CSE Duration 90 minutes Question sheet code 2315
Notes: - Stu. ID and Stu. Fullname fields at the bottom of the question sheets and on the top of
the answer sheet must be filled in. Students answer on the answer sheets.
- The exam consists of 60 multiple-choice questions, 08 of which are harmonized
with the assignment.

Q 1. (L.O.3.2) Find the recursion depth of the call foo(5) (the number of function calls until the base case is
reached including the first call).
int foo(int n){
if (n == 0 || n == 1) return 1;
else return foo(n - 1) * foo(n - 2);
}
   
A 8 B 5 C 3 D 11

Q 2. (L.O.3.1) What will be displayed if the following code snippet is run?


#include <stdio.h>
#include <string.h>
int main(){
if (strcmp("HCMUT", "HCM") > 0){
printf("Inside if");
} else if (strcmp("HCMUT", "HCM") == 0){
printf("Inside else if");
} else {
printf("Inside else");
}
return 0;
}
   
A Inside if B Inside else if C An error D Inside else

Q 3. (L.O.1.2) What will be the output if the following code snippet is run?
#include <stdio.h>
int main(){
int x = 5;
if(x = 6) printf("Yes");
else printf("No");
return 0;
}
   
A Yes B No C Nothing D An error

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2315 Page 1/12


Q 4. (L.O.3.1) What does the following function do?
void foo(int* a, int b){
int c = *a;
*a = b;
b = c;
}
 
A To assign value of parameter b to param- B To assign value of a variable whose address
eter &a is kept by a to parameter b
C To swap the values of parameter a and D To assign value of parameter b to a vari-
parameter b able whose address is kept by a
Q 5. (L.O.3.1) Which statement is the most correct?
 
A All code statements in the do-while loop B All code statements in the while loop are
are executed at least once. executed at least once.
C All choices are correct. D All code statements in the for loop are
executed at least once.
Q 6. (L.O.2.1) Given an array a of six elements, which expression can be used to get the value of the fourth
element of the array?
   
A a[2] B a[3] C a[1] D a[4]

Q 7. (L.O.1.2) Find the most appropriate condition that can be used to check if the character kept by a variable
c is an english letter?

A (((’a’ <= c) && (c <= ’z’)) && ((’A’ <= c) && (c <= ’Z’)))

B (((’a’ <= c) || (c <= ’z’)) || ((’A’ <= c) && (c <= ’Z’)))

C (((’a’ <= c) && (c <= ’z’)) || ((’A’ <= c) && (c <= ’Z’)))

D (((’a’ <= c) || (c <= ’z’)) && ((’A’ <= c) || (c <= ’Z’)))

Q 8. (L.O.3.2) What will be the output if the following code snippet is run?
#include <stdio.h>
void fun(int * ptr){
*ptr = 30;
}
int main(){
int y = 20;
fun(&y);
printf("%d", y);
return 0;
}
   
A Compile-time error B 30 C Runtime error D 20

Q 9. (L.O.1.1) What will be displayed if printf("C:\blue\red") is run?


   
A C:\blue\red B bluered C C:bluered D edue

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2315 Page 2/12


Q 10. (L.O.3.1) Given the definition of a struct Date representing a day below. The struct Date consists of
three components: the variable day represents the day, the variable month represents the month, and the
variable year represents the year. It is assumed that even months have 30 days and odd months have 31
days. For example, February and April have 30 days, and September and November have 31 days.
struct Date{
int day;
int month;
int year;
};

Given the following function implemented in C programming language, the function takes the parameter
date of struct Date as input and returns a struct Date representing the previous date of the input
date.
struct Date getPreviousDate(struct Date date){
struct Date pre_date;
pre_date.day = date.day - 1;
if (pre_date.day == 0) {
pre_date.month = /* (a) */;
if (/* (b) */) {
pre_date.year = date.year - 1;
/* (c) */
}
else {
pre_date.year = date.year;
}
pre_date.day = /* (d) */;
} else {
pre_date.month = date.month;
pre_date.year = date.year;
}
return pre_date;
}

For the given example code:


struct Date date = {20, 11, 2023};
struct Date pre_date = getPreviousDate(date);
After execution, pre_date’s components day, month, and year will be 19, 11, and 2023 respectively.
Choose the valid codes to fill in the comments /* (a) */, /* (b) */, /* (c) */, and /* (d) */ respectively.

A (a) date.day - 1, (b) pre_date.month == 0, (c) pre_date.month = 12,
(d) 30 + pre_date.month % 2
B (a) date.day - 1, (b) pre_date.month == 1, (c) No code here, (d) 30

C (a) date.day, (b) pre_date.month == 1, (c) pre_date.month = 12, (d)
31 - pre_date.month % 2
D (a) date.day, (b) pre_date.month == 0, (c)
pre_date.month = date.month, (d) 30 + pre_date.month % 2
Q 11. (L.O.2.1) In the C programming language, which operator is used to perform the modulus operation?
   
A % B - C / D *

Q 12. (L.O.3.2) Which statement about what a function results in is true ?


 
A All choices are correct. B A function must always return a specific
 value (number, string, character,. . . )
C A function cannot return any value (num- D A function can be set to not return any
ber, string, character, . . . ) value or to return a specific value.
Q 13. (L.O.1.1) If printf("%d", 0246) is called, it will display what?
   
A 6420 B 0246 C 246 D Other choice

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2315 Page 3/12


Q 14. (L.O.2.1) Given that the variable display is a one-dimensional array of characters. display is used to store
strings representing the days of the week. The array display is allocated with a sufficient number of elements
to perform the operations below.
For the following code snippet 8.1:
// code 8.1
printf("%s", display);

After executing the above code on the display array, the desired output (output 1) is:
|MON||TUE||WED||THU||FRI||SAT||SUN|
Assuming that currently, all characters in display are spaces, except for the last character being the null
terminator.
Given the following code snippet to modify the display variable so that when the code snippet 8.1 is
executed, the desired output (output 1) is achieved.
char day_of_weeks[][/*(a)*/] = {"MON", "TUE", "WED", "THU", "FRI", "SAT",
"SUN"};
for(int i = 0; i < 7; ++i){
display[/*(b)*/] = display[/*(c)*/] = ’|’;
/*(d)*/;
}

Choose the valid codes to fill in the comments /* (a) */, /* (b) */, /* (c) */, and /* (d) */ respectively:

A (a) 4, (b) 5 * i, (c) 5 * i + 4, (d) strncpy(display + 5 * i + 1, day_of_weeks[i], 3)
or strcpy(display + 5 * i + 1, day_of_weeks[i])
B (a) 4, (b) 5 * i, (c) 5 * i + 4, (d) strncpy(display + 5 * i + 1, day_of_weeks[i], 3)

C (a) 3, (b) i, (c) i + 4, (d) strcpy(display + 5 * i + 1, day_of_weeks[i])

D (a) 4, (b) 5 * i, (c) 5 * i + 4, (d) strcpy(display + 5 * i + 1, day_of_weeks[i])

Q 15. (L.O.3.2) Consider the following function


int fn(int x, int y){
if(x != y) return (x + y);
else return (x - y);
}

If printf("%d", fn(3, fn(4, 5))) is run, the output will be


   
A 27 B 21 C 12 D 15

Q 16. (L.O.1.2) What will be displayed if the following code snippet is run?
int n = 20, i = 1;
while (i < n) {
i *= 2;
n /= 2;
printf("%d ", n);
}
   
A 10 10 5 2 B 10 5 2 C 10 5 2 1 D 20 10 5 2

Q 17. (L.O.1.1) Which will be displayed if printf("%5.2d", 10) is run?


   
A 10 B 10.00 C 00010 D ␣␣␣10

Q 18. (L.O.2.1) Which is the hexadecimal representation of the initial value of variable m in the declaration
int m = 48 | 12;?
   
A 0x30 B 0x4 C 0xc D 0x3c

(L.O.2.1) How many memory


Q 19.  cells that are neededto store string “HELLO␣WORLD!”?

A 15 B 12 C 13 D 11

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2315 Page 4/12


Q 20. (L.O.2.2) Which operations on struct are illegal?
 
A All choices are correct. B Define a struct containing a pointer to a
 variable whose type is struct itself
C Type-casting D Dynamic allocation

Q 21. (L.O.2.1) Which will be the output if the following code snippet is run?
#include <stdio.h>
int main(){
int a = 5;
++a;
a += a++;
printf("%d", a);
return 0;
}
   
A 10 B 12 C 13 D 11

Q 22. (L.O.1.1) The call printf("%d", 0xB7) will display what if it is run?
   
A 183 B 117 C 0 D B7

Q 23. (L.O.2.2) Which is the size of a struct consisting of one 1-dimensional array with 10 elements of type int
and one variable of type char? The size of int is 4 bytes and the size of char is 1 byte.
   
A 44 bytes B 11 bytes C 5 bytes D 41 bytes

Q 24. (L.O.1.2) What will be the value of variable S if the following code snippet is run?
int S = 0;
for(int i = 1; i < 10; i+=2) S += i;
printf("%d", S);
   
A 10 B 55 C 45 D 25

Q 25. (L.O.2.2) Consider structure (struct) SV as below, the call sizeof(SV) will return which value? Assume
that size of type int is 4 bytes and size of type char is 1 byte.
typedef struct SV{
char mssv[8];
char name[20];
int dtb;
} SV;
   
A 28 B 34 C 32 D 40

Q 26. (L.O.3.2) What will be the output if the following program is run?
#include <stdio.h>
int compare(char s[], char r[]){
if (s < r) return 1; else return 0;
}
int main(){
char s[4] = "cat";
char r[4] = "cat";
printf("%d", compare(r, s));
return 0;
}
   
A A garbage value B 0 C 1 D An error

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2315 Page 5/12


Q 27. (L.O.1.2) Which will be the output if the following code snippet is run?
int a = 6, b = 8;
if(a > 5){
a -= 1; b += 1;
}else{
a += 1; b -= 1;
}
printf("a = %d, b = %d", a, b);
   
A a = 6, b = 8 B a = 5, b = 9 C a = 5, b = 8 D a = 6, b = 9

Q 28. (L.O.2.2) What will be the output if the following code snippet is run?
#include <stdio.h>
int main(){
int x = 10, *ptr;
ptr = &x;
*ptr = 20;
printf("%d", x);
}
   
A A garbage value B 20 C Error D 10

Q 29. (L.O.1.1) Given the C-string char s[6] = {’h’, ’e’, ’\0’, ’l’, ’l’, ’o’}, what will happen if
printf("%6c", s) is run? 
A A warning about incorrect conversion B String “he” will be displayed.
specification will be displayed. 
C String “hello” will be displayed. D String “he␣llo” will be displayed.

Q 30. (L.O.1.2) In a nested if-else statement, the conditions are checked in which order?
 
A In the order that the programmer specifies B From the condition of the innermost if to
at the running time. the condition of the outermost if.
C From the condition of the outermost if to D The conditions are checked at the same time.
the condition of the innermost if.
Q 31. (L.O.1.2) What will be the output if the following code snippet is run?
#include <stdio.h>
void solve(){
int ch = 2;
switch(ch){
case 1: printf("1 ");
case 2: printf("2 ");
case 3: printf("3 ");
default: printf("None");
}
}
int main(){
solve();
return 0;
}
   
A None B 1 2 3 None C 2 D 2 3 None

Q 32. (L.O.2.2) Which expression can be used to get the value of the third element of an array a of six elements?
   
A &(a + 2) B *a + 3 C *(*a + 3) D *(a + 2)

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2315 Page 6/12


Q 33. (L.O.2.2) Given pointer pA2 keeping the address of a pointer pA1 and pA1 keeping the address of variable
A of type int, which expression can be used to get the value of A and to assign that value to a variable x
also of type int?
  
A All choices are correct. B int x = **pA2; C int x = *(pA2);

D int x = *pA2;

Q 34. (L.O.2.2) What will be the output if the following code snippet is run?
struct stu{int x, y;};
struct stu s1 = {1,4};
struct stu s2 = {2,3};
if(s1.y > s2.y)
printf("True");
else
printf("False");
   
A FalseTrue B False C TrueFalse D True

Q 35. (L.O.2.2) Given a struct with two member row and col and a variable x of that struct data type. Which
of the following calls can be used to display the values of the data members row and col of variable x?
 
A printf(x); B None of the other choices is correct.
 
C printf("%d, %d", x.row, x.col); D printf("x.row, x.col");

Q 36. (L.O.3.2) What will be the output if the following program is run?
#include <stdio.h>
int func(int a, int b){
a = a - 1;
return a * b;
}
int main(){
int a = 3, b = 4;
int c = func(a, b);
printf("%d %d %d", a, b, c);
return 0;
}
   
A 2 4 12 B 3 4 8 C 2 4 8 D 3 4 12

Q 37. (L.O.1.1) Which will be displayed if the following code snippet is run with input “Hello␣world!”?
char c = ’A’;
char s[5];
scanf("%s", s);
printf("%s%c", s, c);
   
A Hello␣ B Hello C HellA D HelloA

Q 38. (L.O.1.1) Which declaration is correct in C programming language?


   
A int my n = 50; B int my_n = 50; C int $my_n = 50; D int my_n = 5,0;

Q 39. (L.O.3.1) What will be displayed if the following code snippet is run?
int i = -1; do{printf("Hello!");}while(i++);
 
A Hello!Hello!Hello! B None of the other choices is correct.
 
C Hello!Hello! D Hello!

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2315 Page 7/12


Q 40. (L.O.1.1) The function main contains the following codes:
int a; char c;
scanf("%d%c", &a, &c);
printf("%d %c", a, c);

If “32␣a” is entered, what will be displayed?


   
A An error B 32a C 32␣␣ D 32␣a

The information below applies to questions 41–42. Given the Task structure defined as follows:
struct Task {
int num;
char title[MAX_LENGTH_TITLE + 1];
char description[MAX_LENGTH_DESCRIPTION + 1];
char time[MAX_LENGTH_TIME + 1];
enum Status status;
};

And the addTask function declaration as follows:


bool addTask(struct Task * array_tasks, int no_tasks, char * new_title, char *
new_description, char * new_time);

The function adds a new task to the end of the array with member variables corresponding to new_title,
new_description, new_time. The first task added to the application has num equal to 1. Tasks added after
the first task will have num equal to the num of the previously added task plus 1.
For an implementation in C of the function:
bool addTask(struct Task * array_tasks, int no_tasks, char * new_title, char *
new_description, char * new_time) {
if(no_tasks >= MAX_NO_TASKS){
return false;
}
/*(a)*/(array_tasks[/*(b)*/].title, new_title);
/*(a)*/(array_tasks[/*(b)*/].description, new_description);
/*(a)*/(array_tasks[/*(b)*/].time, new_time);
array_tasks[/*(b)*/].status = IN_PROGRESS;
array_tasks[/*(b)*/].num = /*(c)*/;
return true;
}

(L.O.2.1) The code for /*


Q 41.  (a) */ can be  
A strcat B strlen C strtok D strcpy

Q 42. (L.O.2.1) The code for /* (b) */ and /* (c) */ can respectively be
 
A (b) no_tasks, (c) no_tasks B (b) no_tasks + 1, (c) no_tasks + 1
 
C (b) no_tasks, (c) no_tasks + 1 D (b) no_tasks + 1, (c) no_tasks + 2

Q 43. (L.O.2.1) Which way is correct to define a parameter of a function as a 2-dimensional C-array with elements
of type of a 1-dimensional C-array consisting of 10 integers as elements?
   
A int s[][10] B int s[10][] C int s[][] D int s[10][10]

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2315 Page 8/12


Q 44. (L.O.1.1) What will be the value of x if the following code snippet is run?
#include <stdio.h>
void solve(){
int x = printf("Hello");
printf("%d", x);
}
int main(){
solve();
return 0;
}
   
A 0 B 10 C 5 D 1

The information below applies to questions 45–46. Knowing that the boolean function
inList(char c, char * s) returns True if the character c is in the string s, otherwise it returns
False. Implement the function checkTitle(char * raw_title) as follows:
int checkTitle(char * raw_title){
int len_raw_title = strlen(raw_title);
// violate the string length
if(len_raw_title > 100){
return len_raw_title;
}
// first character is not space
/* (a) */{
return 0;
}
// check validity for middle characters
for(int i = 0; i < len_raw_title; ++i){
if(isLowerLetter(raw_title[i])
|| isUpperLetter(raw_title[i])
|| isDigit(raw_title[i])
|| inList(raw_title[i], " ,.-:|/")
){
/* (c) */
}
else {
return i;
}
}
// last character is not space
if(raw_title[len_raw_title-1] == ’ ’){
return len_raw_title-1;
}
return -1;
}

(L.O.1.2) Which can be /* (a) */?


Q 45. 
A if(raw_title[0] == ’ ’) B ifraw_title[0] = ’ ’
 
C if(raw_title[0] = ’ ’) D IF(raw_title[0] == ’ ’)

Q 46. (L.O.2.1) The code for /* (c) */ can be


  
A None of the other choices is correct. B continue; C goto;

D break;

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2315 Page 9/12


Q 47. (L.O.3.2) To request the operating system for a memory space exactly 64 bits in the heap region which
call can be used?
   
A calloc(8, 8) B malloc(8) C malloc(64) D calloc(1, 64)

The information below applies to questions 48–49. It is known that the title of the Add command is defined as
follows:

• The title can only contain characters that are one of the following types: lowercase letters, uppercase letters,
digits, or special characters (including spaces, commas, periods, hyphens, colons, | and /).

• The title must not start or end with a space.

The function checkTitle(char * raw_title) has the following output:

• If the title is valid according to the above conditions, return -1;

• If the title is invalid due to violating the maximum length condition, return the current length of the title.

• If the title is invalid due to violating other conditions (besides the maximum length condition), return the
position of the first character that violates the condition.

Q 48. (L.O.1.2) With the input raw_title = "Do homework", what is the output of the checkTitle func-
tion?
   
A 2 B 12 C 11 D -1

Q 49. (L.O.1.2) With the input raw_title = "Buy(Pencil, Eraser) ", what is the output of the
checkTitle function?   
A 20 B -1 C 3 D 19

Q 50. (L.O.3.2) What is the purpose of the following function?


void foo(char * s, int m, int n){
while(m < n){
char c = *(s + m);
*(s + m) = *(s + n);
*(s + n) = c;
m++; n--;
}
}
 
A There will be an error if the function is B To reverse any part of a string.
called. 
C To reverse a string and then reverse it back. D Nothing because the string will not be
changed.
Q 51. (L.O.1.2) The function main consists of the following codes:
int a = 6; if(a == 5) printf("a = 5");

What will be displayed if the program is run?


   
A a = 5 B a = 6 C a = 8 D a = 7

Q 52. (L.O.1.2) In the C programming language, the else statement is used together with
   
A switch B while C if D for

Q 53. (L.O.2.2) In the C programming language, which keyword is used to define a structured data type?
   
A record B struct C type D class

Q 54. (L.O.2.2) Which declaration is the declaration of a double pointer in C programming language?
   
A int **val; B int *val; C int *&val; D int &val;

Stu. ID: . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . Code: 2315 Page 10/12


Q 55. (L.O.2.1) In programming language C, which conditon can be used to determine if an integer is a multiple
of both 3 and 5?
 
A (num % 3 == 0) && (num % 5 == 0) B (num % 3 == 0) || (num % 5 == 0)
 
C (num % 3 != 0) && (num % 5 == 0) D (num % 3 == 0) && (num % 5 != 0)

Q 56. (L.O.3.2) What is the purpose of the following function?


int foo(int arr[], int n){
int a = -1;
int b = 0;
for(int i = 0; i < n; i++){
int count = 0;
for(int j = 0; j < n; j++){
if (arr[j] == arr[i]) count++;
}
if(count > b){
b = count;
a = arr[i];
}
}
return a;
}
 
A To count the number of distinct elements B To count the number of element n in the
of the array. array.
C To find the element that appears most fre- D To find the sum of the elements of the array.
quently in the array.
Q 57. (L.O.2.1) What will be the output if the following code snippet is run?
int a[2][3] = {{1, 2, 3}, {4, 5, 6}};
int *p = a;
printf("%d", *(p+4));
   
A An error B 3 C 4 D 5

Q 58. (L.O.3.1) Which will be displayed if the following code snippet is run and the value of m (of type int) is 1?
switch(m){
case 1: printf("%d", m++);
case 2: printf("%d", ++m);
default: printf("%d", m);
}
   
A 1 B 2 C 233 D 133

Stu. ID: . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . Code: 2315 Page 11/12


Q 59. (L.O.3.2) What will be the output if the following program is run?
#include <stdio.h>
void TT(int x, int *y){
x = x + *y;
*y = *y + x;
}
int main(){
int a = 6, b = 8;
TT(a, &b);
printf("a = %d, b = %d", a, b);
return 0;
}
   
A a = 6, b = 22 B a = 6, b = 14 C a = 6, b = 8 D a = 14, b = 8

Q 60. (L.O.2.1) Assume p is void pointer whose value is the address of a variable x of type int*. Which way is
correct to get the value 
whose address is kept by x?
 Choose the most correct answer.
A **((int **)p) B (int)(**p) C *(*((int **)p)) D All choices are correct.

Stu. ID: . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . Code: 2315 Page 12/12


Semester/Academic year 1 2023-2024
FINAL EXAM (Answer Key) Date 26/12/2023
Course title Introduction to Programming
UNIVERSITY OF TECHNOLOGY Course ID CO1003
FACULTY OF CSE Duration 90 minutes Question sheet code 2315

   
Q 1. B Q 17. D Q 33. B Q 47. B
  
Q 2. A Q 18. D Q 34. D 
   Q 48. D
Q 3. A Q 19. C Q 35. B

   Q 49. C
Q 4. D Q 20. C Q 36. B
  
Q 5. A Q 21. C 
Q 37. B Q 50. B
  
Q 6. B Q 22. A 
Q 38. B Q 51. A
  
Q 7. C Q 23. A Q 39. C 
  Q 52. C

Q 8. B Q 24. D Q 40. C 
  Q 53. B
Q 9. D Q 25. C 

  Q 41. D Q 54. A
Q 10. A Q 26. C 

  Q 55. A
Q 11. A Q 27. B Q 42. C
 
  Q 56. C
Q 12. D Q 28. B Q 43. A
 
  Q 57. D
Q 13. D Q 29. A Q 44. C
  
Q 14. B Q 30. C  Q 58. D
  Q 45. A 
Q 15. C Q 31. D  Q 59. A
  Q 46. B 
Q 16. B Q 32. D Q 60. D
Lecturer(s): Approved by:
Head of Department of CS

Semester/Academic year 1 2023-2024


FINAL EXAM
Date 26/12/2023
Course title Introduction to Programming
UNIVERSITY OF TECHNOLOGY Course ID CO1003
FACULTY OF CSE Duration 90 minutes Question sheet code 2316
Notes: - Stu. ID and Stu. Fullname fields at the bottom of the question sheets and on the top of
the answer sheet must be filled in. Students answer on the answer sheets.
- The exam consists of 60 multiple-choice questions, 08 of which are harmonized
with the assignment.

Q 1. (L.O.3.2) Find the recursion depth of the call foo(5) (the number of function calls until the base case is
reached including the first call).
int foo(int n){
if (n == 0 || n == 1) return 1;
else return foo(n - 1) * foo(n - 2);
}
   
A 5 B 8 C 3 D 11

Q 2. (L.O.3.2) To request the operating system for a memory space exactly 64 bits in the heap region which
call can be used?
   
A malloc(8) B calloc(8, 8) C malloc(64) D calloc(1, 64)

Q 3. (L.O.1.1) Which will be displayed if printf("%5.2d", 10) is run?


   
A 10.00 B 10 C 00010 D ␣␣␣10

Q 4. (L.O.2.2) What will be the output if the following code snippet is run?
struct stu{int x, y;};
struct stu s1 = {1,4};
struct stu s2 = {2,3};
if(s1.y > s2.y)
printf("True");
else
printf("False");
   
A False B FalseTrue C TrueFalse D True

Q 5. (L.O.2.2) Which declaration is the declaration of a double pointer in C programming language?


   
A int *val; B int **val; C int *&val; D int &val;

Q 6. (L.O.2.2) Which expression can be used to get the value of the third element of an array a of six elements?
   
A *a + 3 B &(a + 2) C *(*a + 3) D *(a + 2)

Q 7. (L.O.2.2) Which operations on struct are illegal?


 
A Define a struct containing a pointer to a B All choices are correct.
variable whose type is struct itself 
C Type-casting D Dynamic allocation

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2316 Page 1/12


Q 8. (L.O.3.1) What will be displayed if the following code snippet is run?
#include <stdio.h>
#include <string.h>
int main(){
if (strcmp("HCMUT", "HCM") > 0){
printf("Inside if");
} else if (strcmp("HCMUT", "HCM") == 0){
printf("Inside else if");
} else {
printf("Inside else");
}
return 0;
}
   
A Inside else if B Inside if C An error D Inside else

Q 9. (L.O.3.2) Which statement about what a function results in is true ?


 
A A function must always return a specific B All choices are correct.
value (number, string, character,. . . ) 
C A function cannot return any value (num- D A function can be set to not return any
ber, string, character, . . . ) value or to return a specific value.
Q 10. (L.O.1.1) The call printf("%d", 0xB7) will display what if it is run?
   
A 117 B 183 C 0 D B7

Q 11. (L.O.3.2) What will be the output if the following program is run?
#include <stdio.h>
void TT(int x, int *y){
x = x + *y;
*y = *y + x;
}
int main(){
int a = 6, b = 8;
TT(a, &b);
printf("a = %d, b = %d", a, b);
return 0;
}
   
A a = 6, b = 14 B a = 6, b = 22 C a = 6, b = 8 D a = 14, b = 8

Q 12. (L.O.2.1) What will be the output if the following code snippet is run?
int a[2][3] = {{1, 2, 3}, {4, 5, 6}};
int *p = a;
printf("%d", *(p+4));
   
A 3 B An error C 4 D 5

Q 13. (L.O.3.1) Which will be displayed if the following code snippet is run and the value of m (of type int) is 1?
switch(m){
case 1: printf("%d", m++);
case 2: printf("%d", ++m);
default: printf("%d", m);
}
   
A 2 B 1 C 233 D 133

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2316 Page 2/12


Q 14. (L.O.1.2) In a nested if-else statement, the conditions are checked in which order?
 
A From the condition of the innermost if to B In the order that the programmer specifies
the condition of the outermost if. at the running time.
C From the condition of the outermost if to D The conditions are checked at the same time.
the condition of the innermost if.
Q 15. (L.O.3.1) What will be displayed if the following code snippet is run?
int i = -1; do{printf("Hello!");}while(i++);
 
A None of the other choices is correct. B Hello!Hello!Hello!
 
C Hello!Hello! D Hello!

(L.O.2.1) How many memory


Q 16.  cells that are neededto store string “HELLO␣WORLD!”?

A 12 B 15 C 13 D 11

Q 17. (L.O.2.2) Given pointer pA2 keeping the address of a pointer pA1 and pA1 keeping the address of variable
A of type int, which expression can be used to get the value of A and to assign that value to a variable x
also of type int?
  
A int x = **pA2; B All choices are correct. C int x = *(pA2);

D int x = *pA2;

Q 18. (L.O.2.1) Assume p is void pointer whose value is the address of a variable x of type int*. Which way is
correct to get the value 
whose address is kept by x?
 Choose the most correct answer.
A (int)(**p) B **((int **)p) C *(*((int **)p)) D All choices are correct.

Q 19. (L.O.2.1) Given that the variable display is a one-dimensional array of characters. display is used to store
strings representing the days of the week. The array display is allocated with a sufficient number of elements
to perform the operations below.
For the following code snippet 8.1:
// code 8.1
printf("%s", display);

After executing the above code on the display array, the desired output (output 1) is:
|MON||TUE||WED||THU||FRI||SAT||SUN|
Assuming that currently, all characters in display are spaces, except for the last character being the null
terminator.
Given the following code snippet to modify the display variable so that when the code snippet 8.1 is
executed, the desired output (output 1) is achieved.
char day_of_weeks[][/*(a)*/] = {"MON", "TUE", "WED", "THU", "FRI", "SAT",
"SUN"};
for(int i = 0; i < 7; ++i){
display[/*(b)*/] = display[/*(c)*/] = ’|’;
/*(d)*/;
}

Choose the valid codes to fill in the comments /* (a) */, /* (b) */, /* (c) */, and /* (d) */ respectively:

A (a) 4, (b) 5 * i, (c) 5 * i + 4, (d) strncpy(display + 5 * i + 1, day_of_weeks[i], 3)

B (a) 4, (b) 5 * i, (c) 5 * i + 4, (d) strncpy(display + 5 * i + 1, day_of_weeks[i], 3)
or strcpy(display + 5 * i + 1, day_of_weeks[i])
C (a) 3, (b) i, (c) i + 4, (d) strcpy(display + 5 * i + 1, day_of_weeks[i])

D (a) 4, (b) 5 * i, (c) 5 * i + 4, (d) strcpy(display + 5 * i + 1, day_of_weeks[i])

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2316 Page 3/12


Q 20. (L.O.3.1) Given the definition of a struct Date representing a day below. The struct Date consists of
three components: the variable day represents the day, the variable month represents the month, and the
variable year represents the year. It is assumed that even months have 30 days and odd months have 31
days. For example, February and April have 30 days, and September and November have 31 days.
struct Date{
int day;
int month;
int year;
};

Given the following function implemented in C programming language, the function takes the parameter
date of struct Date as input and returns a struct Date representing the previous date of the input
date.
struct Date getPreviousDate(struct Date date){
struct Date pre_date;
pre_date.day = date.day - 1;
if (pre_date.day == 0) {
pre_date.month = /* (a) */;
if (/* (b) */) {
pre_date.year = date.year - 1;
/* (c) */
}
else {
pre_date.year = date.year;
}
pre_date.day = /* (d) */;
} else {
pre_date.month = date.month;
pre_date.year = date.year;
}
return pre_date;
}

For the given example code:


struct Date date = {20, 11, 2023};
struct Date pre_date = getPreviousDate(date);
After execution, pre_date’s components day, month, and year will be 19, 11, and 2023 respectively.
Choose the valid codes to fill in the comments /* (a) */, /* (b) */, /* (c) */, and /* (d) */ respectively.

A (a) date.day - 1, (b) pre_date.month == 1, (c) No code here, (d) 30

B (a) date.day - 1, (b) pre_date.month == 0, (c) pre_date.month = 12,
(d) 30 + pre_date.month % 2
C (a) date.day, (b) pre_date.month == 1, (c) pre_date.month = 12, (d)
31 - pre_date.month % 2
D (a) date.day, (b) pre_date.month == 0, (c)
pre_date.month = date.month, (d) 30 + pre_date.month % 2

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2316 Page 4/12


Q 21. (L.O.3.2) What is the purpose of the following function?
int foo(int arr[], int n){
int a = -1;
int b = 0;
for(int i = 0; i < n; i++){
int count = 0;
for(int j = 0; j < n; j++){
if (arr[j] == arr[i]) count++;
}
if(count > b){
b = count;
a = arr[i];
}
}
return a;
}
 
A To count the number of element n in the B To count the number of distinct elements
array. of the array.
C To find the element that appears most fre- D To find the sum of the elements of the array.
quently in the array.
Q 22. (L.O.1.1) What will be displayed if printf("C:\blue\red") is run?
   
A bluered B C:\blue\red C C:bluered D edue

Q 23. (L.O.3.2) What will be the output if the following program is run?
#include <stdio.h>
int func(int a, int b){
a = a - 1;
return a * b;
}
int main(){
int a = 3, b = 4;
int c = func(a, b);
printf("%d %d %d", a, b, c);
return 0;
}
   
A 3 4 8 B 2 4 12 C 2 4 8 D 3 4 12

Q 24. (L.O.3.2) Consider the following function


int fn(int x, int y){
if(x != y) return (x + y);
else return (x - y);
}

If printf("%d", fn(3, fn(4, 5))) is run, the output will be


   
A 21 B 27 C 12 D 15

Q 25. (L.O.1.1) Given the C-string char s[6] = {’h’, ’e’, ’\0’, ’l’, ’l’, ’o’}, what will happen if
printf("%6c", s) is run? 
A String “he” will be displayed. B A warning about incorrect conversion
 specification will be displayed.
C String “hello” will be displayed. D String “he␣llo” will be displayed.

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2316 Page 5/12


Q 26. (L.O.1.2) The function main consists of the following codes:
int a = 6; if(a == 5) printf("a = 5");

What will be displayed if the program is run?


   
A a = 6 B a = 5 C a = 8 D a = 7

Q 27. (L.O.2.1) Which is the hexadecimal representation of the initial value of variable m in the declaration
int m = 48 | 12;?
   
A 0x4 B 0x30 C 0xc D 0x3c

Q 28. (L.O.1.2) What will be the output if the following code snippet is run?
#include <stdio.h>
int main(){
int x = 5;
if(x = 6) printf("Yes");
else printf("No");
return 0;
}
   
A No B Yes C Nothing D An error

Q 29. (L.O.3.2) What will be the output if the following program is run?
#include <stdio.h>
int compare(char s[], char r[]){
if (s < r) return 1; else return 0;
}
int main(){
char s[4] = "cat";
char r[4] = "cat";
printf("%d", compare(r, s));
return 0;
}
   
A 0 B A garbage value C 1 D An error

Q 30. (L.O.1.1) Which declaration is correct in C programming language?


   
A int my_n = 50; B int my n = 50; C int $my_n = 50; D int my_n = 5,0;

Q 31. (L.O.3.2) What is the purpose of the following function?


void foo(char * s, int m, int n){
while(m < n){
char c = *(s + m);
*(s + m) = *(s + n);
*(s + n) = c;
m++; n--;
}
}
 
A To reverse any part of a string. B There will be an error if the function is
 called.
C To reverse a string and then reverse it back. D Nothing because the string will not be
changed.
Q 32. (L.O.2.2) Given a struct with two member row and col and a variable x of that struct data type. Which
of the following calls can be used to display the values of the data members row and col of variable x?
 
A None of the other choices is correct. B printf(x);
 
C printf("%d, %d", x.row, x.col); D printf("x.row, x.col");

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2316 Page 6/12


Q 33. (L.O.1.2) Find the most appropriate condition that can be used to check if the character kept by a variable
c is an english letter?

A (((’a’ <= c) || (c <= ’z’)) || ((’A’ <= c) && (c <= ’Z’)))

B (((’a’ <= c) && (c <= ’z’)) && ((’A’ <= c) && (c <= ’Z’)))

C (((’a’ <= c) && (c <= ’z’)) || ((’A’ <= c) && (c <= ’Z’)))

D (((’a’ <= c) || (c <= ’z’)) && ((’A’ <= c) || (c <= ’Z’)))

Q 34. (L.O.1.2) What will be the output if the following code snippet is run?
#include <stdio.h>
void solve(){
int ch = 2;
switch(ch){
case 1: printf("1 ");
case 2: printf("2 ");
case 3: printf("3 ");
default: printf("None");
}
}
int main(){
solve();
return 0;
}
   
A 1 2 3 None B None C 2 D 2 3 None

Q 35. (L.O.2.2) Which is the size of a struct consisting of one 1-dimensional array with 10 elements of type int
and one variable of type char? The size of int is 4 bytes and the size of char is 1 byte.
   
A 11 bytes B 44 bytes C 5 bytes D 41 bytes

Q 36. (L.O.3.2) What will be the output if the following code snippet is run?
#include <stdio.h>
void fun(int * ptr){
*ptr = 30;
}
int main(){
int y = 20;
fun(&y);
printf("%d", y);
return 0;
}
   
A 30 B Compile-time error C Runtime error D 20

Q 37. (L.O.1.2) What will be the value of variable S if the following code snippet is run?
int S = 0;
for(int i = 1; i < 10; i+=2) S += i;
printf("%d", S);
   
A 55 B 10 C 45 D 25

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2316 Page 7/12


Q 38. (L.O.1.2) What will be displayed if the following code snippet is run?
int n = 20, i = 1;
while (i < n) {
i *= 2;
n /= 2;
printf("%d ", n);
}
   
A 10 5 2 B 10 10 5 2 C 10 5 2 1 D 20 10 5 2

Q 39. (L.O.2.1) Given an array a of six elements, which expression can be used to get the value of the fourth
element of the array?
   
A a[3] B a[2] C a[1] D a[4]

Q 40. (L.O.1.2) In the C programming language, the else statement is used together with
   
A while B switch C if D for

Q 41. (L.O.1.1) If printf("%d", 0246) is called, it will display what?


   
A 0246 B 6420 C 246 D Other choice

Q 42. (L.O.2.2) Consider structure (struct) SV as below, the call sizeof(SV) will return which value? Assume
that size of type int is 4 bytes and size of type char is 1 byte.
typedef struct SV{
char mssv[8];
char name[20];
int dtb;
} SV;
   
A 34 B 28 C 32 D 40

The information below applies to questions 43–44. Given the Task structure defined as follows:
struct Task {
int num;
char title[MAX_LENGTH_TITLE + 1];
char description[MAX_LENGTH_DESCRIPTION + 1];
char time[MAX_LENGTH_TIME + 1];
enum Status status;
};

And the addTask function declaration as follows:


bool addTask(struct Task * array_tasks, int no_tasks, char * new_title, char *
new_description, char * new_time);

The function adds a new task to the end of the array with member variables corresponding to new_title,
new_description, new_time. The first task added to the application has num equal to 1. Tasks added after
the first task will have num equal to the num of the previously added task plus 1.
For an implementation in C of the function:
bool addTask(struct Task * array_tasks, int no_tasks, char * new_title, char *
new_description, char * new_time) {
if(no_tasks >= MAX_NO_TASKS){
return false;
}
/*(a)*/(array_tasks[/*(b)*/].title, new_title);
/*(a)*/(array_tasks[/*(b)*/].description, new_description);
/*(a)*/(array_tasks[/*(b)*/].time, new_time);

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2316 Page 8/12


array_tasks[/*(b)*/].status = IN_PROGRESS;
array_tasks[/*(b)*/].num = /*(c)*/;
return true;
}

(L.O.2.1) The code for /*


Q 43.  (a) */ can be  
A strlen B strcat C strtok D strcpy

Q 44. (L.O.2.1) The code for /* (b) */ and /* (c) */ can respectively be
 
A (b) no_tasks + 1, (c) no_tasks + 1 B (b) no_tasks, (c) no_tasks
 
C (b) no_tasks, (c) no_tasks + 1 D (b) no_tasks + 1, (c) no_tasks + 2

Q 45. (L.O.1.1) What will be the value of x if the following code snippet is run?
#include <stdio.h>
void solve(){
int x = printf("Hello");
printf("%d", x);
}
int main(){
solve();
return 0;
}
   
A 10 B 0 C 5 D 1

Q 46. (L.O.2.1) In programming language C, which conditon can be used to determine if an integer is a multiple
of both 3 and 5?
 
A (num % 3 == 0) || (num % 5 == 0) B (num % 3 == 0) && (num % 5 == 0)
 
C (num % 3 != 0) && (num % 5 == 0) D (num % 3 == 0) && (num % 5 != 0)

Q 47. (L.O.2.2) What will be the output if the following code snippet is run?
#include <stdio.h>
int main(){
int x = 10, *ptr;
ptr = &x;
*ptr = 20;
printf("%d", x);
}
   
A 20 B A garbage value C Error D 10

The information below applies to questions 48–49. Knowing that the boolean function
inList(char c, char * s) returns True if the character c is in the string s, otherwise it returns
False. Implement the function checkTitle(char * raw_title) as follows:
int checkTitle(char * raw_title){
int len_raw_title = strlen(raw_title);
// violate the string length
if(len_raw_title > 100){
return len_raw_title;
}
// first character is not space
/* (a) */{
return 0;
}
// check validity for middle characters

Stu. ID: . . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . . Code: 2316 Page 9/12


for(int i = 0; i < len_raw_title; ++i){
if(isLowerLetter(raw_title[i])
|| isUpperLetter(raw_title[i])
|| isDigit(raw_title[i])
|| inList(raw_title[i], " ,.-:|/")
){
/* (c) */
}
else {
return i;
}
}
// last character is not space
if(raw_title[len_raw_title-1] == ’ ’){
return len_raw_title-1;
}
return -1;
}

(L.O.1.2) Which can be /* (a) */?


Q 48. 
A ifraw_title[0] = ’ ’ B if(raw_title[0] == ’ ’)
 
C if(raw_title[0] = ’ ’) D IF(raw_title[0] == ’ ’)

Q 49. (L.O.2.1) The code for /* (c) */ can be


  
A continue; B None of the other choices is correct. C goto;

D break;

The information below applies to questions 50–51. It is known that the title of the Add command is defined as
follows:

• The title can only contain characters that are one of the following types: lowercase letters, uppercase letters,
digits, or special characters (including spaces, commas, periods, hyphens, colons, | and /).

• The title must not start or end with a space.

The function checkTitle(char * raw_title) has the following output:

• If the title is valid according to the above conditions, return -1;

• If the title is invalid due to violating the maximum length condition, return the current length of the title.

• If the title is invalid due to violating other conditions (besides the maximum length condition), return the
position of the first character that violates the condition.

Q 50. (L.O.1.2) With the input raw_title = "Do homework", what is the output of the checkTitle func-
tion?
   
A 12 B 2 C 11 D -1

Q 51. (L.O.1.2) With the input raw_title = "Buy(Pencil, Eraser) ", what is the output of the
checkTitle function?   
A -1 B 20 C 3 D 19

Q 52. (L.O.2.1) Which way is correct to define a parameter of a function as a 2-dimensional C-array with elements
of type of a 1-dimensional C-array consisting of 10 integers as elements?
   
A int s[10][] B int s[][10] C int s[][] D int s[10][10]

Stu. ID: . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . Code: 2316 Page 10/12


Q 53. (L.O.1.1) The function main contains the following codes:
int a; char c;
scanf("%d%c", &a, &c);
printf("%d %c", a, c);

If “32␣a” is entered, what will be displayed?


   
A 32a B An error C 32␣␣ D 32␣a

Q 54. (L.O.1.2) Which will be the output if the following code snippet is run?
int a = 6, b = 8;
if(a > 5){
a -= 1; b += 1;
}else{
a += 1; b -= 1;
}
printf("a = %d, b = %d", a, b);
   
A a = 5, b = 9 B a = 6, b = 8 C a = 5, b = 8 D a = 6, b = 9

Q 55. (L.O.2.1) Which will be the output if the following code snippet is run?
#include <stdio.h>
int main(){
int a = 5;
++a;
a += a++;
printf("%d", a);
return 0;
}
   
A 12 B 10 C 13 D 11

Q 56. (L.O.1.1) Which will be displayed if the following code snippet is run with input “Hello␣world!”?
char c = ’A’;
char s[5];
scanf("%s", s);
printf("%s%c", s, c);
   
A Hello B Hello␣ C HellA D HelloA

Q 57. (L.O.2.2) In the C programming language, which keyword is used to define a structured data type?
   
A struct B record C type D class

Q 58. (L.O.3.1) What does the following function do?


void foo(int* a, int b){
int c = *a;
*a = b;
b = c;
}
 
A To assign value of a variable whose address B To assign value of parameter b to param-
is kept by a to parameter b eter &a
C To swap the values of parameter a and D To assign value of parameter b to a vari-
parameter b able whose address is kept by a

Stu. ID: . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . Code: 2316 Page 11/12


Q 59. (L.O.3.1) Which statement is the most correct?
 
A All code statements in the while loop are B All code statements in the do-while loop
executed at least once. are executed at least once.
C All choices are correct. D All code statements in the for loop are
executed at least once.
Q 60. (L.O.2.1) In the C programming language, which operator is used to perform the modulus operation?
   
A - B % C / D *

Stu. ID: . . . . . . . . . . . . . . . . . Stu. Fullname: . . . . . . . . . . . . . . . . . Code: 2316 Page 12/12


Semester/Academic year 1 2023-2024
FINAL EXAM (Answer Key) Date 26/12/2023
Course title Introduction to Programming
UNIVERSITY OF TECHNOLOGY Course ID CO1003
FACULTY OF CSE Duration 90 minutes Question sheet code 2316

   
Q 1. A Q 17. A Q 33. C Q 48. B
   
Q 2. A Q 18. D Q 34. D Q 49. A
  
Q 3. D Q 19. A Q 35. B
   
Q 4. D Q 20. B Q 36. A Q 50. D
   
Q 5. B Q 21. C Q 37. D Q 51. C
  
Q 6. D Q 22. D Q 38. A

   Q 52. B
Q 7. C Q 23. A Q 39. A
   
Q 8. B Q 24. C Q 40. C Q 53. C
   
Q 9. D Q 25. B Q 41. D Q 54. A
  

Q 10. B Q 26. B Q 42. C Q 55. C
 
Q 11. B Q 27. D  
Q 43. D Q 56. A
 
Q 12. D Q 28. B  
  Q 44. C Q 57. A
Q 13. D Q 29. C 

  Q 45. C Q 58. D
Q 14. C Q 30. A 
  Q 46. B 
Q 15. C Q 31. A  Q 59. B
  Q 47. A 
Q 16. C Q 32. A Q 60. B

You might also like