Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 201 – Elementary Computer Programming 1 Extra Exercises Sourceshttp://www.seas.upenn.edu/~cse110/exams/old/midterm1_fall05.pdfhttp://www.csd.uwo.ca/courses/CS026b/oldmidt/midterm.doc.

Similar presentations


Presentation on theme: "CSE 201 – Elementary Computer Programming 1 Extra Exercises Sourceshttp://www.seas.upenn.edu/~cse110/exams/old/midterm1_fall05.pdfhttp://www.csd.uwo.ca/courses/CS026b/oldmidt/midterm.doc."— Presentation transcript:

1 CSE 201 – Elementary Computer Programming 1 Extra Exercises Sourceshttp://www.seas.upenn.edu/~cse110/exams/old/midterm1_fall05.pdfhttp://www.csd.uwo.ca/courses/CS026b/oldmidt/midterm.doc

2 CSE 201 – Elementary Computer Programming2 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 CSE 201 – Elementary Computer Programming3 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 CSE 201 – Elementary Computer Programming4 Value of expressions   What is the value of each of the following valid expressions? (a) (2 2) (b) (!false) && (!false) (c)( 5 > 2 ) == ( 3 2 ) == ( 3 <= 3 )

5 CSE 201 – Elementary Computer Programming5 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 CSE 201 – Elementary Computer Programming6 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 CSE 201 – Elementary Computer Programming7 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 CSE 201 – Elementary Computer Programming8 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 CSE 201 – Elementary Computer Programming9 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 CSE 201 – Elementary Computer Programming10 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; x = x*2;}

11 CSE 201 – Elementary Computer Programming11 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 CSE 201 – Elementary Computer Programming12 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 CSE 201 – Elementary Computer Programming13 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 CSE 201 – Elementary Computer Programming14 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 CSE 201 – Elementary Computer Programming15 Value and Type a."Java" + "is" + "great" b."123 + 456" c.3 / 2 * 1.0 d."The answer is: " + 5 + 5 e."The answer is: " + (5 + 5)  For each of the following Java expressions, provide the result of evaluating the expression, and identify the type of this value.

16 CSE 201 – Elementary Computer Programming16 Value and Type 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)  For each of the following Java expressions, provide the result of evaluating the expression, and identify the type of this value.

17 CSE 201 – Elementary Computer Programming17 Code Tracing 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);  Trace this code segment and report what this code prints to the screen

18 CSE 201 – Elementary Computer Programming18 Code Understanding int sum = 0; int i = 51; while (i < 100) { sum = sum + (i*i); sum = sum + (i*i); i++; i++; }  What does this code do?

19 CSE 201 – Elementary Computer Programming19 Code Understanding int sum = 0; int i = 51; while (i < 100) { sum = sum + (i*i); sum = sum + (i*i); i++; i++; }  What does this code do?

20 CSE 201 – Elementary Computer Programming20 Fix Code Scanner in = new Scanner(System.in); int ans; ans = in.nextInt(); while (ans > 0 && ans 0 && ans < 10){ ans = in.nextInt(); }  Fix code to implement a validation loop to ensure that the user enters a number between 1 and 9 inclusive?

21 CSE 201 – Elementary Computer Programming21 Code Understanding 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);  What does this code do?


Download ppt "CSE 201 – Elementary Computer Programming 1 Extra Exercises Sourceshttp://www.seas.upenn.edu/~cse110/exams/old/midterm1_fall05.pdfhttp://www.csd.uwo.ca/courses/CS026b/oldmidt/midterm.doc."

Similar presentations


Ads by Google