0% found this document useful (0 votes)
13 views11 pages

Python Test Questions and Answers

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views11 pages

Python Test Questions and Answers

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PYTHON TEST-2

[Link] is a/an ............... language.


1. Compiled
2. Interpreted
3. Compiled & Interpreted
4. None of these
[Link] interactive interpreter of Python is termed as ............... .
1. Python shell
2. Python Script mode
3. Python Editor mode
4. Python command line
[Link] three greater than signs (>>>) are called the Python ............... .
1. Cursor
2. Command prompt
3. Pointer
4. Blinking cursor
[Link] is a case-sensitive language. This means ............... .
1. Capital and small letters are same for Python
2. Python doesn't care about the case of alphabets
3. Python treats capital and small letters as different
4. Python automatically capitalizes the small letters
[Link] which set of values will the Python code (s = (a**4) + 5*5**(b + b)) give the output as:
141?
1. a = 1, b = 2
2. a = 3, b = 2
3. a = 2, b = 1
4. a = 3, b = 1
6................ mode of Python gives instant result of typed statement.
1. Interactive mode
2. Script mode
3. Both Interactive and Script mode
4. None of these
[Link] (A): Python is a cross-platform language.
Reasoning (R): Python code can run on a variety of platforms which makes programs very
portable as any program written in one platform can easily be used on another.
1. Both A and R are true and R is the correct explanation of A.
2. Both A and R are true but R is not the correct explanation of A.
3. A is true but R is false.
4. A is false but R is true.
[Link] (A): Python comes with its own IDLE.
Reasoning (R): IDLE is Python's Integrated Development and Learning Environment which
allows programmers to easily write, modify and execute Python programs.
1. Both A and R are true and R is the correct explanation of A.
2. Both A and R are true but R is not the correct explanation of A.
3. A is true but R is false.
4. A is false but R is true.
[Link] of the following are the fundamental building blocks of a Python program?
1. Identifiers
2. Constants
3. Punctuators
4. Tokens
[Link] name cannot be composed of special characters other than ............... .
1. #
2. Hyphen (-)
3. $$
4. Underscore (_)
[Link] takes ............... indented spaces after the function declaration statement by default.
1. 5
2. 6
3. 4
4. 3
[Link] line comments in Python begin with ............... symbol.
1. #
2. "
3. '''
4. %
[Link] is the order of precedence of arithmetic operators given below in Python?
(1) Parenthesis
(2) Exponential
(3) Multiplication
(4) Division
(5) Addition
(6) Subtraction
1. 1,2,3,4,5,6
2. 2,3,4,5,6,1
3. 1,3,2,6,4,5
4. 4,6,5,2,3,1
[Link] statement in Python is terminated by ............... .
1. Semicolon (;)
2. Colon (:)
3. Comma (,)
4. None of these
[Link] reserved words used by Python interpreter to recognize the structure of a program are
termed as ............... .
1. Identifiers
2. Tokens
3. Literals
4. Keywords
[Link] can be the maximum possible length of an identifier?
1. 31
2. 63
3. 79
4. Can be of any length
[Link] of the following operators is floor division?
1. +
2. /
3. //
4. >
[Link] of the following is an invalid statement?
1. a=b=c=2
2. a,b,c=10,20,30
3. abc = 20 30 40
4. a_b_c=20
[Link] is the result of this statement:
10>5 and 7>12 or not 18>3
1. 10
2. True
3. False
4. None
[Link] will be the output of the following Python code?
>>> 6*3+4**2//5-8
1. 13
2. 14
3. Error
4. None
[Link] will be the output of the following Python code?
>>> a=72.55
>>> b=10
>>> c=int(a + b)
>>> c
1. 72.55
2. 72
3. 82
4. None of these
[Link] of the following is not a decision-making statement?
1. if-elif statement
2. for statement
3. if-else statement
4. if statement
[Link] symbol used to end the if statement:
1. Semicolon (;)
2. Hyphen ( - )
3. Underscore ( _ )
4. Colon (:)
[Link] of the following is a valid keyword?
1. IF
2. If
3. if
4. None of these
[Link] many times will the following code be executed?
a=5
while a > 0:
print(a)
print("Thank You")
1. 5 times
2. Once
3. Infinite
4. None of these
[Link] statement is used to iterate itself over a range of values or a sequence?
1. if
2. while
3. do-while
4. for
[Link] the output of the following Python program:
for x in range(1, 20, 3):
print(x, end =',')
1. 1,20,3,
2. 1,4,7,10,13,16,19,
3. 13,6,9,12,15,18,
4. 20,40,60,80,100,
[Link] abandons the current iteration of the loop?
1. continue
2. break
3. stop
4. infinite
[Link] error in the following code (if any) and correct it by rewriting the code and underline
the corrections:
code=input ("Enter season code : ")
if code=w:
print "winter season"
elif code==r:
PRINT "rainy season"
else:
Print "summer season"
[Link] the output of the following:
for x in range (1, 6) :
for y in range (1, x+1):
print (x, ' ', y)
[Link] the output of the following:
for x in range (10, 20):
if (x == 15):
break
print(x)
[Link] the output of the following:
for x in range (10, 20):
if (x % 2 == 0):
continue
print (x)
[Link] the output of the following program on execution if x = 50:
if x > 10:
if x > 25:
print ( 'ok' )
if x > 60:
print ( 'good' )
elif x > 40:
print ( 'average' )
else:
print ('no output')
[Link] the output of the following code:
for i in range (2) :
for j in range (1) :
if i+2 == j:
print ("+",end=" ")
else:
print ("o",end=" ")
[Link] the output of the following when num1 = 4, num2 = 3, num3 = 2.
num1 = '5' + '5'
print (num1)
[Link] the output of the following program segment:
i = 0; sum = 0
while i < 9:
if i % 4 == 0:
sum = sum + i
i=i+2
print (sum)
9. WAP to print the sum of the series 1 - x1/2! + x2/3! - x3/4! ............... xn/(n + 1)! —
exponential series.
[Link] to print the sum of the series 1 - x2/4! + x3/6! - x4/8! + x5/10! ............... xn/(2n)! —
exponential series.
[Link] to display the sum of the given series:
Sum = 1 + (1 + 2) + (1 + 2 + 3) + (1 + 2 + 3.....n)
Question 21(b)
WAP to print the following pattern:
A
BB
CCC
DDDD
EEEEE
Question 21(c)
WAP to print the following pattern:
1
21
321
4321
54321

Solutions

ection 1: Multiple Choice Questions

[Link] Answer [Link] Answer

1 3. Compiled & Interpreted [3] 16 4. Can be of any


length

2 1. Python shell [4] 17 3. //

3 2. Command prompt [5] 18 3. abc = 20 30 40

4 3. Python treats capital and small letters as different 19 3. False


[6]

5 2. a = 3, b = 2 20 2. 14

6 1. Interactive mode 21 3. 82

7 1. Both A and R are true and R is the correct 22 2. for statement


explanation of A. [7]

8 1. Both A and R are true and R is the correct 23 4. Colon (:)


explanation of A. [8]

9 4. Tokens [9] 24 3. if

10 4. Underscore (_) [10] 27 3. Infinite

11 4. 4 [11] 28 4. for
12 1. # [12] 29 2. 1,4,7,10,13,16,19,

13 1. 1,2,3,4,5,6 [13] 30 1. continue

14 4. None of these

15 4. Keywords [14]

Section 2: Coding Questions

1. Find error in the following code (if any) and correct it by rewriting the code
and underline the corrections:

Original Code:

python

code=input ("Enter season code : ")


if code=w:
print "winter season"
elif code==r:
PRINT "rainy season"
else:
Print "summer season"
Use code with caution.

Corrected Code (Corrections are underlined):

python
code=input ("Enter season code : ")
if code == 'w' :
print("winter season")
elif code == 'r' :
print("rainy season")
else:
print("summer season")
Use code with caution.

2. Write the output of the following:

python
for x in range (1, 6) :
for y in range (1, x+1):
print (x, ' ', y)
Use code with caution.

Output:
1 1
2 1
2 2
3 1
3 2
3 3
4 1
4 2
4 3
4 4
5 1
5 2
5 3
5 4
5 5

3. Write the output of the following:

python
for x in range (10, 20):
if (x == 15):
break
print(x)
Use code with caution.

Output:
10
11
12
13
14

4. Write the output of the following:

python
for x in range (10, 20):
if (x % 2 == 0):
continue
print (x)
Use code with caution.

Output:
11
13
15
17
19

5. Write the output of the following program on execution if x = 50:

python
if x > 10:
if x > 25:
print ( 'ok' )
if x > 60:
print ( 'good' )
elif x > 40:
print ( 'average' )
else:
print ('no output')
Use code with caution.

Output:
ok
average

6. Write the output of the following code:

python
for i in range (2) :
for j in range (1) :
if i+2 == j:
print ("+",end=" ")
else:
print ("o",end=" ")
Use code with caution.

Output:
o o

7. Give the output of the following when num1 = 4, num2 = 3, num3 = 2.

python
num1 = '5' + '5'
print (num1)
Use code with caution.

Output:
55

8. Find the output of the following program segment:


python
i = 0; sum = 0
while i < 9:
if i % 4 == 0:
sum = sum + i
i = i + 2
print (sum)
Use code with caution.

Output:
4

9. WAP to print the sum of the series 1 - x¹/2! + x²/3! - x³/4! ............... xⁿ/(n + 1)!
— exponential series.

python
import math

def series_sum_9(x, n):


sum_val = 1
for i in range(1, n + 1):
term = (x**i) / [Link](i + 1)
if i % 2 == 1:
sum_val -= term
else:
sum_val += term
return sum_val

# Example usage:
# x_val = 1
# n_terms = 3
# print(f"Sum of the series: {series_sum_9(x_val, n_terms)}")
Use code with caution.

10. WAP to print the sum of the series 1 - x²/4! + x³/6! - x⁴/8! + x⁵/10! ...............
xⁿ/(2n)! — exponential series.
(Note: There appears to be a slight inconsistency in the exponents/factorials pattern
description compared to a standard exponential series. The code below follows the
explicit pattern provided: exponents 2, 3, 4... and factorials 4!, 6!, 8!... with alternating
signs starting after the first term.)

python
import math

def series_sum_10(x, n):


sum_val = 1
for i in range(2, n + 1):
term = (x**i) / [Link](2 * i)
if (i - 1) % 2 == 1:
sum_val += term
else:
sum_val -= term
return sum_val

# Example usage:
# x_val = 1
# n_terms = 5
# print(f"Sum of the series: {series_sum_10(x_val, n_terms)}")
Use code with caution.

11. WAP to display the sum of the given series:


Sum = 1 + (1 + 2) + (1 + 2 + 3) + (1 + 2 + 3.....n)

python
def series_sum_11(n):
total_sum = 0
for i in range(1, n + 1):
current_sub_sum = 0
for j in range(1, i + 1):
current_sub_sum += j
total_sum += current_sub_sum
return total_sum

# Example usage:
# num = 3
# print(f"Total sum: {series_sum_11(num)}") # Output for n=3
is 1 + 3 + 6 = 10
Use code with caution.

Question 21(b) WAP to print the following pattern:


A
B B
C C C
D D D D
E E E E E

python
def print_pattern_b(rows):
for i in range(rows):
char = chr(ord('A') + i)
print((char + ' ') * (i + 1))

# print_pattern_b(5)
Use code with caution.
Question 21(c) WAP to print the following pattern:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1

python
def print_pattern_c(rows):
for i in range(1, rows + 1):
for j in range(i, 0, -1):
print(j, end=" ")
print()

# print_pattern_c(5)

You might also like