CSCI1301 Midterm / Final
For the following code, the program will print the word "Here" and then print: print("Here ", end="") print("There " + "Everywhere") print("But not " + "in Texas")
"There Everywhere" on the same line as "Here"
Assume str = "abcdefg", to check whether string str contains "cd", use ________
"cd" in str
What output is produced by the following code fragment? str = 'abcd' for x in range( len(str) ): print( x.upper(), end=' ')
Error
What will be the output of the following Python code? for x in range(2.0): print(x, end=' ')
Error
What output is produced by the following code fragment? x = 0 while x < 100: x *= 2 print(x)
Error. Go to infinite loop
A while statement always executes its loop body at least once.
False
Every if statement requires an associated else statement, but not every else statement requires an associated if statement.
False
In Python, the symbol "=" and the symbol "==" are used synonymously (interchangeably).
False
The function must return a value.
False
What are the outputs of the following Python script ? str = "abcd1234" print( str.isalnum( ) ) print( str.isalpha( ) ) print( str.isdigit( ) ) print( str.islower( ) ) print( str.capitalize( ) )
True False False True Abcd 1234
What output is produced by the following code fragment? limit =100; num1 = 15; num2 = 40 if limit <= 100 : if ( num1 == num2) : print( "lemon") print("orange")print( "grape")
orange grape
What output is produced by the following code fragment? str = "abc123_def456_$5.0" i = 0 while i< len(str): if str[i].isalnum(): print( str[i].upper(), end=' ') i += 1
A B C 1 2 3 D E F 4 5 6 5 0
What output is produced by the following code fragment? str = 'abcd' for character in str: print(character.upper(), end=' ')
A B C D
What are the two main types of functions in Python?
Built-in function & User defined function
Given the following tuple: my_tuple = (5, 10, 15, 25, 30) Suppose you want to update the value of this tuple at 3rd index to 20. Which of the following option will you choose?
The above actions are invalid for updating tuple
A syntax error is a _____________________.
The program contains invalid code that cannot be understood. It is found before the program is ever run by the interpreter.
What is wrong, logically, with the following code? if x > 10: print("Large") elif x > 6 and x <= 10: print("Medium") elif x > 3 and x <= 6: print("Small") else : print("Very small")
There is no logical error, but there is no need to have (x <= 10) in the second conditional or (x <= 6) in the third conditional
What will be the output of the following Python code? >>> url ='https://ung.edu/' >>> url.startswith('http')
True
What is the correct extension of the Python file?
.py
Assume x=80; y=30; z=4 What is the result of x // y // z?
0
What will be the output of the following Python code? for x in range(10): if x == 5: break else: print( x, end=' ') else: print("Here")
0 1 2 3 4
What will be the output of the following Python code? for x in range(10): if x == 5: continue else: print( x, end=' ') else: print("Here")
0 1 2 3 4 6 7 8 9 Here
What will be the output of the following Python code? for x in range(5): if x == 5: break else: print( x, end=' ') else: print("Here")
0 1 2 3 4 Here
Assume a=80.0; b=30.0; c=4.0 What is the result of a / b / c?
0.67
What output of result in following Python program? list1 = [1,2,3,4] list2 = [2,4,5,6] result1 = list1 + list2 result2 = list1 * 2 print( result1 ) print( result2 )
1 2 3 4 2 4 5 6 1 2 3 4 1 2 3 4
Choose the container that best fits the described data. 1) Student test scores that may later be adjusted, ordered from best to worst. 2) A single student's name and their final grade in the class. 3) Names and current grades for all students in the class. Question options:
1) List 2) Tuple 3) Dict
What output is produced by the following code fragment? x = 0 for i in range(1, 5): x += i print(x)
10
What is the output of the following Python's code fragment: x = [-12, -43, 55, 67, -32, 12, -2]print( x[-2] )
12
What output is produced by the following code fragment? x = 1 while x < 100: x *= 2 print(x)
128
What value is contained in the integer variable size after the following statements are executed? size = 18 size = size + 12 size = size * 2 size = size / 4
15
Which expression checks whether the list my_list contains the value 15?
15 in my_list
What output is produced by the following code fragment? x = 1 while x < 100: x *= 2 if x == 64: break print(x, end=' ')
2 4 8 16 32
What output is produced by the following code fragment? x = 1 while x < 100: x *= 2 if x == 64: break print(x, end=' ') else: print(x)
2 4 8 16 32
What output is produced by the following code fragment? x = 1 while x < 100: x *= 2 if x == 64: continue print(x, end=' ')
2 4 8 16 32 128
What output is produced by the following code fragment? x = 1 while x < 100: if x == 64: break x *= 2 print(x, end=' ')
2 4 8 16 32 64
What output is produced by the following code fragment? x = 1 while x < 100: x *= 2 print(x, end=' ') else: print(x)
2 4 8 16 32 64 128 128
What output is produced by the following code fragment? num = 1; max = 20 while num < max: num += 4 print(num)
21
Given str="Hello, labor day!" what is the output of str.count( 'l' )?
3
Given the nested if-else structure below: if a > 0 : if b < 0: x = x + 5 else: if a > 5: x = x + 4 else : x = x + 3 else : x = x + 2 If x is currently 0, a = 5 and b = 5, what will x become after the above statement is executed? Question
3
What is the output of print "yxyxyxyxyxy".count('yxy') ?
3
What will be the output of the following Python code? def printMax(a, b):if a > b:print(a, 'is maximum')elif a == b:print(a, 'is equal to', b)else:print(b, 'is maximum') x = 3 y = 4 printMax( x+1, y)
4 is equal to 4
What is the output of print( len( [ 'abcd', 786 , 2.23, 'john', 10 ] ) )?
5
Given str="Hello, labor day!" what is the output of str.find( 'a' )?
8
How many iterations will the following for loop execute? for count in range(2, 20, 2): print( count, end= ' ' )
9
What will be the output of the following Python code? str = 'abcd' print(str.upper())
ABCD
What will be the output of the following Python code? str = 'abcd'print(str.upper())print(str[1].upper())
ABCD B
What is the output of print str.title() if str = 'abcd' ?
Abcd
Where is function defined in Python?
All of above: Module Class Another function
Which are the advantages of functions in python?
All of above: Reducing duplication of code Decomposing complex problems into simpler pieces Improving clarity of the code
Which of the following lines is a properly formatted comment? # This is a comment " " " This is a comment " " " " " " This is a comment " " "
All of them
Which of the following is not a core data type in Python programming? Number, String, Boolean List, Tuple Dict, Set Clsss
Clsss
What is "Hello World".replace( "l", "e")?
Heeeo Wored
Which of the following best describes this code snippet? if (count != 400) print("Hello World!")
If the variable count is not equal to 400, "Hello World" will be printed.
Which of the following is used to define a block of code in Python language? Reserved key Indentation Brackets [ ] Parentheses ( )
Indentation
Does python code need to be compiled or interpreted?
Python code is both compiled and interpreted
What will be the output of the following Python code? student= ( 'Jack', 19, 3.6) result= ", ".join( str(d) for d in student ) print( student )
Jack, 19, 3.6
What output is produced by the following code fragment? str = "abcdef" while i in str: print( i, end=' ')
NameError, i is not defined.
What is the output of print str[ : : 3 ] if str = 'Python Programming' ?
Ph oai
Consider the following code that will assign a letter grade of 'A', 'B', 'C', 'D', or 'F' depending on a student's test score: if score >= 90: grade = 'A' if score >= 80: grade = 'B' if score >= 70: grade = 'C' if score >= 60: grade = 'D' else : grade = 'F'
This code will work correctly only if grade < 70
A function definition must be evaluated by the interpreter before the function can be called.
True
A local variable is defined inside a function, while a global variable is defined outside any function.
True
Assume done = False; x = 10; y = 12 The expression not done or x > y is true.
True
Assume y = 11; str = "Goodbye" The expression len( str ) < y is true
True
The statement if a >= b : a = a+ 1 else : b = b -1 will do the same thing as the statement if a < b : b = b-1 else : a = a+1
True
What is the output of print 'abcdefcdghcd'.split('cd') ?
['ab', 'ef', 'gh', '']
What is the output of print 'abcdefcdghcd'.split('cd', 1) ?
['ab', 'efcdghcd']
What is the content of mylist in the following Python's code fragment? mylist = [-12, -43, 55, 99, -32, 12, -2]mylist.insert(2, 67)
[-12, -43, 67, 55, 99, -32, 12, -2]
What is the output of the following python code fragment?: list1 = [1, 2, 5, 7]list2 = [4, 2, 5, 3]print([item for item in list1 if item not in list2])
[1, 7]
What will be the output from the following Python code? x = ['a', 'b', 'c', 1, 2, 3] print( x [ : : -1] ) print( x [ : : 1] ) print( x [ : : 2] ) print( x [ 1: : 2] ) print( x [ 1 : 4] )
[3, 2, 1, 'c', 'b', 'a']['a', 'b', 'c', 1, 2, 3]['a', 'c', 2]['b', 1, 3]['b', 'c', 1]
Which of the following is a legal Python identifier? 1ForAll _oneForAll one/4/all one For all
_oneForAll
What output is produced by the following code fragment? str = 'abcd' for character in str: print(character, end=' ') str.upper()
a b c d
What output is produced by the following code fragment? str = "abcdef" i = 0 while i< len(str): print( str[i], end=' ') i += 1
a b c d e f
In Python, a block statement is
a set of statements that are all indented to the same column.
What are the outputs of print the following statements ? 'xyyabcdxyy'.strip('xyy') 'xyyabcdxyy'.lstrip('xyy') 'xyyabcdxyy'.rstrip('xyy')
abcd abcdxyy xyyabcd
Which correctly defines two parameters x and y for a function definition:
calc_val (x, y) :
Given a function definition:def calc_val(a, b, c):and given variables i, j, and k, which are valid arguments in the call calc_val( )?
calc_val( k, i+ j, 99 )
Suppose dictionary d1 = {"Susan":42, "John":40, "Peter":45} To get Susan's age, what command do we use?
d1.get("Susan")
Which keyword is used for function?
def
Suppose dictionary d1 = {"Susan":42, "John":40, "Peter":45} to delete the entry for "john" what command do we use?
del d1["John"]
Which of the following is NOT a valid way to define Python's dictionary?
dic = {('Albert': 23), ('Mary': 23), ('Simon': 43)}
To find distance between point (x1, y1) and point (x2, y2), which expression is correct?
distance = math.sqrt( (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) )
Which of the following for loop will cause the body of the loop to be executed 100 times?
for x in range (0, 100)
Which of the following expressions best represents the condition "if the grade is between 75 and 100"? Question options:
if 75 < grade and grade < 100 :
Given that str is a String, what does the following loop do? str = 'abcdef' for x in str[ : : -1 ] : print( x, end=' ')
it prints s out backwards: f e d c b a
Given that str is a String, what does the following loop do? str = 'abcdef' for x in str[ : -1 ] : print( x, end=' ')
it prints str out forwards: a b c d e f
What data type for [ 'abcd', 786 , 2.23, 'john', 10 ] ?
list
Want to add 20 at 3rd place in my_tuple my_tuple = (5, 10, 15, 25, 30) And update the tuple as (5, 10, 15, 20, 25, 30) Which of the following code can help to realize the purpose?
my_tuple = (5, 10, 15, 25, 30)mylist = list(my_tuple)mylist.insert(3, 20)my_tuple = tuple(mylist)
What output is produced by the following code fragment? num = 1; max = 20 while num < max: if num%2 == 0: print( num, end=' ') num += 1
print even number from 2 to 18: 2, 4, 6, 8, 10, 12, 14, 16, 18
What output is produced by the following code fragment? for value in range (20, -1, -1): if value % 4 != 0: print( value, end=', ' )
print from 20 down to 0, except those that are evenly divisible by 4: 19, 18, 17, 15, 14, 13, 11, 10, 9, 7, 6, 5, 3, 2, 1,
Which of the following functions is a built-in function in python? randint( ) print() sqrt() factorial()
print()
For the following code, how to rewrite it by short hand if...else statement? if x > y: print( x + 1 ) else : print( y - 1 )
print(x + 1) if x > y else print(y-1)
Assuming num1 = 1; num2 = 2 what output is produced by the following code fragment given the assumptions below? if num1 < num2 : print( 'red', end=' ')if (num1 +5) < num2 : print( 'white', end=' ' )else : print( 'blue', end=' ')print( 'yellow')
red blue yellow
What will be the output of the following Python code? colors = ('red', 'green', 'blue') rgb = '-'.join(colors)
red-green-blue
What will be the output of the following Python code? "abcdef".find("cd") == "cd" in "abcdef"
return False
Assume str is 'abcd', to output it by upper case: A B C D. Which code fragment is correct?
str = 'abcd' for x in range(len(str) ): print( str[x].upper(), end=' ')
What is the output of print str[2:5] if str = 'Python Programming'?
tho
What is the output of print str[2: ] if str = 'Python Programming'?
thon Programming
Which of the following is an example of an invalid assignment or declaration statement? age = 30 money, dollars = 0; cents = 0 years = 1; months = 12; days = 365 x + 1 = 3
x + 1 = 3
Assume x = ['a', 'b', 'c', 1, 2, 3], to print items backward in x, which following code is NOT correct?
x = ['a', 'b', 'c', 1, 2, 3] for i in ''.join( reversed(x) ): print(i, end=' ') print()
Which statement reads value as integer type? x = input( ) x = input( 'Enter an integer' ) x =int( input( 'Enter an integer' ) ) x, y = input( 'Enter two integer' ).split( )
x =int( input( 'Enter an integer' ) )