ProgrammingModerateTCS NQT · Infosys
What is printed?
What is printed?
int cnt[26] = {0};
for (const char *p = "banana"; *p; p++) cnt[*p - 'a']++;
int best = 0;
for (int i = 1; i < 26; i++) if (cnt[i] > cnt[best]) best = i;
printf("%c%d", 'a' + best, cnt[best]);- A. a6
- B. b1
- C. a3
- D. n2
Correct answer: C. a3
Subtracting 'a' turns each letter into an index 0 to 25 (valid because the lower-case letters are consecutive in ASCII). banana has a × 3, n × 2, b × 1, so the most frequent letter is a with count 3.
More C Programming questions
- In a binary search over int indices, why is mid = lo + (hi - lo) / 2 preferred to mid = (lo + hi) / 2?
- What is printed? c int A[2][2] = {{1, 2}, {3, 4}}, B[2][2] = {{0, 1}, {1, 0}}, C[2][2] = {0}; for (int i = 0; i < 2; i++) for (int j = 0…
- When should a variable be declared volatile?
- What is printed? c void f(int a, int n) { if (n <= 1) return; a[n - 2] += a[n - 1]; f(a, n - 1); } int main(void) { int a[] = {1, 2, 3…
- With a 32-bit unsigned int x, which expression has undefined behaviour?
- What is printed? c char s[] = "7392"; int n = 0; for (int i = 0; s[i]; i++) n = n 10 + (s[i] - '0'); printf("%d %d", n % 1000, n / 100 %…