wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python sec-AT loop and string

Total questions: 54

Worksheet time: 29mins

Name
Class
Date
1.

What is the primary purpose of the if statement in Python?

a)

To execute a block of code based on a condition

b)

To perform mathematical operations

c)

To repeat a block of code

d)

To define a function

2.

What will be the output of the following code snippet? x = 10 if x > 5: print("x is greater than 5")

a)

x is greater than 5

b)

x is less than 5

c)

x is equal to 5

d)

No output

3.

Which keyword is used to execute a block of code if the condition in the if statement is false?

a)

else

b)

elif

c)

while

d)

for

4.

What is the purpose of the elif statement in Python?

a)

To execute a block of code if the previous conditions are false

b)

To define a loop

c)

To perform arithmetic operations

d)

To exit from a loop

5.

How can you execute multiple statements under a single if block in Python?

a)

Separate statements with a semicolon

b)

Indent the statements to the same level

c)

Use the and keyword between statements

d)

Use the elif keyword

6.

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")

a)

x is less than 5

b)

x is greater than 15

c)

x is between 5 and 15

d)

No output

7.

What is the syntax for a while loop in Python?

a)

while condition:

b)

while condition():

c)

while (condition):

d)

while loop condition:

8.

How can you exit a loop prematurely in Python?

a)

Using the break statement

b)

Using the continue statement

c)

Using the pass statement

d)

Using the exit function

9.

What is the purpose of the for loop in Python?

a)

To execute a block of code repeatedly until a condition is false

b)

To iterate over items in a sequence

c)

To execute a block of code a fixed number of times

d)

To define a function

10.

What is the syntax for a for loop in Python?

a)

for item in sequence:

b)

for item in range(n):

c)

for index in range(len(sequence)):

d)

All of the above

11.

What will the following code snippet print? fruits = ["apple", "banana", "cherry"] for fruit in fruits: print(fruit)

a)

apple banana cherry

b)

[“apple”, “banana”, “cherry”]

c)

0 1 2

d)

No output

12.

How can you skip the current iteration of a loop and continue with the next iteration?

a)

Using the skip statement

b)

Using the pass statement

c)

Using the break statement

d)

Using the continue statement

13.

What is the purpose of the range() function in Python?

a)

To generate a sequence of numbers

b)

To iterate over items in a sequence

c)

To define a function

d)

To execute a block of code repeatedly until a condition is false

14.

What will the following code snippet print? for i in range(3): print(i)

a)

0 1 2

b)

1 2 3

c)

2 1 0

d)

3 2 1 0

15.

What will be the output of the following code? for i in range(1, 6): if i == 3: continue print(i)

a)

1 2

b)

1 2 3

c)

1 2 4 5

d)

1 2 4

16.

What will be printed by the following code? num = 5 while num > 0: print(num) num -= 1 if num == 3: break else: print("Done")

a)

5 4 3 2 1

b)

5 4

c)

Done

d)

5 4 3

17.

What is the output of the following code? num = 0 while num < 5: print(num) num += 1 else: print("Loop completed.")

a)

0 1 2 3 4

b)

0 1 2 3 4 Loop completed.

c)

Loop completed.

d)

This code will result in an error.

18.

What will be the output of the following code? for i in range(3): for j in range(3): print(i * j, end=' ') print()

a)

0 0 0 0 1 2 0 2 4

b)

0 1 2 0 2 4 0 3 6

c)

0 0 0 0 1 2 0 2 4 0 3 6

d)

0 0 0 0 1 2 0 1 2

19.

What is the purpose of the pass statement in Python?

a)

To exit from a loop prematurely

b)

To skip the current iteration of a loop

c)

To execute a block of code if a condition is false

d)

To do nothing and act as a placeholder

20.

What is the purpose of the else block in a loop in Python?

a)

To execute if the loop encounters an error

b)

To execute if the loop completes without encountering a break statement

c)

To execute if the loop encounters a continue statement

d)

To execute if the loop encounters a pass statement

21.

What is the output of the following code? for i in range(1, 6): if i % 2 == 0: print(i) continue print("*")

a)

2 4 *

b)

2 4 *

c)

2 * 4

d)

2 * 4

22.

What will be the output of the following code? x = 5 while x > 0: print(x) x -= 1 else: print("Done")

a)

5 4 3 2 1 Done

b)

