Select which is true for for loop
A. Python’s for loop used to iterates over the items of list, tuple, dictionary, set, or string
B. else clause of for loop is executed when the loop terminates naturally
C. else clause of for loop is executed when the loop terminates abruptly
D. We use for loop when we want to perform a task indefinitely until a particular condition is met
A
Select which is true for Python function
A. A Python function can return only a single value
B. A function can take an unlimited number of arguments.
C. A Python function can return multiple values
D. Python function doesn’t return anything unless and until you add a return statement
B, C, D
Strings are immutable in Python, which means a string cannot be modified.
A. True
B. False
True
myString = "pynative"
stringList = ["abc", "pynative", "xyz"]
print(stringList[1] == myString)
True
Select the correct access mode to open a file only for appending?
'a'
x = 0
while (x < 100):
x+=2
print(x)
100
def fun1(name, age=20):
print(name, age)
fun1('Emma', 25)
Emma 25
str1 = "My salary is 7000";
str2 = "7000"
print(str1.isdigit())
print(str2.isdigit())
False
True
aList = [10, 20, 30, 40, 50, 60, 70, 80]
print(aList[2:5])
print(aList[:4])
[30, 40, 50]
[10, 20, 30, 40]
Select the correct method to write a list of lines to a file
A. write(list)
B. writelines(list)
C. writelist(list)
B
for num in range(2,-5,-1):
print(num)
2
1
0
-1
-2
-3
-4
What is an exception in Python?
A. A syntax error
B. A runtime error
C. A logical error
D. A compile-time error
B
str1 = 'Welcome'
print (str1[:6] + ' PYnative')
Welcom PYnative
my_list = ["Hello", "Python"]
print("-".join(my_list))
Hello-Python
Which method is used to read file line by line
A. read(1)
B. readlines(1)
C. readline()
D. line()
C
var = 10
for i in range(10):
for j in range(2, 10, 1):
if var % 2 == 0:
continue
var += 1
var+=1
else:
var+=1
print(var)
21
def add(a, b):
return a+5, b+5
result = add(3, 2)
print(result)
(8,7)
str1 = "my isname isisis jameis isis bond";
sub = "is";
print(str1.count(sub, 4))
6
numbers = [10, 20]
items = ["Chair", "Table"]
for x in numbers:
for y in items:
print(x, y)
10 Chair
10 Table
20 Chair
20 Table
If the file is opened in write mode and already exists, it truncates the existing content and places the filehandle at the beginning of the file.
A. True
B. False
A
x,a,b = 0,0,-5
if a > 0:
if b < 0:
x = x + 5
elif a > 5:
x = x + 4
else:
x = x + 3
else:
x = x + 2
print(x)
2
def fun1(num):
return num + 25
fun1(5)
print(num)
What prints?
NameError
str1 = "PYnative"
print(str1[1:4], str1[:5], str1[4:], str1[0:-1], str1[:-1])
Yna PYnat tive PYnativ PYnativ
student = {
"name": "Emma",
"class": 9,
"marks": 75
}
What is the way to get and print the value of marks key.
print(student['marks'])
What is the purpose of the append() mode when opening a file?
A. Open the file for reading.
B. Open the file for writing and truncate its existing contents.
C. Open the file for writing and append new content at the end.
D. Open the file for writing and overwrite its existing contents.
C