Exam 1 Review: Solutions Capture the Dragons
Total Page:16
File Type:pdf, Size:1020Kb
CSCI 1101B: Introduction to Computer Science Instructor: Prof. Harmon Exam 1 Review: Solutions Capture the Dragons Boolean Expressions 1. Of the following, which are Boolean expressions? (a)2+3 Not Boolean (b)2>3 Boolean (c) num >= 2 Boolean (d) num1 = num2 Not Boolean 2. If x = 3, and x <= y evaluates to True, what values can y have? y >= 3 3. Simplify each of the following expressions: (a) my_num or True Depends on what the value of my_num is. See rules as in #5. (b) False and "Mary" or True True (c) False and ("Mary" or True) False 4. Is x =< y the same as x <= y? No. Python only recognizes the latter. The former is invalid syntax. 5. If x = 1 and y = 2, explain the following results: (a) >>> x and y # = 2 (b) >>> x or y # = 1 Python uses lazy evaluation. • The expression x and y first evaluates x. If x is equivalent to False (or empty/zero), its value is returned. Otherwise, y is evaluated and the resulting value is returned. • The expression x or y first evaluates x. If x is equivalent to True (or non-empty/nonzero), its value is returned. Otherwise, y is evaluated and the resulting value is returned. 1 CSCI 1101B: Introduction to Computer Science Exam 1 Review: Solutions Conditionals 1. What is indentation, and how is it related to the concept of a code block? Indentation is the placement of text (to the left or right). Python uses indenta- tion to associate code into groups, or blocks. 2. What is the difference between a chained conditional and a nested conditional? Chained => if, elif, else Nested => conditionals exist (are "nested") inside each other 3. Simplify the following: x = -67 if x < 0: print("Negative, lieutenant.") else: ifx>0: print("I'm positive.") else: print("My hero, zero!") This code becomes more readable by changing the nested conditional into a chained conditional. 4. What will the following code print given that x = 3, y = 5, and z = 2? ifx<y and x < z: print("even if you're little") elify<x and y < z: print("you can doa lot") else: print("no matter what they tell you") The "no matter what they tell you" case will be printed. Functions 1. What is a function signature, and why is it useful? The function signature tells us the data type for the inputs and outputs of our function. 2. What is the difference between... (a) ...an argument and a parameter? An argument is an actual value passed into the function. A parameter is the variable that is named in the function header. (b) ...a function and a procedure? Both are functions, but a procedure does not return any value. 3. What does scope mean in programming? Can you give an example? Examples vary. Broadly, the scope of some entity refers to which parts of a program and see and/or use that entity. 4. What is the control flow (flow of execution) for the following program: def times_42(num): return num * 42 times_42() Python reads the function header, and then the function call. Because no argu- ment is provided, there will be an error. 2 CSCI 1101B: Introduction to Computer Science Exam 1 Review: Solutions Methods 1. What’s the difference between a built-in function, a method, and a module? A module is a file (can have variables and functions inside). A method is a kind of function that is associated with a class of object. A built-in function performs a common operation and can be used for multiple types. 2. What does "Go U Bears!".isupper() evaluate to? False 3. Can you simplify the above expression to "Go U Bears!".upper()? No 4. Your friend tells you that the following code keeps resulting in a ValueError: daisy_idx = my_string.index("daisies") What advice would you give them? Use find() instead. 5. What does the following evaluate to? ("dance"*2+"rev").rfind("dance") 5 Strings 1. What’s the difference between "\n" and "\t"? Newline vs. tab 2. Given two strings a and b, write an expression that will evaluate to the difference between the length of a and the length of b. For example, if a = "pizza" (length 5) and b = "cat" (length 3), your expression should yield 2. abs(len(a) - len(b)) 3. Given an arbitrary string named s, how would you use slicing notation to retrieve: (a) The first letter? s[0] or s[0:1] (b) The last letter? s[-1] or s[-1:] (c) The first two characters? s[0:2] or s[:2] (d) The substring starting from index 4 until the end of the string? s[4:] 3 CSCI 1101B: Introduction to Computer Science Exam 1 Review: Solutions Types 1. What is the difference between 3 and "3"? 3 is an int value. "3" is a string value. 2. How do we check the type of a data value? With the built-in type() function. 3. What is the function signature for abs()? (number or bool) ! number 4. List the operators we have talked about in order of precedence (Hint: there are at least 27 across the following categories: arithmetic, comparison, equality, assignment, membership, and logical). Operator Category () Arithmetic ** Arithmetic +, - (unary) Arithmetic *, /, //, % Arithmetic +, - (binary) Arithmetic <, <=, >, >= Comparison ==, != Equality =, +=, -=, /=, *=, **=, %=, //= Assignment in Membership and, or, not Logical 5. What types of operands (int, float, string, or bool) are acceptable for each of the arith- metic operators +, -, *, and /? Is the output the same regardless of the data type of the operands? No, the output will not be the same. You can experiment in the shell to see what happens in each case. 4.