0% found this document useful (0 votes)
53 views8 pages

File - 995010559 - 1741228460 - Practice Question 7 B

The document contains a series of Python code snippets and questions related to their outputs, error types, and string manipulations. It covers various concepts such as tuples, string methods, loops, and random number generation. The document is structured in a quiz format, asking for predictions and explanations of the code behavior.

Uploaded by

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

File - 995010559 - 1741228460 - Practice Question 7 B

The document contains a series of Python code snippets and questions related to their outputs, error types, and string manipulations. It covers various concepts such as tuples, string methods, loops, and random number generation. The document is structured in a quiz format, asking for predictions and explanations of the code behavior.

Uploaded by

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

1.

s="python"
s1=""
s2=""
s3=""
if s[-1]=='n':
s1 = s[0:3]
print(s1)
if 'h' in s:
s2=s[0] + 'i'
print(s2)
if 'z' not in s:
s3 = '7' + s[1:] + 'w'
print(s3)
else:
s = s* 3
print(s)
2. s="python string fun"
while True:
if s[0]=='p':
s = s[3:]
elif s[-1]=='n':
s=s[:2]
else:
break
print(s)

First iteration thon string fun


hon string fun

3. What will be the output of the following code


Msg=”CompuTer”
Msg1=”
for i in range(0, len(Msg)):
if Msg[i].isupper():
Msg1=Msg1+Msg[i].lower()
elif i%2==0:
Msg1=Msg1+’*’
else:
Msg1=Msg1+Msg[i].upper()
print(Msg1)

Final Output cO*P*t*R


4.
Which of the following statement will return an error? T1 is a tuple.
A.T1+(23) B.T1+[3] C. A & B D. None of the
above

5. Consider the following code: Find the Output:


import random
print(int(30+random.random()*5), end=’’)
print(int(30+random.random()*5), end=’’)
print(int(30+random.random()*5), end=’’) 2
print(int(30+random.random()*5))

+
Which string method is used to implement the following:
1. To count the number of characters in the string.len()
2. To change the first character of the string in capital letter. Capitilize
3. To check whether given character is letter or a number. isalnum
4. To change lowercase to uppercase letter. Upper()
5. Change one character into another character. replce()
7. Consider the following code :
import math
import random
print(str(int(math.pow(random.randint(2,4),2))), end =" ")
print(str(int(math.pow(random.randint(2,4),2))), end =" ")
print(str(int(math.pow(random.randint(2,4),2))))
What could be the possible outputs out of the given choices, also state the reason
for each option, why :
a) 2 3 4 b) 9 4 4 c) 16 16 16 d) 2 4 9 e) 4 9 4 f) 4 4 4
8.
What type of error is shown by following statement?
>>>t1=(1,2)
>>>t2
A. Value Error B. Type Error C. Name Error D. None of
the above

9.
Predict the output:
a=(1)*3
print(type(a))
a. int b. None c. No output. d. tuple
10.
Consider a declaration L = (1, 'Python', '3.14').
Which of the following represents the data type of L?
a. list
b. tuple
c. dictionary
d. string
11.
Given a Tuple tup1= (10, 20, 30, 40, 50, 60, 70, 80, 90).
What will be the output of print (tup1 [3:7:2])?
a. (40,50,60,70,80)
b. (40,50,60,70)
c. [40,60]
d. (40,60)
12. The return type of the input() function is
a. string
b. integer
c. list
d. tuple
13. Consider a tuple tup1 = (10, 15, 25, and 30). Identify the statement that will result
in an error.
a. print(tup1[2])
b. tup1[2] = 20
c. print(min(tup1))
d. print(len(tup1))
14. What is the output of following code:
T=(100)
print(T*2)
a. Syntax error
b. (200,)
c. 200
d. (100,100)
15.
Find the output of the following :
Text = "gmail@com"
L = len(Text)
nText = ""
for i in range (0,L):
if Text[i].isupper():
nText = nText + Text[i].lower()
elif Text[i].isalpha():
nText = nText + Text[i].upper()
else:
nText = nText + '#&'
print(nText) GMAIL#&COM(Output)
16. Find the output of the following :
word="green vegetables"
print(word.find('g',2)) 8
print(word.find('veg',2)) 6
print(word.find('tab',4,15)) 10
17. Evaluate the following expression and identify the correct answer.
16 - (4 + 2) * 5 + 2**3 * 4
a. 54
b. 46
c. 18
d. 32
18. What will be the output of the following code?
import random
List=["Delhi","Mumbai","Chennai","Kolkata"]
for y in range(4):
x = random.randint(1,3)
print(List[x],end="#")
a. Delhi#Mumbai#Chennai#Kolkata#
b. Mumbai#Chennai#Kolkata#Mumbai#
c. Mumbai# Mumbai #Mumbai # Delhi#
d. Mumbai# Mumbai #Chennai # Mumbai
19. Predict the output of the following code snippet :
x="hello world"
print(x[:2],x[:-2],x[-2:])
print(x[2:-3],x[-4:-2])

