CS111 - Python Review
In Python3, which functions are used to accept input from the user 1. input() 2. raw_input() 3. rawinput() 4. string()
1
What is the output of the following print() function? print(sep = '--', 'Ben', 25, 'California') 1. Syntax Error 2. Ben-25-California 3. Ben 25 California
1
What is the result of this code? 7%(5//2)
1
sentence ="The quick brown fox jumps over the lazy dog." print(sentence.count("fox")) print(sentence.count("dog")) print(sentence.count("cap")) #print(sentence.count(25,"fox") print(sentence.count("o")) print(sentence.count("o",15)) print(sentence.count("o",15,30))
1 1 0 Error 4 3 2
Select all the valid String creation in Python 1. str1 = "str1" 2. str1 = 'str1' 3. str1 = str("str1") 4. str1 = "'str1"'
1,2
What is the output of the following code? p, q, r = 10, 20 ,30 print(p, q, r)
10 20 30
What is the output of the following code?: def print_sum(variable, y): _____variable += 1 ____print(variable + y) print_sum(5, 8)
14
def outerFun(a, b): ____def innerFun(c, d): ________return c + d ____return innerFun(a, b) ____return a result = outerFun(5, 10) print(result)
15
What is the output of the following x = 36 / 4 * (3 + 2) * 4 + 2 print(x)
182.0
What is the output of the following code? print(int(2.999))
2
What is the type of the following variable x = -5j 1. int 2. complex 3. real 4. imaginary
2
Write the code '2 raised to the 5th'
2**5
what is the code for the remainder of 20//6?
20 % 6
What is the output of this code? float("210" * int(input("Enter a number: "))) Enter a number: 2
210210.0
What is the output for this? 10 / 2
5.0
What is the output of print(2 ** 3 ** 2) 64 or 512
512
What is the output of the following code? str1 = "my isname isisis jameis isis bond"; sub = "is"; print(str1.count(sub, 4))
6
What is the output of the following code? var = "James" * 2 * 3 print(var)
JamesJamesJamesJamesJamesJames
What is the result of the following code? str = "Hello world!" print(str[6])
w
What is the output of the following string comparison print("John" > "Jhon") print("Emma" < "Emm")
True False
Evaluate as either *True* or *False* 1 == 1 and 2 == 2 1 == 1 and 2 == 3 1 == 1 or 2 == 3 2 < 1 or 3 != 3 not 1 == 1 not 1 > 7
True False True False False True
Taking into consideration operator precedence, evaluate the following as either *True* or *False*. False == False or True False == (False or True) (False == False) or True
True False True *In other words, == has a higher precedence than or
Evaluate these as *True* or *False* 7 > 5 7 > 7 7 >= 7.0
True, False, True
Evaluate these as *True* or *False* 2 == 2 2 != 3 "hello" == "hello"
True, True, True
Assuming it has already been assigned, delete the variable 'variable'.
del variable
What is an *elif* statement short for?
else if
What is the output of the following? "5" + 4
error
x = 75 def myfunc(): ____x = x + 1 ____print(x) myfunc() print(x)
error
print("spam" * 3.0)
error, can't multiply a string and a float
Any statement that consists of a word followed by information in parentheses is called a ___ call.
function
What is the data type of print(type(0xFF))
int
What is the data type of print(type(10))
int
Convert the string "2" to an integer.
int("2")
chr(50 + ord('9'))
k
What is the output of the following code? var= "James Bond" print(var[2::-1])
maJ
What is the result of this code? if 1 + 1 * 3 == 6: ____print("yes") else: ____print("no")
no
What is a *float*?
numbers that have decimals
if you are unsure of operator precedence, which operators should you use
parenthesis - they are the highest precedence
Are concatenated strings created with single or double quotes?
single or double quotes
What is the output of the following code?: def print_with_exclamation(word): ____print(word + "!") print_with_exclamation("spam") print_with_exclamation("eggs")
spam! eggs!
What is the output of the following? x = "spam" x +="eggs" print(x)
spameggs
What is the output of the following: >>>print("spam" * 3)
spamspamspam
User input always has the type ___.
string
Bitwise shift operators (<<, >>) has higher precedence than Bitwise And(&) operator (true or false)
true
How many times can you reassign a variable?
As many times as you want!
What is the output of print(abs(-45.300))
45.3
Write a string that outputs the text 'Brian's mother'
'Brian\'s mother'
What is the exponentiation operator?
**
What are Python's Boolean operators?
*and*, *or*, and *not*
In addition to using pre-defined functions, you can create your own functions by using the ___ statement.
*def*
What is the output of the following code x = 50 def fun1(): ____x = 25 ____print(x) fun1() print(x)
25 50
What is the output of the following code? valueOne = 5 ** 2 valueTwo = 5 ** 3 print(valueOne) print(valueTwo)
25 125
What is the answer from the following code: def count_zeros(string): ____total = 0 ____for c in string: ________if c == 0: ____________total += 1 ____return total print(count_zeros('00102'))
3
What is the output for this? 20 // 6
3
What is the output of the following str1 = "PYnative" print(str1[1:4], str1[:5], str1[4:], str1[0:-1], str1[:-1]) 1. PYn PYnat ive PYnativ vitanYP 2. Yna PYnat tive PYnativ vitanYP 3. Yna PYnat tive PYnativ PYnativ
3
What is the output of the following assignment operator y = 10 x = y += 2 print(x) 1. 12 2. 10 3. SynatxError
3
What is the output of the following round() function call? print(round(100.2563, 3)) print(round(100.000056, 3)) 1. 100.256 and 100 2. 100.256 and 100.000 3. 100.256 and 100.0
3
Which function is used to get the ASCII code of a character 1. ascii(number) 2. char(number) 3. chr(number)
3
What is the output of this code? num = 7 if num > 3: ____print("3") if num < 5: ____print("5") if num == 7: ____print("7")
3 7
What is the answer from the following code: def calculate (num1, num2=4): ____res = num1 * num2 ____ print(res) calculate(5, 6)
30
What is the output of the following code? def add(x, y): ____return x + y def do_twice(func, x, y): ____return func(func(x, y), func(x, y)) a = 5 b = 10 print(do_twice(add, a, b))
30
What is the output of the following? int("3" + "4")
34
x = 6 y = 2 print(x ** y) print(x // y)
36 3
Which operator has higher precedence in the following list 1. % Modulus 2. & BitWise AND 3. **, Exponent 4. > Comparison
4
sentence ="The quick brown fox jumps over the lazy dog." print(sentence.rfind("quick")) print(sentence.rfind("quick",20)) print(sentence.rfind(".")) print(sentence.rfind("i")) print(sentence.rfind("e",20)) print(sentence.rfind("e",35))
4 -1 43 6 33 -1
sentence ="The quick brown fox jumps over the lazy dog." print(sentence.find("quick")) print(sentence.find("quick",20)) print(sentence.find(".")) print(sentence.find("i")) print(sentence.find("e",20)) print(sentence.find("e",10,25))
4 -1 43 6 28 -1
What is the output for this? 6 * 7.0
42.0
What is the output of the following code?: def max(x, y): ____if x >= y: ________return x _____else: ________return y print(max(4, 7)) z = max(8, 5) print(z)
7 8
What is the output of this code? spam = "7" spam = spam + "0" eggs = int(spam) + 3 print(float(eggs))
73.0
Force x, which is assigned the integer 8, to become a string
>>>str(x)
y = chr(65) print(y) print(chr(123))
A {
What is a nested if statement?
An if statement within an if statement
What is the output of the following function call def fun1(name, age=20): ____print(name, age) fun1('Emma', 25)
Emma 25
What is the output of the following code? var1 = 1 var2 = 2 var3 = "3" print(var + var2 + var3)
Error
What is the output of the following number comparison function call print( (1.1 + 2.2) == 3.3 )
False
What is the output of the following string operations (True False) str = "My salary is 7000" print(str.isalnum())
False
What is the output of the following code: print(bool(0), bool(3.14159), bool(-3), bool(1.0+1j))
False True True True
print(bool(0), bool(3.14159), bool(-3), bool(1.0+1j))
False True True True
for each of the following are the statements True or False? 50 < 5 "Abc" < "Def" 50 < 100 "50" < "100" 10%3 <= 1 10%3 == 0 10%3 !=0
False, True, True, False, True, False, True
Python is processed at runtime by the __. There is no need to __ the program before executing it.
Interpretor; compile
What 3 characters are allowed in Python programming? In terms of naming variables, which of these 3 are you not allowed to have as the first character?
Letters, numbers, and underscores. Numbers
s = 'Don Quijote' print(s[4]) print(s[-1]) print(s[-7]) print(s[4:8]) print(s[:]) print(s[:4]) print(s[4:]) print(s[:6] + s[6:]) print(s[-7:-3]) print(s[4::-1])
Q e Q Quij Don Quijote Don Quijote Don Quijote Quij Q noD
What is the result of this code? x = 4 y = 2 if not 1 + 1 == y or x == 4 and 7 == 8: ____print("yes") elif x > y: ____print ("no")
Result is no *Step 1: if not 1 + 1 == y or x == 4 and 7 == 8 As per the operator precedence, the first 'and' is considered, x == 4 and 7 == 8, which evaluates to False. With this, the expression can be simplified to... if not 1 + 1 or False
class_name = "CS 111" campus = "CSUSM" print(class_name[1]) print(campus[0:2]) print(campus[:2]) print(class_name[3:]) print(campus[:3], class_name[:2]) print(campus[int(class_name[4])
S CS CS 111 CSU CS S
What is the output of the following code salary = 8000 def printSalary(): ____salary = 12000 ____print("Salary:", salary) printSalary(); print("Salary:", salary)
Salary: 12000 Salary: 8000
What is concatenation?
The addition of two or more strings.
What part of an if statement should be indented?
The statements within it
A string is immutable in Python? . (True or False)
True
Python does not support a character type; a single character is treated as strings of length one. (True or False)
True
a, b = 12, 5 if a + b: ____print('True') else: ____print('False')
True
How do you create a new line?
\n
Write the expression 'x = x + 3' more concisely. Write the expression 'x = x * 3 more concisely.
x +=3 x *=3
What is the answer from the following code: str = "pynative" print (str[1:3])
yn