Docsity

CSCI 1301 MIdterm Practice (Python) Questions With Answers, Exams of Nursing

Toggle sidebar
bg1
CSCI 1301 MIdterm Practice (Python) Questions
With Answers
Question 1.
Which of the following is the correct extension of the Python file?
Correct Answer: .py
Question 2.
Does python code need to be compiled or interpreted?
Correct Answer: python code is both compiled and interpreted
Question 3.
A syntax error is a _____________________.
Correct Answer: The program contains invalid code that cannot be understood.
It is found before the program is ever run by the interpreter.
Question 4.
Which of the following is used to define a block of code in Python language?
Correct Answer: Indentation
Question 5.
Which of the following functions is a built-in function in python?
Correct Answer: print( )
Question 6.
Which of the following lines is a properly formatted comment?
# This is a comment / " " " This is a comment " " " / " " " This is
a comment
" " "
Correct Answer: All of them
Question 7.
Which of the following is a legal Python identifier?
Correct Answer: _oneForAll
Question 8.
Which of the following is an example of an invalid assignment or declaration statement?
Correct Answer: x + 1 = 3
Question 9.
Assume x=80; y=30; z=4
What is the result of x // y // z?
Correct Answer: 0
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20

Partial preview of the text

Download CSCI 1301 MIdterm Practice (Python) Questions With Answers and more Exams Nursing in PDF only on Docsity!

CSCI 1301 MIdterm Practice (Python) Questions

With Answers

Question 1.

Which of the following is the correct extension of the Python file?

Correct Answer: .py

Question 2.

Does python code need to be compiled or interpreted?

Correct Answer: python code is both compiled and interpreted

Question 3.

A syntax error is a _____________________.

Correct Answer: The program contains invalid code that cannot be understood. It is found before the program is ever run by the interpreter.

Question 4.

Which of the following is used to define a block of code in Python language?

Correct Answer: Indentation

Question 5.

Which of the following functions is a built-in function in python?

Correct Answer: print( )

Question 6.

Which of the following lines is a properly formatted comment?

This is a comment / " " " This is a comment " " " / " " " This is

a comment

" " "

Correct Answer: All of them

Question 7.

Which of the following is a legal Python identifier?

Correct Answer: _oneForAll

Question 8.

Which of the following is an example of an invalid assignment or declaration statement?

Correct Answer: x + 1 = 3

Question 9.

Assume x=80; y=30; z=

What is the result of x // y // z?

Correct Answer: 0

Assume a=80.0; b=30.0; c=4.0 What is the result of a / b / c?

Correct Answer: 0.

Question 11.

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

Correct Answer: 15

Question 12.

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"

Correct Answer: "There Everywhere" on the same line as "Here"

Question 13.

Which statement reads value as integer type?

Correct Answer: x =int( input( 'Enter an integer' ) )

Question 14.

To find distance between point (x1, y1) and point (x2, y2), which expression is correct?

Correct Answer: distance = math.sqrt( (x2 -x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) )

Question 15.

Assume

age = 10

pet = "dog"

pet_name = "Gerald"

To generate the output "Gerald is 10 years old dog", which print statement is NOT correct?

Correct Answer: print( f"pet_name, ' is ', age, ' years old ', pet " )

Question 16.

Which of the following is not a core data type in Python programming?

Correct Answer: Clsss

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)

Correct Answer: [-12, -43, 67, 55, 99, -32, 12, -2]

Question 24.

What output of result in following Python program? list1 = [1,2,3,4]

list2 = [2,4,5,6]

result1 = list1 + list

result2 = list1 * 2

print( result1 )

print( result2 )

Correct Answer: 1 2 3 4 2 4 5 6 1 2 3 4 1 2 3 4

Question 25.

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])

Correct Answer: [1 ,7]

Question 26.

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?

Correct Answer: 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.)

Question 27.

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?

Correct Answer: my_tuple = (5, 10, 15, 25, 30)

mylist = list(my_tuple) mylist.insert(3, 20) my_tuple = tuple(mylist)

Question 28.

Which of the following is NOT a valid way to define Python's dictionary?

Correct Answer: dic = {('Albert': 23), ('Mary': 23), ('Simon': 43)}

Suppose dictionary

d1 = {"Susan":42, "John":40, "Peter":45}

to delete the entry for "john" what command do we use?

Correct Answer: del d1["John"]

Question 30.

Suppose dictionary

