Name CS/IT114: Exam #2, 12/10/2015 (100 points total) *** PLEASE TURN OFF ALL CELLPHONES *** You may use your crib sheet only (one HANDWRITTEN 8-1/2 x 11 page both sides). You will have until the end of test period, unless you get time-and-a-half. Spend only about one minute per point on each question to complete the exam on time. 1. Expression Evaluation (20 Points) See corresponding sections of Exam 1, Practice Exam 1, and part 4 on the CS110 practice exam (plus solutions): http://www.cs.umb.edu/~bobw/cs110/exam1.pract.pdf 2. Selection Statements (20 points): What is printed by the following code segment? double first = 0.0; int second = 46; int third = 23; String s1 = "hello", s2 = "World"; if (third > second) System.out.println("a"); if (first < third / (double) second) System.out.println("b"); if (s1.compareto(s2) > 0) System.out.println("c"); else System.out.println("d"); System.out.println("e"); System.out.println("f"); switch (third % 3) { case 3: System.out.println("g"); break; case 2: System.out.println("h"); break; case 1: System.out.println("i"); default: System.out.println("j"); break; // Printed? (Yes or No) // a. // b. // c. // d. // e. // f. // g. // h. // i. // j.
3. Loops (20 points): Study the following code segment. int j = 5; while (j >= 0) { for (int i = 0; i <= j; i++) System.out.print('-'); System.out.println((char) ('z' - j) + "."); j--; a. What is printed? b. How would you alter the code to make the letters print at the start of each line? (Otherwise, output should look the same.) See also part 6a + solution on the CS110 practice exam: http://www.cs.umb.edu/~bobw/cs110/exam1.pract.pdf
4. Arrays (20 points) // Part a. int[] numbers = { 0, 2, 1, 3, 2, 1 ; char[] chars = new char[numbers.length]; for (int i = 0; i < chars.length; i++) chars[i] = (char)('q' + numbers[i]); // consider writing out contents of chars below //What prints? System.out.println((int) Math.pow(chars.length, 2)); // System.out.println(chars[3] + ": " + numbers[3]); // System.out.println(chars[5] - 'a'); // System.out.println("hello" + chars[1]); // System.out.println(("" + chars[5]).touppercase()); // // Part b. String str = "Hello, how are you today?"; // Declare a char array called "chars2" and set it equal to the // char array of String s. (You can call "tochararray()" using str) // Using a loop (of your choice) print every third character // of chars2. (HINT: If x is some integer >= zero, then x % 3 // will always be either 0, 1, or 2.) // Reverse the order of the elements in chars2. (HINT: The // variable str still has the original String, and the last // position in it is the length minus one.) See also part 3a + solution on the second CS110 practice exam: http://www.cs.umb.edu/~bobw/cs110/exam2.pract.pdf
5. Program Writing (20 Points) Finish the "main" method that will create two String arrays to hold authors and titles of the books (where the size of each array is the number of books), execute a loop that will prompt the user for the book author (and store it in the authors array) and for the book title (and store it in the titles array), and run a second loop to print each book. Sample output (will be different each time depending on user input): How many books? 3 Book #1 Author? Stephen King Title? The Stand Book #2 Author? R. L. Stine Title? Goosebumps The underlined portion represents what the user has typed in, in response to the program's prompts. Book #3 Author? JRR Tolkien Title? The Hobbit Your books (3 total): The Stand, by Stephen King Goosebumps, by R. L. Stine The Hobbit, by JRR Tolkien Fill in the missing parts inside of main: import java.util.scanner; public class Books { public static void main (String[] args) { Scanner scan = new Scanner (System.in); int numbooks; System.out.print("How many books?"); numbooks = scan.nextint(); scan.nextline();
Answers: 1. Expression Evaluation: See resources indicated. 2. Selection Statements a. No b. Yes c. Yes d. No e. Yes f. Yes g. No h. Yes i. No j. No 3. Loops a. ------u. -----v. ----w. ---x. --y. -z. b. int j = 5; while (j >= 0) { System.out.print((char) ('z' - j)); for (int i = 0; i <= j; i++) System.out.print('-'); System.out.println("."); j--; Result: u------. v-----. w----. x---. y--. z-. 4. Arrays A. 36 t: 3 17 hellos R B. String str = "Hello, how are you today?"; char[] chars2 = str.tochararray(); for (int i = 0; i < chars2.length; i++) if (i % 3 == 2) System.out.println(chars2[i]); for (int i = 0; i < chars2.length; i++) chars2[i] = str.charat(str.length() - 1 - i);
5. Writing Programs import java.util.scanner; public class Books { public static void main (String[] args) { Scanner scan = new Scanner (System.in); int numbooks; System.out.print("How many books?"); numbooks = scan.nextint(); scan.nextline(); String[] authors = new String[numBooks]; String[] titles = new String[numBooks]; System.out.println(); for (int i = 0; i < numbooks; i++) { System.out.println("Book #" + (i+1)); System.out.print("Author? "); authors[i] = scan.nextline(); System.out.print("Title? "); titles[i] = scan.nextline(); System.out.println(); System.out.println("Your books (" + numbooks + " total)"); for (int i = 0; i < numbooks; i++) System.out.println(titles[i] + ", by " + authors[i]);