20.
Find the output:
mystr="sTUdeNT"
newstr = ""
count = 0
for i in mystr:
if count%2 != 0:
newstr = newstr + str(count)
elif i.islower():
newstr = newstr + i.upper()
else:
newstr = newstr + i
count += 1
newstr = newstr + mystr[:1]
print("The new string is:", newstr)

21.
Find the output of the following Python code :
for Subject in [‘Physics’,’Chemistry’,’Mathematics’,’English’]:
print(Subject)
if Subject[0]==’M’
break
else:
print(‘Compulsory’)
print(‘Optional’)

22.
What are the possible outcome(s) executed from the following code ? Also
specify the maximum and minimum values that can be assigned to variable
PICKER.
import random
PICKER = random randint (0, 3)
COLOR [‘Blue’,’Pink’,’Green’,’Red’]
for I in COLOR :
for J in range (1, PICKER):
Print (I, end = ‘;’)
Print ()
23. myString = "ijk"
stringList = ["abc", "ijk", "xyz"]

print(stringList[1] == myString)
print(stringList[1] is myString)
24. Which of the following number of iterations won’t be printed when this Python
code is run?

for letter in 'Python':

if letter == 'h':

continue

print('Current Letter : ' + letter)


25. String="Hi User"
for i in range(10):
print(String[i])
26.
s="Welcome students" Come students
while s: Come stden
if s[0]=='W':
s = s[3:]
elif s[-1]=='s':
s=s[:-2]
else:
break
print(s)
27. Find and write the output of the following python code:
s='school2@com'
k=len(s)
m=" "
for i in range(0,k):
if(s[i].isupper()):
m=m+str(int(s[i].islower()))
elif s[i].isalpha():
m=m+str(int(s[i].isupper()))
else:
m=m+'bb'
print(m)
28.
Difference between the following:
1) slice() and [:]
2) in and not in
3) [n:] and[:n]
4) count() and index()
5) find() and in
6) lstrip() and rstrip()
7) split() and partition()

29.
Find the output: The new string is:
mystr="wElCo#Me" W1EL3CO5#M7Ew

newstr = ""
count = 0
for i in mystr:
if count%2 != 0:
newstr = newstr + str(count)
if i.islower():
newstr = newstr + i.upper()
else:
newstr = newstr + i
count += 1
newstr = newstr + mystr[:1]
print("The new string is:", newstr)

30. ccc
31.

32. Which of these Python data structures cannot have duplicate items and does not support
ordering?
a. list
b. tuple
c. dictionary
d. set
33.
Find the output:
mystr="HARMONIOUS"
L = len(mystr)
str2=''
str3=''
for i in range(0,L,2):
str2=str2 + mystr[i+1]+mystr[i]
for ch in str2:
if ch>='R' and ch<='U':
str3+='$'
else:
str3+=ch.lower()
print(str3)
print(mystr)

34.

35.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy