ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
print("a" "b" * 2, ("a" + "b") * 2, "-".join(["a", "b"]) * 2, "x".join("123"))- A. abb abab a-ba-b 1x2x3
- B. abab abab a-ba-b 1x2x3
- C. abab abab a-b-a-b 1x2x3
- D. abab abab a-ba-b x1x2x3x
Correct answer: B. abab abab a-ba-b 1x2x3
Adjacent string literals are concatenated at compile time before * is applied, so 'a' 'b' * 2 is 'ab' * 2. join puts the separator only between items, and a string is an iterable of characters.
More Python Programming questions
- 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)…
- What is printed? python nums = [4, -1, 3, -1, 2] print(sorted(nums, key=lambda v: (v < 0, -v)), sorted(set(nums), reverse=True)[:2])
- What is printed? python def walk(d, depth=0): out = [] for k, v in d.items(): out.append(f"{k}{depth}") if isinstance(v, dict): out +=…
- What is printed? python def hanoi(n): return 0 if n == 0 else 2 hanoi(n - 1) + 1 def calls(n): return 1 if n == 0 else 1 + 2 calls(n - 1)…