Key_for_6EExam3_Ch5_

Key_for_6EExam3_Ch5_ - 1 3 9 27 81 b public class Test1...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Key for Exam3 Part I: Multiple Choice Questions: (1 pts each) 1. c 2. b 3. a 4. d 5. a 6. b 7. c 8. a 9. b 10. a 11. a 12. e 13. d Part II. (2 pts) Fix Errors in the following code: a. public class Test1 { public static void main(String[] args) { double i = 1; while (i <= 5) { xMethod(i); // xMethod returns double i++; } } public static double xMethod( int double i) { i++; return i; } } Part III. (9 pts) Show the printout of the following code: a. public class Test1 { public static void main(String[] args) { int i = 1; while (i <= 5) { xMethod(i); i++; 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
} } public static void xMethod(int i) { int num = 1; for (int j=1; j<=i; j++) { System.out.print(num + " "); num *= 3; } System.out.println(); } } 1 1 3 1 3 9 1 3 9 27
Background image of page 2
Background image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 3 9 27 81 b. public class Test1 { public static void main(String args) { int i = 1; while (i <= 5) { xMethod(i); i++; } System.out.println("i is " + i); } public static void xMethod(int i) { do { if (i%2 != 0) System.out.print(i + " "); i--; } while (i >= 1); System.out.println(); } } 1 1 3 1 3 1 5 3 1 2 i is 6 c. 4 3 2 1 Part IV: (8 pts): a. public static int sum(int number) { int sum = 0; while (number != 0) { sum += number % 10; number = number / 10; } return sum; } b. public static void displayNumber(int n) { for (int i = 1; i <= n; i++) System.out.print(i + " "); } 3...
View Full Document

{[ snackBarMessage ]}

Page1 / 3

Key_for_6EExam3_Ch5_ - 1 3 9 27 81 b public class Test1...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online