ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
a = [5, 3, 5, 1] a.remove(5) a.insert(10, 7) a.insert(-10, 0) print(a, a.index(5), a.pop(1))
- A. [0, 3, 5, 1, 7] 2 3
- B. [0, 3, 1, 7] 2 5
- C. [3, 5, 1, 7, 0] 1 5
- D. [0, 5, 1, 7] 2 3
Correct answer: D. [0, 5, 1, 7] 2 3
remove deletes only the first 5, and out-of-range insert indices clamp to the ends: [0, 3, 5, 1, 7]. print receives the list object itself and formats it only after all arguments are evaluated, by which time pop(1) has removed 3.
More Python Programming questions
- What is printed? python a = [1, 2, 3, 4] b = a a = a.extend([5]) or a print(a is b, b[-1], [].extend([1]))
- What is printed? python s = "Mississippi" print(s.count("ss"), s.count("issi"), s.replace("ss", "S"), s.lower().index("s", 4))
- What is printed? python print("a" "b" 2, ("a" + "b") 2, "-".join(["a", "b"]) 2, "x".join("123"))
- What is printed? python s = "abc" print(repr(s[1:1] + s[3:] + s[-1:-2]), s[0:0] == s[5:9], len(s[-5:5]), s[True:])
- What is printed? python w = ["bob", "Al", "al", "Bo"] print(sorted(w), sorted(w, key=str.lower))
- What is printed? python people = [("ann", 30), ("bob", 25), ("cid", 30), ("dan", 25)] people.sort(key=lambda p: p[0], reverse=True)…