Python Programming Questions For Fresher

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

1.

 What is the maximum length of a Python identifier?


a. 32
b. 16
c. 128
d. No fixed length is specified.
 
2. What will be the output of the following code snippet?
print(2**3 + (5 + 6)**(1 + 1))

a. 129
b. 8
c. 121
d. None of the above.

Correct Answer
The above code will print 129 by following the BEDMAS rule of operator precedence.

3. What will be the output of the following code snippet?


a = [1, 2, 3]
a = tuple(a)
a[0] = 2
print(a)

a. [2, 2, 3]
b. (2, 2, 3)
c. (1, 2, 3)

Error.
Correct Answer
Since we convert a to a tuple and then try to change its content, we will get an error since tuples
are immutable.

4. What will be the output of the following code snippet?


a = [1, 2, 3, 4, 5]
sum = 0
for ele in a:
sum += ele
print(sum)

a. 15
b. 0
c. 20
d. None of these
Correct Answer
The above code calculates the sum of all elements in the list.

5. What will be the output of the following code snippet?


count = 0
while(True):
if count % 3 == 0:
print(count, end = " ")
if(count > 15):
break;
count += 1

a. 0 1 2 ….. 15
b. Infinite Loop
c. 0 3 6 9 12 15
d. 0 3 6 9 12

Correct Answer
The above code prints the multiples of 3 not greater than 15, and then breaks off.

6. What will be the output of the following code snippet?


def solve(a, b):
return b if a == 0 else solve(b % a, a)
print(solve(20, 50))
a. 10
b. 20
c. 50
d. 1

Correct Answer
The above function basically calculates the gcd of 2 numbers recursively. The gcd of 20 and 50
is 10, so the answer is A.

7. What will be the output of the following code snippet?


def solve(a):
a = [1, 3, 5]
a = [2, 4, 6]
print(a)
solve(a)
print(a)
a. [2, 4, 6]. [2, 4, 6]
b. [2, 4, 6], [1, 3, 5]
c. [1. 3. 5], [1, 3, 5]
d. None of these.

Correct Answer
This is a consequence of “Pass by Object Reference” in Python.

8. What will be the output of the following code snippet?


def func():
global value
value = "Local"

value = "Global"
func()
print(value)

a. Local
b. Global
c. None
d. Cannot be predicted

Correct Answer
We set the value of “value” as Global. To change its value from inside the function, we use the
global keyword along with “value” to change its value to local, and then print it.

9. What will be the output of the following code snippet?


a=3
b=1
print(a, b)
a, b = b, a
print(a, b)

a. 3 1    1 3
b. 3 1    3 1
c. 1 3    1 3
d. 1 3    3 1

Correct Answer
The above code snippet swaps 2 numbers in Python.
10. What will be the output of the following code snippet?
def thrive(n):
if n % 15 == 0:
print("thrive", end = “ ”)
elif n % 3 != 0 and n % 5 != 0:
print("neither", end = “ ”)
elif n % 3 == 0:
print("three", end = “ ”)
elif n % 5 == 0:
print("five", end = “ ”)
thrive(35)
thrive(56)
thrive(15)
thrive(39)

a. five neither thrive three


b. five neither three thrive
c. three three three three
d. five neither five neither

Correct Answer
Multiples of both 3 and 5 prints thrive. Multiples of neither 3 nor 5 prints neither. Multiples of 3
prints three and multiple of 5 prints five.

11. What will be the output of the following code snippet?


def check(a):
print("Even" if a % 2 == 0 else "Odd")

check(12)

a. Even
b. Odd
c. Error
d. None

Correct Answer
The program uses ternary operators to check if a given number is even or not.

12. What will be the output of the following code snippet?


example = ["Sunday", "Monday", "Tuesday", "Wednesday"];
del example[2]
print(example)
a. ['Sunday', 'Monday', 'Tuesday', 'Wednesday']
b. ['Sunday', 'Monday', 'Wednesday']
c. ['Monday', 'Tuesday', 'Wednesday']
d. ['Sunday', 'Monday', 'Tuesday']

Correct Answer
The del keyword deletes an element from a list at a given index.

13. What will be the output of the following code snippet?


numbers = (4, 7, 19, 2, 89, 45, 72, 22)
sorted_numbers = sorted(numbers)
even = lambda a: a % 2 == 0
even_numbers = filter(even, sorted_numbers)
print(type(even_numbers))
a. filter
b. int
c. list
d. tuple

Correct Answer
The filter function returns an object of type “filter”.

14. What will be the output of the following code snippet?


