Data Structures & AlgorithmsModerateTCS NQT · Infosys pattern
What is printed? (C strings)
What is printed? (C strings)
char s[] = "Placement";
printf("%zu %zu", strlen(s), sizeof(s));- A. 9 10
- B. 9 9
- C. 10 10
- D. 9 8
Correct answer: A. 9 10
strlen counts characters before the terminating '\0': 9. sizeof on a char array includes the terminator, so the array occupies 10 bytes. For a char * pointer, sizeof would give the pointer size instead.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- What is printed? (pointer arithmetic on an int array) c int a[] = {10, 20, 30, 40, 50}; int p = a + 1; printf("%d ", (p + 2)); printf("%d…
- What is printed? (2-D array in row-major order) python R, C = 3, 4 base, size = 1000, 4 def addr(i, j, rowmajor=True): return base + size…
- What is printed? (Python string immutability and methods) python s = "Hello World" t = s.replace("o", "0", 1) u = s.swapcase()[::2]…
- What is printed? (queue simulation – hot potato with step k) python from collections import deque q = deque(range(1, 8)); out = [] while…
- What is printed? (Josephus recurrence) python def josephus(n, k): r = 0 for m in range(2, n + 1): r = (r + k) % m return r + 1…
- What is printed? (hash set for pairs with a given difference) python def pairsdiff(a, k): s = set(a) return sorted((x, x + k) for x in s…