Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Lecturer: Đặng Lê (Date) 15/08/2022 Approved by: (Date)

Quang
(Signature and Fullname) (Signature and Fullname)

Semester / Academic year 3 2021-2022


FINAL EXAM
Date 21-08-2022
Course title Introduction to Programming
UNIVERSITY OF TECHNOLOGY - VNUHCM Course ID CO1003
FACULTY OF CSE Duration 90 minutes Question sheet code 2763
Notes: - Students are only allowed to use 2 sheets of A4 handwritten paper.
- Do not use PCs, laptops and other electrical devices

a*= 3 : a = a*3
I. Multiple choice(6.0 pts)
a = 1;
a += 4 : a = a+4 = 5

1. Given the following command line, what value will be 3. The value of M after running the program is:
printed to the screen?
1 # include <s t d i o . h>
1 int x=5 , y =10; Tạo ra 2 biến nguyên là x và y với x = 5 và y= 10 2 int main ( )
2 float z ; tạo ra biến số thực z 3 {
3 z=( float ) x/y ; cho biến thực z = x/y = 5/10 = 1/2 = 0.5 4 int i =1 ,M=1 ,S=1; tạo biến số nguyên
4 p r i n t f ( " %.2 f " , z ) ; in ra biến số thực z :%f và phần .2 để in ra 2 số sau 5 while ( i <=3) vòng lặp while (<điều kiện>) : lặp đến khi nào điều
6 { kiện vẫn đúng
dấu phẩy
7 S+=M; i s m
8 M∗= i ; 1 2 1
(a) 0.00
9 i ++; 2 3 2
(b) 0.50 10 } 3 5 6
11 return 0 ; 4 x x
(c) 1/2 12 }
(d) All answers are incorrect
(a) 8
(b) 5
(c) 4
(d) 6

4. If a variable is a pointer to a structure, then which of


the following operator is used to access data members
of the structure through the pointer variable?
(a) . x

(b) & return the address of anything

(c) ->
(d) * pointer to a variable : cái gì đó không phải structure

5. A mode which is used to open an existing file for read-


2. Given the following command line, what value will be ing
printed to the screen?
(a) w writing
1 # include <s t d i o . h> khai báo thư viện
x<y = 10<9 = 0 (b) w+ writing again after reading
2
x>y = 10>9 = 1
3 int main ( ) khai báo chương trình chính
4 {
++x : cộng x trước khi chạy lệnh (c) a+
5 int x=10 , y =9; 2 biến số nguyên y-- : trừ y sau khi chạy lệnh
(d) r
6 p r i n t f ( " % d % d % d % d " , x<y , x+y,++x , y−−) ;
r+ reading again after writing
7 return 0 ;
8 } 6. which one is not built-in datatype of the C program-
%d : in ra biến số nguyên
ming language kiểu dữ liệu nào không có sẵn trong c ?
(a) 1 19 11 9 (a) char kiểu dữ liệu lưu trữ 1 kí tự duy nhất

(b) 0 19 10 9 (b) short int kiểu số nguyên nhưng bé hơn int

(c) 0 19 11 8 (c) float kiểu số thực

(d) 0 19 11 9 (d) array không có trong C

Stu. ID: .......................................... Stu. Fullname: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Page: 1/6


