23
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam

1 CS 177 Week 6 Recitation Slides Review for Midterm Exam

Embed Size (px)

DESCRIPTION

Question 1 Q1. Which numbers are printed by the following loop: (A) 22 (B) 21 (C) 0 (D) 20 sum = 0 x = 10 while x > 1 : sum = sum + x x = x - 3 print (sum)

Citation preview

Page 1: 1 CS 177 Week 6 Recitation Slides Review for Midterm Exam

1

CS 177 Week 6 Recitation Slides

Review for Midterm Exam

Page 2: 1 CS 177 Week 6 Recitation Slides Review for Midterm Exam

2

Announcements

Page 3: 1 CS 177 Week 6 Recitation Slides Review for Midterm Exam

Question 1

Q1. Which numbers are printed by the following loop:

(A) 22 (B) 21 (C) 0 (D) 20

sum = 0   x = 10   while x > 1 :  

sum = sum + x   x = x - 3   print (sum)

Page 4: 1 CS 177 Week 6 Recitation Slides Review for Midterm Exam

4

Question 1

How many iterations in total? After 1st iteration, x=7 After 2nd , x=4 After 3rd, x=1 Then exit the loop

What is the value of sum in each iteration? After 1st, sum=0+(x=10)=10 After 2nd, sum=10+(x=7)=17 After 3rd, sum=17+(x=4)=21

Page 5: 1 CS 177 Week 6 Recitation Slides Review for Midterm Exam

5

Question 2

Q2. Consider the following code snippet. What is the output of calling:

fun(10, 20, “BOB”)?

(A) -10 (B) 30 (C) -1 (D) 0

def fun(a, b, c): if (a < b or c[0] == "A"):

if(a < 0 and c[0] == "B"): return a + b

else: print (a - b )

else: return -1

Page 6: 1 CS 177 Week 6 Recitation Slides Review for Midterm Exam

6

Question 2

After the function call, input arguments are initialized as follows: a= 10, b=20, c=“BOB” For 1st if statement, since a<b is True, the expression is True Then it evaluates if(a < 0 and c[0] == "B"): It is False since a<0 is False Then print a-b will be executed Result is -10

Page 7: 1 CS 177 Week 6 Recitation Slides Review for Midterm Exam

7

Question 3

Lab 5 solutiondef integerDecision(number): if number <=0: print("Input number is not positive.") elif number < 50 and number > 0: print("Input number is less than 50.") elif number >=50 and number<=100: if number == 60: print("I FOUND 60.") else: print("Input number ",number, " is >= 50 and <=100") elif number > 100: print("Input number is greater than 100.") else: print("It's a strange number.")

Page 8: 1 CS 177 Week 6 Recitation Slides Review for Midterm Exam

8

Question 3

#TODO 1.01 #Test if the input number is less than or equal to 0

#TODO 1.02 #Test if the input number is less than 50

if number <=0: print("Input number is not positive.")

elif number < 50 and number > 0: print("Input number is less than 50.")

Page 9: 1 CS 177 Week 6 Recitation Slides Review for Midterm Exam

9

Question 3

#TODO 1.03 #Test if the input number is greater than or equal to 50, and

less than or equal to 100, but not equal to 60

#1.04 #Inside the condition of 1.03, test if the number is equal to 60

elif number >=50 and number<=100:

if number == 60: print("I FOUND 60.") else: print("Input number ",number, " is >= 50 and <=100")

Page 10: 1 CS 177 Week 6 Recitation Slides Review for Midterm Exam

10

Question 3

#TODO 1.05 #Test the input number is greater than 100

elif number > 100: print("Input number is greater than 100.") else: print("It's a strange number.")

Page 11: 1 CS 177 Week 6 Recitation Slides Review for Midterm Exam

11

Question 4

Q8. Which of the following statements produce the same output?

