ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
def g():
print("start", end=" ")
yield 1
print("mid", end=" ")
yield 2
it = g()
print("made", end=" ")
print(next(it), end=" ")
print(list(it))- A. made start 1 mid [2]
- B. start made 1 mid [2]
- C. made start 1 mid [1, 2]
- D. made start mid 1 [2]
Correct answer: A. made start 1 mid [2]
Calling g() only creates a generator; no body code runs, so "made" is printed first. next(it) runs up to the first yield, printing "start" and producing 1. list(it) resumes after that yield, printing "mid", collects 2, then the generator ends. The 1 was already consumed, so the list is [2].
More Python Programming questions
- What is printed? python x = 1 def f(flag): if flag: x = 2 return x print(f(True), end=" ") try: print(f(False)) except UnboundLocalError…
- What is printed? python class A: pass class B(A): pass try: class C(A, B): pass print("ok", end=" ") except TypeError: print("TypeError"…
- What is printed? python class R: def iter(self): print("it", end=" ") return iter([1, 2, 3]) r = R() print(2 in r, 5 in r)
- What is printed? python d = {1: "a", 2: "b"} for k in d: d[k] = d[k] 2 try: for k in d: d[k 10] = "x" except RuntimeError: print("RE"…
- What is printed? python s = "abcbaXabcba" print(s.strip("ab"), s.removeprefix("ab"), s.lstrip("cba"))
- What is printed? python print(-7.5 // 2, -7.5 % 2, 7 // -2.0, 7 % -2.0)