what is the output of the following code snippet - written exam 3 Si106

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

What is the output of the following code snippet? words = ['a', 'abba', 'bab', 'cab', 'bac', 'abc'] for word in words: count = 0 if word[0] == 'b': count += 1 print(count)

0 0 1 0 1 0 (count is reset to 0 inside the loop, so the value printed in each iteration only reflects the condition for the current word.)

What is the output of the following code snippet? words = ['the', 'quick', 'brownish', 'fox', 'jumped'] for word in words: count = 0 if len(word) > 4: count += 1 print(count)

0 1 1 0 1 (The count variable is reset to 0 within each iteration of the loop, so it only increments for the current word if its length is greater than 4: 'the' (length 3) -> count is 0 'quick' (length 5) -> count is 1 'brownish' (length 8) -> count is 1 'fox' (length 3) -> count is 0 'jumped' (length 6) -> count is 1)

What is the output of the following code snippet? my_list = ["cat","dog","fish"] for i in range(len(my_list)): print(i)

0 1 2 (The loop iterates over the range of the length of my_list, which is 3)

What is the output of the following code snippet? words = ['a', 'abba', 'bab', 'cab', 'bac', 'abc'] for i in range(len(words[1])): print(i)

0 1 2 3 (The loop iterates over the range of the length of the second word words[1] ("abba") which is 4.)

What is the output of the following code snippet? words = ['the', 'quick', 'brownish', 'fox', 'jumped'] count = 0 for word in words: if len(word) >= 5: count += 1 print(count)

3 (The words list has strings with varying lengths. The code checks each word to see if its length is greater than or equal to 5: 'the' (length 3) -> not counted 'quick' (length 5) -> counted 'brownish' (length 8) -> counted 'fox' (length 3) -> not counted 'jumped' (length 6) -> counted Thus, the count becomes 3.)

What is the output of the following code snippet? words = ['a', 'abba', 'bab', 'cab', 'bac', 'abc'] count = 0 for word in words: if word[0] == 'a': count += 1 print(count)

3 (There are 3 words in the list that start with 'a'.)

What is the output of the following code snippet? READ IT CAREFULLY!! var1 = 10 var2 = 5 var3 = 10 if var1 < var2 or var3: print("A") elif var2 > var3: print("B") else: print("C") print("Done")

A Done (This code snippet evaluates the following: var1 < var2 is 10 < 5, which is False. var3 evaluates to 10, which is True (non-zero numbers evaluate to True in Python). The or operator returns True if either operand is True. Since the first condition if var1 < var2 or var3 is True, it prints "A" and the execution stops further checks in elif and else. Then it prints "Done".)

What is the output of the following code snippet? READ IT CAREFULLY!! words = ['a', 'bbc', 'bab','e'] for word in words: if 'a' or 'e' in word: print(word)

a bbc bab e (The condition if 'a' or 'e' in word always evaluates to True.)

What is the output of the following code snippet? alist = ["a", "b", "c", "d"] atuple = (1, 2, 3, "still") a dictionary = {1: "all", 2: "b", "b": "c", "all": "still"} print(alist[1]) print(atuple[-2]) print(adictionary[2])

b 3 b (The respective values are accessed directly by their indexes and keys.)

What is the output of the following code snippet? alist = ["a", "b", "c", "d"] atuple = (1, 2, 3, "still") adictionary = {1: "all", 2: "b", "b": "c", "all":"still"} print(alist[-1]) print(atuple[0]) print(adictionary[1])

d 1 all (alist[-1] accesses the last element in alist, which is "d". atuple[0] accesses the first element in atuple, which is 1. adictionary[1] accesses the value associated with the key 1 in adictionary, which is "all".)


संबंधित स्टडी सेट्स

Medical Terminology Chapter 12 - Skin: The Integumentary System Word Parts

View Set

Chapter 9 Compensating Employees

View Set

Business Plus 1 Unit 6, Greeting Guests

View Set

clinical psych ch 7 possible quiz questions

View Set