231 CO1003 Final
231 CO1003 Final
Head of Department of CS
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 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’)))
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 /).
• 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
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;
}
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;
};
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;
}
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)
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 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)
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 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
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 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]
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;
}
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
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 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
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
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 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;
}
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
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 /).
• 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
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;
};
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;
}
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.
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 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 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[][]
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]
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;
}
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 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 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 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
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
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]
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;
};
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;
}
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
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 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
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 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);
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])
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 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 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
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 /).
• 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
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’)))
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;
}
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
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 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 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 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.
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
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 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);
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
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 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;
};
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;
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 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 /).
• 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
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
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;
}
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
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;
}
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
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
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 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
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
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;
}
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 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 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
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
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)
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 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!
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;
};
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;
}
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]
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;
}
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 /).
• 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 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;
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
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.
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
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 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 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 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
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])
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;
}
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 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.
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 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
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 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;
};
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);
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
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 /).
• 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]
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 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