📢 Too many exams? Don’t know which one suits you best? Book Your Free Expert 👉 call Now!


    Question

    Consider the following Python code: text =

    "banana" char_to_find = 'a' count = 0 for i in range(len(text)):     if text[i] == char_to_find:         count += 1         if count == 2:             print(i)             break What will be the output of this code?
    A 1 Correct Answer Incorrect Answer
    B 3 Correct Answer Incorrect Answer
    C 5 Correct Answer Incorrect Answer
    D 2 Correct Answer Incorrect Answer
    E None (no output) Correct Answer Incorrect Answer

    Solution

    Correct Answer: B (b-0, a-1, n-2, a-3, n-4, a-5) - i=0, text='b' - i=1, text='a', count=1 - i=2, text='n' - i=3, text='a', count=2. count == 2 is true. Prints 3. Breaks.

    Practice Next
    ask-question