CS Xii-Ch 1
CS Xii-Ch 1
Question 1
1. my_day_2
2. 2nd_day ✓
3. Day_two
4. _2
Question 2
1. eval ✓
2. assert
3. nonlocal
4. pass
Question 3
1._init_
2. in ✓
3. it
4. on
Question 4
1. Lists
2. Dictionary
3. Tuples
4. Class ✓
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
The expression 8/4/2 will evaluate equivalent to which of the following expressions:
1. 8/(4/2)
2. (8/4)/2 ✓
Which among the following list of operators has the highest precedence?
1. <<, >>
2. ** ✓
3. I
4. %
Question 11
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. #
Which of the following four code fragments will yield following output?
E
i
Select all of the function calls that result in this output
n
a 1. print('''Eina\nMina\nDika''')
2. print('''EinaMinaDika''')
M
i 3. print('Eina\nMina\nDika')✓
n 4. print('EinaMinaDika')
a
Question 16
D
i
Which of the following is valid arithmetic operator in Python :
k
a 1. // ✓
2. ?
3. <
4. and
Question 2
Question 3
A keyword is a word having special meaning and role as specified by programming language.
Question 4
The data types whose values cannot be changed in place are called immutable types.
Question 5
In a Python expression, when conversion of a value's data type is done automatically by the compiler
without programmer's intervention, it is called implicit type conversion.
Question 6
Question 8
A break statement skips the rest of the loop and jumps over to the statement following the loop.
Question 9
The continue statement skips the rest of the loop statements and causes the next iteration of the loop to
take place.
Question 10
True/False Questions
Question 1
Question 2
The value of the expressions 4/(3*(2 - 1)) and 4/3*(2 - 1) is the same.
True
Question 3
The value of the expressions 4/(3*(4 - 2)) and 4/3*(4 - 2) is the same.
False
Question 4
Question 5
A string can be surrounded by three sets of single quotation marks or by three sets of double quotation
marks.
True
Question 6
Question 8
Question 9
Question 10
In a nested loop, a break statement terminates all the nested loops in one go.
False
What are tokens in Python? How many types of tokens are allowed in Python? Exemplify your
answer.
Answer
The smallest individual unit in a program is known as a Token. Python has following tokens:
Question 2
Answer
Keywords are reserved words carrying special meaning and purpose to the language
compiler/interpreter. For example, if, elif, etc. are keywords. Identifiers are user defined names for
different parts of the program like variables, objects, classes, functions, etc. Identifiers are not
reserved. They can have letters, digits and underscore. They must begin with either a letter or
underscore. For example, _chk, chess, trail, etc.
What are literals in Python? How many types of literals are allowed in Python?
Answer
Literals are data items that have a fixed value. The different types of literals allowed in Python are:
1. String literals
2. Numeric literals
3. Boolean literals
4. Special literal None
5. Literal collections
Question 4
Can nongraphic characters be used and processed in Python? How? Give examples to support your
answer.
Answer
Yes, nongraphic characters can be used in Python with the help of escape sequences. For example,
backspace is represented as \b, tab is represented as \t, carriage return is represented as \r.
Question 5
Out of the following, find those identifiers, which cannot be used for naming Variables or Functions in
a Python program:
• Price*Qty
• class
• For
• do
• 4thCol
• totally
• Row31
• _Amount
Answer
Question 6
How are floating constants represented in Python? Give examples to support your answer.
Answer
Question 7
Answer
Question 8
What are operators ? What is their function? Give examples of some unary and binary operators.
Answer
Operators are tokens that trigger some computation/action when applied to variables and other objects
in an expression. Unary plus (+), Unary minus (-), Bitwise complement (~), Logical negation (not) are
a few examples of unary operators. Examples of binary operators are Addition (+), Subtraction (-),
Multiplication (*), Division (/).
Question 9
Answer
An expression is any legal combination of symbols that represents a value. For example, 2.9, a + 5, (3
+ 5) / 4.
A statement is a programming instruction that does something i.e. some action takes place. For
example:
print("Hello")
a = 15
b = a - 10
Question 10
Answer
A Python program can contain various components like expressions, statements, comments, functions,
blocks and indentation.
Question 11
Variables are named labels whose values can be used and processed during program run. Variables are
important for a program because they enable a program to process different sets of data.
Question 12
Describe the concepts of block and body. What is indentation and how is it related to block and body?
Answer
A block in Python, represents a group of statements executed as a single unit. Python uses indentation
to create blocks of code. Statements at same indentation level are part of same block/suite and
constitute the body of the block.
Question 13
Answer
Data types are used to identify the type of data a memory location can hold and the associated
operations of handling it. The data that we deal with in our programs can be of many types like
character, integer, real number, string, boolean, etc. hence programming languages including Python
provide ways and facilities to handle all these different types of data through data types. The data types
define the capabilities to handle a specific type of data such as memory space it allocates to hold a
certain type of data and the range of values supported for a given data type, etc.
Question 14
Answer
1. Integers (signed)
2. Booleans
Question 15
What are immutable and mutable types? List immutable and mutable types of Python.
Answer
Mutable types are those whose values can be changed in place whereas Immutable types are those that
can never change their value in place.
1. Lists
2. Dictionaries
3. Sets
1. Integers
2. Floating-Point numbers
3. Booleans
4. Strings
5. Tuples
Question 16
What is the difference between implicit type conversion and explicit type conversion?
Answer
Example: Example:
a, b = 5, 25.5 a, b = 5, 25.5
c=a+b c = int(a + b)
Question 17
An immutable data type is one that cannot change after being created. Give three reasons to use
immutable data.
Answer
Three reasons to use immutable data types are:
1. Immutable data types increase the efficiency of the program as they are quicker to access than
mutable data types.
2. Immutable data types helps in efficient use of memory storage as different variables containing the
same value can point to the same memory location. Immutability guarantees that contents of the
memory location will not change.
3. Immutable data types are thread-safe so they make it easier to parallelize the program through multi-
threading.
Question 18
What is entry controlled loop? Which loop is entry controlled loop in Python?
Answer
An entry-controlled loop checks the condition at the time of entry. Only if the condition is true, the
program control enters the body of the loop. In Python, for and while loops are entry-controlled loops.
Answer
The pass statement of Python is a do nothing statement i.e. empty statement or null operation
statement. It is useful in scenarios where syntax of the language requires the presence of a statement
but the logic of the program does not. For example,
f
o
r
Question 20
i
Below are seven segments of code, each with a part coloured. Indicate the data type of each coloured
i
part by choosing the correct type of data from the following type.
n
(a) int
r
(b) float
a
(c) bool
(d) strn
g
(e) function
(f) listeof int
(g) list( of str
1
(i) if 0temp < 32 :
)
:
print ("Freezing")
(ii) L = ['Hiya', 'Zoya', 'Preet']
i
print(L[1])
(iii) f
M = i []
f
(iv) o=
=
r
L
2
i
(v)
= :
i i p
[
(vi) fa
n
' s
H s
r
i n
a
y e
n
a %l
g
' s
e
, 2e
:
(
' = print("i =", i)
3
Z =
)
o
y 0
a :
CLASS_XII_COMPUTER SCIENCE CHAPTER_1 PYTHON REVISION TOUR Page 11
' :
, M
p.
' ra
L
i
(vii)
n
L
p
Answer
u
=
t
(i) bool
l
(ii) str
[
i
(iii)
' list of int
n
(iv) int
H
e
(v)
i bool
.
(vi) list of str
y
s
(vii)
a str
p
'
l
Type
,
i
B: Application Based Questions
t
'
Question 1
(
Z
)
o
Fill in the missing lines of code in the following code. The code reads in a limit amount and a list of
y
prices and prints the largest price that is less than the limit. You can assume that all prices and the limit
w
a
are positive numbers. When a price 0 is entered the program terminates and prints the largest price that
h
'
is less than the limit.
i
,
l
#Read
e the limit
'
l
P
i
L
r
m
e
i
!
e
t
=
t
Answer
'
=
(
]
#Read the limit
l
f
)
p
i
l
r
m
o
:
i
i
a
n print(L)
t
t
t L = L[1 :]
(
(
=
i
L
n
[
f
p
0
l
u
]
o
t
a
(
+
t
"
(
E
L
i
n
[
n
t
1
p
e
]
u
r
)
t
CLASS_XII_COMPUTER SCIENCE CHAPTER_1 PYTHON REVISION TOUR Page 12
(
t
"
h
E
e
n
if max_price > 0:
p
r
Question 2a
i
n
t
Predict the output of the following code fragments:
(
count = 0
"
w
L
Answer
a h
r i
Output
g l
e e
H
s
e
t c
l o
l
P u
o
r n
i t 2b
Question
H
c
Predict
e < the output of the following code fragments:
l
x
l = 1 10
=
y
o = 0
" 0
w
, :
Answer
H h
e ip
m
Output
l lr
a
x e
l
10 0i
_
o
9 1xn
p
8 2 t
r
H
7 3>
Explanation
i
e
6 4 (
c
l "
l yXH
e y Output Remarks
o :e
)
l
H pl
e
e ro
l
s i
l "
l n)
e
o t
: c
(print("Prices exceed limit
H o
xof", limit);
e u
,
l n
l t
o y
)
+
H =
e x
l 1
=
l
o
CLASS_XII_COMPUTER SCIENCE CHAPTER_1 PYTHON REVISION TOUR Page 13
x
H
e -
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
Predict the output of the following code fragments:
k
e
e
p
Answer
g
Output
o
100
i
90
n
80
g
70
Explanation
60
=
50
Inside while loop, the line x = x - 10 is decreasing x by 10 so after 5 iterations of while loop x will
T
become 40. When x becomes 40, the condition if x < 50 becomes true so keepgoing is set
to
r False due to which the while loop stops iterating.
u
e
Question 2d
x
Predict the output of the following code fragments:
=
1
0
0
w
h
i
l
e
CLASS_XII_COMPUTER SCIENCE CHAPTER_1 PYTHON REVISION TOUR Page 14
k
e
e
x = 45
w
h
Answeri
l
This is
e an endless (infinite) loop that will keep printing 45 continuously.
As thex loop control variable x is not updated inside the loop neither there is any break statement inside
the loop so it becomes an infinite loop.
<
Question 2e
5
Predict
0 the output of the following code fragments:
f :
Answero
r
p
Outputr
x
i
1 n
2 i
t
3 n
4 (
Explanation
5 x
x will[be assigned each of the values from the list one by one and that will get printed.
)
1
,
Question
2 2f
Predict
, the output of the following code fragments:
f 3
Answer
o,
Output
r4
1
2 p,
3 5
4 i]
5 n:
6
Explanation
7 rp
8 ar
range(1,10) will generate a sequence like this [1, 2, 3, 4, 4, 5, 6, 7, 8, 9]. p will be assigned each of the
9 i
values
nn from this sequence one by one and that will get printed.
gt
e
((
1x
,)
1
0
)
:
p
r
i
n
CLASS_XII_COMPUTER SCIENCE CHAPTER_1 PYTHON REVISION TOUR Page 15
t
(
Question 2g
f
Answer
o
r
Output
z
-500
-400i
-300n
-200
-100r
0 a
Explanation
100 n
200 g
range(-500,
300 e 500, 100) generates a sequence of numbers from -500 to 400 with each subsequent
number
400 ( incrementing by 100. Each number of this sequence is assigned to z one by one and then z gets
printed
- inside the for loop.
5
Question
0 2h
0
, the output of the following code fragments:
Predict
x = 510
y = 50
Answer
f 0
o
,
This code
r generates No Output.
1
Explanation
0
i
0
The x-y
) * 2 in range(x-y * 2) is evaluated as below:
i
x -n
:y * 2
⇒ 10 - 5 * 2
⇒ 10 p
r- 10 [∵ * has higher precedence than -]
⇒0 a r
i
n
Thus nrange(x-y * 2) is equivalent to range(0) which returns an empty sequence — [ ].
g
t
e
Question
( 2i
(
x
Predict
- the output of the following code fragments:
z
)
y
2
)
:
p
r
CLASS_XII_COMPUTER SCIENCE CHAPTER_1 PYTHON REVISION TOUR Page 16
i
n
t
c = 0
f
o
r
x
Answer
i
Output
n
50
r
Explanation
Outeraloop executes 10 times. For each iteration of outer loop, inner loop executes 5 times. Thus, the
n c += 1 is executed 10 * 5 = 50 times. c is incremented by 1 in each execution so final value
statement
g
of c becomes 50.
e
(
Question 2j
1
0 the output of the following code fragments:
Predict
)
:
x = [1,2,3]
counter = 0
w f
Answero
h
r
i
Outputl
y
e
%
i
c
*
n
* * o
* * * u
r
%% n a
t
* n
* * e g
Explanation
* * * r
e
%%% (
In
* this< code, the for loop is nested inside the while loop. Outer while loop runs 3 times and prints % as
per 5
* *the elements in x in each iteration. For each iteration of while loop, the inner for loop executes 3
times)printing * as per the elements in x.
l
* * * :
e
Question
n 2k c += 1
print ( (c)
x the output of the following code fragments:
Predict
)
:
p
r
i
n
t
(
x
[
c
CLASS_XII_COMPUTER SCIENCE CHAPTER_1 PYTHON REVISION TOUR Page 17
o
u
n
f
o
r
Answer
x
Output
L i
n
A
Explanation
'
M l
The for
a loop extracts each letter of the string 'lamp' one by one and place it in variable x. Inside the
loop,
P mx is converted to uppercase and printed.
p
' 2l
Question
:
Predict the output of the following code fragments:
p
x r
i
= n
Answert
' (
Output
o s
n t
o
e t r
n
' w .
Explanation
e o uthe while loop, each letter of x and y is accessed one by one and printed.
Inside
Question
y p 2m
p the output of the following code fragments:
Predict
=
x e
r
Answer
'
= (
t x
Output
w
" )
o
a )
a
'
p
p
counter = 0
p
w
l
l
e h
e
, i
l
p
p e
e
e
a
a c
r
r o
, u
p n
e
p t
a
e e
c
a r
h
c
h <
CLASS_XII_COMPUTER SCIENCE CHAPTER_1 PYTHON REVISION TOUR Page 18
"
l
y e
n
Explanation
x.split(", ") breaks up string x into a list of strings so y becomes ['apple', 'pear', 'peach']. The for loop
iterates over this list and prints each string one by one.
Question 2n
=
'
Answer
a
p
Output
p
l
a
e
p
,
p
Explanation
l
p
e
e
x.split(', ') breaks up string x into a list of strings so y becomes ['apple', 'pear', 'peach', 'grapefruit']. The
a
for
P loop iterates over this list. apple and grapefruit are less than m (since a and g comes before m) so
r
they are converted to lowercase and printed whereas pear and peach are converted to uppercase and
E
,
printed.
A
R
p
Question
e 3
P
a
E
Find and write the output of the following python code:
c
A
h
for
C Name
,
H in
['Jaye
grapefruit
g
Answers',
r
'Ramya
a
Output',
p
e 'Tarun
J
f a',
a
r 'Suraj
y
u '] :
e
i print
s
t (Name)
' i
F f
i
y
n N
i
= a
s m
h
x e
e
. [
s
d 0
p
! ]
l
CLASS_XII_COMPUTER
i
R = SCIENCE CHAPTER_1 PYTHON REVISION TOUR Page 19
t
a =
(
m
'
y '
Got it!
Explanation
The for loop iterates over each name in the list and prints it. If the name does not begin with the letter
T, Finished! is printed after the name. If the name begins with T, break statement is executed that
terminates the loop. Outside the loop, Got it! gets printed.
Question 4(i)
How many times will the following for loop execute and what's the output?
f
o
Answer
r
The loops execute 0 times and the code produces no output. range(-1, 7, -2) returns an empty sequence
i there are no numbers that start at -1 and go till 6 decrementing by -2. Due to empty sequence, the
as
loops don't execute.
i
n
Question 4(ii)
r
How many times will the following for loop execute and what's the output?
a
n
f
g o
Answer
e r
(
Loop executes for 5 times.
- i
1
Output
, i
* n
7
*
, r
*
* a
Explanation
-
* n
2 g
range(1,3,1)
) returns [1, 2]. For first iteration of outer loop j is in range [0, 1] so inner loop executes
e
twice.
: For second iteration of outer loop j is in range [0, 1, 2] so inner loop executes 3 times. This
(
makes the total number of loop executions as 2 + 3 = 5.
1
f ,
Question
o 3 5
r ,
Is the1loop in the code below infinite? How do you know (for sure) before you run it?
j )
m = 3
:
n
i = 5
n f
o
r r
a
n j
g
e i
CLASS_XII_COMPUTER
n SCIENCE CHAPTER_1 PYTHON REVISION TOUR Page 20
(
3 r
) a
w
h
i
l
eAnswer
nThe loop is not infinite. To know this without running it we can analyze how n is changed inside the
loop in the following way:
<
n=2*n-m
1
0Substituting value of m from m = n - 1,
:
n = 2 * n - (n - 1)
m
⇒n=2*n-n+1
⇒ n = 2n - n + 1
=
⇒n=n+1
nTherefore, inside the loop n is incremented by 1 in each iteration. Loop condition is n < 10 and initial
value of n is 5. So after 5 iterations, n will become 10 and the loop will terminate.
-
*
Solution
n
x = int(input("Enter x: "))
if x-< 0:
p
r m
Output
i
n p
E
t r
n
( i
t
" n
e
n t
r
e (
g n
x
a ,
t
:
i m
v
- )
e
5
"
)
n
e
e
g
l
a
i
t
CLASS_XII_COMPUTER SCIENCE CHAPTER_1 PYTHON REVISION TOUR Page 21
f
i
v
x
e
Question 2
Write a program that returns True if the input number is an even number, False otherwise.
Solution
i
f
Output
x
E
n %
t
e 2
Question 3
r
Write=a Python program that calculates and prints the number of seconds in a year.
a =
Solution
n 0
u :
days
m = 365
hours
b p = 24
mins
e r= 60
secs
r i= 60
Output
secsInYear
: n
= dayst *
Number
hours of seconds in a year =
1 ( *
31536000
mins
0 "* 4
Question
secsT
print("Num
T
Write ra Python program that accepts two integers from the user and prints a message saying if first
ber
r
numberof
u is divisible by second number or if it is not.
seconds
u e in
a
e year" =",
Solution
secsInYear
)
)
E
else:
a
n print("False")
t
=
e
r
i
n
a
t
(
n
i
u
n
m
p
b
u
e
t
r
(
:
"
E
5
n
CLASS_XII_COMPUTER
t SCIENCE CHAPTER_1 PYTHON REVISION TOUR Page 22
F
e
a
r
l
print(a, "is not divisible
by", b)
Output
E
n
t
e
r
Question 5
f
Write a program that asks the user the day number in a year in the range 2 to 365 and asks the first day
i
of the year — Sunday or Monday or Tuesday etc. Then the program should display the day on the day-
r
number that has been input.
s
t
Solution
n
dayNames
u = ["MONDAY",
"TUESDAY",
m "WEDNESDAY",
"THURSDAY",
b "FRIDAY",
"SATURDAY",
e "SUNDAY"]
r
d
:
a
y
1
N
5
Output
u
m
E
E
n
n
=
t
t
Question
e 6
e
i
r
r
One
n foot equals 12 inches. Write a function that accepts a length written in feet as an argument and
returns
t
s this length written in inches. Write a second function that asks the user for a number of feet and
d
returns
(
e this value. Write a third function that accepts a number of inches and displays this to the
a
screen.
i
c Use these three functions to write a program that asks the user for a number of feet and tells
y
them
n
o the corresponding number of inches.
p
n
n
u
d
u
t
m
n
(
b
u
"
e
m
E
r
b
n
:
e
t
r
e
2
:
r
4
3
5
d
15 is divisible by 5
a
F
i
y
E
r
n
s
n
t
u
CLASS_XII_COMPUTER SCIENCE CHAPTER_1 PYTHON REVISION TOUR Page 23
e
m
r
d
b
a
e
f
y
r
Solution
d
e
f
f
e
e
t
Output
T
o
E
I
n
n
t
Question
c 7
e
h
r
Write a program that reads an integer N from the keyboard computes and displays the sum of the
e
numbers from N to (2 * N) if N is nonnegative. If N is a negative number, then it's the sum of the
s
l
numbers from (2 * N) to N. The starting and ending points are included in the sum.
(
e
l
n
Solution
g e
t n
n F
h
e
= e
i
n t
i )
n :
f
Output
t
e
( l N: 5
e
Enter
i e
t
Sum = 45
n n
:
p I N: -5
Enter
u n
1
Sum = -45
t c
5
( h
"
L
E
e =
n
n
t
g l
e
t e
r
h n
F
N
i e
:
n e
t
"
i
)
n *
)
c
h 1
s
e 2
u
s
m r
= e
CLASS_XII_COMPUTER SCIENCE CHAPTER_1 PYTHON REVISION TOUR Page 24
= t
1 u
8
0 r
Question 8
Write a program that reads a date as an integer in the format MMDDYYYY. The program will call a
function that prints print out the date in the format <Month Name> <day>, <year>.
Sample run :
E
n
t
Solution
e
r
months = ["January",
"February", "March",
d
"April", "May", "June",
a
"July", "August",
t
"September", "October",
e
"November", "December"]
Output
:
dateStr =
input("Enter
E date
1
in MMDDYYYY
n
2
format:
Question 9")
t
2
monthIndex =
e
5
int(dateStr[:2])
r
Write - a table on two columns — table that helps converting miles into
a program that prints
2
1
kilometres.
0
m
d
1
o
a
Solution
9
n
t
t
e
p
D
h
r
e
i
i
c
n
=
Output
n
e
t
m
M
m
Miles | Kilometres
(
b
M
o
1
' 1.60934
e
D
n
10
M 16.0934
r
D
t
20
i 32.1868
Y
h
30 48.2802
l
2
Y
s
40 64.3736
e
5
Y
[
50 80.467
s
,
Y
m
60 96.5604
o
70
|
2 112.6538
f
n
0
o
t
K
1
r
h
i
9
m
I
l
a
n
o
t
d
m
:
e
e
x
t
1
]
r
2
e
2
d
s
5
a
CLASS_XII_COMPUTER
' SCIENCE CHAPTER_1 PYTHON REVISION TOUR Page 25
2
y
)
0
1
=
p
9
80 128.7472
90 144.8406
100 160.934
Question 10
Write another program printing a table with two columns that helps convert pounds in kilograms.
Solution
p
r
i
Output
n
t
Pounds | Kilograms
(
1
' 0.4535
10
P 4.535
20
o 9.07
30
u 13.605
40
n 18.14
50
d 22.675
60
s 27.21
70 31.745
80
| 36.28
90 40.815
100
K 45.35
i
Question 11
l
o
Write
g a program that reads two times in military format (0900, 1730) and prints the number of hours
and
r minutes between the two times.
a
A
m sample run is being given below :
s
P
'
l
)
e
Solution
a
p
s
ft =
r
e
inpu
i
t("P
n
e
leas
t
n
e
(
t
ente
1
e
r
,
r
the
firs
"
t
t
\
h
time
t
e
:
" ")
st
, =
f
inpu
CLASS_XII_COMPUTER
i
t("P SCIENCE CHAPTER_1 PYTHON REVISION TOUR Page 26
0
r
leas
.
s
e
4
t
ente
diff = sMins - fMins;
C
Output
o
n
P
v
l
e
e
r
a
t
s
e
t
h
e
e
n
t
d
e
i
r
f
f
t
e
h
r
e
e
n
f
c
i
e
r
s
t
t
o
t
h
i
o
m
u
e
r
s
:
&
0
9
m
0
i
0
n
s
P
l
h
e
r
a
s
s
e
=
e
d
n
i
t
f
e
f
r
CLASS_XII_COMPUTER SCIENCE CHAPTER_1 PYTHON REVISION TOUR Page 27
/
t
/
h