{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Midterm04Solved

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

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

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

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

View Full Document
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
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

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

View Full Document
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.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### 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
Ask a homework question - tutors are online