Let's review these basic exercises. What does this print: x = 14 while x < 15: x = x + 1 print x, What does this print: x = 14 while x < 15: x = x + 1 print x, What does this print: i = 0 while i < 3: print 1, i = i + 1 print 0, What does this print: i = 0 while i < 3: print 1, i = i + 1 print 0, What does this print: x = 10 y = 3 while x > 0 and y > 0: x = x - y print x, What does this print: x = 10 y = 3 while x > 0 and y > 0: x = x - y print x, Is this an example of an infinite loop? count = 0; while count > 0: print count count = count + 1; Is this an example of an infinite loop? count = 0 count = count + 1 while not (count > 0): print count count = count + 1 Is this an example of an infinite loop? count = 0 count = count - 1 while not (count > 0): a = count + 1 print a count = a - 2 Is this an example of an infinite loop? count = 0 count = count - 1 while count <= 0: a = count - 1 print a count = a + 2 Is this an example of an infinite loop? count = 0 while count >= 0: a = count - 1 print a count = a + 2 Is this an example of an infinite loop? i = 0 while i < 10: print i i = i - 1 Is this an example of an infinite loop? i = 0 while i < 10: j = i + 3 print j i = j - 1 Is this an example of an infinite loop? i = 0 while i != 10: j = i + 3 print i i = j - 1 Is this an example of an infinite loop? i = 0 while i != 10: print i i = i + 3 Is this an example of an infinite loop? x = 0 if x >= 0: print x x++ What does this print: for i in range(10): print i a) a line of 19 numbers b) a column of 10 numbers c) a line of 9 numbers d) a column of 9 numbers What does this print: for i in range(10): print i, a) a line of 19 numbers b) a column of 10 numbers c) a line of 9 numbers d) a column of 9 numbers What does this print: for j in range(4): for i in range(10): print i a) 4 lines of 10 numbers each b) 10 lines of 4 numbers each c) 1 line with 40 numbers on it d) 40 lines of 1 number each What does this print: for j in range(4): for i in range(10): print i, a) 4 lines of 10 numbers each b) 10 lines of 4 numbers each c) 1 line with 40 numbers on it d) 40 lines of 1 number each What does this print: for j in range(4): for i in range(10): print i, print a) 4 lines of 10 numbers each b) 10 lines of 4 numbers each c) 1 line with 40 numbers on it d) 40 lines of 1 number each What does this print: for j in range(10): for i in range(4): print i, print a) 4 lines of 10 numbers each b) 10 lines of 4 numbers each c) 1 line with 40 numbers on it d) 40 lines of 1 number each What does this print: for j in range(10): for i in range(4): print j, print a) 4 lines of 10 numbers each (which count the columns) b) 10 lines of 4 numbers each (which count the columns) c) 4 lines of 10 numbers each (that count the lines) d) 10 lines of 4 numbers each (that count the lines) What does this print: for j in range(4): for i in range(10): print j, print a) 4 lines of 10 numbers each (which count the columns) b) 10 lines of 4 numbers each (which count the columns) c) 4 lines of 10 numbers each (that count the lines) d) 10 lines of 4 numbers each (that count the lines) What does this print: for i in range(10): for j in range(10): if i == 0 or j == 0 or i == j: print j, else: print " ", print a) numbers organized in an "X" sign b) numbers organized in a square (full of numbers) c) numbers in the shape of an arrow d) an empty square whose contour (border) is made out of numbers What does this print: for i in range(10): for j in range(10): if i == j or i + j == 10: print j, else: print " ", print a) numbers organized in an "X" sign b) numbers organized in a square (full of numbers) c) numbers in the shape of an arrow d) an empty square whose contour (border) is made out of numbers What does the following program's output look like? for i in range(10): for j in range(10): if i == j and i + j == 10: print j, else: print " ", print a) a long line of numbers b) a long column of numbers c) a single number will be printed, the number 5 d) numbers organized in a pattern other than those listed above Write the code below using a for loop: i = 0 sum = 0 while i < 100: sum = sum + i i = i + 2 Needless to say think what the code computes and do the same with a for. Describe in a few words what the loop above does: a) calculates the sum of all even integers in the interval [0, 100] b) calculates the sum of all odd integers in the interval [0, 100] c) calculates the sum of all odd integers in the interval [0, 100) d) calculates the sum of all even integers in the interval [0, 100) e) none of the above, but i does take 100 different values Assume two integer variables n and m. Write a boolean expression that reads like this: m is greater than n or (m + n) is divisible by 19 Hint: assume m has a value of 1 and n a value of 1899 for testing. a) (m > n) or (m + n % 19) == 0 b) (m > n) or (m + n % 19 == 0) c) (m > n) or m + n % 19 == 0 d) (m > n) or (m + n) % 19 == 0 e) none of the above At a certain university, if a student drops a class on or before September 20 (i.e. 9/20), then no record of the course appears on the student's transcript. A grade of W appears if the withdrawal takes place on or before October 15 (i.e. 10/15), and a grade of F or I is recorded otherwise. Assume that the month and day that a student withdraws are stored in two separate int variables called month and day. Select the condition that should be inserted into the following if statement if # What goes here? : print "Assign a grade of W." so that the message will be printed if the student is to receive a grade of W. a) (month >= 9 and month <= 10) and (day <= 15 or day > 20) b) (month == 9 and day > 20) or (month == 10 and day <= 15) c) (month >= 9) and (day > 20 or day <= 15) d) (month == 9 and day > 20) and (month == 10 and day <= 15) e) (month == 9 or month == 10) && (15 < day and day <= 20) Which of the following expressions is logically equivalent to (x<= y)? Check all that apply: a) (y >= x) b) (x < y) and (x == y) c) (x == y) or (x < y) d) (x - y) <= 0 e) (x <= y) and (x == x) Assume that expr1 evaluates to true, expr2 evaluates to false, and expr3 evaluates to true. Which of the following expressions evaluates to true? Check all that apply. a) expr1 and expr2 b) expr1 and expr3 c) expr1 or expr3 d) expr3 or (expr1 and expr2) e) (expr3 or expr1) and expr2 Which of the following code fragments is logically equivalent to this one? if x > y: z = z + 1 else: z = z + 2 Check all that apply. More than one answer may be correct. a) if x > y: z = z + 1 if x <= y: z = z + 2 b) if not (x > y): z = z + 2 else: z = z + 1 c) z = z + 1 if x <= y: z = z + 1 d) if x > y: z = z + 1 elif x <= y: z = z + 2 e) z = z + 2 if not (x > y): z = z - 1 Assume that x is an integer variable. Simplify the following boolean expression: (x < 5) and (x < 25) x < 5 5 < x < 25 not (x > 5) x < 25 none of the above Assume that x is an integer variable. Simplify the following boolean expression: (x < 5) or (x < 25) x < 5 5 < x < 25 not (x > 5) x < 25 none of the above Assume that x is an integer variable. Simplify the following boolean expression: (x > 3) or (x < 5) x == 4 True x > 3 x < 5 none of the above Hints: Try to answer the following questions first: * What if x is 6? * What if x is 10? * What if x is 2? Assume that x is an integer variable. Simplify the following boolean expression: (x > 3) and (x < 5) x == 4 True x > 3 x < 5 none of the above Assume that x and y are integer variables. Consider the following nested if statement. if x > 3: if x <= 5: y = 1; elif x != 6: y = 2 else: y = 3 else: y = 4 If y has the value 2 after executing the above program fragment, then what do you know about x? (x == 4) or (x == 6) x >= 7 (x != 6) and (x < 5) (x <= 3) none of the above Assume that x is an integer variable. Simplify the following boolean expression. not (((x > 0) or (x == 0)) and (x < 10)) Warning: Don't overlook the logical negation at the front. True (x != 0) or (x >= 10) False (x < 0) or (x >= 10) (x < 0) and (x >= 10) Assume that x is an integer variable. Simplify the following boolean expression. not (((x > 0) or (x == 0)) and (x < 10)) Warning: Don't overlook the logical negation (bang!) at the front. True (x != 0) or (x >= 10) False (x < 0) or (x >= 10) (x < 0) and (x >= 10) The legal grades are 'a', 'b', 'c', 'd', and 'f'. Assume legal is a boolean variable and grade a string of one character: Which of the following code fragments will assign legal * the value True if grade contains a legal grade, * and False otherwise? Check all that apply. A: if grade >= 'a' and grade <= 'f': if grade != 'e': legal = True else: legal = False else: legal = False; B: legal = False; if grade >= 'a' and grade <= 'd': legal = True; elif grade == 'f': legal = True; C: legal = (grade >= 'a' and grade <= 'd') or grade == 'f' D: legal = (grade >= 'a' and grade <= 'f') or grade != 'e'; E: if grade == 'a': legal = True; elif grade == 'b': legal = True; elif grade == 'c': legal = True; elif grade == 'd': legal = True; elif grade == 'f': legal = True; else: legal = False; Consider the following code fragment when embedded in a complete program: if x > 3: if x <= 5: y = 1 elif x != 6: y = 2 else: y = 3; Assume that x has a value of 6 at the beginning of the fragment. What value does the variable y hold after the fragment gets executed? 1 2 3 6 impossible to determine from the information given Consider the following code fragment when embedded in a complete program: if x > 3: if x <= 5: y = 1 elif x != 6: y = 2 else: y = 3; Assume that x has a value of 6 at the beginning of the fragment. What value does the variable y hold after the fragment gets executed? 1 2 3 6 impossible to determine from the information given For the following 5 (five) questions consider the following method definitions: def f(x): return g(x) + h (h (x)) def g(x): return 4 * h (x) def h(x): return x * x + k (x) - 1 def k(x): return 2 * (x + 1) Now here are the questions: What does f(2) evaluate to? 1 185 92 400 none of the above What does g(h(2)) evaluate to? 1 185 92 400 none of the above What does k(g(2) + h(2)) evaluate to? 1 185 92 400 none of the above What does f(0) + f(1) + f(2) evaluate to? 1 185 92 400 none of the above What does f(-1) + g(-1) + h(-1) + k(-1) evaluate to? 1 185 92 400 none of the above Consider the following method: def fun(m): n = 0 if (m == 0): n = 1 else: n = m + fun(m - 2) return n Please answer the following two questions: What does fun(4) evaluate to? 4 5 7 it never ends none of the above What does fun(5) evaluate to? 4 5 7 it never ends none of the above Write a function named lengths that * takes a list of strings and * returns a list whose values are * the lengths of the corresponding values (strings) in the given list. Write a program that reads a string and then reports a string profile as follows: a) all letters in the word are reported once b) for each letter the number of occurrences is indicated c) the letters are listed by number of occurrences, in descending order For example if we enter "banana" the report is: a appears 3 times n appears 2 times b appears 1 times The Fire Extinguisher Problem: Suppose you determine that the fire extinguisher in your kitchen loses x percent of its foam every day. How long before it drops below a certain threshold (y percent), at which point it is no longer serviceable? Write a program that lets the user input the x and y and then reports how many weeks the fire extinguisher will last. Write a function that a) receives two lists of integers a and b and b) returns the list of all integers that appear in both a and b The list returned should not have duplicates. Write a function that takes a list of integers and returns a copy of this list with all the duplicates removed.