1 / 21

Extra Exercises

Extra Exercises. Sources http://www.seas.upenn.edu/~cse110/exams/old/midterm1_fall05.pdf http://www.csd.uwo.ca/courses/CS026b/oldmidt/midterm.doc. 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)

ltillman
Download Presentation

Extra Exercises

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Extra Exercises Sources http://www.seas.upenn.edu/~cse110/exams/old/midterm1_fall05.pdf http://www.csd.uwo.ca/courses/CS026b/oldmidt/midterm.doc

  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? (A) int y = 5; int x = y; y = y * 2; (B) int x = 5; x = x / 2; x = x + 3 * x - 3;

  3. 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 );

  4. 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 )

  5. 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; }

  6. 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; }

  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=1; while (x<10) { System.out.println(x); }

  8. 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;

  9. 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;

  10. 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; }

  11. 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

  12. 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.

  13. 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.

  14. 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.

  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. a. "Java" + "is" + "great" b. "123 + 456" c. 3 / 2 * 1.0 d. "The answer is: " + 5 + 5 e. "The answer is: " + (5 + 5)

  16. 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. 'b' > 'a' g. 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)

  17. 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);

  18. Code Understanding • What does this code do? int sum = 0; int i = 51; while (i < 100) { sum = sum + (i*i); i++; i++; }

  19. Code Understanding • What does this code do? int sum = 0; int i = 51; while (i < 100) { sum = sum + (i*i); i++; i++; }

  20. 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(); }

  21. 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);

More Related