7. What will be printed to the screen with the following 11. What will be printed to the screen with the following
program? program? nhà của a: 12/56 Hoàng Hoa Thám - mặc áo xanh có số 2
1 # include <s t d i o . h> 1 # include <s t d i o . h> a : áo xanh số 2
2 int f u n c t i o n 1 ( int n ) ; 2 void f u n c t i o n 1 ( int a , int b ) ;
&a : 12/56 ...
3 int main ( ) chương trình chính luôn được chạy đầu tiên 3 void f u n c t i o n 2 ( int ∗a , int ∗b ) ;
int *e;
4 { 4 int main ( ) chương trình chính
*e = &a;
5 int n=5 ,M; khai báo số nguyên 5 { e >> a : áo xanh số 2
6 M=f u n c t i o n 1 ( n ) ; M = giá trị của chương trình con trả về : function1(5) 6 int a =1 ,b=2 , c =3 ,d=4; khai báo
printf("%d", e); >> áo xanh số 2
7 p r i n t f ( " % d " ,M) ; =>> M = 9 7 function1 (a , b) ; a = 2; b = 1; printf("%d", *e); >> 12/56
8 return 0 ; 8 p r i n t f ( " a = %d , b = %d , c = %d , d = % d " , a , b
9 } , c , d) ;
10 int f u n c t i o n 1 ( int n ) chương trình con trả về integer, lấy vào 1 integer 9 f u n c t i o n 2 (&c ,&d ) ; c = 4; d= 3;
11 { 10 return 0 ;
n function1 &c : trả về địa chỉ của c
12 if ( n==1) 11 }
5 + function1(3)
13 return 1 ; trả về số 1 function1(5) 5 12 void f u n c t i o n 1 ( int a , int b ) trả về : X - input : 2 integer
14 return n+f u n c t i o n 1 ( n−2) function1(3)
; 3 3 + function1(1) 13 {
15 } trả về hàm function1(1) 1 1 14 int temp ;
15 temp = a ; temp = 1
16 a = b; a=2
(a) 6 17 b = temp ; b = 1
18 }
(b) 16 19 void f u n c t i o n 2 ( int ∗a , int ∗b ) trả về : X - input : 2 pointer
20 { 34H 12C
(c) 4
21 int temp ;
(d) 9 22 temp = ∗ a ; temp = 34H
23 ∗ a = ∗b ; a = 12C
24 ∗b = temp ; b = 34H
25 }

8. What is wrong about the structure in C language? (a) a=1, b=2, c=3, d=4
(a) A structure is a collection of elements that can (b) a=2, b=1, c=4, d=3
be of same datatype (c) a=1, b=2, c=4, d=3
(b) A structure is a collection of elements that can- (d) a=2, b=1, c=3, d=4
not be of different datatype
12. Given the following command line, what will be printed
(c) Elements of a structure are called members to the screen?
1 # include <s t d i o . h>
2 int main ( ) i a[i] Dk1 DK2 DK3
3 { 0 1 T F F
4 int a [ 5 ] = { 1 , 4 , 2 0 , 1 7 , 2 9 } ; 1 4 T T T
9. All keywords in C are in ... 5 int i ; 2 20 F T T
6 int Min=2 ,Max=5;
(a) Lower-Case letters 7 for ( i =0; i <5; i ++) 3 17 F F T
8 { 4 29 F F T
(b) Upper-Case letters 9 if ( ( a [ i ]<=Max) | | ( ( a [ i ]%2)==0) | | ( a [ i ]>=
(c) Camel-Case letters Min ) )
10 { dau hoac
(d) None of the mentioned 11 p r i n t f ( "%d\t" , a [ i ] ) ;
12 break ; ngat thang for, while
13 }
14 }
15 p r i n t f ( " done " ) ;
16 return 0 ;
10. Give the following program 17 }
1 # include <s t d i o . h>
i++ : 0, 1, 2 - 3
2 int main ( ) (a) 1 done
3 { ++i : 1, 2 - 3
4 int i =1; ++i (b) 1 4 20 17 29 done
5 for ( i =0; i <3; i ++) vòng lặp for
6 { (c) 4 20 17 29 done
7 p r i n t f ( "%d\t" , i ) ;
\t : cách
(d) done
8 }
9 return 0 ; i = 3 13. What value does the following command output to the
10 }
screen?
What will be printed to the screen with the following 1 p r i n t f ( " |%06.0 f | " , 1 2 3 . 1 2 3 ) ;
program?
(a) 012 (a) |123000|
(b) 0\t1 2 3 (b) |000123|
(c) 12 (c) |0123.1|
(d) 123 (d) All answers are incorrect

