COMP-202 Fall 2010 Midterm solutions -------------------------------------------------------------------------------- Question 1 -------------------------------------------------------------------------------- a) 1 - 3 * (1 + 6) / 2 Operator Context Result + (1+6) 7 * 21 / 10 - -9 b) 1/2 + 8 * (1%5 - 1) Operator Context Result % 1 - 0 / 0 * 0 + 0 c) 2.5 * 2 + (int) 0.9/0.2 Operator Context Result cast 0 / 0 * 5.0 + 5.0 d) !(1 < 1) != (7%2 == 0) || 4/4/4 > 1 Operator Context Result < false % 1 == false ! true first / 1 second / 0 > false != true || true -------------------------------------------------------------------------------- Question 2 -------------------------------------------------------------------------------- 3*45 20 0.75 -------------------------------------------------------------------------------- Question 3 -------------------------------------------------------------------------------- Note that there are two horizontal spaces between each character, and two leading spaces to the left of the bottom-left '*'. * * * @ * * * @ @ * * * * @ @ @ * * * * * @ @ @ @ -------------------------------------------------------------------------------- Question 4 -------------------------------------------------------------------------------- There are in fact 9 errors (we overlooked the one on line 17). Anyone who gets at least 8 of the following errors should get full marks. Line 1: (compile-time) Missing brace: class body not properly defined. Line 2: (run-time) Main should be main Line 5: (logical) Infinite loop "for (int p = 2; p <= 15; p--)" Line 9, 10, 11 or 11: (compile-time) Dangling else (due to location of {} on lines 9 and 12. Line 17: (logical) 1 is not prime Line 22: (compile-time) num % i = 0 does not evaluate to boolean Line 33 or 34: (compile-time) lastDigit is out of scope Line 30 or 34: (compile-time) constant variable is reassigned Line 36: (compile-time) Missing return statement -------------------------------------------------------------------------------- Question 5 -------------------------------------------------------------------------------- a) The ADDRESS to the memory location that contains array {4, 3, 44, 9, 5}; b) input[0] == 4 and input[1] == 3 c) input[0] == 9 and input[1] == 0 d) 5 9 44 3 4 -1 -------------------------------------------------------------------------------- Question 6.1 and 6.2 -------------------------------------------------------------------------------- public class LikeBookUtils{ static int countValues(boolean[] boolArray, boolean target){ int total = 0; for(int i = 0; i < boolArray.length; i++){ if(boolArray[i] == target) total++; } return total; } public static int computeMostPopular(boolean[][] likeTable){ int current = 0, max = 0, id = 0; for(int i = 0; i < likeTable.length; i++){ current = countValues(likeTable[i], true); if(current > max){ max = current; id = i; } } return id; } } -------------------------------------------------------------------------------- Question 6.3 -------------------------------------------------------------------------------- import java.util.Scanner; public class PopularityAnalyzer{ public static void main(String[] args){ Scanner input = new Scanner(System.in); int numPeople; System.out.print("Enter the number of people: "); numPeople = input.nextInt(); // students may assume that numPeople > 0 boolean[][] lTable = new boolean[numPeople][numPeople]; for(int x = 0; x < lTable.length; x++){ for(int y = 0; y < lTable[0].length; y++) { if(x == y) lTable[x][y] = false; else{ System.out.print("Person " + y + " likes Person " + x + "?"); lTable[x][y] = input.nextBoolean(); } } } System.out.print("The most popular person is: "); System.out.println(LikeBookUtils.computeMostPopular(lTable)); } }