0% found this document useful (0 votes)
71 views

The Joy of Computing Using Python: Assignment 3

The document summarizes the solutions to 10 questions related to Python programming. It explains concepts like lists, loops, functions, file handling, and more. Some key points: - Question 1 tests printing items from a list, with the correct answer being to print each item on a new line. - Question 2 tests summing the values in a list using a for loop. - Question 3 tests inserting an item into a list at a specific index using the insert() method. - Question 4 tests reversing a list order using the reverse() method and printing. - Question 5 defines the purpose of the break statement as ending the execution of the current loop.

Uploaded by

Farheen Bano
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

The Joy of Computing Using Python: Assignment 3

The document summarizes the solutions to 10 questions related to Python programming. It explains concepts like lists, loops, functions, file handling, and more. Some key points: - Question 1 tests printing items from a list, with the correct answer being to print each item on a new line. - Question 2 tests summing the values in a list using a for loop. - Question 3 tests inserting an item into a list at a specific index using the insert() method. - Question 4 tests reversing a list order using the reverse() method and printing. - Question 5 defines the purpose of the break statement as ending the execution of the current loop.

Uploaded by

Farheen Bano
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

The Joy of Computing using Python

Assignment 3

NOTE: Python 3.7 has been used for this Assignment

1. What is the expected output for the following code?


c a r t =[ ' c o f f e e ' , ' s u g a r ' , ' c h e e s e ' , ' b u t t e r ' ]
f o r item in c a r t :
i f item== ' s u g a r ' :
print ( ' j a g g e r y ' )
else :
print ( item )

(a) ['coffee', 'jaggery', 'cheese ', 'butter ']


(b) ['coffee ', 'sugar ', 'cheese ', 'butter ']
(c) coffee
jaggery
cheese
butter
(d) coffee jaggery cheese butter

CORRECT ANSWER: (c)


Solution: The items in the list named ‘cart’ are only displayed, not the list itself. So the first two options are
wrong. The print statement adds by default a newline character at the end. Therefore only option three is
right.
2. Which of the following code prints the sum of weights of people in the lift.

(a) sum=0
w e i g h t s =[97 , 5 2 , 6 5 , 4 3 , 7 7 ]
f o r w in w e i g h t s :
sum=sum+w
print (sum)

(b) sum=0
w e i g h t s =[97 , 5 2 , 6 5 , 4 3 , 7 7 ]
f o r w in range ( len ( w e i g h t s ) ) :
sum=sum+w
print (sum)

(c) sum=0
w e i g h t s =[97 , 5 2 , 6 5 , 4 3 , 7 7 ]
f o r w in w e i g h t s :
sum=sum+w
print (sum)

(d) sum=0
w e i g h t s =[97 , 5 2 , 6 5 , 4 3 , 7 7 ]
f o r w in w e i g h t s :
sum=w
print (sum)

1
CORRECT ANSWER: (a)
First option is correct. Indenting is wrong in the second and third options whereas in the third option, the
sum is not calculated by adding the items in the list in the last option.
3. Consider a python list named ‘book titles’. Pick the statement to add ‘Who moved my cheese?’ as the third
item.
Given: book titles = [‘Exam Warriors’, ‘Evil in the Mahabharata’, ‘6 TIMES THINNER’, ‘The Driver in the
Driverless Car’, ‘Evolution’]

(a) book titles.append(2,‘Who moved my cheese?’)


(b) book titles.insert(2,‘Who moved my cheese?’)
(c) book titles.insert(3,‘Who moved my cheese?’)
(d) book titles.append(3,‘Who moved my cheese?’)

CORRECT ANSWER: (b)


Solution: book titles.insert(2,‘Who moved my cheese?’) is correct.
insert() function is used to add an item to the list at a desired index position. The indexing starts from 0.
Therefore option two is correct.
4. Pick the relevant output for the given code.
n=[1 ,4 ,2 ,8 ,21 ,17]
n. reverse ()
print ( n )

(a) [1, 2, 4, 8, 17, 21]


(b) [21, 17, 8, 4, 2, 1]
(c) [17, 21, 8, 2, 4, 1]
(d) [1, 4, 2, 8, 21, 17]

