Python/Python Mcq Set 13 Sample Test,Sample questions

Question: ``` What will be the output of the following Python code? def a(n): if n == 0: return 0 elif n == 1: return 1 else: return a(n-1)+a(n-2) for i in range(0,4): print(a(i),end=" ")```

1.0 1 2 3

2.An exception is thrown

3.0 1 1 2 3

4. 0 1 1 2

1.False True

2. False False

3. True True

4.True False

1. [-4, 16]

3.Error

4.-4 16

1. [1, 4, 5 ]

2.Error

3. [-2, -3]

4.[1, -2, -3]

Question: ``` What will be the output of the following Python code? l=[1, 2, 3, 4, 5] m=map(lambda x:2**x, l) print(list(m))```

1.[1, 4, 9, 16, 25 ]

2. [2, 4, 8, 16, 32 ]

3.[1, 0, 1, 0, 1]

4.error

Question: ` Which of these is not true about recursion?`

1. Making the code look clean

2.A complex task can be broken into sub-problems

3.Recursive calls take up less memory

4.Sequence generation is easier than a nested iteration

Question: ``` Which type of copy is shown in the following python code? l1=[[10, 20], [30, 40], [50, 60]] ls=list(l1) ls [[10, 20], [30, 40], [50, 60]]```

1.Shallow copy

2.Deep copy

3.memberwise

4.all of the mentioned

Question: ```Fill in the line of the following Python code for calculating the factorial of a number. def fact(num): if num == 0: return 1 else: return _________```

1. num*fact(num-1)

2. (num-1)*(num-2)

3.num*(num-1)

4.fact(num)*fact(num-1)

Question: `In _______ copy, the base address of the objects are copied. In ________ copy, the base address of the objects are not copied.`

1.deep. shallow

2.memberwise, shallow

3. shallow, deep

4.deep, memberwise

Question: `In _________copy, the modification done on one list affects the other list. In ________ copy, the modification done on one list does not affect the other list.`

1. shallow, deep

2. memberwise, shallow

3.deep, shallow

4. deep, memberwise

Question: ```Observe the following Python code? def a(n): if n == 0: return 0 else: return n*a(n - 1) def b(n, tot): if n == 0: return tot else: return b(n-2, tot-2)```

1.Both a() and b() arenâ€™t tail recursive

2.Both a() and b() are tail recursive

3.b() is tail recursive but a() isnâ€™t

4.b() is tail recursive but a() isnâ€™t

Question: ```Read the following Python code carefully and point out the global variables? y, z = 1, 2 def f(): global x x = y+z```

1. x

2. y and z

3. x, y and z

4.Neither x, nor y, nor z

Question: `What happens if a local variable exists with the same name as the global variable you want to access?`

1. Error

2. The local variable is shadowed

3.Undefined behavior

Question: `What happens if the base condition isnâ€™t defined in recursive programs?`

1. Program gets into an infinite loop

2.Program runs once

3.Program runs n number of times where n is the argument given to the function

4.An exception is thrown

Question: `What is tail recursion?`

1.A recursive function that has two base cases

2.A function where the recursive functions leads to an infinite loop

3. A recursive function where the function doesnâ€™t return anything and just prints the values

4. A function where the recursive call is the last thing executed by the function

Question: `What is the base case in the Merge Sort algorithm when it is solved recursively?`

1.n=0

2.n=1

3.A list of length one

4.An empty list

Question: ```What will be the output of the following Python code and state the type of copy that is depicted? l1=[2, 4, 6, 8] l2=[1, 2, 3] l1=l2 l2```

1. [2, 4, 6, 8], shallow copy

2.[2, 4, 6, 8], deep copy

3. [1, 2, 3], shallow copy

4.[1, 2, 3], deep copy

Question: ```What will be the output of the following Python code? a = [1, 2, 3, 4, 5] b = lambda x: (b (x[1:]) + x[:1] if x else []) print(b (a))```

1. 1 2 3 4 5

2.[5,4,3,2,1]

3. []

4.Error, lambda functions canâ€™t be called recursively

1.10

