WorksheetsPython Programming Intermediate-1
Total questions: 15
Worksheet time: 12mins
Consider the file "d://test.txt" with the contents as given below:
Hi there,
I am loving Python.
Python is Simple
Choose the correct output from the above code snippet from the given options:
Read String is : Hi there,
Read String is : Python is simple.
Syntax error
Read String is : Hi there,
I am loving Python.
Python is simple.
Consider the file "d://test.txt" with the contents as given below:
Hi there,
I am loving Python.
Python is Simple
Choose the correct output from the above code snippet from the given options:
Read String is : Hi there
Read String is : Hi there,
Syntax error
Hi there,
I am loving Python.
Python is simple.
Consider a file test.txt in D Drive with the following contents:
I Love Python
Choose the output for the following Python code from the given options(Select all applicable):
Stmt2 prints the contents of the file test.txt
Stmt5 will print number of tokens(words) in the file separated by a space ' '
Stmt6 should be uncommented to close the open file.
Stmt3 results in an error
Consider a file read.txt in D Drive with the following contents:
Banana
Apple
Grapes
Choose the output for the following Python code from the given options:
{}
{0: 'Banana', 1: 'Apple', 2: 'Grapes'}
[0: 'Banana', 1: 'Apple', 2: 'Grapes']
{'Banana':0, 'Apple':1, 'Grapes':2}
Consider a file read.txt in D Drive with the following contents:
Banana
Apple
Grapes
Choose the output for the following Python code from the given options:
[]
['Banana', 'Apple', 'Grapes']
{'Banana', 'Apple', 'Grapes'}
Error
Consider a list, fruits=["Mango", "Banana", "Guava", "Strawberry"]. Match the following:
a. fruits[-4:]
b. fruits[-2:]
c. fruits[-4:-1]
d. fruits[1:4]
Select the correct answer for a, b, c & d
['Banana', 'Guava', 'Strawberry']
['Mango', 'Banana', 'Guava', 'Strawberry']
['Mango', 'Banana']
['Mango', 'Banana', 'Guava']
['Guava', 'Banana', 'Mango']
Assume the following list definition:
a = ['foo', 'bar', 'baz', 'qux', 'quux', 'corge']
print(a[4::-2])
a[:] is a
max(a[2:4] + ['grault'])
print(a[-5:-3])
List a is defined as follows:
a = ['a', 'b', 'c']
Which of the following statements adds 'd' and 'e' to the end of a, so that it then equals ['a', 'b', 'c', 'd', 'e']:
a += 'de'
a += ['d', 'e']
a.append(['d', 'e'])
a[len(a):] = ['d', 'e']
a.extend(['d', 'e'])
What gets printed?
names = ['Amir', 'Barry', 'Chales', 'Dao']
print(names[-1][-1])
A
r
Amir
Dao
o
What gets printed from the above piece of code?
11
12
21
22
33
names1 = ['Amir', 'Barry', 'Chales', 'Dao']
loc = names1.index("Edward")
print(loc)
-1
0
4
Edward
An exception is thrown
Which of the following are true of Python lists?
These represent the same list:['a', 'b', 'c'] ['c', 'a', 'b']
A given object may appear in a list more than once
All elements in a list must be of the same type
A list may contain any type of object except another list
There is no conceptual limit to the size of a list
You have a list a defined as follows:
a = [1, 2, 7, 8]
Write a Python statement using slice assignment that will fill in the missing values so that a equals [1, 2, 3, 4, 5, 6, 7, 8].
(a)
Same nested list as the previous question:
x = [10, [3.141, 20, [30, 'baz', 2.718]], 'foo']
What expression returns the list ['baz', 2.718]?
(a)
List a is defined as follows:
a = [1, 2, 3, 4, 5]
Select all of the following statements that remove the middle element 3 from a so that it equals [1, 2, 4, 5]:
a[2:2] = []
del a[2]
a[2] = []
a[2:3] = []
a.remove(3)
