Midterm04Solved - Question 1a) What is printed by the...

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

View Full Document Right Arrow Icon
1 Question 1a) What is printed by the following Java program? int s; int r; int i; int [] x = {4, 8, 2, -9, 6}; s = 1; r = 0; i = x.length - 1; while (i > 0) { s = s * -1; i = i - 1; r = r + s * x[i]; } System.out.println(r); a) -13 b) 20 c) -9 d) -21 e) 11 f) 7 Trace of program xs r i Initial values ?? ? ? x = {4, 8, 2, -9, 6} {4, 8, 2, -9, 6} s = 1 1 r = 0 0 i = x.length - 1 4 while (i > 0): true s = s * -1 -1 i = i - 1 3 r = r + s * x[i] 9 while (i > 0): true s = s * -1 1 i = i - 1 2 r = r + s * x[i] 11
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Trace of program xs r i Values from previous table {4, 8, 2, -9, 6} 1 11 2 while (i > 0): true s = s * -1 -1 i = i - 1 1 r = r + s * x[i] 3 while (i > 0): true s = s * -1 1 i = i - 1 0 r = r + s * x[i] 7 while (i > 0): false System.out.println(r) Question 1a) What is printed by the following Java program? int s; int r; int i; int [] x = {4, 8, 2, -9, 6}; s = 1; r = 0; i = x.length - 1; while (i > 0) { r = -(-9) + 2 – 8 + 4 s = s * -1; i = i - 1; r = r + s * x[i]; } System.out.println(r); a) -13 b) 20 c) -9 d) -21 e) 11 f) 7
Background image of page 2
3 Question 1b) • The array of integers "a" (type int) initially contains {1 3 8}. Only one of the following contains {1, 3, 8}. Boolean expressions evaluates to false. Circle the letter next to the expression that evaluates to false. a) (a[1] < 3) || ( (a[1] / 2) == 1) b) !( (a[0] % a[2]) == (a[0] % a[1]) ) c) !(!(false) && (a[2] < a[1]) ) d) Math.pow(a[1],2) > a[2] e) Math.abs(a[0] - a[1] + 2) <= 0 Question 1b) • The array of integers "a" (type int) initially Only one of the following contains {1, 3, 8}. Boolean expressions evaluates to false. Circle the letter next to the expression that evaluates to false. a) (a[1] < 3) || ( (a[1] / 2) == 1) 3 3 F T T 1
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 Question 1b) • The array of integers "a" (type int) initially contains {1 3 8}. Only one of the following contains {1, 3, 8}. Boolean expressions evaluates to false. Circle the letter next to the expression that evaluates to false.
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/06/2011 for the course ITI 1120 taught by Professor Inkpen during the Fall '07 term at University of Ottawa.

Page1 / 13

Midterm04Solved - Question 1a) What is printed by the...

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

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