PCEP 30 02 PCEP Certified Entry Level Python Programmer · Free Practice Question Easy

Question 26

What will be the output of the following code ?

l = [1,2,3,4,5]

i = 0

while i < len(l):

    if l[i] == 3:

        break

    print(i)

    i+=1

  • A

    1

    2

    3

    4

    5

  • B

    1

    2

    3

  • C

    0

    1

  • D

    1

    2

Reveal correct answer

Correct answer: C

Explanation

Initialize a list l with values [1, 2, 3, 4, 5].

Initialize a variable i to 0.

Start a while loop that continues as long as i is less than the length of the list l.

Inside the loop:

Check if the element at the current index (l[i]) is equal to 3.

If it is, break out of the loop using the break statement.

Print the current value of i if the condition is not met.

Increment i by 1 (i += 1).


So, below will be the output.

0

1


Discussion

Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.

You must be logged in to post a comment.

Preparing For

Your Certification?

255+ certifications
Detailed explanations
Free PDF samples

Has All The Questions You Need