CSCI 1301 MIdterm Practice (Python)
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
Assume a=80.0; b=30.0; c=4.0 What is the result of a / b / c?
0.67
What will be the output of the following Python code? def change(x = 0, y = 1): x = x + y y = y + 1 print(x, y) change(x = 0 , y = 1) change(x = 1 , y = 0) change(y = 1 , x = 0) change(y = 0 , x = 1)
1 2 1 1 1 2 1 1
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.
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
Which expression checks whether the list my_list contains the value 15?
15 in my_list
In Python, the symbol "=" and the symbol "==" are used synonymously (interchangeably).
False
The function must return a value
False
What is the output of print str[ : : 3 ] if str = 'Python Programming' ?
Ph oai
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
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
What will be the output of the following Python code? def func(a, b=5, c=10): print('a is', a, 'and b is', b, 'and c is', c) func(3, 7) func(25, c = 24) func(c = 50, a = 100)
a is 3 and b is 7 and c is 10 a is 25 and b is 5 and c is 24 a is 100 and b is 5 and c is 50
What are the outputs of print the following statements ? 'xyyabcdxyy'.strip('xyy') 'xyyabcdxyy'.lstrip('xyy') 'xyyabcdxyy'.rstrip('xyy')
abcd abcdxyy xyyabcd
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"?
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? limit =100; num1 = 15; num2 = 40 if limit <= 100 : if ( num1 == num2) : print( "lemon") print("orange") print( "grape")
orange grape
What will be the output of the following Python code? x = 50 def func(x): print('x is', x) x = 2 print('Changed local x to', x) func(x) print('x is now', x)
x is 50 Changed local x to 2 x is now 50
Which of the following 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 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
Given str="Hello, labor day!" what is the output of str.count( 'l' )?
3
What are the two main types of functions in Python?
Built-in function & User defined function
Which of the following is used to define a block of code in Python language?
Indentation
What output is produced by the following code fragment? str = "abcdef" while i in str: print( i, end=' ')
NameError, i is not defined.
What will be the output of the following Python code? >>> url ='https://ung.edu/' >>> url[ : :-1].startswith('/')
None
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 (Once a tuple is created, you cannot change its values. Tuples are unchangeable, or immutable as it also is called.)
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.
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 will be the output of the following Python code? >>> url ='https://ung.edu/' >>> url.startswith('http')
True
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 Abcd1234
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?
_oneForAll
In Python, a block statement is
a set of statements that are all indented to the same column.
Which correctly defines two parameters x and y for a function definition: def calc_val( ):
calc_val (x, y) :
Suppose dictionary d1 = {"Susan":42, "John":40, "Peter":45} To get Susan's age, what command do we use?
d1.get("Susan")
Does python code need to be compiled or interpreted?
python code is both compiled and interpreted
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) print(rgb)
red-green-blue
What will be the output of the following Python code? "abcdef".find("cd") == "cd" in "abcdef"
return False
What will be the output of the following Python code? def change(a, b): a = a + 1 b = b + 1 print(a, b) change( a = 1, b = 0 ) change( a = 0, b = 1 )
2 1 1 2
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: x *= 2 print(x, end=' ') else: print(x)
2 4 8 16 32 64 128 128
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?
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
What will be the output of the following Python code? def foo( *args ): result = 0 for i in args: result += i return result print( foo(1, 2, 3) ) print( foo(1, 2, 3, 4, 5) )
6 15
Given str="Hello, labor day!" what is the output of str.find( 'a' )?
8
What will be the output of the following Python code? def power(x, y=2): result = 1 for i in range(y): result = result * x return result print( power(3) ) print( power(3, 3) ) print( power(4) ) print( power(4, 3) )
9 27 16 64
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 will be the output of the following Python code?
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
Which are the advantages of functions in python?
All of above
Where is function defined in Python?
All of the mentioned (Module, Class, Another function)
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?
Clsss
What will be the output of the following Python code? student= ( 'Jack', 19, 3.6) result= ", ".join( student ) print( student )
Error
What output is produced by the following code fragment? x = 0 while x < 100: x *= 2 print(x)
Error. Go to infinite loop
What output is produced by the following code fragment? x = 1 while x < 100: if x == 64: continue x *= 2 print(x, end=' '
Error. Go to infinite loop
Every if statement requires an associated else statement, but not every else statement requires an associated if statement.
False
What is "Hello World".replace( "l", "e")?
Heeeo Wored
What will be the output of the following Python code? def say(message, times = 1): print(message * times) say('Hello') say('World', 5)
Hello WorldWorldWorldWorldWorld
Which of the following best describes this code snippet?
If the variable count is not equal to 400, "Hello World" will be printed.
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 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
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
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 )
Which keyword is used for function?
def
What output is produced by the following code fragment? for value in range (20, -1, -1): if value % 4 != 0: print( value, end=', ' )
print even number from 2 to 18: 2, 4, 6, 8, 10, 12, 14, 16, 18
Which of the following functions is a built-in function in python?
print( )
Assume age = 10 pet = "dog" pet_name = "Gerald" To generate the output "Gerald is 10 years old dog", which print statement is NOT correct?
print( f"pet_name, ' is ', age, ' years old ', pet " )
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)
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)
For str = "abc123_def456_$5.0", which code can print letters only? The output should be: a b c d e f
str = "abc123_def456_$5.0" i = 0 while i< len(str) -1 : if str[i].isalpha(): print( str[i], end=' ') elif str[i].isdigit(): pass i += 1
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?
x + 1 = 3
Which statement reads value as integer type?
x =int( input( 'Enter an integer' ) )
What will be the output of the following Python code? x = 50 def func( ): global x print('x is', x) x = 2 print('Changed global x to', x) func( ) print('x is now', x)
x is 50 Changed global x to 2 Value of x is 2