ProgrammingModerateGATE CS · Product-company placement
What is printed?
What is printed?
x = 7 kind = "even" if x % 2 == 0 else "odd" sign = "pos" if x > 0 else "zero" if x == 0 else "neg" print(kind, sign, ["no", "yes"][x > 5])
- A. odd pos yes
- B. odd pos no
- C. even pos yes
- D. odd zero yes
Correct answer: A. odd pos yes
Conditional expressions chain right-to-left like if/elif/else. A bool used as an index is 0 or 1, so ['no', 'yes'][True] is 'yes'.
More Python Programming questions
- What is printed? python count = 0 for i in range(1, 4): for j in range(i): if (i + j) % 2: continue count += 1 print(count, i, j)
- What is printed? python a = [4, 2, 7] b = a b.sort() c = sorted(a, reverse=True) print(a, c, a is b, c is a)
- 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"…