python MockTest

Python Questions
#### What will be the output of the following code? ```python def func(x, y=[]): y.append(x) return y print(func(1)) print(func(2)) ``` 1. [ ] [1] and [2] 2. [x] [1] and [1, 2] 3. [ ] [1, 2] and [2, 1] 4. [ ] [1, 2] and [1, 2] #### What does the following list comprehension return? ```python [(x, y) for x in range(3) for y in range(3)] ``` 1. [ ] List of tuples containing all combinations of numbers from 0 to 3 2. [x] List of tuples containing all combinations of numbers from 0 to 2 3. [ ] List of tuples containing all permutations of numbers from 0 to 3 4. [ ] List of tuples containing all permutations of numbers from 0 to 2 #### What is the output of the following code? ```python def f(x): if x == 0: return 0 return x + f(x-1) print(f(4)) ``` 1. [x] 10 2. [ ] 24 3. [ ] 4 4. [ ] 16 #### What will be the output of the following code? ```python x = [1, 2, 3] y = [1, 2, 3] print(x == y) print(x is y) ``` 1. [x] True and False 2. [ ] True and True 3. [ ] False and False 4. [ ] False and True #### What is the output of the following code? ```python def foo(): try: return 1 finally: return 2 print(foo()) ``` 1. [ ] 1 2. [x] 2 3. [ ] 1 and 2 4. [ ] Error #### What will be the output of the following code? ```python a = 5 def outer(): a = 10 def inner(): nonlocal a a += 1 print(a) inner() outer() ``` 1. [ ] 6 2. [x] 11 3. [ ] Error 4. [ ] 10 #### What is the output of the following code? ```python def f(x): yield x + 1 yield x + 2 g = f(2) print(list(g)) ``` 1. [x] [3, 4] 2. [ ] [3] 3. [ ] [4, 5] 4. [ ] Error #### What is the output of the following code? ```python x = [1, 2, 3] y = [1, 2, 3] print(x == y) print(x is y) ``` 1. [x] True and False 2. [ ] True and True 3. [ ] False and False 4. [ ] False and True #### What will be the output of the following code? ```python x = 10 def foo(): global x x += 5 foo() print(x) ``` 1. [ ] 10 2. [ ] 19 3. [ ] 5 4. [x] 15 #### What is the output of the following code? ```python def f(x=[]): return x print(f()) print(f()) ``` 1. [x] [] and [] 2. [ ] [[]] and [[]] 3. [ ] [None] and [None] 4. [ ] [[]] and [] #### What will be the output of the following code? ```python def foo(a, b=[]): b.append(a) return b print(foo(1)) print(foo(2, [])) print(foo(3)) ``` 1. [ ] [1], [2], [3] 2. [x] [1], [2], [1, 3] 3. [ ] [1], [2], [3, 3] 4. [ ] [1], [2], [1, 2] #### What is the output of the following code? ```python x = 10 def foo(): x = 5 def bar(): nonlocal x x += 1 print(x) bar() foo() ``` 1. [x] 6 2. [ ] 6 and 7 3. [ ] 5 4. [ ] Error #### What will be the output of the following code? ```python a = [1, 2, 3] def foo(x): x += [4] foo(a) print(a) ``` 1. [ ] [1, 2, 3] 2. [x] [1, 2, 3, 4] 3. [ ] [1, 2, 3, 4], [1, 2, 3, 4] 4. [ ] Error #### What does the following list comprehension return? ```python [2 ** x for x in range(5)] ``` 1. [ ] [0, 2, 4, 6, 8] 2. [x] [1, 2, 4, 8, 16] 3. [ ] [1, 2, 6, 8, 16] 4. [ ] [2, 4, 8, 16, 32] #### What is the output of the following code? ```python x = 10 def foo(): global x x = 5 return x print(foo()) print(x) ``` 1. [ ] 5 and 10 2. [x] 5 and 5 3. [ ] 10 and 10 4. [ ] 5 and 25 #### What will be the output of the following code? ```python def f(): try: return 1 finally: return 2 print(f()) ``` 1. [ ] 1 2. [x] 2 3. [ ] 1 and 2 4. [ ] Error #### What is the output of the following code? ```python def f(x): if x == 0: return 0 return x + f(x-1) print(f(4)) ``` 1. [x] 10 2. [ ] 24 3. [ ] 4 4. [ ] 16 #### What will be the output of the following code? ```python x = [1, 2, 3] y = [1, 2, 3] print(x == y) print(x is y) ``` 1. [x] True and False 2. [ ] True and True 3. [ ] False and False 4. [ ] False and True #### What is the output of the following code? ```python def foo(): try: return 1 finally: return 2 print(foo()) ``` 1. [ ] 1 2. [x] 2 3. [ ] 1 and 2 4. [ ] Error #### What will be the output of the following code? ```python a = 5 def outer(): a = 10 def inner(): nonlocal a a += 1 print(a) inner() outer() ``` 1. [ ] 6 2. [x] 11 3. [ ] Error 4. [ ] 10


Table of Contents