ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
int x = 7;
int result = x % 2 == 0 ? x / 2 : x * 3 + 1;
printf("%d", result);- A. 10
- B. 22
- C. 21
- D. 3
Correct answer: B. 22
x % 2 == 0 (7%2==0, i.e. 1==0) is false, so the else branch runs: x*3+1 = 21+1 = 22 (this is one step of the Collatz sequence for odd x).
More C Programming questions
- What is printed? c #define PRINT(x) printf(#x " = %dn", x) int main(void) { int count = 5; PRINT(count); }
- What is printed? c #define MULTILINE(x) do { x = x 2; x = x + 1; } while (0) int main(void) { int v = 5; MULTILINE(v); printf("%d", v); }
- What is printed? c #define DEBUGPRINT(fmt, ...) printf(fmt, VAARGS) int main(void) { DEBUGPRINT("%d and %dn", 1, 2); }
- What is printed? c #define SAFEDIV(a, b) ((b) != 0 ? (a) / (b) : 0) int main(void) { int x = 10, y = 0; printf("%d", SAFEDIV(x, y)); }
- What is printed? c #if 1 #define VALUE 100 #elif 0 #define VALUE 200 #else #define VALUE 300 #endif printf("%d", VALUE);
- What is printed? c #define ISPOSITIVE(x) ((x) > 0) #define ISNEGATIVE(x) ((x) < 0) #define ISZERO(x) ((x) == 0) int classify(int n) { if…