d1 = {"Susan":42, "John":40, "Peter":45}

To get Susan's age, what command do we use?

Correct Answer: d1.get("Susan")

Question 31.

What is the output of print str[2:5] if str = 'Python Programming'?

Correct Answer: tho

Question 32.

What is the output of print str[2: ] if str = 'Python Programming'?

Correct Answer: thon Programming

Question 33.

What is the output of print str[ : : 3 ] if str = 'Python Programming'?

Correct Answer: Ph oai

Question 34.

What will be the output of the following Python code?

Correct Answer: ABCD

Question 35.

What is the output of print str.title() if str = 'abcd'?

Correct Answer: Abcd

Question 36.

Given str="Hello, labor day!" what is the output of str.count( 'l' )?

Correct Answer: 3

Question 37.

Given str="Hello, labor day!" what is the output of str.find( 'a' )?

Correct Answer: 8

Question 38.

What will be the output of the following Python code? >>> url ='https://ung.edu/'

>>> url.startswith('http')

Correct Answer: True

What are the outputs of print the following statements?

'xyyabcdxyy'.strip('xyy')

'xyyabcdxyy'.lstrip('xyy') 'xyyabcdxyy'.rstrip('xyy')

Correct Answer: abcd abcdxyy xyyabcd

Question 49.

Assume str = "abcdefg", to check whether string str contains "cd", use ________

Correct Answer: "cd" in str

Question 50.

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( ) )

Correct Answer: True False False True Abcd

Question 51.

Every if statement requires an associated else statement, but not every else statement requires an associated if statement.

Correct Answer: False

Question 52.

Which of the following best describes this code snippet?

Correct Answer: If the variable count is not equal to 400, "Hello World" will be printed.

Question 53.

Which of the following expressions best represents the condition "if the grade is between 75 and 100"?

Correct Answer: if 75 < grade and grade < 100 :

Question 54.

In Python, a block statement is

Correct Answer: a set of statements that are all indented to the same column.

Question 55.

In Python, the symbol "=" and the symbol "==" are used synonymously (interchangeably).

Correct Answer: False

Assume done = False; x = 10; y = 12

The expression not done or x > y is true.

Correct Answer: True

Question 57.

Assume y = 11; str = "Goodbye" The expression len( str ) < y is true

Correct Answer: True

Question 58.

The statement

if a >= b :

a = a+ 1

else :

b = b -

will do the same thing as the statement if a < b :

b = b-

else :

a = a+

Correct Answer: True

Question 59.

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?

Correct Answer: 3

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")

Correct Answer: 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

Question 64.

For the following code, how to rewrite it by short hand if...else statement?

if x > y: print( x + 1 )

else : print( y - 1 )

Correct Answer: print(x + 1) if x > y else print(y-1)

Question 65.

For the following code, how to rewrite it by short hand if...else statement?

if x > y:

print( x + 1 )

else :

print( y - 1 )

Correct Answer: print(x + 1) if x > y else print(y-1)

Question 66.

Which expression checks whether the list my_list contains the value 15?

Correct Answer: 15 in my_list

Question 67.

Which of the following for loop will cause the body of the loop to be executed 100 times?

Correct Answer: for x in range (0, 100)

Question 68.

What output is produced by the following code fragment?

x = 0

for i in range(1, 5):

x += i

print(x)

Correct Answer: 10

Given that str is a String, what does the following loop do?

str = 'abcdef'

for x in str[ : -1 ] :

print( x, end=' ')

Correct Answer: it prints str out forwards: a b c d e f

Question 70.

Given that str is a String, what does the following loop do?

str = 'abcdef'

for x in str[ : : -1 ] :

print( x, end=' ')

Correct Answer: it prints s out backwards: f e d c b a

Question 71.

What output is produced by the following code fragment?

for value in range (20, -1, -1):

if value % 4 != 0:

print( value, end=', ' )

Correct Answer: print even number from 2 to 18: 2, 4, 6, 8, 10, 12, 14, 16, 18

Question 72.

What output is produced by the following code fragment?

x = 0

while x < 100:

x *= 2

print(x)

Correct Answer: Error. Go to infinite loop

Question 73.

What output is produced by the following code fragment?

x = 1

while x < 100:

x *= 2

print(x)

Correct Answer: 128

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)

Correct Answer: 2 4 8 16 32

Question 79.

What output is produced by the following code fragment?

str = "abcdef"

while i in str:

