ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
x, *y = "hello" print(x, y)
- A. h ello
- B. h ['e', 'l', 'l', 'o']
- C. 'h' 'ello'
- D. hello []
Correct answer: B. h ['e', 'l', 'l', 'o']
Unpacking a string iterates its characters; x takes the first character 'h', and the starred name *y collects the rest as a LIST of individual characters, not a substring.
More Python Programming questions
- What is printed? python print([x for x in range(20) if x % 2 == 0 if x % 3 == 0])
- What is printed? python print([x if x % 2 == 0 else -x for x in range(5)])
- What is printed? python words = ["banana", "kiwi", "apple", "fig"] print(sorted(words, key=len)) print(sorted(words, key=lambda w: w[-1]))
- What is printed? python s = "Hello World" print(s.swapcase()) print(s.title()) print(" a b ".split())
- What is printed? python print("a-b--c".split("-")) print("a-b--c".split("-", 1))
- What is printed? python d = {"b": 2, "a": 1, "c": 3} print(sorted(d.items())) print(dict(sorted(d.items())))