ProgrammingModerateGATE CS · Product-company placement
What is printed?
What is printed?
a = [4, 2, 7] b = a b.sort() c = sorted(a, reverse=True) print(a, c, a is b, c is a)
- A. [4, 2, 7] [7, 4, 2] True False
- B. [2, 4, 7] [7, 4, 2] True False
- C. [2, 4, 7] [7, 4, 2] False False
- D. [2, 4, 7] [2, 4, 7] True True
Correct answer: B. [2, 4, 7] [7, 4, 2] True False
b is the same list as a, so sorting through b sorts a in place. sorted() always returns a new list.
More Python Programming questions
- What is printed? python s = "banana" print(s.replace("a", "o", 2), s.upper().count("A"), s[::2], s.startswith(("x", "ba")))
- What is printed? python d = {"x": [1, 2], "y": [3]} flat = [v for vs in d.values() for v in vs] lengths = {k: len(v) for k, v in…
- What is printed? python def f(n): out = [] while n: n, r = divmod(n, -2) if r < 0: n, r = n + 1, r + 2 out.append(str(r)) return…
- What is printed? python def gen(n): if n == 0: yield "" return for s in gen(n - 1): yield s + "0" if not s.endswith("1"): yield s + "1"…
- What is printed? python cache = {} def ways(n): if n < 0: return 0 if n == 0: return 1 if n not in cache: cache[n] = ways(n - 1) + ways(n…
- What is printed? python def perms(s): if len(s) <= 1: return [s] out = [] for i, c in enumerate(s): for p in perms(s[:i] + s[i + 1:])…