Done 5 4 3 2 1

c)

5 4 3 2 1

d)

Done

23.
What letter will be printed on the screen after running this code:
a)
e
b)
x
c)
t
d)
Nothing prints
24.

Each item in a string has an "address" or index. The first index in a Python string is what?

a)

0

b)

1

c)

a

d)

A

25.
What output will this code produce?
a)
Exeter
b)
4
c)
6
d)
city
26.
What output will this code produce?
a)
3
b)
20
c)
25
d)
17
27.
What output will this code produce?
a)
1
b)
3
c)
2
d)
4
28.

Returning a range of characters (part of a string) is done using which syntax?

a)

Slice

b)

Splice

c)

Cut

d)

Select

29.

What is the output from this code?

a)

ell

b)

llo

c)

ello,

d)

Hello

30.

What is the output from this code?

a)

, W

b)

Wor

c)

orl

d)

lro

31.

Python accepts single ('), double (") and triple (''' or """) quotes to denote strings. Which of the following is NOT acceptable Python syntax?

a)

print('Hello World')

b)

print("Hello World")

c)

print('Hello World")

d)

print(""'Hello World'"")

32.

What is printed by the following statement?

print("P" not in "APCSP")

a)

True

b)

False

c)

Error

33.

Evaluate the following expression:

"dog" < "Dog"

a)

They are the same word

b)

True

c)

False

34.

For strings, the + operator represents

a)

Concatenation

b)

Addition

c)

Appending

d)

Recantation

35.

data = "No Way!" The expression len(data) evaluates to:

a)

7

b)

9

c)

8

d)

6

36.

What will the output be from the following code?

print("3+4")

a)

7

b)

34

c)

3+4

d)

SyntaxError

37.

print("12"*3)

What would this print?

a)

121212

b)

36

c)

24

d)

12*3

38.
What will the output be from the following code?
print("Hello" + "world" + "today")
a)
Helloworldtoday
b)
Hello world today
c)
"Hello" "world" "today"
d)
SyntaxError
39.
What is the word (command) used to display numbers and text on the screen?
a)
print
b)
input
c)
output
d)
command
40.
What is a variable?
a)
A box(memory location) where you store values
b)
a type of graphics
c)
Data type
d)
a type of memory
41.
What symbol is used in python to assign values to a variable?
a)
equals =
b)
plus + 
c)
forward slash /
d)
asterisk *
42.
What is the name of the programming language we are learning?
a)
Scratch
b)
Python
c)
Pie thin
d)
Scribble
43.

Is python an interpreter or compiler programming language?

a)

Compiler

b)

Interpreter

44.

Which of the operator can be used in Strings?

a)

+

b)

*

c)

Both of the above

d)

None of the above

45.

When we create a program to extract each alphabet from a given string, what is it known as?

a)

Transpose

b)

Traverse

c)

Translate

d)

Terminate

46.

Which of the following is not a comparison operator?

a)

<

b)

>

c)

==

d)

!==

47.

word = "amazing"

For the given string if we run word[2:5] what does it mean?

a)

It will extract alphabets from index 2 to index 4

b)

It will extract alphabets from index 2 to index 5

c)

It will extract alphabets from position 2 to position 4

d)

It will extract alphabets from position 2 to position 5

48.

string="HELlo"

What will be the result of string.isupper()?

(a)  

49.

s1="Hello"

n1=10

print(s1+n1)

What is the error in this code?

a)

Print statement not written in quotes

b)

Numeric value not written in quotes

c)

colon not written after the print statement

d)

string cannot be added to a number

50.

example = "helle"

print(example.rfind("e") )

a)

-4

b)

4

c)

3

d)

1

51.

What will the below Python code will return?

If str1="save paper,save plants"


print(str1.find("save"))

a)

It returns the first index position of the first occurance of "save" in the given string str1.

b)

It returns the last index position of the last occurance of "save" in the given string str1.

c)

It returns the last index position of the first occurance of "save" in the given string str1.

d)

It returns the first index position of the last occurance of "save" in the given string str1.

52.

In Python, list is mutable

a)

False

b)

True

53.

Which of the following would give an error?

a)

list1=[]

b)

list1=[]*3

c)

list1=[2,8,7]

d)

None of the above

54.

Sort () function in list by default sort the values in __________

a)

Ascending order

b)

Decending order