Tải bản đầy đủ (.pdf) (15 trang)

Đề thi học kì 1 môn Kỹ thuật lập trình năm 2017-2018 - Trường Đại học Bách Khoa TP. Hồ Chí Minh (Final exam semester 1 of Programming Fundamentals)

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (377.96 KB, 15 trang )

FINAL EXAMINATION
Course: Programming Fundamentals

CourseID: CO1013

Date: Tuesday, January 09, 2018
Time: 90 minutes
Type: closed-book examination with no electronic equipment

Exam Code:

Student Name:

Student Identifier:

F17101

General information:
-

-

-

This examination includes three parts. Part A consists of 24 multiple choice questions
each of which has a max grade of 0.25. Part B consists of 3 writing questions each of
which has a max grade of 2.0. Part C is your answer area.
For the questions in Part A, please use a mark (check or cross-check) for an answer with
a, b, c, or d. If you have another answer that does not match any among a, b, c, and d,
please choose e. Others and then write your answer in the Others answer section. If you
make another choice, please delete your previous choice by marking a deletion on it. If


you choose an answer and then delete it multiple times, please write your final answer to
that question explicitly in Part C.
For the questions in Part B, programs in your answers must be written in the C language
and the coding style must be considered.
For any comments or suggestions on the examination, please feel free to write them down
at the end of Part C.

Part A. Multiple choice questions
Q.1. Which statement is correct about C?
a. C is a high-level typed programming language using a compiler for code translation.
b. Source codes written in C can be run on any machine with at least one main() function.
c. Anybody can create a C program for his/her own purpose on a computer.
d. Answers a, b, and c are all correct.
e. Others
Q.2. Which declaration is valid for a variable that keeps the number of students in class?
a. char number[5];

b. unsigned int number;

c. int *number;

d. char *number;
1


e. Others
Q.3. Which identifier is valid for a variable that keeps a floating-point number for a
grade?
a. float grade_2;


b. float 2_grade;

c. float _@grade;

d. float &_grade;

e. Others
Q.4. Which data type has the largest range with the smallest minimum value and the
largest maximum value for its variables?
a. char

b. unsigned int

c. int

d. long int

e. Others
Q.5. Given variables: aPtr, aVal, and anExp, which expression is incorrect?
a. ('A' + 32)^1.5
b. ('ptr' + 's')*2 - &aPtr
c. (anExp/'a' - *aPtr)*aVal + 8
d. (*&aVal + 2)/anExp*10-'2'
e. Others
Q.6. Which function call allows receiving a character value and a double value in
sequence input by a user from the keyboard?
a. scanf("%c%f", 'a', &1.23456);

c. scanf("%c%lf", &var1, &var2);


b. scanf("%f%c", &var1, 'a');

d. scanf("%lf %c", &var1, &var2);

e. Others
Q.7. Which function call allows printing a positive number 123 with a plus sign and left
justification?
a. printf("%+123d");

c. printf("%-+d", 123);

b. printf("%+d", +123);

d. printf("%-+f", 123);

e. Others

2


