ISRO Computer Science (CSE) 2007 - Question 23

All ISRO Computer Science (CSE) papers | ISRO Computer Science (CSE) 2007

Study the following program: // precondition: x >= 0 public void demo(int x) { System.out.print(x % 10); if (x / 10 != 0) { demo(x / 10); } System.out.print(x % 10); } Which of the following is printed as a result of the call demo(1234)?

  • A. 1441
  • B. 3443
  • C. 12344321
  • D. 43211234

Answer: D. 43211234

Explanation: The function prints the last digit, recurses, then prints the last digit again. This effectively prints the number reversed, then the number forward. 43211234.

| | All questions of ISRO Computer Science (CSE) 2007