cse 201 – elementary computer programming 1 extra exercises...

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

Upload: justina-morgan

Post on 18-Jan-2016

231 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSE 201 – Elementary Computer Programming 1 Extra Exercises Sourcescse110/exams/old/midterm1_fall05.pdf

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

Page 2: CSE 201 – Elementary Computer Programming 1 Extra Exercises Sourcescse110/exams/old/midterm1_fall05.pdf

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;

Page 3: CSE 201 – Elementary Computer Programming 1 Extra Exercises Sourcescse110/exams/old/midterm1_fall05.pdf

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

Page 4: CSE 201 – Elementary Computer Programming 1 Extra Exercises Sourcescse110/exams/old/midterm1_fall05.pdf

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 )

Page 5: CSE 201 – Elementary Computer Programming 1 Extra Exercises Sourcescse110/exams/old/midterm1_fall05.pdf

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;

}}

Page 6: CSE 201 – Elementary Computer Programming 1 Extra Exercises Sourcescse110/exams/old/midterm1_fall05.pdf

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;

}}

Page 7: CSE 201 – Elementary Computer Programming 1 Extra Exercises Sourcescse110/exams/old/midterm1_fall05.pdf

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

}}

Page 8: CSE 201 – Elementary Computer Programming 1 Extra Exercises Sourcescse110/exams/old/midterm1_fall05.pdf

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;

Page 9: CSE 201 – Elementary Computer Programming 1 Extra Exercises Sourcescse110/exams/old/midterm1_fall05.pdf

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;

Page 10: CSE 201 – Elementary Computer Programming 1 Extra Exercises Sourcescse110/exams/old/midterm1_fall05.pdf

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

Page 11: CSE 201 – Elementary Computer Programming 1 Extra Exercises Sourcescse110/exams/old/midterm1_fall05.pdf

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

Page 12: CSE 201 – Elementary Computer Programming 1 Extra Exercises Sourcescse110/exams/old/midterm1_fall05.pdf

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.

Page 13: CSE 201 – Elementary Computer Programming 1 Extra Exercises Sourcescse110/exams/old/midterm1_fall05.pdf

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.

Page 14: CSE 201 – Elementary Computer Programming 1 Extra Exercises Sourcescse110/exams/old/midterm1_fall05.pdf

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.

Page 15: CSE 201 – Elementary Computer Programming 1 Extra Exercises Sourcescse110/exams/old/midterm1_fall05.pdf

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.

Page 16: CSE 201 – Elementary Computer Programming 1 Extra Exercises Sourcescse110/exams/old/midterm1_fall05.pdf

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.

Page 17: CSE 201 – Elementary Computer Programming 1 Extra Exercises Sourcescse110/exams/old/midterm1_fall05.pdf

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

Page 18: CSE 201 – Elementary Computer Programming 1 Extra Exercises Sourcescse110/exams/old/midterm1_fall05.pdf

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?

Page 19: CSE 201 – Elementary Computer Programming 1 Extra Exercises Sourcescse110/exams/old/midterm1_fall05.pdf

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?

Page 20: CSE 201 – Elementary Computer Programming 1 Extra Exercises Sourcescse110/exams/old/midterm1_fall05.pdf

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?

Page 21: CSE 201 – Elementary Computer Programming 1 Extra Exercises Sourcescse110/exams/old/midterm1_fall05.pdf

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?