Q.8. Given the following statements, what is printed on screen if input is -2?
int aMin = 5, input;
aMin>input?printf("less

than

aMin"):printf("greater

than


or

equal

to

aMin");

a. "less than aMin"

c. "greater than or equal to aMin"

b. printf("less than aMin")

d. printf("greater than or equal to aMin")

e. Others
Q.9. Given the following statements, what is printed on screen?
int a=10, b=5, c=0;
if (b+cif (a-belse printf("&&&");
else printf("###");
a. ***

c. ###

b. &&&

d. Nothing on screen as the statement is incorrect.


e. Others
Q.10. Given a switch statement as follows, what is printed on screen?
int month = 4;
switch (month) {
case 2: printf("28 or 29 days"); break;
case 4: case 6: case 9: case 11: printf("30 days"); break;
case 1: case 3: case 5: case 7: case 8: case 10: case 12: printf("31 days"); break;
default: printf("invalid");
}
a. "28 or 29 days"
3


b. "30 days"
c. "31 days"
d. "invalid"
e. Others
Q.11. Given the following statements, what should be written at (11)…………… for a
sum of all the even numbers less than N?
int i, N=100, sum=0;
for (i=0; i<=N; i++) (11)……………
a. if (2*i
c. sum += 2*i;

b. if (2*i
d. sum += i;


e. Others
Q.12. Given the following statements, what is printed on screen?
int j, M=20;
for (j=M; j>=2; j-=2) {
if (j%2 || j%5) continue;
printf("%d\t", j);
}
a. 18 16

14

12

b. 20 15

10

5

8

6

4

2

c. 20

10


d. 20

10

0

e. Others
Q.13. Which statement creates an infinite loop? Using: int i=0, j=0;
a. while (i==j) i++;

c. do j++; while (i=j);

b. do i++, j++; while (i!=j);

d. for (; i>=j; i++, j+=2);

e. Others

4


Q.14. Given the following function, which statement is correct about this function?
int getPower10 (int pow) {
if (pow>0) return 10*getPower10(pow-1);
return 1;
}
a. getPower10 has two parameters: one for its input and another for its output.
b. getPower10 is a recursive function with one parameter of the int data type.
c. getPower10 is defined to calculate the 10-th power of pow. If pow is zero, it returns 1.

Otherwise, it returns 10pow.
d. Answers a, b, and c are all incorrect.
e. Others
Q.15. Given the following function using the aforementioned getPower10 function,
which function call is valid?
int getDigit (int number, int position) {
if (number<0) number = -number;
int power = getPower10 (position-1);
if (position<=0 || power>number) return -1;
return (number/power)%10;
}
a. int pos = 3, num = 12345, digit = getDigit (num, pos)
b. int pos = 0, num = 12345, digit = getDigit (num, &pos);
c. int pos = 3, num = 12345, digit = getDigit (*pos, num);
d. int pos = 0, num = 12345, digit = getDigit (num);
e. Others

5


Q.16. Given the following function for extracting the character at the middle position in a
string. If the length of a given string is even, the character with a smaller index at the left
side is returned. If the character is a letter, it is returned in upper case.
char getMiddle (char aString[]) {
………………
}
Rearrange the following code segments into a sequence from the left to the right so that
we can obtain a valid definition of the aforementioned getMiddle function.
(i).


if (aMiddleChar>='a' && aMiddleChar<='z') return aMiddleChar-32;

(ii).

int length = strlen (aString);
char aMiddleChar;

(iii).

if (length%2) aMiddleChar = aString[length/2];
else aMiddleChar = aString[length/2-1];

(iv).

return aMiddleChar;

a. (iii), (iv), (ii), (i)

c. (ii), (i), (iii), (iv)

b. (ii), (iii), (i), (iv)

d. (ii), (iii), (iv), (i)

e. Others
Q.17. Given the following statements, how many bytes were allocated for the array arr?
char arr[][5] = {{'a', 'c'}, {'b', 'k', 'n'}, {'q', 's', 'r', '0'}};
a. 5 bytes

c. 15 bytes


b. 9 bytes

d. Unknown due to a lack of details

e. Others
Q.18. Which character is returned with access to arr in Q.17 as: arr[1][2]?
a. 'c'

b. 'k'

c. 'n'

d. '\0'

e. Others

6


Q.19. Given the following function for checking the symmetric property of a onedimensional array of integer numbers, complete the source code at (19) ………… for a
valid definition. If the array is symmetric, the function returns 1. Otherwise, it returns 0.
int isSymmetric (int arr[], int n) {
int i=0, j=n-1;
for (; i<=j; i++, j--)
(19) …………………
return 1;
}
a. if (arr[i] == arr[j]) return 0;


c. if (arr[i] == arr[j]) return 1;

b. if (arr[i] != arr[j]) return 1;

d. if (arr[i] != arr[j]) return 0;

e. Others
Q.20. Given the following program, what is printed on screen?
#include <stdio.h>
int dotProduct (int a[], int b[], int n) {
int i=0, sum=0;
for (; ireturn sum;
}
void main() {
int r1 = 2, c1 = 3, r2 = 3, c2 = 2;
int m1[2][3] = {{1, 1, 1}, {2, 2, 2}};
int m2[3][2] = {{0, 1}, {1, 0}, {1, 1}};
int m[2][2];
int i, j;
7


for (i=0; ifor (j=0; jint col2[r2], k;
for (k=0; km[i][j] = dotProduct (m1[i], col2, r2);
}
for (i=0; i

for (j=0; jprintf ("%d\t", m[i][j]);
printf ("\n");
}
}
a.

1

1

1

2

2

2

0

1

1

0

1

1


e. Others

b.

c.

d.

1

1

2

2

2

2

4

4

0

1

1


0

Q.21. Given the following statements, place the address operator (&) appropriately into
(1) ……, (2) ……, and/or (3) …… so that we can get valid declarations.
float aFloat = 5.5;
float *pFloat = (1)……… aFloat;
float **ppFloat = (2)………pFloat;
void *pVoid = (3)……….pFloat;

8


a. (1), (2)

b. (1), (3)

c. (2), (3)

d. (1), (2), (3)

e. Others
Q.22. Given the following statements, what is printed on screen?
int Int1 = 5, Int2 = 2;
int *pInt1 = NULL, *pInt2 = NULL;
int **ppInt = &pInt1;
pInt1 = &Int2;
pInt2 = *ppInt;
pInt1 = &Int1;
*pInt1 += 2;

*pInt2 -= 3;
printf ("%d\t%d", Int1, Int2);
a. 5

2

b. 7

-1

c. 2

4

d. 4

2

e. Others
Q.23. Given the following function, what are the values of the variables f, g, p after the
function call func (…)?
void func (int *a, int *b, int *c) {
int t = *a;
*a = *b + 1;
*b = *c + 1;
*c = t + 1;
}
int f = 7, g = 11, p = 3;
func (&p, &f, &g);


9


a. f=7, g=11, p=3

b. f=12, g=4, p=8

c. f=1, g=1, p=1

d. Unknown values

e. Others
Q.24. Given the following function calls, which ones are used correctly to read a
character and an integer number from a formatted text file controlled by pFile into
variables aChar and anInteger, respectively?
(i). fputc (&aChar, pFile);
(ii). aChar = fgetc (pFile);
(iii). fscanf (pFile, “%d”, &anInteger);
(iv). fread (anInteger, sizeof (int), 1, pFile);
a. (i), (ii), (iii), (iv)

b. (i), (iv)

c. (ii), (iv)

d. (ii), (iii)

e. Others

Part B. Writing questions

Q.25. Given the following program, you are asked to discuss memory leaks caused in its
execution. Determine which memory locations get uncontrolled. (2 points)

10


Q.26. Using pseudo codes, write a program to recommend the k nearest locations to each
tourist from a given list of n locations. Each tourist is recorded via his/her location. A
location is represented as a vector (x, y) in a 2-dimensional space. Distance between two
locations L1 = (x1, y1) and L2 = (x2, y2) is computed using the Euclidean distance: d(L1,
L2) = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)). Modularization is required, i.e. at least one
function is defined in this program. (2 points)
Input: a given list of n locations, tourist’s location Lq, the number k of the nearest
locations for recommendation
Output: k nearest locations with respect to Lq
Q.27. Write a corresponding C program of the program in the previous question Q.26.
Modularization is required, i.e. at least one function different from the main () function is
defined in this program. (2 points)

Faculty/Department:

Lecturer: Dr. Vo Thi Ngoc Chau

11


Name: ………………………………………………………………………. ID: ………………………………… Exam Code: F17101
Part C. Answer section
C.1. Answers for the multiple choice questions:
Answer 1


2

3

4

5

6

7

8

9

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

a
b
c
d
Others

C.2. Area for writing the Others answers of the multiple choice questions (1-24) that are
different from a, b, c, and d choices and the answers of the writing questions (25-27):

12



13


14


15



×