Stu. ID: .......................................... Stu. Fullname: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Page: 2/6


14. Given the following program, what will be printed to 18. The first and second arguments of fopen() are
the screen?
(a) A character string containing the name of the
1 # include <s t d i o . h> user and the second argument is the mode
2 int main ( )
3 { (b) A character string containing file pointer and
4 int c h o o s e _ c a s e = 5 ; the second argument is the mode
5 switch ( c h o o s e _ c a s e )
6 { (c) A character string containing the name of the
7 case 1 : file and the second argument is the mode
8 p r i n t f ( "1" ) ;
9 break ; (d) None of the mentioned
10 case 2 :
11 p r i n t f ( "2" ) ;
12 break ;
13 default : nhung TH co`n lai 19.
14 p r i n t f ( "3" ) ; 1 # include <s t d i o . h>
15 break ; 2 int f u n c t i o n 1 ( int a , int b ) ;
16 } 3 int main ( )
17 return 0 ; 4 {
18 } 5 int x = 4 , y =10;
6 p r i n t f ( "%d" , f u n c t i o n 1 ( x , y ) ) ;
7 return 0 ;
(a) 1 8 }
9 int f u n c t i o n 1 ( int a , int b )
(b) 2 10 { neu a so le? thi tinh
(c) 3 11 if ( a %2!=0) tong
12 {
(d) Do not print anything 13 return a+b ;
14 } neu a so chan~ thi tinh
15 else hieu
16 {
15. Given the following command line, what value will be 17 return a−b ;
printed to the screen? 18 }
i a[i] Th1 Th2 TH3 19 }
1 # include <s t d i o . h>
2 int main ( ) 01 T T F
3 { The above program will print to the screen
1 4 T F T
4 int a [ 5 ] = { 1 , 4 , 2 0 , 1 7 , 2 9 } ;
5 int i ;
2 20 F F T (a) 15
6 int Min=2 ,Max=5; 3 17 F T T (b) 3
7 for ( i =0; i <5; i ++) 4 29 F T T
8 { (c) -6
9 if ( ( a [ i ]<=Max) | | ( ( a [ i ]%2) !=0)&&(a [ i ]>=
Min ) ) (d) Do not print anything
10 p r i n t f ( "%d\t" , a [ i ] ) ;
11 }
12 return 0 ;
13 } 20. Which of the following array declarations is incor-
rect?
(a) 1 4 20 17 29 (a) int a[5]; khai báo mảng số nguyên có 5 phần tử (sizeof(int) * 5)
int : 4 bytes
(b) 4 20 17 29 char : 1 byte (b) int a[2] = malloc(2 * sizeof(2)); sai
float : 4 bytes
(c) 14 (c) int a[] = {8,2,6}; khai báo mảng số nguyên có các phần tử như
sau
(d) 1 4 17 29 (d) int* a = malloc(2 * sizeof(2));
khai báo con trỏ a chỉ về 1 mảng số nguyên gồm 2 phần tử là số nguyên
malloc : memory allocation (malloc chỉ đi chung với pointer)

16. Give a 1D array a[]={1,3,6,8,9}


21. What will be printed to the screen with the following
(a) a[0]=1 a[4]=9 program?
(b) a[0]=3 a[4]=9 1 # include <s t d i o . h>
sizeof cua char: do lon' =1
2 # include < s t r i n g . h>
(c) a[0]=3 a[5]=9 3 int main ( ) strlen: do dai` cua chuoi~ ky tu
(d) All answers are incorrect 4 {
5 char s t r N [ 2 0 ] = " C Programming " ;
6 p r i n t f ( " % ld % ld " , sizeof ( s t r N ) , s t r l e n ( s t r N ) )
;
17. Which of the following is true about the C programming 7 return 0 ;
language? 8 }

(a) Less understandable by computers


(b) Translator program is called compiler (a) 19 13
(c) The C language is a high-level (b) 21 13
language that needs a compiler. (c) 22 13
(d) All answers are correct (d) All answers are incorrect

Stu. ID: .......................................... Stu. Fullname: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Page: 3/6


22. The operator used to get value at address stored in a 24. What will be printed to the screen with the following
pointer variable is program?
(a) * 1 # include <s t d i o . h>
2 # include <math . h>
(b) & 3 struct P o i n t {
4 int x ;
(c) &&
5 int y ;
(d) || 6 int z ;
7 };
23. The value of S after running the program is: 8 int main ( )
9 {
1 # include <s t d i o . h> S=2 10 struct P o i n t A={ −1 ,1 ,1} ,B= { 1 , 1 , 1 } ;
2 int main ( ) 11 float l e n g t h ;
3 { M=2
12 l e n g t h = s q r t ( pow (B . x−A. x , 2 )+pow (B . y−A. y , 2 )
4 int i =1 ,M=1 ,S=1; i=2 +pow (B . z−A. z , 2 ) ) ;
5 while ( i <=3) 13 p r i n t f ( " %.2 f " , l e n g t h ) ;
6 { S=4 14 return 0 ;
7 S+=M; 15 }
8 M∗=2;
M4
9 i ++; i=3
10 } (a) 2
11 return 0 ; S=8
12 }
M=8 (b) 4.00
i=4 (c) 2.00
(a) 8 (d) 6
(b) 6
(c) 5
(d) All answers are incorrect

II. Constructive Questions (4 pts)

25. - Write a complete program with the following requirements


(0.5 pts) a) sum all even elements of the 1-D array.
(0.5 pts) b) sort the elements of the 1-D array in order from smallest to largest.
(0.5 pts) c) Print the index and the value of the odd elements of the array.

