C Quiz 2Ali KhudiyevApril 30, 2025 Welcome to your C Quiz 2 No copyright. You can use questions from this quiz freely (as long as you are not a shitty teacher or something). One rule: DO NOT GET HELP FROM THE INTERNET OR OTHER TOOLS! Author: Me. Feel free to reach out if you spot typos or errors. 1. What is the size of struct S? /** sizeof(char) = 1* sizeof(int) = sizeof(float) = 4* sizeof(double) = sizeof(T*) = 8*/struct S{ int i; char c; float f; double d; }; 16 24 17 32 None of the above None 2. What is the output of the program below? #include <stdio.h>int main(){ printf("%zu, %zu", sizeof(struct{ char c; char *s; }), sizeof(struct{ char c1; char *s; char c2; })); return 0;} 16, 16 9, 10 10, 10 16, 24 None of the above None 3. What is the output of the program below? #include <stdio.h>int main(){ printf("%zu, %zu", sizeof(struct{ double d; void *p; }*), sizeof(struct{ char c1[6]; double d; int i[3]; char c2[5]; })); return 0;} 8, 40 16, 40 16, 64 8, 64 8, 48 None 4. What is the output of the following program? #include <stdio.h>int main(){ printf("%zu, %zu", sizeof(union{ char c1; char c2; char c3; }), sizeof(union{ char c, s[2]; }), sizeof(union{ char (*s)[2], c; }); return 0;} 3, 3, 3 1, 2, 16 3, 4, 4 1, 4, 4 1, 2, 8 None 5. What is the output of the following program? #include <stdio.h>int main(){ printf("%zu, ", sizeof(struct{ char c[5]; int i[2]; union{ void *p[2]; char c[8]; } u; struct{ char c1[2], float f[2]; char c2[6]; } s; })); printf("%zu, ", sizeof(union{ char *(*pc)[4]; int (*pf[2])(void); struct{ float (*(*pf)[2])(void); char c; } s; union{ char c[4]; int i; } u; })); printf("%zu\n", sizeof(union{ struct{ int i; union{ struct{ char c; int (*i[2])[3]; } s; char c[17]; } u; char c; } s; char *(*c[3])[4][5]; })); return 0;} 60, 32, 480 52, 32, 160 56, 16, 40 52, 24, 40 60, 16, 160 None 6. Which line(s) would cause undefined behavior if uncommented? #include int main(){ // int i; if(i) printf("non-zero"); // option 1 // int *p = malloc(1); // option 2 // int *p = realloc(NULL, 0); // option 3 // int arr[3][3] = {0}; printf("%d", arr[1][5]); // option 4 return 0;} Option 1 Option 2 Option 3 Option 4 None of the above 7. What will be the output of the program below? #include <stdio.h>int main(){ int arr[] = {0, 1, 2, 3, 4, 5}; int (*arr2)[3] = (int*[3])arr; arr2[1][1] = -1; int *arr3[3] = {arr, arr+2, arr+4}; arr3[1][1] = -2; for(int i=0; i<6; ++i) printf("%d ", arr[i]);} 0, 1, 2, -2, 4, 5 0, 1, 2, -2, -1, 5 0, 1, 2, -1, -2, 5 0, 1, 2, 3, -2, 5 None of the above None 8. Which line(s) would cause undefined behavior if uncommented? #include <stdio.h>int main(){ // int i = 1; signed char c = (char)i; // option 1 // int i = 1; unsigned char c = (char)i; // option 2 // const int i = 1; *(int*)&i = 2; // option 3 // signed int i = -1; ++*(unsigned int*)&i; // option 4 return 0;} Option 1 Option 2 Option 3 Option 4 None of the above 9. Why does the executable for the program below not contain if-else statement when compiled with -O2 flag? #include <stdio.h>int randint(){ int x; return x;}int main(){ int x; scanf("&d", &x); if(x > 5) randint(); else printf("test wasn't called"); return 0;} randint function has undefined behavior None of the above None 10. Which line(s) would cause undefined behavior if uncommented? #include <stdio.h>// int i = i; // option 1int main(){ // int i = 0; int arr[3] = {i, ++i, --i}; // option 2 // long int i = (long int)&i; // option 3 // long int i = i; // option 4 // int i = 0; int j = i++ + ++i; // option 5 return 0;} Option 1 Option 2 Option 3 Option 4 Option 5 11. Which statement is true? const char *pc;int const *pi;float *const pf1;float *const **const pf2;const double *const (*pd)[2]; Only immutables are *pc, pi, pf1, pf2, *pf2, **pf2, (*pd)[0/1], *(*pd)[0/1] Only mutables are pc, *pi, *pf1, **pf2, ***pf2, pd Only immutables are *pc, pi, pf1, pf2, **pf2, (*pd)[0], *(*pd)[0] Only mutables are pc, pi, *pf1, *pf2, ***pf2, pd None of the above None 12. Which statements are true about the "static" keyword? #include static int myint = 0;static getg_myint(){ return myint; }int main(){ static int myint = 1, myint2 = 2; return 0;} getg_myint() function call can be made from other C translation units getg_myint() function call cannot be made from other translation units myint is a static variable, myint2 is not myint and myint2 are static variables Global myint variable can only be read inside main() by calling getg_myint() 13. Which line(s) would cause compilation error if uncommented? #include <stdio.h>// extern int i = 0; // option 1// extern int foo(char); // option 2int main(){ // extern int var; // option 3 // extern int bar(); // option 4} Option 1 Option 2 Option 3 Option 4 None of the above 14. What does the inline keyword do? inline int add(int a, int b){ return a+b; } Nothing extra, it is just a syntactic sugar Gives error when compiled with >0 optimization Gives error when compiled with no optimization Avoid subroutine call in assembly by adding the inline definition to the caller None of the above 15. What is the difference between the "volatile" and "restrict" type qualifiers? #include <stdio.h>int main(){ volatile int *pi = 0; restrict char *pc = 0; const volatile void *restrict p = 0; return 0;} There is no difference between the two type qualifiers Compiler won't reorder read/write operations on pi Compiler won't reorder read/write operations on pc "const volatile" doesn't make any sense There could be no other alias for the memory segment pc points to 16. What is the purpose of using extern "C" in a C++ program below? extern "C" void test();int main(){ test(); return 0;} It forces C++ compiler to keep C-like function signature It forces C compiler to keep C++-like function signature It is just a decorator for code readability It causes compile time error when compiled with a C++ compiler None of the above None 17. What will be the output of program below? #include // sizeof(char) = 1, sizeof(short) = 2, sizeof(int) = 4, sizeof(T*) = 8int main(){ int i = 0x01234567; printf("%d, %d", *(char*)&i, *((short*)&i+2)); return 0;} (Little Endian) 0x67, 0x0123 (Big Endian) 0x01, 0x6745 (Little Endian) 0x67, 0x2301 (Big Endian) 0x01, 0x4567 None of the above 18. What will be the output of the program below (assuming little endianness)? #include // sizeof(char) = 1, sizeof(short) = 2, sizeof(int) = 4, sizeof(int*) = sizeof(long int) = 8int main(){ int i = 0x01234567, j = 0xaabbccdd; struct{ int i; int *p; } s = { .i=0xdeadbeef, .p=(int*)0xcafebabefee1f00d }; printf("%d, %ld", *(int*)((short*)&j+1), *(long int*)((unsigned long long)&s.p-6)); return 0;} 0xdeadbeef, 0x0000deadbeefcafe 0x01234567, 0xdeadbeef 0x4567aabb, 0xf00d00000000dead 0xdeadbeef, 0xccdddeadbeefcafe None of the above None 19. What will be the ouput of the program below? #include // sizeof(signed int) = sizeof(unsigned int) = 4int main(){ if(1 && 2) printf("1"); if(1 & 2) printf("2"); if((signed)-1 >> 31 == 0xffffffff) printf("3"); if((unsigned)-1 >> 31 == 1) printf("4"); return 0;} 124 134 1234 1 12 None 20. What will the output of the program below? #include <stdio.h>int main(){ if(1 || *(int*)1) printf("success\n"); else printf("failure\n"); if(*(int*)1 || 1) printf("success\n"); else printf("failure\n"); return 0;} success success failure failure SIGSEGV success SIGSEGV success failure None Time's up