2.25

3. Junk value

4.error

Question: ```What will be the output of the following Python code? def check(n): if n < 2: return n % 2 == 0 return check(n - 2) print(check(11))```

1. False

2. True

3.1

4.An exception is thrown

Question: ```What will be the output of the following Python code? def f(): global a print(a) a = "hello" print(a) a = "world" f() print(a)```

1. hello hello world

2. world hello hello

3. hello world world

4. world hello world

1.Error

2.4

3.Junk value

4. 1

1.1 2 3 4

2.5 10 15 4

3.10 20 30 40

4. 5 10 15 40

1.outer error

2.inner error

3.outer inner

4. error

1. Error

2.13

3.13 x

4. x

1. Error

2.100

3.101

4. 99

1. Error

2.12

3. 15

4.1512

1.[3,2,4]

2. [2,3,4]

3.Error

4.[3,4,2]

1.error

2.hello

3.16

4.16 hello

1. 50

2. 100

3. 74

4.Infinite loop

1. 13

2. 10

3.2

4.5

1.13

2. 7

3.Infinite loop

4.17

1.error

2. butter error

3. bitter error

4.bitterbutter

1. Error

2.10

3.24

4.No output

Question: ```What will be the output of the following Python code? l1=[1, 2, 3, (4)] l2=l1.copy() l2 l1```

1.[1, 2, 3, (4)] [1, 2, 3, 4]

2.[1, 2, 3, 4] [1, 2, 3, (4)]

3.[1, 2, 3, 4] [1, 2, 3, 4]

4.[1, 2, 3, (4)] [1, 2, 3, (4)]

1. True

2.False

3. Error

Question: ```What will be the output of the following Python code? l1=[10, 20, 30, [40]] l2=copy.deepcopy(l1) l1[3][0]=90 l1 l2```

1.[10, 20, 30, [40]] [10, 20, 30, 90]

2.Error

3.[10, 20, 30 [90]] [10, 20, 30, [40]]

4.[10, 20, 30, [40]] [10, 20, 30, [90]]

1.False, False

2. False, True

3.True, True

4.True, False

Question: ```What will be the output of the following Python code? l=[1, -2, -3, 4, 5] def f1(x): return x<-1 m1=map(f1, l) print(list(m1))```

1.[False, False, False, False, False]

2.[False, True, True, False, False]

3. [True, False, False, True, True]

4. [True, True, True, True, True]

1.011

2.110

3.3

4.Infinite loop

Question: ```What will be the output of the following Python code? odd=lambda x: bool(x%2) numbers=[n for n in range(10)] print(numbers) n=list() for i in numbers: if odd(i): continue else: break```

1. [0, 2, 4, 6, 8, 10]

2. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

3.[1, 3, 5, 7, 9]

4. error

1. Error

2.7

3.8

4.15

1.2

2.1

3. 0

4. error

1.100

2. 90

3. 80

4. error

1. Error

2.12 4

3.4 12

4.4 15

Question: `Which is the most appropriate definition for recursion?`

1. A function that calls itself

2.A function execution instance that calls another execution instance of the same function

3. A class method that calls another class method

4.An in-built method that is automatically called

1. list

2. set

3.dictionary

4.tuple

Question: `Which of the following statements is false about recursion?`

1.Every recursive function must have a base case

2.Infinite recursion can occur if the base case isnâ€™t properly mentioned

3.A recursive function makes the code easier to understand

4.Every recursive function must have a return value

Question: `Which of these is false about recursion?`

1.Recursive function can be replaced by a non-recursive function

2.Recursive functions usually take more memory space than non-recursive function

3.Recursive functions run faster than non-recursive function

4.Recursion makes programs easier to understand

Question: `Which of these is not true about recursion?`

1. Itâ€™s easier to code some real-world problems using recursion than non-recursive equivalent

2. Recursive functions are easy to debug

3.Recursive calls take up a lot of memory

4.Programs using recursion take longer time than their non-recursive equivalent

More MCQS

Olete Team
Online Exam TestTop Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on Online Exam Testwebsite is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!