ProgrammingEasyTCS NQT · Infosys pattern
What is printed?
What is printed?
int a[] = {4, 8, 15, 16, 23};
printf("%zu %d", sizeof(a) / sizeof(a[0]), a[sizeof(a) / sizeof(a[0]) - 1]);- A. 20 23
- B. 5 16
- C. 5 23
- D. 4 23
Correct answer: C. 5 23
sizeof(a) is the size of the whole array (5 × sizeof(int)) and sizeof(a[0]) is one element, so the quotient is the element count, 5, whatever the size of int. The last valid index is 5 − 1 = 4, which holds 23. 20 is sizeof(a) itself with 4-byte int.
More C Programming questions
- What is printed? c printf("%d %d %d", 5 > 3 && 2 > 4, !0, !!7);
- What is printed? c int a = 5, b = 3; a = a ^ b; b = a ^ b; a = a ^ b; printf("%d %d", a, b);
- What is printed? c printf("%d %d %d", strcmp("abc", "abd") < 0, strcmp("ab", "abc") < 0, strncmp("abcx", "abcy", 3) == 0);
- What is printed? c int i = 3.99; int j = -3.99; int k = (int)(0.29 100); printf("%d %d %d", i, j, k);
- What is printed? c / 4-byte int with 4-byte alignment / struct S { int a; char b; } arr[5]; printf("%td %td", &arr[4] - &arr[1], (char…
- What is printed? c char s[] = "abcdef"; char p = s + 2; p[1] = 'X'; printf("%s %s %c %zu", s, p, (p - 2), strlen(p + 4));