--- title: MTK 題庫 description: --- # MTK 題庫 ###### tags: `Interview` * [搭配閱讀](https://hackmd.io/yo8egq8CSYOM3S688dAaMQ?view) ## 1. ```c= #include <stdio.h> typedef ____(1)____; int f1(int c) { return 1+c; } int f2(int c) { return 2+c; } int f3(int c) { return 3+c; } fi g(int c){ // return to a function pointer static fi sf[] = { f1, f2, f3 }; // declare a function pointer array return sf[c]; } int main(int argc, char* argv[]) { int result = g(argc)(argc); // 假設argc=1 , g(argc)(argc) => sf[c](argc) => f2(argc) printf("%d", result); return 0; } Ans : int (*fi)(int c) ``` ```c= Re-write void (*(*papf)[3]) (char *); typedef__________; pf(*papf)[3]; Ans: typedef void (*pf) (char *) Expalin: void (*(*papf)[3]) (char *) 原語是 declare papf as pointer to array 3 of pointer to function (pointer to char) returning void 可拆成這樣看 void (*) (char *) , (*papf)[3] ``` ## 2. ```c= #include <stdio.h> //typedef int (*fi)(int c); int f1(int c) { return 1+c; } int f2(int c) { return 2+c; } int f3(int c) { return 3+c; } int (*g(int c))(int c){ static int (*sf[3])(int c) = { f1, f2, f3 }; // prototype = int (*f[])(int) return sf[c]; } int main(int argc, char* argv[]){ int result = g(argc)(argc); printf("%d", result); return 0; } ``` ## 3. ```c= #include <stdio.h> int main(int argc, char* argv[]){ enum color{red, green, blue}; typedef enum color mycolor; mycolor m = green; printf("%d\n",m); return 0; } ``` Ans: 1 ## 4. ```c= #define SUM(a,b) a+b 請問 SUM(10,1) * 5 = ? Ans: 15 Explain: #define SUM(a,b) (a+b) // macro只是代替字,所以有括號才是正確的 SUM(10,1) * 5 = 55 ``` ## 5. ```c= #include <stdio.h> #include <stdarg.h> void fun(char *msg, ...); int main() { fun("IndiaBIX", 1, 4, 7, 11, 0); return 0; } void fun(char *msg, ...) { va_list ptr; int num; va_start(ptr, msg); // 將參數(1,4,7,11,0)放在ptr指向的一塊memory num = va_arg(ptr, int); // va_arg取參數(以int的方式取), num = 1 num = va_arg(ptr, int); // va_arg取參數(以int的方式取), num = 4 printf("%d\n", num); } Ans: 4 Explain: va_list 其實是 typedef char * va_list; va_start 是一個macro, 把取出的指標(va_list)指到第一個不定參數,在這邊就是指到1 va_arg 是一個macro, 會將 ptr 按照 第二個參數的size移動ptr ``` ## 6. ```c= #include <stdio.h> int f(int n) { return 2*n; } int main() { int n = 2; printf("%d", f(f(f(f(f(f(f(f(n)))))))) ); return 0; } ``` Ans: 512 ## 7. * Explain ```c= int* (*var1) (int *(*)(int *))[10]; ``` Ans : declare var1 as pointer to function (pointer to function (pointer to int) returning pointer to int) returning array 10 of pointer to int ## 8. ```c= int cnt = 10; const char *pc = "welcome"; while (*pc++) { cnt++; } printf("%d\n", cnt); ``` Ans: 17 ## 9. ```c= 請使用C語言的#define,實作 DAYS_OF_MONTH(n) 期中n可能為6~12,算出來的值必須為n月份的天數 Ans: #define DAYS_OF_MONTH(n) \ (n == 6 || n == 9 || n == 11) ? 30 : 31 ``` ## 10. Here is a c file with the code as below: ```c= int A; static int B; int C[10]; void func(int D) { int E; static int F; int G[10]; ... } For variables A,B,C,D,E,F,G: (1) What var are used "STACK" memory? D,E,G (2) What's the different of B and F which are declared by "static"? F屬於Func的全域變數 B屬於整個c的全域變數 ``` ## 11. ```c= unsigned long A = 0x0001111; unsigned long B = 0x0000202; unsigned long C; C = A & (~B); C = C | B; What's the value of C? ``` Ans 0x00001313 ## 12. ```c= #include <stdio.h> int main() { int ref[] = {8, 4, 0, 2}; int *ptr; int index; for (index = 0, ptr = ref; index < 2; index++, ptr++) { printf("%d %d\n", ref[index], *ptr); } (*ptr)++; printf("%d %d\n", ref[index], *ptr); return 0; } What's the above output value? Ans: 8 8 4 4 1 1 ``` ## 13. ```c= int *f1(void) { int x = 10; return (&x); // error/warning : function returns address of local variable , 實際回傳會變成 return 0; } inf *f2(void) { int *ptr; *ptr = 10; // pointer to address 10 鐵定是錯的 return ptr; } int *f3(void) { int *ptr; ptr = (int *)malloc(sizeof(int)); return ptr; } Which of the above three functions are likely to cause problem with pointers (a) Only f3 (b) Only f1, f3 (c) Only f1, f2 (d) f1, f2, f3 Ans: c ``` ## 14. ```c= Given an integer variable a, a) set the bit2 b) clear the bit2 c) inverse the bit2 (0->1, 1->0) Ans: a. a | 4 b. a & (~4) c. a ^ 4 ``` ```c= write a code a) set the specific bit b) clear the specific bit c) inverse the specific bit (0->1; 1->0) Ans: a. #define setBit(x, n) ((x) |= (1 << n)) b. #define clearBit(x, n) ((x) &= ~(1 << n)) c. #define inverseBit(x, n) ((x) ^= (1 << n)) ``` ## 15. ```c= Consider the following program: void e(int); void main() { int a; a = 0; e(a); } void e(int n) { if (n < 3) { e(++n); printf("%d", n); e(++n); } } The output for this program is: Ans: 3 2 1 3 ``` ## 16. ```c= extern void func0(void); extern void func1(void); extern void func2(void); extern void func3(void); extern void func4(void); extern void func5(void); void MAIN(int N) { if N is 0, please execute func0(void) if N is 1, please execute func1(void) if N is 2, please execute func2(void) if N is 3, please execute func3(void) if N is 4, please execute func4(void) if N is 5, please execute func5(void) } N保證有上述的6種值 請使用C實作,用你認為值型最快的方式實現 但不准用if, switch case的敘述句 Ans: void (*f[])() = {func0, func1, func2, func3, func4, func5}; f[N](); ``` * [ref](https://medium.com/@racktar7743/c%E8%AA%9E%E8%A8%80-function-pointer%E7%9A%84%E6%87%89%E7%94%A8-%E5%9B%9B-function-pointer-array-d0d624db8406) ## 17. ```c= extern void func0(void); extern void func1(void); extern void func2(void); extern void func3(void); extern void func4(void); extern void func5(void); void MAIN(int N) { if N is 33, please execute func0(void) if N is 67, please execute func1(void) if N is 194, please execute func2(void) if N is 234, please execute func3(void) if N is 321, please execute func4(void) if N is 832, please execute func5(void) } N保證有上述的6種值 請用C的if實作,無論N為何,用你認為最快的方式實現 ``` ## 18. ```c= #include <stdio.h> int count; int calc(int x) { count ++; return x > 0 ? calc(x-1) + calc(x-2) + 1 : 1; //從後面先做,也就是calc(x-2) } int main() { int result = calc(5); printf("Result %d, %d\n", result, count); return 0; } Ans: 25 25 ``` ## 19. ```c= 請寫出下列程式之輸出結果 i = 0; j = 10; for (i = 0; i < 3; i++) { j += i } printf("%d", j); Ans: 13 ``` ## 20. ```c= void main() { int x = 0; if (x = 0 || x == 0) printf("1"); if (x == 0) printf("2"); printf("3"); } what is the output on screen? Ans: 123 ``` ## 21. ```c= What are commonly put in the stack? (A) Local variables // stack (B) Global variables // .bss (C) Const data // Read Only - .data (D) Mallloced data // heap (E) Temporary storage for register contents // ??? (F) Jump address within a function // ??? (G) Function paremeters // stack (h) Function return address // stack (I) Function return value // eax ``` ## 22. ```c= What value does f() returns? int x = 5; int f() { int x = 3; { extern int x; return x; } } Ans: 5 ``` ## 23. ```c= int x; void foo(){ int y; } what is the initialized value individually? Ans: x = 0 y = indeterminate ``` ## 24. ```c= void main() { char s1[] = "MTK"; char s2[] = "MTK"; if (s1 == s2) printf("same"); else printf("different"); } Ans : different ``` ## 25. ```c= Can the code be compiled? #include <stdio.h> #define SWAP(a, b, c)(c t; t=a, a=b, b=t) int main() { int x = 10, y = 20; SWAP(x, y, int); printf("%d %d\n", x, y); return 0; } Ans: error: expected expression before ‘;’ token 10 | SWAP(x, y, int); ``` ## 26. ```c= #include <stdio.h> #define INC(x) x*=2;x+=1 int main() { int i, j; for (i = 0, j = 1; i < 5; ++i) // 沒括弧導致 x+=1 在macro展開的時候不被包在for裡 INC(j); printf("Result %d", j); return 0; } What's the output? Ans: 33 ``` ## 27. ```c= What is the output of the following code? void swap(int* x, int* y) { int* temp=x; x = y; x = temp; } void main() { int a=10; int b=20; swap(&a,&b); printf("%d %d", a, b); } Ans: 10 20 ``` ## 28. ```c= #include <stdio.h> int main(){ int goto=5; printf("%d",goto); return 0; } Ans: Compile error Explain: goto是保留字 ``` ## 29. ```c= void foo() { int i = 0; do { ++i; if (i < 3) { continue; } if (i == 5) { break; } } while(0); printf("%d\n", i); } Ans: 1 ``` ## 30. ``` What does the following declaration mean? int (*ptr)[10]; Ans: ptr is a pointer to an array 10 of integers ``` ## 31. ```c= Please use bitwise operation to swap two integer. void swap(int *x, int *y) { ....... } Ans: *x ^= *y; *y ^= *x; *x ^= *y; ``` ## 32. ```c= Given an one dimensional array of integer, please find the maxium sum of subarray. For example: Array = {2, 3, -8, 1, 2, 3}; Max subarray is {1, 2, 3}, sum = 6 input will consist of two lines, first line with single integer n indicates the number of elements in the array, the second line contains n space-seperated integers in the array Ans: int max_subarray(int *a, int n) { int max = a[0]; for (int i = 1; i < n; i++) { if (max + a[i] < a[i]) { max = a[i]; } else { max += a[i]; } if (r < max) r = max; } return max; } ``` ## 33. Explain “#error” ```c= 寫給編譯器看的,在編譯時中斷錯誤的,例如缺少定義什麼東東。 #ifndef _CPP_ #error C++ compiler required. #endif ``` ## 34. Explain “struct” and “union” * 在struct中,每個成員都有自己的記憶體空間 * 在union中,每個成員共用一個大空間(取最大size的成員空間) ## 35. Explain “volatile”. Can we use “const” and “volatile” in the same variable? Can we use “volatile” in a pointer? ## 36. ```c= int a[5] ={1,2,3,4,5}; int *p = (int *)(&a+1); ask: the value of *(a+1), (*p-1)? Ans: *(a+1) = 2 (*p-1) = a[5] (也就是一個unknown的stack位置) Explain: (int *)&a 會變成 a pointer to an array 5 of nteger 所以當 (&a+1) 的時候 p 會指向 (address of a) + (5 * sizeof(int)) 的位置 可以看成是 a[5] 的位置 ``` ## 37. (我不會延伸==) * write a code that check the input is a multiple of 3 or not without using division or mod ```c= int isMult3(unsigned int n) { if ( n == 0 ) return 1; if ( n < 3 ) return 0; n = (n >> 2) + (n & 3); /* Recursive call till you get n or a special terminate condition */ return(isMultN(n)); } ``` ```c= #include <stdlib.h> int isMult3(unsigned int n) { int odd_c = 0, even_c = 0; /* variables to count odd and even SET bits */ if (n == 0) // return true if difference is 0. return 1; if (n == 1) // return false if the difference is not 0. return 0; while (n) { if (n & 1) // odd bit is SET, increment odd_C odd_c++; n >>= 1; if (n & 1) // even bit is SET, increment even_c even_c++; n = n >> 1; } /* Recursive call till you get 0/1 */ return(isMultN(abs(odd_c - even_c))); } ``` > 這是因為 3 可以寫成 0011 而在 3 的倍數上一直加 3 ,odd_c 、 even_c 的差值會有兩種結果出現 > 差值相等 (0011+0011 = 0110) > 相差為 3n (10101+00011 = 11000) ## 38. ```c= int a[5]={1,2,3,4,5}; int *p=a; *(p++)+=123; *(++p)+=123; Ans: 124 2 126 4 5 Expalin *p++ 同等於 *(p++) ``` ## 39. * Reverse a string ```c= #include <stdio.h> #include <stdlib.h> #include <string.h> #define SWAP(x, y) do { \ char tmp = x; \ x = y; \ y = tmp; \ } while (0) void revStr(char *s) { int len = strlen(s); for (int i = 0; i < len/2; i++) { SWAP(s[i], s[len-i-1]); } } int main() { char s[] = "abcdefg"; revStr(s); printf("%s\n", s); } ``` ## 40. * 寫一個“標準”巨集MIN ,這個巨集輸入兩個參數並返回較小的一個。 ```c= # define MIN(x,y) ((x)<(y)?(x):(y)) ``` ## 41. * 寫出一個function判斷輸入的數是2的次方 ```c= int define2N(int n) { return (n&(n-1) == 0 && n != 0); } ``` ## 42. * 用預處理指令#define 聲明一個常數,用以表明1年中有多少秒(忽略閏年問題) ```c= #define SEC_PER_YEAR (60*60*24*365) ```