0% found this document useful (0 votes)
5 views5 pages

Solved Python Paper

The document contains a solved question paper for a Python class test, including multiple-choice questions, programming tasks, and theoretical explanations. It covers various topics such as data types, logical operations, string manipulation, and control structures. Each section is divided into questions with specified marks, providing answers and explanations for each query.

Uploaded by

tiwaripratham768
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)
5 views5 pages

Solved Python Paper

The document contains a solved question paper for a Python class test, including multiple-choice questions, programming tasks, and theoretical explanations. It covers various topics such as data types, logical operations, string manipulation, and control structures. Each section is divided into questions with specified marks, providing answers and explanations for each query.

Uploaded by

tiwaripratham768
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

Solved Question Paper — Python (Class Test)

Instructions: All programming answers are in Python. MCQ answers include the option and the text.

SECTION A (1 mark each)


1. C) List — 'List' is not a Python token; tokens are keywords, literals, operators, identifiers, separators.
2. C) // — floor division operator to get integer result without decimals.
3. B) break — 'break' terminates a loop.
4. D) none of these — a='help' and b='3' (string); a*b will raise TypeError because you cannot multiply
string by string. (If b were integer 3, output would be 'helphelphelp').
5. B) — a = 56,78,32,12 creates a tuple.
6. D) None — AND, OR and NOT are logical gates; 'None' (option) is not a logical gate (so 'none of these'
is correct meaning all listed are gates).
7. C) true — the code prints the literal string true (lowercase).
8. B) Script Mode — script mode is used for multi-line programs.
9. C) 27#4 — after assignment a,b = b**3, a**2 => a=27, b=4; print shows '27 # 4'. (Note spacing from
print).
10. B) L[-3] — for L=[2,4,3,5], L[-3] is 4.
11. B) print(t+5) — incorrect; you cannot add a tuple and an integer.
12. A) 0 — loop reduces a from 30 to 0 and terminates; final value is 0.
13. D) sqrt() — sqrt() is not a built-in; it's in math module.
14. D) - — subtraction operator cannot be used with strings.
15. B) [Link]() — pop() returns (and removes) the last element (myList[-1]).
16. B) Output is 32 — expression equals 10 + 10*2 + 2 = 32, printed after the text.
17. C) No output (given options) — actual program prints -4 and -5 because range(-5,-7,-1) produces -5
and -6; printing i+1 gives -4 and -5. (None of the provided options match exactly.)
18. D) Both (a) and (b) — type() and input() are built-in; sqrt() is not.
19. D) tuple(sorted(myTup, reverse=True)) — sort descending with reverse=True.
20. A) Both (A) and (R) are true and R is the correct explanation — 'pass' is a keyword; unlike comments,
pass is a no-op statement seen and executed by interpreter (does nothing but is not ignored).
21. C) (A) is True but (R) is False — lists do support negative indexes (A true). R is incorrect because
'positional indexes' alone does not explain negative indexing (R is false/incomplete).

SECTION B (2 marks each)


22A) Truth table for (A + B)' C (we interpret + as OR, ' as NOT, adjacency as AND).
Let X = (A OR B)' AND C = (NOT (A OR B)) AND C
A B | A OR B | NOT(A OR B) | C | Output
0 0 | 0 | 1 | 0 | 0
0 0 | 0 | 1 | 1 | 1
0 1 | 1 | 0 | 0 | 0
0 1 | 1 | 0 | 1 | 0
1 0 | 1 | 0 | 0 | 0
1 0 | 1 | 0 | 1 | 0
1 1 | 1 | 0 | 0 | 0
1 1 | 1 | 0 | 1 | 0
22B) Truth table for A' + B' C' (interpret ' as NOT, adjacency as AND, + as OR).
Output = NOT A OR (NOT B AND NOT C)
A B C | NOT A | NOT B | NOT C | (NOT B AND NOT C) | Output
0 0 0 | 1 | 1 | 1 | 1 | 1
0 0 1 | 1 | 1 | 0 | 0 | 1
0 1 0 | 1 | 0 | 1 | 0 | 1
0 1 1 | 1 | 0 | 0 | 0 | 1
1 0 0 | 0 | 1 | 1 | 1 | 1
1 0 1 | 0 | 1 | 0 | 0 | 0
1 1 0 | 0 | 0 | 1 | 0 | 0
1 1 1 | 0 | 0 | 0 | 0 | 0

23A) Mutable and Immutable types in Python:


Mutable types (can be changed in-place): list, dict, set, bytearray, user-defined mutable classes.
Immutable types (cannot be changed in-place): int, float, complex, str, tuple, frozenset, bytes.
24) Difference between split() and partition():
split(sep=None, maxsplit=-1) -> returns a list of substrings split at occurrences of sep (or whitespace if sep
None). Can split multiple times.
partition(sep) -> returns a tuple of three parts: (head, sep, tail). It splits at the first occurrence of sep and
always returns a 3-tuple.
25A) Program: compute 4*x**4 + 3*y**3 + 9*z**2 + 6*pi
# 25A
import math
x = float(input("Enter x: "))
y = float(input("Enter y: "))
z = float(input("Enter z: "))
result = 4*(x**4) + 3*(y**3) + 9*(z**2) + 6*[Link]
print("Result =", result)
25B) Alternative (if chosen) equation code would be provided similarly.
26A) range() function: returns an immutable sequence of numbers. Example: range(1,6) produces
1,2,3,4,5.
26B) Program to read tuple and print even integer elements:
# 26B
t = tuple(int(x) for x in input("Enter tuple elements separated by space: ").split())
for x in t:
if x % 2 == 0:
print(x, end=' ')

