Download presentation
Presentation is loading. Please wait.
Published byHarvey Powell Modified over 8 years ago
1
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam
2
2 Announcements
3
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)
4
Question 1 How many iterations in total? After 1 st iteration, x=7 After 2 nd, x=4 After 3 rd, x=1 Then exit the loop What is the value of sum in each iteration? After 1 st, sum=0+(x=10)=10 After 2 nd, sum=10+(x=7)=17 After 3 rd, sum=17+(x=4)=21 4
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 5 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
6
Question 2 After the function call, input arguments are initialized as follows: a= 10, b=20, c=“BOB” For 1 st 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 6
7
Question 3 Lab 5 solution 7 def integerDecision(number): if number <=0: print("Input number is not positive.") elif 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.")
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 8 if number <=0: print("Input number is not positive.") elif number 0: print("Input number is less than 50.")
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 9 elif number >=50 and number<=100: if number == 60: print("I FOUND 60.") else: print("Input number ",number, " is >= 50 and <=100")
10
Question 3 #TODO 1.05 #Test the input number is greater than 100 10 elif number > 100: print("Input number is greater than 100.") else: print("It's a strange number.")
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. 11
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 12
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) 13 myList = [0,1,2,3,4] myString = “ hello” 1) mylist[len(mylist)] 2) b + 6 = 17 3) c = "Hello" + "World" 4) myString[2] = ‘j’
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 14
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 15 a = 15 b = 13 c = 12 d = 6 e = 10 if (a>b and d>4) or (e a+b): print ("good" ) else: print ("ok“)
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 16
17
Question 7 Q15. Consider the following five loops (i-v). Which loops will generate the same output? (i) (ii) 17 i = 0 while(i < 11): print (i) for i in range(0,10): print (i)
18
Question 7 (iii) (iv) (v) (A) i, ii, iii, iv (B) i, ii, iv, v (C) i, iii, iv (D) i, iii, v 18 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)
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 19
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 20 for i in range(1,10,2): if i % 2 == 0: print (i)
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 21
22
Question 9 Q19. Considering the following code snippet, what would the output be if you ran the function t4()? 22 x = 0 y = 10 def t1(): x = 1 def t2(): x = 2 def t3(): x = y
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? 23 def t4(): x = 4 t1() t2() t3() print (x)
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.