Font size
WorksheetsPython sec-AT loop and string
Total questions: 54
Worksheet time: 29mins
What is the primary purpose of the if statement in Python?
To execute a block of code based on a condition
To perform mathematical operations
To repeat a block of code
To define a function
What will be the output of the following code snippet? x = 10 if x > 5: print("x is greater than 5")
x is greater than 5
x is less than 5
x is equal to 5
No output
Which keyword is used to execute a block of code if the condition in the if statement is false?
else
elif
while
for
What is the purpose of the elif statement in Python?
To execute a block of code if the previous conditions are false
To define a loop
To perform arithmetic operations
To exit from a loop
How can you execute multiple statements under a single if block in Python?
Separate statements with a semicolon
Indent the statements to the same level
Use the and keyword between statements
Use the elif keyword
What will the following code snippet print? x = 10 if x < 5: print("x is less than 5") elif x > 15: print("x is greater than 15") else: print("x is between 5 and 15")
x is less than 5
x is greater than 15
x is between 5 and 15
No output
What is the syntax for a while loop in Python?
while condition:
while condition():
while (condition):
while loop condition:
How can you exit a loop prematurely in Python?
Using the break statement
Using the continue statement
Using the pass statement
Using the exit function
What is the purpose of the for loop in Python?
To execute a block of code repeatedly until a condition is false
To iterate over items in a sequence
To execute a block of code a fixed number of times
To define a function
What is the syntax for a for loop in Python?
for item in sequence:
for item in range(n):
for index in range(len(sequence)):
All of the above
What will the following code snippet print? fruits = ["apple", "banana", "cherry"] for fruit in fruits: print(fruit)
apple banana cherry
[“apple”, “banana”, “cherry”]
0 1 2
No output
How can you skip the current iteration of a loop and continue with the next iteration?
Using the skip statement
Using the pass statement
Using the break statement
Using the continue statement
What is the purpose of the range() function in Python?
To generate a sequence of numbers
To iterate over items in a sequence
To define a function
To execute a block of code repeatedly until a condition is false
What will the following code snippet print? for i in range(3): print(i)
0 1 2
1 2 3
2 1 0
3 2 1 0
What will be the output of the following code? for i in range(1, 6): if i == 3: continue print(i)
1 2
1 2 3
1 2 4 5
1 2 4
What will be printed by the following code? num = 5 while num > 0: print(num) num -= 1 if num == 3: break else: print("Done")
5 4 3 2 1
5 4
Done
5 4 3
What is the output of the following code? num = 0 while num < 5: print(num) num += 1 else: print("Loop completed.")
0 1 2 3 4
0 1 2 3 4 Loop completed.
Loop completed.
This code will result in an error.
What will be the output of the following code? for i in range(3): for j in range(3): print(i * j, end=' ') print()
0 0 0 0 1 2 0 2 4
0 1 2 0 2 4 0 3 6
0 0 0 0 1 2 0 2 4 0 3 6
0 0 0 0 1 2 0 1 2
What is the purpose of the pass statement in Python?
To exit from a loop prematurely
To skip the current iteration of a loop
To execute a block of code if a condition is false
To do nothing and act as a placeholder
What is the purpose of the else block in a loop in Python?
To execute if the loop encounters an error
To execute if the loop completes without encountering a break statement
To execute if the loop encounters a continue statement
To execute if the loop encounters a pass statement
What is the output of the following code? for i in range(1, 6): if i % 2 == 0: print(i) continue print("*")
2 4 *
2 4 *
2 * 4
2 * 4
What will be the output of the following code? x = 5 while x > 0: print(x) x -= 1 else: print("Done")
5 4 3 2 1 Done
Done 5 4 3 2 1
5 4 3 2 1
Done
Each item in a string has an "address" or index. The first index in a Python string is what?
0
1
a
A
Returning a range of characters (part of a string) is done using which syntax?
Slice
Splice
Cut
Select
What is the output from this code?
ell
llo
ello,
Hello
What is the output from this code?
, W
Wor
orl
lro
Python accepts single ('), double (") and triple (''' or """) quotes to denote strings. Which of the following is NOT acceptable Python syntax?
print('Hello World')
print("Hello World")
print('Hello World")
print(""'Hello World'"")
What is printed by the following statement?
print("P" not in "APCSP")
True
False
Error
Evaluate the following expression:
"dog" < "Dog"
They are the same word
True
False
For strings, the + operator represents
Concatenation
Addition
Appending
Recantation
data = "No Way!" The expression len(data) evaluates to:
7
9
8
6
What will the output be from the following code?
print("3+4")
7
34
3+4
SyntaxError
print("12"*3)
What would this print?
121212
36
24
12*3
print("Hello" + "world" + "today")
Is python an interpreter or compiler programming language?
Compiler
Interpreter
Which of the operator can be used in Strings?
+
*
Both of the above
None of the above
When we create a program to extract each alphabet from a given string, what is it known as?
Transpose
Traverse
Translate
Terminate
Which of the following is not a comparison operator?
<
>
==
!==
word = "amazing"
For the given string if we run word[2:5] what does it mean?
It will extract alphabets from index 2 to index 4
It will extract alphabets from index 2 to index 5
It will extract alphabets from position 2 to position 4
It will extract alphabets from position 2 to position 5
string="HELlo"
What will be the result of string.isupper()?
(a)
s1="Hello"
n1=10
print(s1+n1)
What is the error in this code?
Print statement not written in quotes
Numeric value not written in quotes
colon not written after the print statement
string cannot be added to a number
example = "helle"
print(example.rfind("e") )
-4
4
3
1
What will the below Python code will return?
If str1="save paper,save plants"
print(str1.find("save"))
It returns the first index position of the first occurance of "save" in the given string str1.
It returns the last index position of the last occurance of "save" in the given string str1.
It returns the last index position of the first occurance of "save" in the given string str1.
It returns the first index position of the last occurance of "save" in the given string str1.
In Python, list is mutable
False
True
Which of the following would give an error?
list1=[]
list1=[]*3
list1=[2,8,7]
None of the above
Sort () function in list by default sort the values in __________
Ascending order
Decending order