27) Rewrite for-loop using while-loop (prints numbers divisible by 3 between 1 and 9):
# 27 using while
i = 1
while i < 10:
if i % 3 == 0:
print(i)
i += 1
28) Logic circuit for PQ + QR(Q+R) — Simplify expression first:
QR(Q+R) = QR*Q + QR*R = Q R Q + Q R R = Q R (since Q*Q=Q and R*R=R) => QR
So expression = PQ + QR = Q(P + R). Draw an OR gate for (P+R), AND with Q.

SECTION C (3 marks each)


29A) Count occurrences of substring in a string:
# 29A - count substring occurrences (overlapping)
s = input("Enter the string: ")
sub = input("Enter substring to find: ")
count = 0
start = 0
while True:
idx = [Link](sub, start)
if idx == -1:
break
count += 1
start = idx + 1 # for overlapping matches. Use idx+len(sub) for non-overlapping.
print("Occurrences:", count)
29B) Frequency of each character in a string:
# 29B
s = input("Enter string: ")
freq = {}
for ch in s:
freq[ch] = [Link](ch, 0) + 1
for ch in sorted(freq):
print(f"'{ch}':", freq[ch])

30A) Pattern printing (odd numbers decreasing columns):


# 30A
n = int(input("Enter Number of Lines: "))
for i in range(n,0,-1):
line = ' '.join(str(2*j-1) for j in range(1,i+1))
print(line)
30B) Right aligned triangle of *:
# 30B
n = int(input("Enter Number of Lines: "))
for i in range(1, n+1):
print(' '*(n-i) + ' '.join('*' for _ in range(i)))

31) Electricity bill calculator:


# 31
units = float(input("Enter units consumed: "))
if units < 50:
charge = 2.60
surcharge = 25
elif units <= 100:
charge = 3.25
surcharge = 35
elif units <= 200:
charge = 5.26
surcharge = 45
else:
charge = 8.25
surcharge = 75
total = units * charge + surcharge
print("Total bill = {:.2f}".format(total))

SECTION D (4 marks each)


32A) append() vs extend():
append(x) adds x as a single element to the end of list. extend(iterable) adds each element of iterable to
the list.
l=[1,2]
[Link]([3,4]) -> [1,2,[3,4]]
[Link]([3,4]) -> [1,2,3,4]
32B) lists vs strings when both are sequences:
Lists are mutable (can change elements), can hold heterogeneous types; strings are immutable and
sequence of characters.
33A) Accept three numbers and print in ascending order without lists/tuples:
# 33A
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
c = float(input("Enter third number: "))
# simple comparisons and swaps
if a > b:
a, b = b, a
if b > c:
b, c = c, b
if a > b:
a, b = b, a
print("Ascending order:", a, b, c)

33B) [Link]() returns float in [0.0,1.0). Expression int(20 + [Link]()*5) yields integers in
range 20..24 inclusive.
So options that are possible depend on values; (i) 20 22 24 25 -> 25 not possible. (ii) 22 23 24 25 -> 25 not
possible. (iii) 23 24 23 24 -> possible. (iv) 21 21 21 21 -> possible if [Link]()*5 yields values in
[0.999..] -> int(20 + 4.999) = int(24.999) = 24 not 21. So the most plausible option: C) 23 24 23 24.
Least possible value = 20. Highest possible value = 24.

34) Fibonacci list generation:


# 34
n = int(input("Enter the limit: "))
a, b = 0, 1
count = 0
while count < n:
print(a, end=' ')
a, b = b, a + b
count += 1

35A) Syntax vs Semantic errors:


Syntax errors: mistakes in Python grammar (e.g., missing colon, unmatched parentheses). Caught by
interpreter before execution.
Semantic errors (logical/runtime): program runs but produces incorrect result due to wrong logic (e.g.,
wrong formula).
35B) Display names from list starting with 'A':
# 35B
names = ["Anita", "Bob", "Aman", "charlie", "alex"]
for name in names:
if [Link]('A'):
print(name)

SECTION E (5 marks each)


36) Conversions:
A) (54)_10 to binary:
54 /2 -> 110110 -> (110110)_2
B) (220)_8 to decimal:
2*8^2 + 2*8 + 0 = 2*64 + 16 = 144
C) (789)_10 to hex:
789 //16 = 49 rem 5; 49//16=3 rem 1; 3//16=0 rem 3 -> read backwards 315 -> (315)_16 => actu
D) (FD4)_16 to octal:
Convert hex FD4 -> binary 1111 1101 0100 -> group into 3 bits from right -> 1 111 110 101 00
E) (110101001)_2 to octal: group from right in threes: 110 101 001 -> 6 5 1 -> (651)_8
(FD4)_16 in decimal = 4052; in octal = 7724
(789)_10 in hex = 315
(54)_10 in binary = 110110
(220)_8 in decimal = 144
(110101001)_2 in octal = 651

37A) Palindrome check (without using built-ins except input/print):


# 37A
n = input("Enter a number: ")
# check palindrome by manual reversal
i = 0
rev = ''
while i < len(n):
rev = n[i] + rev
i += 1
if rev == n:
print(n, "is a palindrome")
else:
print(n, "is not a palindrome")

37B) Given program analysis:


import random
picker = [Link](0,3)
color = ['BLUE','PINK','GREEN','RED' ]
for I in color:
for J in range(I, picker):
print(I, end=' ')
print()
The for J in range(I, picker): is incorrect because range expects integers; I is string. The code will raise a
TypeError. If intent was range(index, picker) different behavior. Possible outputs listed are not produced.
Maximum picker = 3, minimum = 0.

End of solutions.

You might also like