CORRECT ANSWER: (c)


Solution: [17, 21, 8, 2, 4, 1] is correct. The reverse() function exactly reverses the sequence of elements in the
list.
5. Specify the purpose of ‘break’ statement inside a nested loop.
(a) Ends execution of the program
(b) Ends execution of the outermost loop
(c) Skips the current iteration of the loop
(d) Ends the execution of the loop
CORRECT ANSWER: (d)
6. You are given a list, ‘marks’ scored by 30 students. Identify the instruction to find the 2% trimmed mean for
the given data.
(a) m=stats.trim mean(marks,0.2)
(b) m=stats.trim mean(marks,0.03)
(c) m=stats.trim mean(30,0.02)
(d) m=stats.trim mean(marks,0.02)

CORRECT ANSWER: (d)


7. How will you simulate ‘Rolling a Dice’ with six faces by making use of ‘random’ library?
(a) roll= random.choice(1,2,3,4,5,6)
(b) roll= random.range(1,5)
(c) roll= random.randint(1,6)

2
(d) roll= random.random(6)
CORRECT ANSWER: (c)
8. Which of the plots in the options is most likely to be generated from the following code?
import random
import m a t p l o t l i b . p y p l o t a s p l t

def p l a y ( ) :
amt=0
f o r i in range ( 0 , 1 0 0 ) :
r=random . r a n d i n t ( 1 , 1 0 0 0 )
i f ( r !=random . r a n d i n t ( 1 , 1 0 0 0 ) ) :
amt=amt
else :
amt=amt+1000
return amt

l =[]
f o r j in range ( 0 , 1 0 0 ) :
s=0
f o r i in range ( 0 , 1 0 0 ) :
s=s+p l a y ( )
l . append ( s )
x =[]
y =[]
f o r each in l i s t ( set ( l ) ) :
x . append ( each )
y . append ( l . count ( each ) )
p l t . p l o t (x , y , ' ro ' )
p l t . show ( )

(a)

(b)

3
(c)

(d)

CORRECT ANSWER: (b)

9. Assuming, there is no file named ‘file.txt’ on my computer, what does the following code do?
with open ( ' f i l e . t x t ' , ' w ' ) a s f :
f . w r i t e ( ' Hey ! I am w r i t i n g . ' ) ;
f . close ()
with open ( ' f i l e . t x t ' , ' w ' ) a s f :
f . w r i t e ( ' Hey I am w r i t i n g t h e s e c o n d l i n e . ' ) ;
f . close ()
with open ( ' f i l e . t x t ' , ' r ' ) a s f :
print ( f . r e a d ( ) )
f . close ()

(a) Shows error


(b) Displays: Hey I am writing the second line
(c) Displays: Hey! I am writing.Hey I am writing the second line.
(d) Displays: Hey! I am writing.

CORRECT ANSWER: (b)


10. Predict the output
my para= ' i am t o go t o KT i n A '
print ( l i s t ( my para ) )

(a) [‘i’, ‘ ’, ‘a’, ‘m’, ‘ ’, ‘t’, ‘o’, ‘ ’, ‘g’, ‘o’, ‘ ’, ‘t’, ‘o’, ‘ ’,
‘K’, ‘T’, ‘ ’, ‘i’, ‘n’, ‘ ’, ‘A’]
(b) [‘i’, ‘a’, ‘m’, ‘t’, ‘o’, ‘g’, ‘o’, ‘t’, ‘o’, ‘K’, ‘T’, ‘i’, ‘n’, ‘A’]
(c) [‘i’, ‘am’, ‘to’, ‘go’, ‘to’, ‘KT’, ‘in’, ‘A’]
(d) [‘i’, ‘ ’, ‘am’,‘ ’, ‘to’,‘ ’, ‘go’,‘ ’, ‘to’,‘ ’, ‘KT’,‘ ’, ‘in’,‘ ’,
‘A’]

4
CORRECT ANSWER: (a)
First option is correct because every element including whitespace in the input string becomes an element in
the output list.

You might also like