numbers = (4, 7, 19, 2, 89, 45, 72, 22)
sorted_numbers = sorted(numbers)
odd_numbers = [x for x in sorted_numbers if x % 2 != 0]
print(odd_numbers)

a. [7, 19, 45, 89]


b. [2, 4, 22, 72]
c. [4, 7, 19, 2, 89, 45,72, 22]
d. [2, 4, 7, 19, 22, 45, 72, 89]

Correct Answer
The above code basically forms a list containing the odd numbers in the numbers list, in sorted
order.
15. What will be the output of the following code snippet?
dict1 = {'first' : 'sunday', 'second' : 'monday'}
dict2 = {1: 3, 2: 4}
dict1.update(dict2)
print(dict1

a. {'first': 'sunday', 'second': 'monday', 1: 3, 2: 4}


b. {'first': 'sunday', 'second': 'monday'}
c. {1: 3, 2: 4}
d. None of the above.

Correct Answer
The update function in python merges the contents of 2 dictionaries and stores them in the
invoking dictionary.

16. What will be the output of the following code snippet?


a = {'Hello':'World', 'First': 1}
b = {val: k for k , val in a.items()}
print(b)

a. {'Hello':'World', 'First': 1}
b. {'World': 'Hello', 1: 'First'}
c. Can be both A or B
d. None of the above
Correct Answer
This is an example of dict comprehension in Python, in which we are reversing the key-value
pairs from the 1st dictionary into the second.

17. What will be the output of the following code snippet?


word = "Python Programming"
n = len(word)
word1 = word.upper()
word2 = word.lower()
converted_word = ""
for i in range(n):
if i % 2 == 0:
converted_word += word2[i]
else:
converted_word += word1[i]
print(converted_word)

a. pYtHoN PrOgRaMmInG
b. Python Programming
c. python programming
d. PYTHON PROGRAMMING

Correct Answer
In this code snippet, we convert every element in odd index to lower case and every element in
even index to uppercase.

18. What will be the output of the following code snippet?


a = "4, 5"
nums = a.split(',')
x, y = nums
int_prod = int(x) * int(y)
print(int_prod)

a. 20
b. 45
c. 54
d. 4,5

Correct Answer
In this code snippet, we break the given string into its 2 integer components, and then find their
product, considering them as integer types.

19. What will be the output of the following code snippet?


def tester(*argv):
for arg in argv:
print(arg, end = ' ')
tester('Sunday', 'Monday', 'Tuesday', 'Wednesday')

a. Sunday
b. Wednesday
c. Sunday Monday Tuesday Wednesday
d. None of the above.

Correct Answer
We pass a variable number of arguments into the function using *args, and then print their
value.

20. What will be the output of the following code snippet?


def tester(**kwargs):
for key, value in kwargs.items():
print(key, value, end = " ")
tester(Sunday = 1, Monday = 2, Tuesday = 3, Wednesday = 4)
a. Sunday 1 Monday 2 Tuesday 3 Wednesday 4
b. Sunday 1
c. Wednesday 4
d. None of the above

Correct Answer
We can pass multiple key-word arguments to a function using kwargs. Here, we print the
arguments passed to the function in this code snippet.

21. What will be the output of the following code snippet?


from math import *
a = 2.19
b = 3.999999
c = -3.30
print(int(a), floor(b), ceil(c), fabs(c))

a. 2 3 -3 3.3
b. 3 4 -3 3
c. 2 3 -3 3
d. 2 3 -3 -3.3

Correct Answer
Option A will be the correct answer for this code snippet.

22. What will be the output of the following code snippet?


s1 = {1, 2, 3, 4, 5}
s2 = {2, 4, 6}
print(s1 ^ s2)
a. {1, 2, 3, 4, 5}
b. {1, 3, 5, 6}
c. {2, 4}
d. None of the above

Correct Answer
The ^ operator in sets will return a set containing common of elements of its operand sets.

23. What will be the output of the following code snippet?


a = [1, 2, 3, 4]
b = [3, 4, 5, 6]
c = [x for x in a if x not in b]
print(c)

a. [1, 2]
b. [5, 6]
c. [1, 2, 5, 6]
d. [3, 4]

Correct Answer
Above code snippet prints the values in a, which are not present in b.

24. In which language is Python written?


a. C++
b. C
c. Java
d. None of these

Correct Answer
Python is written in the C language.

25. What will be the result of the following expression in Python “2 ** 3 + 5 ** 2”?
a. 65536
b. 33
c. 169
d. None of these

Correct Answer
The above expression will be evaluated as 2^3 + 5^2 = 8 + 25 = 33.

You might also like