Upload
justina-morgan
View
231
Download
0
Embed Size (px)
Citation preview
CSE 201 – Elementary Computer ProgrammingCSE 201 – Elementary Computer Programming 11
Extra ExercisesExtra Exercises
SourcesSourceshttp://www.seas.upenn.edu/~cse110/exams/old/midterm1_fall05.pdfhttp://www.seas.upenn.edu/~cse110/exams/old/midterm1_fall05.pdf
http://www.csd.uwo.ca/courses/CS026b/oldmidt/midterm.dochttp://www.csd.uwo.ca/courses/CS026b/oldmidt/midterm.doc
CSE 201 – Elementary Computer ProgrammingCSE 201 – Elementary Computer Programming
22
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;
CSE 201 – Elementary Computer ProgrammingCSE 201 – Elementary Computer Programming
33
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 ProgrammingCSE 201 – Elementary Computer Programming
44
Value of expressionsValue of expressions
What is the value of each of the following valid expressions?
(a) (a) (2 < 2) || (2 > 2)(2 < 2) || (2 > 2)
(b) (b) (!false) && (!false)(!false) && (!false)
(c)(c) ( 5 > 2 ) == ( 3 <= 3 )( 5 > 2 ) == ( 3 <= 3 )
CSE 201 – Elementary Computer ProgrammingCSE 201 – Elementary Computer Programming
55
Debug According to DescriptionDebug According to Description
Fix the code so that it works according to the supplied documentation (in comments)
/* Calculates sum of all of the even/* Calculates sum of all of the even* numbers from 0 to limit inclusive.* numbers from 0 to limit inclusive.* Assumes limit is initialized above * Assumes limit is initialized above * to a positive integer. */* to a positive integer. */int i = 0;int i = 0;int sum = 0;int sum = 0;while (i != limit)while (i != limit){{sum = sum + i;sum = sum + i;i+=2;i+=2;
}}
CSE 201 – Elementary Computer ProgrammingCSE 201 – Elementary Computer Programming
66
Debug According to DescriptionDebug According to Description
Solution
/* Calculates sum of all of the even/* Calculates sum of all of the even* numbers from 0 to limit inclusive.* numbers from 0 to limit inclusive.* Assumes limit is initialized above * Assumes limit is initialized above * to a positive integer. */* to a positive integer. */int i = 0;int i = 0;int sum = 0;int sum = 0;while (i <= limit)while (i <= limit){{sum = sum + i;sum = sum + i;i+=2;i+=2;
}}
CSE 201 – Elementary Computer ProgrammingCSE 201 – Elementary Computer Programming
77
How Many Iterations?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;int x=1;while (x<10)while (x<10){{System.out.println(x);System.out.println(x);
}}
CSE 201 – Elementary Computer ProgrammingCSE 201 – Elementary Computer Programming
88
How Many Iterations?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;int x=10;while(x<10)while(x<10)System.out.println(x);System.out.println(x);x=x-1;x=x-1;
CSE 201 – Elementary Computer ProgrammingCSE 201 – Elementary Computer Programming
99
How Many Iterations?How Many Iterations?
If we put a ; at while loop How many times x is printed?
int x=10;int x=10;while(x<10);while(x<10);System.out.println(x);System.out.println(x);x=x-1;x=x-1;
CSE 201 – Elementary Computer ProgrammingCSE 201 – Elementary Computer Programming
1010
How Many Iterations?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;int x=1;while(x!=10)while(x!=10){{ x = x*2;x = x*2;}}
CSE 201 – Elementary Computer ProgrammingCSE 201 – Elementary Computer Programming
1111
True/FalseTrue/False
In Java I can have both mystring and myString In Java I can have both mystring and myString as two distinct variables in same program.as two distinct variables in same program.
When writing a computer program, everything When writing a computer program, everything must be precise and unambiguous.must be precise and unambiguous.
The starting index position for a string in Java is The starting index position for a string in Java is 0.0.
When a program written in Java is compiled, it is When a program written in Java is compiled, it is typically compiled to bytecodetypically compiled to bytecode
CSE 201 – Elementary Computer ProgrammingCSE 201 – Elementary Computer Programming
1212
Multiple Choice 1Multiple Choice 1
In Java, the calculation 3 % 4 produces a In Java, the calculation 3 % 4 produces a result of:result of:
a.a. 00b.b. 11c.c. 0.750.75d.d. 33e.e. None of the above.None of the above.
CSE 201 – Elementary Computer ProgrammingCSE 201 – Elementary Computer Programming
1313
Multiple ChoiceMultiple Choice
Which of the following represents a Which of the following represents a character in Java:character in Java:
a.a. aab.b. 'a''a'c.c. "a""a"d.d. All of the above.All of the above.e.e. None of the above.None of the above.
CSE 201 – Elementary Computer ProgrammingCSE 201 – Elementary Computer Programming
1414
Multiple ChoiceMultiple Choice
Which of the following is a valid way to create Which of the following is a valid way to create a String object containing the text a String object containing the text
““Hello there” in Java:Hello there” in Java:
a.a. String tempString = "Hello there";String tempString = "Hello there";b.b. String tempString = "Hello" + " " + String tempString = "Hello" + " " + "there";"there";
c.c. String tempString = new String("Hello String tempString = new String("Hello there");there");
d.d. All of the above.All of the above.e.e. None of the above.None of the above.
CSE 201 – Elementary Computer ProgrammingCSE 201 – Elementary Computer Programming
1515
Value and TypeValue and Type
a.a. "Java" + "is" + "great""Java" + "is" + "great"
b.b. "123 + 456""123 + 456"
c.c. 3 / 2 * 1.03 / 2 * 1.0
d.d. "The answer is: " + 5 + 5"The answer is: " + 5 + 5
e.e. "The answer is: " + (5 + 5)"The answer is: " + (5 + 5)
For each of the following Java expressions, For each of the following Java expressions, provide the result of evaluating the expression, provide the result of evaluating the expression, and identify the type of this value.and identify the type of this value.
CSE 201 – Elementary Computer ProgrammingCSE 201 – Elementary Computer Programming
1616
Value and TypeValue and Type
f.f. 'b' > 'a''b' > 'a'
g.g. 1 + 2 * 3 – 4 / 5 1 + 2 * 3 – 4 / 5
h.h. (((int) 1.0) % 5 + 0.5 * 2) / 2.0 – (((int) 1.0) % 5 + 0.5 * 2) / 2.0 – ((double) 1)((double) 1)
i.i. 3 / 6 * (16.3 * 20.2 + 960 / 6.0)3 / 6 * (16.3 * 20.2 + 960 / 6.0)
For each of the following Java expressions, For each of the following Java expressions, provide the result of evaluating the expression, provide the result of evaluating the expression, and identify the type of this value.and identify the type of this value.
CSE 201 – Elementary Computer ProgrammingCSE 201 – Elementary Computer Programming
1717
Code TracingCode Tracing
int numPeople = 4;int numPeople = 4;System.out.println(numPeople);System.out.println(numPeople);double bill = 100.00;double bill = 100.00;System.out.println(bill);System.out.println(bill);double tip = bill * 0.2;double tip = bill * 0.2;System.out.println(tip);System.out.println(tip);double total = bill + tip;double total = bill + tip;System.out.println(total);System.out.println(total);double totalPerPerson = total / numPeople;double totalPerPerson = total / numPeople;System.out.println(totalPerPerson);System.out.println(totalPerPerson);
Trace this code segment and report what this Trace this code segment and report what this code prints to the screencode prints to the screen
CSE 201 – Elementary Computer ProgrammingCSE 201 – Elementary Computer Programming
1818
Code UnderstandingCode Understanding
int sum = 0;int sum = 0;int i = 51;int i = 51;while (i < 100) {while (i < 100) { sum = sum + (i*i);sum = sum + (i*i); i++;i++; i++;i++;}}
What does this code do?What does this code do?
CSE 201 – Elementary Computer ProgrammingCSE 201 – Elementary Computer Programming
1919
Code UnderstandingCode Understanding
int sum = 0;int sum = 0;int i = 51;int i = 51;while (i < 100) {while (i < 100) { sum = sum + (i*i);sum = sum + (i*i); i++;i++; i++;i++;}}
What does this code do?What does this code do?
CSE 201 – Elementary Computer ProgrammingCSE 201 – Elementary Computer Programming
2020
Fix CodeFix Code
Scanner in = new Scanner(System.in);Scanner in = new Scanner(System.in);int ans;int ans;ans = in.nextInt();ans = in.nextInt();while (ans > 0 && ans < 10)while (ans > 0 && ans < 10){{ans = in.nextInt();ans = in.nextInt();
}}
Fix code to implement a validation loop to ensure that the user enters a number Fix code to implement a validation loop to ensure that the user enters a number between 1 and 9 inclusive?between 1 and 9 inclusive?
CSE 201 – Elementary Computer ProgrammingCSE 201 – Elementary Computer Programming
2121
Code UnderstandingCode Understanding
String foo = "Java!";String foo = "Java!";String bar = "";String bar = "";int i = 0;int i = 0;while( i < foo.length())while( i < foo.length()){{bar = foo.charAt(i) + bar;bar = foo.charAt(i) + bar;i++; //i.e i=i+1;i++; //i.e i=i+1;
}}System.out.println(bar);System.out.println(bar);
What does this code do?What does this code do?