PT 2
PT 2
1. Out of the following, find those identifiers, which cannot be used for naming Variables or functions
in a Python program:
Total * Tax, While, Class, Switch, 3rd Row, finally, Column 31, Total.
2. What will be the output of the following statement:
a) -3.33 b) 6.0 c) 0.0 d) -13.33
3. What possible output from the given output from the given options is expected to be displayed when
the following python code is executed?
import random
signal =[“RED”,”YELLOW”,”GREEN”]
for k in range(2,0,-1)
r=random.randrange(k)
print(signal[r],end=”#”)
a) YELLOW#RED# b) RED#GREEN# c) GREEN#RED# d) YELLOW#GREEN#
4. Identify the invalid python statement from the following:
a) d=dict() b) e={ } c) f=[ ] d) g=dict{ }
5. Consider the statements given below and then choose the correct output from the given options:
mystr=”MISSISSIPPI”
print(mystr[:4]+”#”+mystr[-5:])
a) MISSI#SIPPI b) MISS#SIPPI c) MISS#IPPIS d) MISSI#IPPIS
6. Identify the statement from the following which will raise an error:
a) print(“A”*3) b) print(5*3) c) print(“15”+3) d) print(“15”+”13”)
7. Select the correct output of the following code:
event=”G20 Presidency@2023”
L=event.split(‘ ‘)
print(L[::-2])
a) “G20” b) [“presidency@2023”] c) [“G20”] d) “presidency@2023”
Q 8and Q9 are ASSERTION AND REASONING based questions.
Mark the correct choice as
(a) Both A and R are true and R is the correct explanation for A
(b)Both A and R are true and R is not the correct explanation for A
(c) A is True but R is False (d)A is false but R is True
8. Assertion (A): The expression “HELLO”.sort( ) in python will give an error.
Reason(R): sort() does not exist as a method/function for strings in python.
9. Assertion(A): List is an immutable data type
Reasoning(R): When an attempt is made to update the value of an immutable variable, the old variable is
destroyed and a new variable is created by the same name in memory.
10. State True or False:
“In a Python program, if a break statement is given in a nested loop, it terminates the execution of all
loops in one go.”
11. What will be the output of the following statement:
Which of the following statement(s) would give an error during execution of the following code?
tup = (20,30,40,50,80,79)
print(tup) #Statement 1
print(tup[3]+50) #Statement 2
print(max(tup)) #Statement 3
tup[4]=80 #Statement 4
Options:
a. Statement 1 b. Statement 2 c. Statement 3 d. Statement 4
12. print(3-2**2**3+99/11)
a. 244 b. 244.0 c. -244.0 d. Error
13. Which of the following will delete key-value pair for key = “Red” from a dictionary D1?
a. delete D1("Red") b. del D1["Red"] c. del.D1["Red"] d. D1.del["Red"]
14. Explain fetchall( ) and fetchmany( )
Options:
a. PYTHON-IS-Fun b. PYTHON-is-Fun c. Python-is-fun d. PYTHON-Is –Fun
16. Which string method is used to implement the following:
a) To count the number of characters in the string.
b) To change the first character of the string in capital letter.
c) To check whether given character is letter or a number.
d) To change lowercase to uppercase letter.
e) Change one character into another character.
17. The code given below accepts five numbers and displays whether they are even or odd:
Observe the following code carefully and rewrite it after removing all syntax and logical errors:
Underline all the corrections made:
def evenodd():
for i in range(5):
num=int(input(“enter a number”)
if num/2==0:
print(“even”)
else:
print(“odd”)
evenodd( )
18. Write the output displayed on execution of the following python code:
Ls=[“HIMALAYA”,”NILGIRI”,”ALASKA”,”ALPS”]
d={ }
for s in ls:
if len(s)%4==0:
d[s]=len(s)
for k in d:
print(k,d[k],sep=”#”)
19. Write a user defined function in python name showgrades(s) which takes the dictionary S as an
argument. The dictionary S contains Name:[Eng,Math,Science] as key:value pairs.The function
displays the corresponding grade obtained by the students according to the following grading rules:
i) To remove the item whose key is “NISHA” from a dictionary named students.
For example, if the dictionary students contains {“ANITA”:90,“NISHA”:76,”ASHA”:92}, then after
removal the dictionary should contain {“ANITA”:90,”ASHA”:92
ii) To display the number of occurrences of the substring “is” in a string named message
For example if the string message contains “This is his book”, then the output will be 3.
21. A tuple named subject stores the names of different subjects. Write the python commands to convert
the given tuple to a list and thereafter delete the last element of the list.
23. Sumit wants to write a code in python to display all the details of the passengers from the table flight
in MYSQL database Tavel. The table contains the following attributes:
F_code: Flight code(String)
F_name: Name of the filght(String)
Source: Daparture city of flight(Sring)
Consider the following to establish connectivity between python and MYSQL:
Username: root
Password:airplane
Host: localhost
24. Predict the output of the Python code given below:
25. Kabir wants to write a program in Python to insert the following record in the table named Student in
MYSQL database, SCHOOL:
rno(Roll number )- integer
name(Name) – string
DOB (Date of birth) – Date
Fee – float
Note the following to establish connectivity between Python and MySQL:
Username – root
Password – tiger
Host – localhost
The values of fields rno, name, DOB and fee has to be accepted from the user.
Help Kabir to write the program in Python.