Python Revision Tour - Solutions of Computer Science With Python by Sumita Arora For Class 12 CBSE & NCERT - KnowledgeBoat
Python Revision Tour - Solutions of Computer Science With Python by Sumita Arora For Class 12 CBSE & NCERT - KnowledgeBoat
1. Lists
2. Dictionary
3. Tuples
4. Class ✓
Question 5
1. x^y
2. x**y ✓
3. x^^y
4. none of these
Question 6
1. 14
2. 27 ✓
3. 12
4. 0
Question 7
1. 0.0
2. 0 ✓
3. 1.0
4. 1
Question 8
1. 17
2. 14
3. 15 ✓
4. 23
Question 9
1. 8/(4/2)
2. (8/4)/2 ✓
Question 10
1. <<, >>
2. ** ✓
3. I
4. %
Question 11
Which of the following expressions results
in an error?
1. float('12')
2. int('12')
3. float('12.5')
4. int('12.5') ✓
Question 12
1. print("hello\example\test.txt")
2. print("hello\\example\\test.txt") ✓
3. print("hello\"example\"test.txt")
4. print("hello"\example"\test.txt")
Question 13
1. Boolean
2. String ✓
3. Int
4. Float
Question 14
1. @
2. % ✓
3. + ✓
4. #
Question 15
Eina
Mina
Dika
1. print('''Eina
\nMina
\nDika''')
2. print('''EinaMinaDika''')
3. print('Eina\nMina\nDika')✓
4. print('Eina
Mina
Dika')
Question 16
1. // ✓
2. ?
3. <
4. and
Question 2
Question 3
Question 4
Question 5
Question 6
Question 7
Question 9
Question 10
True/False Questions
Question 1
Question 2
Question 3
Question 4
The expression 2**2**3 is evaluated as:
(2**2)**3.
False
Question 5
Question 6
Question 7
Question 8
Question 9
Question 10
Question 1
Answer
Question 2
Answer
Question 3
Answer
1. String literals
2. Numeric literals
3. Boolean literals
4. Special literal None
5. Literal collections
Question 4
Answer
Question 5
Price*Qty
class
For
do
4thCol
totally
Row31
_Amount
Answer
Question 6
Answer
Floating constants are represented in
Python in two forms — Fractional Form
and Exponent form. Examples:
Question 7
Answer
A string-literal is represented as a
sequence of characters surrounded by
quotes (single, double or triple quotes).
String-literals in Python are implemented
using Unicode.
Question 8
Answer
Question 9
Answer
Question 10
Answer
Question 11
Question 12
Answer
Question 13
Answer
Question 14
Answer
1. Integers (signed)
2. Booleans
Question 15
Answer
1. Lists
2. Dictionaries
3. Sets
1. Integers
2. Floating-Point numbers
3. Booleans
4. Strings
5. Tuples
Question 16
Answer
An implicit type
An explicit type
conversion is
conversion is
automatically
user-defined
performed by the
conversion that
compiler when
forces an
differing data types
expression to be
are intermixed in
of specific type.
an expression.
conversion is conversion is
performed without specified
programmer's explicitly by the
intervention. programmer.
Example: Example:
a, b = 5, 25.5 a, b = 5, 25.5
c=a+b c = int(a + b)
Question 17
Answer
Question 18
Answer
Question 19
Answer
for i in range(10):
if i == 2:
pass
else:
print("i =", i)
Question 20
(a) int
(b) float
(c) bool
(d) str
(e) function
(f) list of int
(g) list of str
(i)
if temp < 32 :
print ("Freezing")
(ii)
(iii)
M = []
for i in range (3) :
M.append(i)
print(M)
(iv)
(v)
if n % 2 == 0 :
print("Freezing")
(vi)
L = inputline.split()
while L != ( ) :
print(L)
L = L[1 :]
(vii)
Answer
(i) bool
(ii) str
(iii) list of int
(iv) int
(v) bool
(vi) list of str
(vii) str
Question 1
Answer
count = 0
while count < 10:
print ("Hello")
count += 1
Answer
Output
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Question 2b
x = 10
y = 0
while x > y:
print (x, y)
x = x ‐ 1
y = y + 1
Answer
Output
10 0
9 1
8 2
7 3
6 4
Explanation
x y Output Remarks
10 0 10 0 1st Iteration
10 0
9 1 2nd Iteration
91
10 0
8 2 91 3rd Iteration
82
10 0
91
7 3 4th Iteration
82
73
10 0
91
6 4 82 5th Iteration
73
64
Question 2c
keepgoing = True
x=100
while keepgoing :
print (x)
x = x ‐ 10
if x < 50 :
keepgoing = False
Answer
Output
100
90
80
70
60
50
Explanation
iterating.
Question 2d
x = 45
while x < 50 :
print (x)
Answer
This is an endless (infinite) loop that will
keep printing 45 continuously.
Question 2e
for x in [1,2,3,4,5]:
print (x)
Answer
Output
1
2
3
4
5
Explanation
Question 2f
Answer
Output
1
2
3
4
5
6
7
8
9
Explanation
Question 2g
Answer
Output
‐500
‐400
‐300
‐200
‐100
0
100
200
300
400
Explanation
Question 2h
x = 10
y = 5
for i in range(x‐y * 2):
print (" % ", i)
Answer
Explanation
sequence — [ ].
Question 2i
c = 0
for x in range(10):
for y in range(5):
c += 1
print (c)
Answer
Output
50
Explanation
x = [1,2,3]
counter = 0
while counter < len(x):
print(x[counter] * '%')
for y in x:
print(y * '* ')
counter += 1
Answer
Output
%
*
* *
* * *
%%
*
* *
* * *
%%%
*
* *
* * *
Explanation
for x in 'lamp':
print(str.upper(x))
Answer
Output
L
A
M
P
Explanation
Question 2l
x = 'one'
y = 'two'
counter = 0
while counter < len(x):
print(x[counter], y[counter])
counter += 1
Answer
Output
o t
n w
e o
Explanation
Question 2m
Answer
Output
apple
pear
peach
Explanation
Question 2n
Predict the output of the following code
fragments:
Answer
Output
apple
PEAR
PEACH
grapefruit
Explanation
Question 3
Answer
Output
Jayes
Finished!
Ramya
Finished!
Taruna
Got it!
Explanation
Question 4(i)
Answer
The loops execute 0 times and the code
produces no output. range(-1, 7, -2)
returns an empty sequence as there are
no numbers that start at -1 and go till 6
decrementing by -2. Due to empty
sequence, the loops don't execute.
Question 4(ii)
for i in range(1,3,1):
for j in range(i+1):
print('*')
Answer
Output
*
*
*
*
*
Explanation
Question 5
m = 3
n = 5
while n < 10:
m = n ‐ 1
n = 2 * n ‐ m
print(n, m)
Answer
n=2*n-m
n = 2 * n - (n - 1)
⇒n=2*n-n+1
⇒ n = 2n - n + 1
⇒n=n+1
Type C: Programming
Practice/Knowledge based
Questions
Question 1
Solution
x = int(input("Enter x: "))
if x < 0:
print("negative")
elif x > 0:
print("positive")
else:
print("zero")
Output
Enter x: ‐5
negative
Enter x: 0
zero
Enter x: 5
positive
Question 2
Solution
x = int(input("Enter a number: "))
if x % 2 == 0:
print("True")
else:
print("False")
Output
Enter a number: 10
True
Enter a number: 5
False
Question 3
Solution
days = 365
hours = 24
mins = 60
secs = 60
secsInYear = days * hours * mins * secs
print("Number of seconds in a year =", se
Output
Question 4
Solution
if a % b == 0:
print(a, "is divisible by", b)
else:
print(a, "is not divisible by", b)
Output
Question 5
Solution
if currDayIdx >= 7:
currDayIdx = currDayIdx ‐ 7
Output
Question 6
Solution
def feetToInches(lenFeet):
lenInch = lenFeet * 12
return lenInch
def getInput():
len = int(input("Enter length in feet
return len
def displayLength(l):
print("Length in inches =", l)
ipLen = getInput()
inchLen = feetToInches(ipLen)
displayLength(inchLen)
Output
Question 7
Solution
n = int(input("Enter N: "))
sum = 0
if n < 0:
for i in range(2 * n, n + 1):
sum += i
else:
for i in range(n, 2 * n + 1):
sum += i
print("Sum =", sum)
Output
Enter N: 5
Sum = 45
Enter N: ‐5
Sum = ‐45
Question 8
Sample run :
Solution
Output
Enter date in MMDDYYYY format: 12252019
December 25, 2019
Question 9
Solution
print('Miles | Kilometres')
print(1, "\t", 1.60934)
for i in range(10, 101, 10):
print(i, "\t", i * 1.60934)
Output
Miles | Kilometres
1 1.60934
10 16.0934
20 32.1868
30 48.2802
40 64.3736
50 80.467
60 96.5604
70 112.6538
80 128.7472
90 144.8406
100 160.934
Question 10
Solution
print('Pounds | Kilograms')
print(1, "\t", 0.4535)
for i in range(10, 101, 10):
print(i, "\t", i * 0.4535)
Output
Pounds | Kilograms
1 0.4535
10 4.535
20 9.07
30 13.605
40 18.14
50 22.675
60 27.21
70 31.745
80 36.28
90 40.815
100 45.35
Question 11
Solution
Output