Data Structures & AlgorithmsModerateTCS NQT · Infosys pattern
What is printed? (pointer arithmetic on an int array)
What is printed? (pointer arithmetic on an int array)
int a[] = {10, 20, 30, 40, 50};
int *p = a + 1;
printf("%d ", *(p + 2));
printf("%d ", *p++ + 2);
printf("%d", *p);- A. 40 32 30
- B. 40 22 30
- C. 30 22 30
- D. 40 22 20
Correct answer: B. 40 22 30
p points to a[1] = 20. *(p + 2) is a[3] = 40. *p++ + 2 reads *p (20) then advances p, so it prints 22, and p now points to a[2] = 30. Postfix ++ binds tighter than *, but the dereference uses the old pointer value.
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? (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…
- What is printed? (Python dict ordering and update) python d = {"b": 1, "a": 2} d["c"] = 3 d["b"] = 4 del d["a"] d["a"] = 5…