Extra Exercises Sources http://www.seas.upenn.edu/~cse110/exams/old/midterm1_fall05.pdf http://www.csd.uwo.ca/courses/CS026b/oldmidt/midterm.doc CSE 201 – Elementary Computer Programming 1 Value of The Variable x For each of the following program fragments, what is the value of the variable x after the statements execute? (A) int y = 5; int x = y; y = y * 2; CSE 201 – Elementary Computer Programming (B) int x = 5; x = x / 2; x = x + 3 * x - 3; 2 Value of The Variable x For each of the following program fragments, what is the value of the variable x after the statements execute? (C) double x = 13 / 2; (D) boolean x = ( 2 == 3 ); CSE 201 – Elementary Computer Programming 3 Value of expressions What is the value of each of the following valid expressions? (a) (2 < 2) || (2 > 2) (b) (!false) && (!false) (c) ( 5 > 2 ) == ( 3 <= 3 ) CSE 201 – Elementary Computer Programming 4 Debug According to Description Fix the code so that it works according to the supplied documentation (in comments) /* Calculates sum of all of the even * numbers from 0 to limit inclusive. * Assumes limit is initialized above * to a positive integer. */ int i = 0; int sum = 0; while (i != limit) { sum = sum + i; i+=2; } CSE 201 – Elementary Computer Programming 5 Debug According to Description Solution /* Calculates sum of all of the even * numbers from 0 to limit inclusive. * Assumes limit is initialized above * to a positive integer. */ int i = 0; int sum = 0; while (i <= limit) { sum = sum + i; i+=2; } CSE 201 – Elementary Computer Programming 6 How Many Iterations? How many times the body of the loop is executed? 0, 1, infinite, or “> 1”? ”> 1” means more than once but not infinite. int x=1; while (x<10) { System.out.println(x); } CSE 201 – Elementary Computer Programming 7 How Many Iterations? How many times the body of the loop is executed? 0, 1, infinite, or “> 1”? ”> 1” means more than once but not infinite. int x=10; while(x<10) System.out.println(x); x=x-1; CSE 201 – Elementary Computer Programming 8 How Many Iterations? If we put a ; at while loop How many times x is printed? int x=10; while(x<10); System.out.println(x); x=x-1; CSE 201 – Elementary Computer Programming 9 How Many Iterations? How many times the body of the loop is executed? 0, 1, infinite, or “> 1”? ”> 1” means more than once but not infinite. int x=1; while(x!=10) { x = x*2; } CSE 201 – Elementary Computer Programming 10 True/False In Java I can have both mystring and myString as two distinct variables in same program. When writing a computer program, everything must be precise and unambiguous. The starting index position for a string in Java is 0. When a program written in Java is compiled, it is typically compiled to bytecode CSE 201 – Elementary Computer Programming 11 Multiple Choice 1 In Java, the calculation 3 % 4 produces a result of: a. 0 b. 1 c. 0.75 d. 3 e. None of the above. CSE 201 – Elementary Computer Programming 12 Multiple Choice Which of the following represents a character in Java: a. a b. 'a' c. "a" d. All of the above. e. None of the above. CSE 201 – Elementary Computer Programming 13 Multiple Choice Which of the following is a valid way to create a String object containing the text “Hello there” in Java: a. String tempString = "Hello there"; b. String tempString = "Hello" + " " + "there"; c. String tempString = new String("Hello there"); d. All of the above. e. None of the above. CSE 201 – Elementary Computer Programming 14 Value and Type For each of the following Java expressions, provide the result of evaluating the expression, and identify the type of this value. a. "Java" + "is" + "great" b. "123 + 456" c. 3 / 2 * 1.0 d. "The answer is: " + 5 + 5 e. "The answer is: " + (5 + 5) CSE 201 – Elementary Computer Programming 15 Value and Type For each of the following Java expressions, provide the result of evaluating the expression, and identify the type of this value. f. g. 'b' > 'a' 1 + 2 * 3 – 4 / 5 h. (((int) 1.0) % 5 + 0.5 * 2) / 2.0 – ((double) 1) i. 3 / 6 * (16.3 * 20.2 + 960 / 6.0) CSE 201 – Elementary Computer Programming 16 Code Tracing Trace this code segment and report what this code prints to the screen int numPeople = 4; System.out.println(numPeople); double bill = 100.00; System.out.println(bill); double tip = bill * 0.2; System.out.println(tip); double total = bill + tip; System.out.println(total); double totalPerPerson = total / numPeople; System.out.println(totalPerPerson); CSE 201 – Elementary Computer Programming 17 Code Understanding What does this code do? int sum = 0; int i = 51; while (i < 100) { sum = sum + (i*i); i++; i++; } CSE 201 – Elementary Computer Programming 18 Code Understanding What does this code do? int sum = 0; int i = 51; while (i < 100) { sum = sum + (i*i); i++; i++; } CSE 201 – Elementary Computer Programming 19 Fix Code Fix code to implement a validation loop to ensure that the user enters a number between 1 and 9 inclusive? Scanner in = new Scanner(System.in); int ans; ans = in.nextInt(); while (ans > 0 && ans < 10) { ans = in.nextInt(); } CSE 201 – Elementary Computer Programming 20 Code Understanding What does this code do? String foo = "Java!"; String bar = ""; int i = 0; while( i < foo.length()) { bar = foo.charAt(i) + bar; i++; //i.e i=i+1; } System.out.println(bar); CSE 201 – Elementary Computer Programming 21