19 20 HK2 - C Programming Language V5 Final Version Examcode 1
19 20 HK2 - C Programming Language V5 Final Version Examcode 1
Student ID:
Quiz 1. What is the output of the following Quiz 2. What is the value of S after executing
program: the following code:
#include <stdio.h> float a[4] = {8,16};
void main(void) int S;
{ S = sizeof(a);
int n = 7;
while (n % 3 != 0)
n++;
printf("%d", n);
}
Answer #1: …9…………. Answer #2: …16………….
Quiz 3. What is the output of the following Quiz 4. What is the output of the following
program: program:
#include <stdio.h> #include <stdio.h>
void main(void) void main(void)
{ {
int a[] = { 10, 4, 5},n=0; int k = 0, a[] = { 1, 2, 3 }, b = *a;
do{ switch (b)
if (a[n] % 2 == 1) {
continue; case 0: ++k;
printf("%d ", a[n]); case 1: k += 2;
} while (++n < 3); case 2: k++; break;
} default: k += 3; break;
}
printf("%d", k);
1
}
Answer #3: …10 4……………. Answer #4: …3……….
Quiz 5. What is the output of the following Quiz 6. What is the output of the following
program: program:
#include <stdio.h> #include <stdio.h>
int main() void main(void)
{ {
int a[10] = {3, 10, 4, 5, 6, 45, 2, 23, 19, 9}, hold; int n = 2;
for (int pass = 1; pass < 10; ++pass) { for (n = 1; n <5; n += 2)
for (int i = 0; i < 9; ++i) { {
if (a[i] > a[i + 1]) { printf("%d ", ++n);
hold = a[i]; }
a[i] = a[i + 1]; }
a[i + 1] = hold;
}
}
}
printf("%d", a[0]);
return 0;
}
Answer #5: ………2……. Answer #6: …2 5………….
Quiz 7. What is the output of the following Quiz 8. What is the output of the following
program: program:
#include <stdio.h> #include <stdio.h>
void main(void) void main(void)
{ {
int n = 6; int a[6] = { 10, 15, 6, 4, 5, 45};
do for (int n = 0; n <6; n++)
{ {
printf("%d ", --n); printf("%d ", a[n]);
} while (n > 2); if (n % 2 == 0)
} break;
}
}
Answer #7: 5 4 3 2……………. Answer #8: …10………….
Quiz 9. What is the output of the following Quiz 10. What is the output of the following
program: program:
#include <stdio.h> #include <stdio.h>
void main(void) int func(int x);
2
{ void main(void)
int a = 1, b = 2, c = 3; {
if ((a <= b) && (b > c)) int a = 8, b = 9;
{ a = c - b; b = func(a);
++b; printf("%d, %d", a, b);
} }
else
{ b = a + c; int func(int x)
++a; {
b--; x--;
} return x/2;
printf("%d, %d", a, b); }
}
Answer #9: …2, 3…………. Answer #10: …8, 3………….
Quiz 11. What is the output of the following Quiz 12. What is the output of the following
program: program:
#include <stdio.h> #include <stdio.h>
int CountCharacters(const char *sPtr); int func(int *m, int *n)
int main(void) {
{ if (*m > *n) return *n;
char string[] = "C Programming Language"; return *m;
printf("%d", CountCharacters(string)); }
return 0; void main()
} {
int CountCharacters(const char *sPtr) int x = 5, y = 3;
{ printf("%d", func(&x, &y));
int t = 0; }
for (; *sPtr != '\0'; sPtr++) { a. 5
if (*sPtr == 'a') t += 1; } b. 3
return t; c. 5, 3
} d. 3, 5
a. 0 e. Other answers
b. 1
c. 3
d. a
e. Other answers
Answer #11: ……3………. Answer #12: …………….
Quiz 13. What is the output of the following Quiz 14. What is the output of the following
program: program:
#include <stdio.h> #include <stdio.h>
struct Student struct Student
3
{ {
char FL[30]; char FL[10];
char ID[30]; char ID[10];
float MidE; float MidE;
float FinalE; float FinalE;
float AP; float AP;
}; };
void main() void main()
{ {
struct Student S = {"abc","19141102",5.5,6}; struct Student S = {"abc","19141102",7,9};
S.AP += (S.MidE + S.FinalE) / 2; struct Student *Sptr;
printf("%s: %0.2f= %0.1f", S.FL, S.AP, S.AP); Sptr = &S;
} Sptr->AP += (Sptr->MidE + Sptr->FinalE) / 2;
printf("%s: %0.1f", Sptr->ID, Sptr->AP);
}
a. abc: 5.75= 5.8 a. 19141102: 8
b. abc: 5.5= 6 b. 19141102: 8.0
c. 19141102: 5.50= 6.0 c. abc: 8.0
d. Other answers d. Other answers
Answer #13: ………………. Answer #14: ……………….
Quiz 15. In the variable names as follows, which Quiz 16. Which of the following operators is a
name is TRUE according to C syntax: non-equal comparison operator?
a. -bien123 a. !
b. 12Bien b. !=
c. _172 c. /=
d. Other answers d. ~=
e. Other answers
Answer #15: ……………. Answer #16: …………….
Quiz 17. What is the output of the following Quiz 18. What is the output of the following
program: program:
#include <stdio.h> #include <stdio.h>
void func(int *n, int *m) int main()
{ {
int a; int a[5] = {1, 2, 3, 4, 5};
a = *n - 8; a=3-8 int *p1 = &a[0];
*n = *m + 7; n=4+7 int *p2 = &a[1];
*m = a; m=-5 *p1 = *p2; // 2 2 3 4 5
} *(p1 + 1) = *p1 + 1; //2 3 3 4 5
void main(void) p2+=2;
{ *(p2 - 1) = *p1 + 2;
int i, a[4] = { 2, 3, 4, 7 }; *p2 = *(p2 + 1) + 3;
4
func(&a[1], &a[2]); printf("%d, %d", a[1], a[2]);
printf("%d, %d", a[1], a[2]); return 0;
} }
a. -8, 7 a. 3, 3
b. 3, 4 b. 4, 3
c. -5, 11 c. 3, 4
d. 11, -5 d. 4, 4
e. Other answers e. Other answers
Answer #17: ……………. Answer #18: …………….
Quiz 19. What is the output of the following Quiz 20. What is the output of the following
program: program:
#include <stdio.h> #include <stdio.h>
void func(void); #include <malloc.h>
void main(void) void main(void)
{ {
func(); int *a, k;
func(); a = (int*)malloc(3*sizeof(int));
} for (k = 0; k < 3; k++)
void func(void) *(a+k) = k+1;
{ printf("%d", *(a+1));
static int x = 20; free(a);
printf("%d ", x++); }
} a. 0
a. 20 20 b. 1
b. 20 21 c. 2
c. 20 d. 3
d. 21 e. Other answers
e. Other answers
Answer #19: ……………. Answer #20: …………….