GATE Computer ScienceHardGATE CS (PYQ pattern)

char s[] = "GATECS"; for (int i = 1; s[i]; i++) s[i] = 'A' + (s[i] + s[i - 1] - 2 * 'A') % 26; printf("%s", s); What is printed?

  1. A. GGTXGU
  2. B. GGZDFY
  3. C. GGZDFX
  4. D. GAZDFX

Correct answer: C. GGZDFX

Letters are 0-based (A = 0). s[i - 1] is read after it has already been updated: G stays 6; A: 0 + 6 = 6 -> G; T: 19 + 6 = 25 -> Z; E: 4 + 25 = 29 mod 26 = 3 -> D; C: 2 + 3 = 5 -> F; S: 18 + 5 = 23 -> X. Using the original neighbours instead gives GGTXGU.

More Programming and Data Structures (GATE) questions

Exams that ask this topic