print( i, end=' ')

Correct Answer: NameError, i is not defined.

Question 80.

What output is produced by the following code fragment?

str = "abcdef"

i = 0

while i< len(str):

print( str[i], end=' ')

i += 1

Correct Answer: a b c d e f

Question 81.

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

Correct Answer: A B C 1 2 3 D E F 4 5 6 5 0

For str = "abc123_def456_$5.0", which code can print letters only?

The output should be: a b c d e f

Correct Answer: 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

Question 83.

Which keyword is used for function?

Correct Answer: def

Question 84.

Which are the advantages of functions in python?

Correct Answer: All of above

Question 85.

What are the two main types of functions in Python?

Correct Answer: Built-in function & User defined function

Question 86.

Where is function defined in Python?

Correct Answer: All of the mentioned (Module, Class, Another function)

Question 87.

The function must return a value

Correct Answer: False

Question 88.

Which correctly defines two parameters x and y for a function definition:

def calc_val( ):

Correct Answer: calc_val (x, y) :

Question 89.

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( )?

Correct Answer: calc_val( k, i+ j, 99 )

Question 90.

A local variable is defined inside a function, while a global variable is defined outside any function.

Correct Answer: True

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)

Correct Answer: 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

Question 97.

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 )

Correct Answer: 2 1

1 2

Question 98.

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)

Correct Answer: 1 2 1 1 1 2 1 1

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) )

Correct Answer: 9 27 16 64

Question 100.

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) )

Correct Answer: 6

15

Question 101.

What will be the output of the following Python code?

def foo( arg1, *args ):

result = 0

for i in args:

result += i

return result

print( foo(1, 2, 3) )

print( foo(1, 2, 3, 4, 5) )

Correct Answer: 5

14

What will be the output of the following Python code? my_list = ['One', 'Two', 'Three']

for i, x in enumerate( my_list):

print('{}: {}'.format(i, x),end=" ")

Correct Answer: 0: One 1: Two 2: Three

Question 109.

What is the output of the following python code fragment?

values = [x**2 for x in range(0, 11)]

print(values)

Correct Answer: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

Question 110.

What will be the value of 'result' in following Python program?

list1 = [1,2,3,4]

list2 = [2,4,5,6]

list3 = [2,6,7,8]

result = list()

result.extend( i for i in list1 if i not in (list2+list3) and i not in result )

result.extend( i for i in list2 if i not in (list1+list3) and i not in result )

result.extend( i for i in list3 if i not in (list1+list2) and i not in result )

print(result)

Correct Answer: [1, 3, 5, 7, 8]

Question 111.

What output in following Python program?

list1 = [1,2,3,4]

list2 = [2,4,5,6]

list3 = [2,6,7,8]

result1 = list1 + list

result2 = list2 + list

for i in result1:

if i not in result2:

print( i )

Correct Answer: 1 3

Question 112.

Assume, you are given two lists:

list1 = [1,2,3,4,5]

list2 = [6,7,8,9,10]

Your task is to update list1 which has all the elements of list1 and list2.

Which of the following python statement would you choose?

Correct Answer: list1.extend(list2)

Which of these about a dictionary is false?

Correct Answer: The keys of a dictionary can be accessed using values

Question 114.

What will be the output of the following Python code?

my_dict={1:"A", 2:"B", 3:"C"}

for i, j in my_dict.items():

print( i, j, end=" " )

Correct Answer: 1 A 2 B 3 C

Question 115.

What will be the output of the following Python code?

my_dict={1:"A", 2:"B", 3:"C"}

for i in my_dict.items():

print( i , end=" " )

Correct Answer: 1: 'A' 2:'B' 3:'C'

Question 116.

What will be the output of the following Python code?

dict1 = {1:"A", 2:"B", 3:"C"}

dict2 = {4:"D", 5:"E"}

dict1.update(dict2)

dict3 = dict1.copy( )

dict3[ 2 ] = "Change"

print(dict3)

Correct Answer: { 1: 'A', 2: 'Change', 3: 'C', 4: 'D', 5: 'E' }

Question 117.

What will be the output of the following Python code?

total={}

def insert( items ):

if items in total:

total[items] += 1

else:total[items] = 1

insert('Apple')

insert('Banana')

insert('Apple')

insert('Orange')

insert('Apple')

insert('Orange')

print( total )

Correct Answer: {'Apple': 3, 'Banana': 1, 'Orange': 2}