1 / 23

CS 177 Week 6 Recitation Slides

CS 177 Week 6 Recitation Slides. Review for Midterm Exam. Announcements. Question 1. sum = 0   x = 10   while x > 1 :   sum = sum + x   x = x - 3   print ( sum). Q1. Which numbers are printed by the following loop: (A) 22 (B) 21 (C) 0 (D) 20. Question 1.

madge
Download Presentation

CS 177 Week 6 Recitation Slides

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Announcements

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

  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

  5. Question 2 • 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 Q2. Consider the following code snippet. What is the output of calling: fun(10, 20, “BOB”)? (A) -10 (B) 30 (C) -1 (D) 0

  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

  7. Question 3 def 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.") Lab 5 solution

  8. Question 3 if number <=0: print("Input number is not positive.") elif number < 50 and number > 0: print("Input number is less than 50.") #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

  9. Question 3 elif number >=50 and number<=100: if number == 60: print("I FOUND 60.") else: print("Input number ",number, " is >= 50 and <=100") #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

  10. Question 3 elif number > 100: print("Input number is greater than 100.") else: print("It's a strange number.") #TODO 1.05 #Test the input number is greater than 100

  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.

  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

  13. Question 5 • myList = [0,1,2,3,4] • myString = “ hello” • 1) mylist[len(mylist)] • 2) b + 6 = 17 • 3) c = "Hello" + "World" • 4) myString[2] = ‘j’ 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)

  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

  15. Question 6 • 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“) 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

  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

  17. Question 7 • for i in range(0,10): • print (i) • i = 0 • while(i < 11): • print (i) Q15. Consider the following five loops (i-v). Which loops will generate the same output? (i) (ii)

  18. Question 7 • for i in range(10) : • print (i) • for i in range(10%10,10%11) : • print (i) • i = -1 • while(i < 10): • i = i + 1 • print (i) (iii) (iv) (v) (A) i, ii, iii, iv (B) i, ii, iv, v (C) i, iii, iv (D) i, iii, v

  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

  20. Question 8 • for i in range(1,10,2): • if i % 2 == 0: • print (i) 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

  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

  22. Question 9 • x = 0 • y = 10   • def t1(): • x = 1   • def t2(): • x = 2   • def t3(): • x = y Q19. Considering the following code snippet, what would the output be if you ran the function t4()?

  23. Question 9 • def t4(): • x = 4 • t1() • t2() • t3() • print (x) (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?

More Related