1 # include <s t d i o . h>


2
3 int main ( )
4 {
5 int a [ 7 ] = { 7 , 6 , 5 , 4 , 3 , 2 , 1 } ;
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

Stu. ID: .......................................... Stu. Fullname: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Page: 4/6


37
38
39
40
41
42
43
44
45
46 return 0 ;
47 }

26. HCMUT-VNU students have a set of experimental data stored in a file showing pressure at several locations along the
nozzle.
Write a program using the C language to perform the following tasks
(0.5 pts) a) Write code to import data from file including ID (int), position along nozzle (float) and pressure value
(float). The structure of file will be showed as below

(0.5 pts) b) Create an array of struct of students to store all data.


(1.0 pts) c) sort the elements of the struct array from small to large positions along the nozzle and calculate average
pressure of data-set
(0.5 pts) d) Print to the screen positions where pressure value larger than the average pressure.
1 // start code
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

Stu. ID: .......................................... Stu. Fullname: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Page: 5/6


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75 // end code

Stu. ID: .......................................... Stu. Fullname: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Page: 6/6


Solution 2763 2022
I. Multiple choice(6.0 pts)

1. (b) 13. (b)


2. (d) 14. (c)

3. (d) 15. (d)


4. (c) 16. (a)
5. (d) 17. (d)
6. (c) 18. (c)

7. (d) 19. (c)


8. (b) 20. (b)
9. (a) 21. (d)

10. (a) 22. (a)


11. (a) 23. (a)
12. (a) 24. (c)

II. Constructive Questions (4 pts)


