Class 12 Pyq Chapter 1
Class 12 Pyq Chapter 1
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
QUESTION 1
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.
QUESTION 3
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:
QUESTION 4
Answer True.
Reason — In Python, variable declaration is implicit. This means that we don't need to explicitly
declare the data type of a variable before using it. The type of a variable is determined dynamically
at runtime based on the assigned value. For example:
x=5 # x is implicitly declared as an integer
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
class ⇒ It is a keyword
Question 6
MyName
True
2ndName
My_Name
Answer
True ⇒ It is a keyword
Question 7
Answer
Real
Question 8
<
**
and
Answer :: **
Reason — Let's go through each option and see if its valid arithmetic operator or not:
** — It is an arithmetic operator.
Question 9
Answer
Question 10
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 11
**
*/
like
||
is
between
in
Answers
** ⇒ Exponentiation operator
is ⇒ Identity operator
in ⇒ Membership operator
Question 12
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 13
Answer
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 18(a)
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 18(b)
Answer
Integer
String
List
Tuple
Question 19
Answer
Integers (signed)
Booleans
Question 20
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.
Question 21
An immutable data type is one that cannot change after being created. Give three reasons to use
immutable data.
Answer
* Immutable data types increase the efficiency of the program as they are quicker to access than
mutable data types.
* 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.
* Immutable data types are thread-safe so they make it easier to parallelize the program through
multi-threading.
Question 21
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 23
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.
Question 24
AnswerThe 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,
for i in range(10):
if i == 2:
pass
else:
print("i =", i)
Question 25
Rewrite the adjacent code in python after removing all syntax error(s). Underline each correction
done in the code.
30 = To
for K in range(0,To)
IF k%4 == 0:
print(K * 4)
Else:
print(K+3).
Answer
To = 30 # Correction 1
if K % 4 == 0: # Correction 3
print(K * 4)
else: # Correction 4
print(K+3) # Correction 5
Explanation
Correction 1 — Variable should be on left side and literals should be on right side.
Correction 5 — Full stop should not be there at the end of print function.
Fill in the missing lines of code in the following code. The code reads in a limit amount and a list of
prices and prints the largest price that is less than the limit. You can assume that all prices and the
limit are positive numbers. When a price 0 is entered the program terminates and prints the largest
price that is less than the limit.
max_price = 0
if max_price > 0:
else :
Answer
max_price = 0
max_price = next_price
if max_price > 0:
print("Largest Price =", max_price)
else :
Question 2b
x = 10
y=0
while x > y:
print (x, y)
x=x-1
y=y+1
Answer
Output
10 0
91
82
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
Inside while loop, the line x = x - 10 is decreasing x by 10 so after 5 iterations of while loop x will
become 40. When x becomes 40, the condition if x < 50 becomes true so keepgoing is set to False
due to which the while loop stops iterating.
Question 2h
x = 10
y=5
Answer
Explanation
x-y*2
⇒ 10 - 5 * 2
⇒0
Question 2i
c=0
for x in range(10):
for y in range(5):
c += 1
print (c)
Answer
Output
50
Explanation
Outer loop executes 10 times. For each iteration of outer loop, inner loop executes 5 times. Thus, the
statement c += 1 is executed 10 * 5 = 50 times. c is incremented by 1 in each execution so final value
of c becomes 50.
Question 2j
x = [1,2,3]
counter = 0
print(x[counter] * '%')
for y in x:
counter += 1
Answer
Output
**
***
%%
**
***
%%%
**
***
Explanation
In this code, the for loop is nested inside the while loop. Outer while loop runs 3 times and prints %
as per 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.
Question 2n
y = x.split(', ')
for z in y:
if z < 'm':
print(str.lower(z))
else:
print(str.upper(z))
Answer
Output
apple
PEAR
PEACH
grapefruit
Explanation
x.split(', ') breaks up string x into a list of strings so y becomes ['apple', 'pear', 'peach', 'grapefruit'].
The for loop iterates over this list. apple and grapefruit are less than m (since a and g comes before
m) so they are converted to lowercase and printed whereas pear and peach are converted to
uppercase and printed.
Question 4(ii)
How many times will the following for loop execute and what's the output?
for i in range(1,3,1):
for j in range(i+1):
print('*')
Answer
Output
Explanation
range(1,3,1) returns [1, 2]. For first iteration of outer loop j is in range [0, 1] so inner loop executes
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.