1) print (11 + (13.0/2) + 2.5) 2) print (11 + 13//2 + 2.5 ) 3) print (11 + 13/2.0 + 2.5 ) 4) print ((11 + 13)/2.0 + 2.5) (A) 1), 2) and 3) (B) 1) and 2) (C) 1) and 3) (D) None of

the above.

Page 12: 1 CS 177 Week 6 Recitation Slides Review for Midterm Exam

12

Question 4

First equation evaluates to 20 Second equation evaluates to 19.5 (13/2=6) Third equation evaluates to 20 Fourth equation evaluates to 14.5 Python will cast an int to double if the int is doing

arithmetic operations (+,/,*,-) with another double value. Integer division will not round

Page 13: 1 CS 177 Week 6 Recitation Slides Review for Midterm Exam

13

Question 5

Q9. Which of the following statements (1-4 below) generate an error?

(A) 1), 2) and 4) (B) 1) and 2) (C) 1) and 4) (D) 1), 2), 3) and 4)

myList = [0,1,2,3,4] myString = “ hello”

1) mylist[len(mylist)] 2) b + 6 = 17 3) c = "Hello" + "World" 4) myString[2] = ‘j’

Page 14: 1 CS 177 Week 6 Recitation Slides Review for Midterm Exam

14

Question 5

mylist[len(mylist)] will throw an error, since the valid index will be 0 to len(mylist)-1

b + 6 = 17 is wrong since there cannot be an expression on the assignment left hand side

myString[2] = ‘j’ is wrong since you cannot change a python string

Page 15: 1 CS 177 Week 6 Recitation Slides Review for Midterm Exam

15

Question 6

Q14. What is the output of the following code?

(A) good (B) It will produce no output (C) ok (D) It will produce an error

a = 15 b = 13 c = 12 d = 6 e = 10 if (a>b and d>4) or (e<5 and c>a+b):

print ("good" )else:

print ("ok“)

Page 16: 1 CS 177 Week 6 Recitation Slides Review for Midterm Exam

16

Question 6

a>b is True (15>13) d>4 is True (6>4) The rest will not be evaluated good will be printed

Page 17: 1 CS 177 Week 6 Recitation Slides Review for Midterm Exam

17

Question 7

Q15. Consider the following five loops (i-v). Which loops will generate the same output?

(i)

(ii)

i = 0 while(i < 11):

print (i)

for i in range(0,10): print (i)

Page 18: 1 CS 177 Week 6 Recitation Slides Review for Midterm Exam

18

Question 7

(iii)

(iv)

(v)

(A) i, ii, iii, iv (B) i, ii, iv, v (C) i, iii, iv (D) i, iii, v

i = -1 while(i < 10):

i = i + 1 print (i)

for i in range(10%10,10%11) :

print (i)

for i in range(10) : print (i)

Page 19: 1 CS 177 Week 6 Recitation Slides Review for Midterm Exam

19

Question 7

(i) will print 0~9 (ii) will print 0~10 (iii) will print 0~9 (iv) 10%10=0, 10%11=10, will print 0~9 (v) will print 0~10

Page 20: 1 CS 177 Week 6 Recitation Slides Review for Midterm Exam

20

Question 8

Q17. Considering the following code snippet, what sequence of numbers is printed?

(A) All even numbers greater than 0 and less than 10 (B) All odd numbers greater than 0 and less than 10 (C) Every other odd number greater than 0 and less

than 10 (D) Nothing

for i in range(1,10,2): if i % 2 == 0:

print (i)

Page 21: 1 CS 177 Week 6 Recitation Slides Review for Midterm Exam

21

Question 8

for i in range(1,10,2): will loop through 1,3,5,7,9 None of them satisfy the condition i%2==0 Nothing will be printed

Page 22: 1 CS 177 Week 6 Recitation Slides Review for Midterm Exam

22

Question 9

Q19. Considering the following code snippet, what would the output be if you ran the function t4()?

  x = 0 y = 10  def t1():

x = 1   def t2(): x = 2   def t3(): x = y

Page 23: 1 CS 177 Week 6 Recitation Slides Review for Midterm Exam

23

Question 9

(A) 4 (B) 2 (C) 1 (D) 10 Within each functions, x is just a local variable. Calling

t1(), t2(), t3() will not change the local variable x in t4(). (A) is correct.

Then how to change a global variable within a function?

def t4(): x = 4 t1() t2() t3() print (x)