25.
1 # include <s t d i o . h>
2
3 int main ( )
4 {
5 int a [ 7 ] = { 7 , 6 , 5 , 4 , 3 , 2 , 1 } ;
6 int i , j ;
7 // a ) sum all even elements of the 1 - D array
8 int sum=0;
9 for ( i =0; i <7; i ++)
10 {
11 if ( a [ i ]%2==0)
12 {
13 sum+=a [ i ] ;
14 }
15
16 }
17 p r i n t f ( " % d \ n " , sum ) ;
18 // b ) sort the elements of the 1 - D array in order from smallest to largest
19 int temp ;
20 for ( i =0; i <6; i ++)
21 {
22 for ( j=i +1; j <7; j ++)
23 {
24 if ( a [ i ]>a [ j ] )
25 {
26 temp=a [ i ] ;
27 a [ i ]= a [ j ] ;
28 a [ j ]=temp ;
29 }
30 }
31 }
32 for ( i =0; i <7; i ++)
33 {
34 p r i n t f ( "%d\t" , a [ i ] ) ;
35 }
36 p r i n t f ( "\n" ) ;
37 // c ) Print the index and the value of the odd elements of the array .
38 for ( i =0; i <7; i ++)
39 {
40 if ( a [ i ]%2!=0)
41 {
42 p r i n t f ( "%d %d\t" , i , a [ i ] ) ;
43 }

Stu. ID: .......................................... Stu. Fullname: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Page: 1/6


44
45 }
46 return 0 ;
47 }

26.
1 # include <s t d i o . h>
2 # define SIZE 50
3 // b ) Create an array of struct of students to store all data .
4 struct Experimental_Data
5 {
6 int i d ;
7 float p o s i t i o n ;
8 float p r e s s u r e ;
9 }
10 int main ( )
11 {
12 int i d [ SIZE ] ;
13 float g1 [ SIZE ] , g2 [ SIZE ] ;
14 int i , j , n = 0 ;
15 float avg , sum=0;
16 /* a ) Write code to import data from file including
17 ID ( int ) , position along nozzle ( float ) and pressure value ( float ) .
18 The structure of file will be showed as below */
19 // openfile
20 FILE ∗ p F i l e = NULL;
21 p F i l e = f o p e n ( " Data . txt " , " r " ) ;
22 if ( p F i l e == NULL) {
23 p r i n t f ( " \ nUnable to open file " ) ;
24 return 0 ;
25 }
26 // read Data
27 while ( f s c a n f ( p F i l e , " % d % f % f " ,& i d [ n ] ,& g1 [ n ] ,& g2 [ n ] ) >0)
28 {
29 if ( i d [ n]==’ ’ ) continue ;
30 n++;
31 }
32
33 // b ) Create an array of struct of students to store all data .
34 struct Experimental_Data Exp_data [ n ] ;
35 for ( i =0; i <n ; i ++)
36 {
37 Exp_data [ i ] . i d=i d [ i ] ;
38 Exp_data [ i ] . p o s i t i o n=g1 [ i ] ;
39 Exp_data [ i ] . p r e s s u r e=g2 [ i ] ;
40 }
41
42 /* c ) sort the elements of the struct array from small to large positions along the nozzle
43 and calculate average pressure of data - set */
44 for ( i =0; i <n−1; i ++)
45 {
46 for ( j=i +1; j <n ; j ++)
47 {
48 if ( Exp_data [ i ] . p o s i t i o n >Exp_data [ j ] . p o s i t i o n )
49 {
50 int temp=Exp_data [ i ] . p o s i t i o n ;
51 Exp_data [ i ] . p o s i t i o n=Exp_data [ j ] . p o s i t i o n ;
52 Exp_data [ j ] . p o s i t i o n=temp ;
53 }
54 }
55 }
56 for ( i =0; i <n ; i ++)
57 {
58 sum+=sum ;
59 }
60 avg = ( float ) sum/n ;
61
62 /* d ) Print to the screen positions where pressure value larger than the average pressure . */
63 for ( i =0; i <n ; i ++)
64 {
65 if ( Exp_data [ i ] . p r e s s u r e >=avg )
66 {
67 p r i n t f ( " % f " , Exp_data [ i ] . p o s i t i o n ) ;
68 }
69 }
70 f c l o s e ( pFile ) ;
71 return 0 ;
72 }

Stu. ID: .......................................... Stu. Fullname: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Page: 2/6

You might also like