You are on page 1of 24

1 Revision Tour SECTION : A

n
ha
1. What is python?
Ans. Python is an interpreted, high-level, general-purpose programming language, which was created by Guido van

as
Rossum. It was first released in 1991. It is used in many applications like Software Development, Web Development,
System Scripting, Mathematics, etc.
features of Python

ak
• Easy to use: due to simple syntax, it is very easy to implement.
• Interpreted Language: In python, every code is executed line by line.
• Cross-platform Language: can run on any platform Windows, Linux, Macintosh, Raspberry Pi, etc.

Pr
• Expressive Language: It supports a wide range of library.
• Free & Open Source: It can be downloaded free of cost.
disadvantages
Lesser Libraries: As compared to other programming languages like c++, java, .Net, etc. it has a lesser number
rs

of libraries.
• Slow Language: Being an interpreted language, the code is executed slowly.
he

• Weak on Type-Binding: A variable that is initially declared int can be changed to string at any point in the
program without any typecasting.
2. What is variable?
Ans. A variable is a named location used to store data in the memory. For example, in the following statement a
ot

variable ‘n’ is created and assigned the value 10 to n.


n=10
Br

3. How can multiple values be assigned to multiple variables and same value to different variables.
Ans. Assigning multiple values to multiple variables:
a,b,c=10,20,”hello”
Assign the same value to multiple variables at once:
al

x=y=z=50

4. Write code to create a variable num and assign 100 value to num.
oy

Ans. num=100

1. tOKens
5. Define token in python.
G

Ans. Token: The smallest individual unit in a program is known as a token. There are five types of tokens allowed
in Python. They are :
• Keywords • Identifiers • Literals • Operators • Punctuators

Score Plus Question Bank with CBSE Sample Paper and Model Test Papers in Computer Science-12 (Term I) 1
(i) Keywords
Keywords are the reserved words in Python. We cannot use a keyword as a variable name, function name or any other
identifier. They are used to define the syntax and structure of the Python language. In Python, keywords are case sensitive.
False class from or
None continue global pass

n
True def if raise
and del import return

ha
as elif in try
assert else is while
async except lambda with

as
await finally nonlocal yield
break for not

ak
(ii) Identifier
An identifier is a name given to entities like class, functions, variables etc.
Rules for writing identifiers

Pr
• Identifier must always start with either a letter or an underscore (_). For example: _str, str, num, _num are all
valid name for the variables.
• Identifiers cannot start with a number. For example, 9num is not a valid variable name.
rs
• The identifier cannot have special characters such as %, $, #.- etc, they can only have alphanumeric characters
and underscore (A to Z, a to z, 0-9 or _ ).
• An identifier cannot contain space.
he

• Identifier is case sensitive in Python which means num and NUM are two different variables in python.
• Keywords cannot be used as identifier.
6. Out of the following, find those identifiers, which cannot be used for naming Variable or Functions in a Python
ot

program: 2 [OD 16]


Total*Tax, While, class, switch, 3rdRow, finally, Column31, _Total
Ans. Total*Tax, class, 3rdRow, finally
Br

7. Identify invalid variable names out of the following. State reason if invalid. [OD 16]
(i) for (ii) –salary (iii) salary12 (iv) product
Ans. Invalid variable names : (i) for and (ii) -salary
Reason : (i) ‘for’ : is a keyword
al

(ii) ‘-salary’ : variable name cannot start with special character.


8. Identify invalid variable name(s) out of the following. State reason if invalid [comptt 16]
oy

(i) While (ii) 123salary (iii) Big (iv) Product


Ans. (ii) 123salary
9. Help Manish in identifying the incorrect variable name with justification from the following:
G

(i) unit@price (ii) fee (iii) userid (iv) avg marks [SP 18]
Ans. (i) unit@price; // Special symbols like „@‟ is not allowed in variable name
(iv) avg marks;// Spaces are not allowed in variable name

2 Score Plus Question Bank with CBSE Sample Paper and Model Test Papers in Computer Science-12 (Term I)
10. Identify the invalid variable names. State the reason if invalid. 1 [2018]
(i) Marks Unit (ii) Product_1 (iii) Sales123 (iv) 2Marks
Ans. Invalid variable names are :
(i) Marks Unit
Reason : Variable Name should not contain space

n
(iv) 2Marks
Reason : Variable Name should not start with digit

ha
11. Which of the following can be used as valid variable identifier(s) in Python? 2[d 17]
(i) total (ii) 7Salute (iii) Que$tion (iv) global
Ans. (i) total

as
12. Which of the following can be used as valid variable identifier(s) in Python? 2 [compt 17]
(i) elif (ii) BREAK (iii) in (iv) _Total
Ans. (ii) BREAK (iv) _Total

ak
13. Which of the following can be used as valid variable identifier(s) in Python? [OD 17]
(i) 4thSum (ii) Total (iii) Number# (iv) _Data
Ans. (ii) Total   (iv) _Data
14. Which of the following is not a valid variable name in Python. Justify reason for it not being a valid name:

Pr
[comptt 2020]
(i) 5Radius (ii) Radius_ (iii) _Radius (iv) Radius
Ans. (i) 5Radius
Reason: variable name in Python cannot start with a digit
rs
15. Which of the following are keywords in Python?
(i) break (ii) check (iii) range (iv) while [comptt 2020]
he

Ans. (i) break  (iii) range   (iv) while


16. Out of the following, find those identifiers, which cannot be used for naming Variable or Functions in a Python
program: 2 [D 16]
_Cost, Price*Qty, float, Switch, Address One,
ot

Delete, Number12, do
Ans. Price*Qty, float, Address One
Br

17. Find the correct identifiers out of the following, which can be used for naming variable, constants or functions
in a python program:
While, for, Float, new, 2ndName, A%B, Amount2, _Counter
Ans. While, Float, Amount2, _Counter
al

18. Find the correct identifiers out of the following, which can be used for naming Variable, Constants or Functions
in python program: [OD 2015]
For, while, INT, NeW, delete, 1stName, Add+Subtract, name1
oy

Ans. For, INT, NeW, name1


19. Out of the following, find those identifiers, which cannot be used for naming Variable, Constants or Functions in
a python program:
_Cost, Price*Qty, float, switch, Address One, Delete, Number12, do
G

Ans. Price*Qty
float
Address One
Score Plus Question Bank with CBSE Sample Paper and Model Test Papers in Computer Science-12 (Term I) 3
20. Out of the following, find those identifiers, which cannot be used for naming Variable, Constants or Functions in
a python program:
Total*Tax, double, Case, My Name, NeW, switch, Column31, _Amount
Ans. Total*Tax
My Name

n
21. Find the invalid identifier from the following: 1[SP 21]
(i) MyName (ii) True (iii) 2ndName (iv) My_Name

ha
Ans. (i) True, (ii) 2ndName
22. Write the type of python keywords and user defined identifiers from the following:
(i) For (ii) Delete (iii) else (iv) Value
Ans. (i) For - user defined identifier (ii) Delete - user defined identifier

as
(iii) else - keyword (iv) Value - user defined identifier
23. Write the type of python keywords and user defined identifiers from the following :
(i) case (ii) _delete (iii) while (iv) 21stName

ak
Ans. (i) keyword (ii) identifier
(iii) keyword (iv) None
24. Write the type of python keywords and user defined identifiers from the following:

Pr
(i) in (ii) While (iii) and (iv) Num_2
Ans. (i) in - Keyword (ii) While - User defined Identifier
(iii) and - Keyword (iv) Num_2 - User defined Identifier
25. Write the type of python keywords and user defined identifiers from the following:
rs
(i) else (ii) Long (iii) break (iv) _count
Ans. (i) keyword (ii) Identifier (iii) keyword (iv) Identifier
he

26. Identify the valid keywords in Python from the following: [2] [comptt 2020]
(i) Queue (ii) False (iii) in (iv) Number
(v) global (vi) method (vii) import (viii) List
Ans. (ii) False  (iii) in  (v) global  (vii) import
ot

27. Write the type of tokens from the following: 1[SP 20]
(i) if (ii) roll_no
Br

Ans. (i) Keyword     (ii) Identifier


(iii) Literal
Literal is a raw data given in a variable. There are various types of literals
(a) Numeric literals: Three types of numeric literals available in python: integer, float and complex
al

Int literals Float literals Complex literals


a=123 x=3.14 z=4.13j
b=839 y= 2.85
oy

(b) String literals: A string literal is a sequence of characters surrounded by quotes. We can use both single, double,
or triple quotes for a string
a= “hello”
G

b= ‘12345’
(c) Boolean literals: A Boolean literal can have any of the two values: True or False
(d) Special literals: Python contains one special literal i.e. None

4 Score Plus Question Bank with CBSE Sample Paper and Model Test Papers in Computer Science-12 (Term I)
(e) Literal Collections: There are four different literal collections: List literals, Tuple literals, Dict literals, and Set
literals.
(i) List literals :: [5,6,7] (ii) Tuple literals :: (1,2,3)
(iii) Dict literals :: {‘x’:1} (iv) Set literals :: {8,9,10}

(iv) Punctuators

n
Most commonly used punctuators in Python are:
‘ “ # \ () {} [] @ , : . =

ha
(v) Python Operators
Python operator is a symbol that performs an operation on one or more operands. An operand is a variable or a value
on which we perform the operation. Python provides a variety of operators described as follows.

as
• Arithmetic Operators • Relational Operators • Assignment Operators • Logical Operators
• Membership Operators • Identity Operators • Bitwise Operators
(1) Arithmetic operators: Arithmetic operators are used to perform arithmetic operations between two operands. It

ak
includes +(addition), - (subtraction), *(multiplication), /(divide), %(remainder), //(floor division), and exponent
(**).
(a) Addition(+): It is used to add two operands. For example, if a = 20, b = 10 => a+b = 30
(b) Subtraction(-): It is used to subtract the second operand from the first operand. For example, a=30,

Pr
b=20>>> a-b=10
(c) Multiplication(*): It is used to multiply one operand with the other. For example, if a = 20, b = 10 => a
* b = 200
(d) Division(/): It returns the quotient after dividing the first operand by the second operand. For example, if
a = 20, b = 10 => a/b = 2. If quotient is coming in float value then division results in a floating-point
rs
value, for example, a=11,b=2 >>> a/b Output: 5.5
(e) Exponentiation(**): It returns the second operand power to first operand. a=2,b=3,a**b i.e. ab=8
he

(f) Floor Division(//): It returns the integer value of the quotient. It dumps the digits after the decimal. a =
10,b = 3 => a//b = 3
(g) Modulus(%): It returns the remainder after dividing the first operand by the second operand. For example,
a = 20, b = 10 => a%b = 0
ot

Addition(+) Subtraction(-) Multiplication(*)


Division(/) Exponentiation(**) Floor Division(//)
Br

Modulus(%)
(2) Relational Operators: Relational Operators are used to compare the value of two operands. It checks whether
an operand is greater than the other, lesser, equal, or a combination of those and returns the Boolean value i.e.
True or False.
al

When the condition for a relative operator is fulfilled, it returns True. Otherwise, it returns False. This return
value can be used in a further statement or expression.
(a) Less than(<): This operator checks if the value of first operand is less than the second operand. (i) 3<4
oy

Output True (ii) 15<10 Output False


(b) Greater than(>): It checks if value of first operand is Greater than the second operand.
>>> 3>4 Output: False
G

(c) Less than or equal to(<=): It checks if the value of first operand is Less than or equal to the second
operand. >>> 7<=7 Output: True
(d) Greater than or equal to(>=): It checks if the value of first operand is Greater than or equal to the second
operand. >>> 0>=0 Output: True
Score Plus Question Bank with CBSE Sample Paper and Model Test Papers in Computer Science-12 (Term I) 5
(e) Equal to(= =): It checks if the value of first operand is Equal to the second operand. 1 is equal to the
Boolean value True, other than 1 is equal to False.
(i) 5= =5 Output: True
(ii) 5= =8 Output: False
(f) Not equal to(!=): It checks if the value of two operands is not equal.

n
(i) 5! =5 Output: False
(ii) 3!=2 Output: True

ha
(3) Assignment operators: The assignment operators are used to assign the value of the right expression to the left
operand. It may manipulate the value by a factor before assigning it.
(a) Assign(=): It assigns the value of the right expression to the left operand., = = is used for comparing and
= is used for assigning.

as
a=10
print(a)
output :10

ak
(b) Add and Assign(+=): It increases the value of the left operand by the value of the right operand and assign
the modified value back to left operand.
a+ = b equal to a = a+ b

Pr
a+=10 equal to a=a+10

a=10 a=10 a=20


a+=20 b=20 a+=5*2
print(a) a+=b print(a)
rs
output :30 print(a,b) output:30
output:30,20
he

(c) Subtract and Assign(-=): It subtracts the value of the left operand by the value of the right operand and
assigns the modified value back to left operand.
a-= b equal to a = a- b
a-=10 equal to a=a-10
ot

a=20 a=10 a=20


a-=5 b=50 a-=5*2
Br

print(a) a-=b print(a)


output :15 print(a,b) output:10
output:-40,50
al

(d) Divide and Assign(/=): It divides the value of the left operand by the value of the right operand and assigns
the modified value back to left operand.
a/= b equal to a = a/ b
oy

a/=10 equal to a=a/10

a=20 a=10 a=20


a/=5 b=20 a/=5*2
G

print(a) a/=b print(a)


output :4.0 print(a,b) output:2.0
output:0.5,20

6 Score Plus Question Bank with CBSE Sample Paper and Model Test Papers in Computer Science-12 (Term I)
(e) Multiply and Assign(*=): It multiplies the value of the left operand with the value of the right operand
and assigns the modified value back to left operand.
a*= b equal to a = a*b
a*=10 equal to a=a*10
a=20 a=10 a=20

n
a*=5 b=20 a*=5*2
print(a) a*=b print(a)
output :100 print(a,b) output:200

ha
output:200,20

(f) Modulus and Assign(%=): It divides the value of the left operand by the value of the right operand and
assigns the remainder value back to left operand.

as
a%= b equal to a = a% b
a%=10 equal to a=a%10

a=20 a=10

ak
a%=5 b=20
print(a) a%=b
output :0 print(a,b)
output:10,20

Pr
(g) Exponent and Assign(**=): It performs exponentiation(power) the value of the left operand by the value
of the right operand and assigns the modified value back to left operand.
a**= b equal to a = a** b i.e. a=ab
a**=10 equal to a=a**10
rs
a=2 a=5
a**=3 b=2
he

print(a) a**=b
output :8 print(a,b)
output:25,2

(h) Floor-Divide and Assign(//=): It performs floor-division the value of the left operand by the value of the
ot

right operand and assigns the modified value back to left operand.
a//= b equal to a = a// b
a//=10 equal to a=a//10
Br

a=20 print(10//3)
a//=5 output:3
print(a)
output :4
al

(4) Logical Operators: The logical operators are conjunctions that can be used to combine more than one condition.
There are three Python logical operator – and, or, and not that come under python operators.
oy

(a) and: If all the expressions are true, then the condition will be true.
a=10 a=10
b=20 b=20
G

a<b and b>15 c=40


output : True a>b and b<c and c= =40
output: False

Score Plus Question Bank with CBSE Sample Paper and Model Test Papers in Computer Science-12 (Term I) 7
(b) or: If one of the expressions is true, then the condition will be true.
a=10 a=10 a=10
b=20 b=20 b=20
a<b or b>15 c=40 a>b or b>20
output : True a>b or b<c or c= =40 output :False

n
output: True
(c) not: This inverts the Boolean value of an expression. It converts True to False, and False to True. If an

ha
expression a is true then not (a) will be false and vice versa.
a=10 not 0 not “xyz” = =”xyz”
b=20 Output: True output: False
not a>b

as
output : True

(5) Membership Python Operators : Python membership operators checks whether the value is a member of a

ak
sequence.
(a) in: It is evaluated to be true if the first operand is found in the second operand (string, list, tuple or
dictionary).

Pr
“x” in “xyz” 5 in [10,20,30]
Output: True Output: False
(b) not in: It is evaluated to be true if the first operand is not found in the second operand (string, list, tuple
or dictionary).
rs
“x” not in “xyz” 5 not in [10,20,30]
Output: False Output: True
he

(6) Python Identity Operators: Identity operators are used to verify if two variables point to the same memory
location or not. Identity operators are of two types ‘is’ and ‘is not’.
(a) is: ’is’ operator returns true if both the operands point to same memory location. The variable having same
ot

value points to same memory location; So, for them this operator returns a ‘true’.
a=10 a=”xyz” “x” is “xyz”
b=10 b=”xyz” Output: False
Br

a is b a is b
Output :True output:True

(b) is not: is not operator returns true if both the operands point to different memory location. It is the opposite
al

of ‘is’ operator.
2 is not 2 “x” is not “xyz” 2 is not 2.0
Output :False Output: True Output:True
oy

(7) Bitwise Operator:


G

& | ^
Binary AND Binary OR Binary XOR

~ Binary Ones Complement << Binary Left Shift >> Binary Right Shift

8 Score Plus Question Bank with CBSE Sample Paper and Model Test Papers in Computer Science-12 (Term I)
28. Which of the following are valid operators in Python? [comptt 19]
(i) += (ii) ^ (iii) in (iv) && (v) between
(vi) */ (vii) is (viii) like
Ans. (i) += (ii) ^ (iii) in (vii) is
29. Which of the following are valid operators in Python: [2019]

n
(i) ** (ii) */ (iii) like (iv) ||
(v) is (vi) ^ (vii) between (viii) in

ha
Ans. (i) ** (v) is (vi) ^ (viii) in
30. Which of the following is valid arithmetic operator in Python: [SP 20]
(i) // (ii) ? (iii) < (iv) and
Ans. (i) //

as
31. Identify the valid arithmetic operator in Python from the following.
(i) ? (ii) < (iii) ** (iv) and [SP 21]
Ans. (iii) **

ak
32. How many types of Data Type available in Python.
Ans. Data Types

Text Type: string

Pr
Numeric Types: int, float, complex
Sequence Types: list, tuple, range
Mapping Type: dict
rs
Set Types: set, frozenset
Boolean Type: bool
he

Binary Types: bytes, bytearray, memoryview


33. Write the names of any four data types available in Python. 2[2019]
Ans. Numbers Integer Boolean Floating Point Complex None Sequences
Strings Tuple List Sets Mappings Dictionary
ot

34. Consider the statement:


first_name = “Ayana”;
Br

(i) What is the datatype of first_name ?


(ii) Is 325 the same as “325” ? Give reason.
Ans. (i) String data type
(ii) No, 325 is a Number/Integer while “325” is a String.
al

35. Consider the statement :


fname = “Rishabh”;
(i) What is the datatype of fname ?
oy

(ii) Is 456 the same as “456” ? Give reason


Ans. (i) String data type
(ii) No, 456 is a Number/Integer while “456” is a string.
G

36. (i) Is 123 the same as 123.0 ? Give reason


(ii) Is “234” the same as “234.0” ? Give reason
Ans. (i) No, 123 is an Integer. And 123.0 is a floating type.
(ii) They are different strings and they are not of Number Datatype.
Score Plus Question Bank with CBSE Sample Paper and Model Test Papers in Computer Science-12 (Term I) 9
37. Write the data type of variables that should be used to store: 1[2018]
(i) Marks of students
(ii) Grades of students(Grade can be ‘A’ or ‘B’ or ‘C’)
Ans. (i) float/int   (ii) string

2. Operator Precedence

n
38. What is the precedence of operators?
Ans. The precedence of an operator specifies how “tightly” it binds two expressions together. The precedence of the

ha
operators is important to know which operator should be evaluated first. The precedence table of the operators
in python is given below:
Operator Description

as
** The exponent operator is given priority over all the others used in the expression.
~+- The negation, unary plus and minus.
* / % // The multiplication, divide, modules, remainder, and floor division.

ak
+- Binary plus and minus
>> << Left shift and right shift

Pr
& Binary and.
^| Binary xor and or
<= < > >= Comparison operators (less than, less than or equal to, greater than, greater than
or equal to).
rs
<> == != Equality operators.
= %= /= //= -= +=
he

*= **= Assignment operators


is is not Identity operators
in not in Membership operators
ot

not or and Logical operators

39. Write the value that will be assigned to variable x after executing the following statement:
Br

x = 3 + 36/12 + 2*5
Ans. 16.0
40. Write the value that will be assigned to variable c after executing the following statement:
C = 25-5*4/2-10+4
al

Ans. 9.0
41. Write the value that will be assigned to variable x after executing the following statement:
oy

x = 20 -5 + 3 * 20/5
Ans. 27.0
42. Evaluate the following expressions: [SP 21]
(a) 6 * 3 + 4**2 // 5 – 8
G

(b) 10 > 5 and 7 > 12 or not 18 > 3


Ans. (a) 13     (b) False

10 Score Plus Question Bank with CBSE Sample Paper and Model Test Papers in Computer Science-12 (Term I)
3. Print function
The print function in Python is used to display the output of variables: string, lists, tuples, range, etc.
Following is the syntax of using the print function:
print(*objects, sep=’ ‘, end=’\n’, file=sys.stdout, flush=False)
Where:

n
• The object can be strings, lists, tuple etc.
• The sep = ‘’ parameter specifies a space between multiple objects. You can use other than space by using this
parameter.

ha
• The end=’\\n\’ means in each call, the print function will end with a new line, if you want you can change that.
• The file=sys.stdout specifies where the print function should send the output. is sys.stdout.
• The flush keyword argument specifies whether to flush or not the output stream. The default value is false.

as
43. Give the output
(i) print(20,sep=”-“,end=”$”)
(ii) print(40,50,60,sep=”*“,end=”!”)
(iii) print(“hello,hi,bye”,sep=”*“,end=”!”)

ak
(iv) print(“hello”,”hi”,”bye”,sep=”*”,end=”!”)
(v) print(2>5,7<10,end=”@”)
(vi) print(“2*3,6*5”,sep=”%”)
(vii) print(2*3,6+5,sep=”+”)

Pr
Ans. (i) 20$ (ii) 40*50*60! (iii) hello,hi,bye! (iv) hello*hi*bye!
(v) False True@ (vi) 2*3,6*5 (vii) 6+11
4. Input() function
The input() method reads a line from input(usually user), converts into a string and returns it.
rs
The syntax of input() method is:
input([prompt])
The input() method takes a single optional argument:
he

prompt (Optional) - prompt is the string we wish to display on the screen. It is optional.

Practice Questions – I
44. What is the difference between mutable and immutable in Python?
ot

Ans. Mutable object can be changed after it is created, and an immutable object can’t. Objects of built-in types like
(int, float, bool, string, tuple, unicode) are immutable. Objects of built-in types like (list, set, dict) are mutable.
45. Distinguish between ‘/’ and ‘%’ operators.
Br

Ans. ‘/’ divides first number with second number and returns the quotient.
‘%’ divides first number with second number and returns the remainder.
46. Write the output of the following Python code: 2 [SP 18]
i=5
al

j=7
x=0
i=i+(j-1)
oy

x=j+1
print(x,”:”,i)
j=j**2
G

x=j+i
i=i+1
print (i,”:”,j)
Ans. 8 : 11  12 : 49
Score Plus Question Bank with CBSE Sample Paper and Model Test Papers in Computer Science-12 (Term I) 11
47. Give the output if a=10 and b=20
a=input(“enter no”)
b=input(“enter 2nd no”)
print(a+b)
Ans. 1020

n
48. Write a program to take two nos and swap two nos.
Ans. a=int(input(“enter a”))

ha
b=int(input(“enter b”))
a,b=b,a
print(a,b)
49. Write a program to take two numbers and show 2nd number is power of 1st . For example if 1st number is 5

as
and 2nd number is 3 then result is 53 = 125
Ans. a=int(input(“enter 1st no “))
b=int(input(“enter power”))

ak
print(a**b)
50. Write a program to take two nos and show the sum of these two nos.
Ans. a=int(input(“enter a”))
b=int(input(“enter b”))

Pr
print(a+b)
51. Write a program to take two float numbers and show the sum of these two.
Ans. a=float(input(“enter a”))
b=float(input(“enter b”))
rs
print(a+b)
52. Write a program to take first name and last name from user and show full name. For example if first name is
“Aman” and last name is “Sharma” then it will display “AmanSharma”(without space).
he

Ans. first=input(“enter 1st name”)


last=input(“enter last name”)
print(first+last)
ot

53. Give the output


(i) a=20
b=30
Br

a,b=b+20,a+30
print(a,b)
ns. 50 50
A
(ii) a=20
al

b=30
a,c=b*10,a/10
print(a,b,c)
oy

Ans. 300 30 2.0


(iii) a=10
b=a*20
G

b,c=b*10,a%10
print(a,b,c)
Ans. 10 2000 0

12 Score Plus Question Bank with CBSE Sample Paper and Model Test Papers in Computer Science-12 (Term I)
Multiple Choice Questions – I
1. Which of the following can be used as valid variable identifier(s) in Python?
(i) total (ii) switch (iii) Que$tion (iv) global
2. Identify invalid variable name out of the following.
(i) While (ii) 123salary (iii) Big (iv) float

n
3. Which of the following is invalid operator in Python?
(i) += (ii) ^ (iii) in (iv) &&

ha
4. Identify invalid operator out of the following.
(i) between (ii) */ (iii) is (iv) //
5. What is the datatype of “321”?

as
(i) None (ii) Integer (iii) String (iv) List
6. Output of the following code is:
print(40,50,60,sep=”!“,end=”,”)
(i) 40,50,60! (ii) 40!50!60! (iii) 40!50!60, (iv) 40,50,60

ak
7. print(2*5,6+5,sep=”+”)
output of above code is
(i) 10,11 (ii) 10+11 (iii) 21 (iv) ”10”+”11”

Pr
8. Write the value that will be assigned to variable x.
x = 6 + 36/6 + 2*5
(i) 45.0 (ii) 17.0 (iii) 26.25 (iv) 22.0
9. Write the value that will be assigned to variable c.
rs
c = 25-5*4/2-10+5
(i) 10.0 (ii) 35.0 (iii) -2.5 (iv) -12.5
10. Write the value that will be assigned to variable a
he

a=20*5+3**2%2//5
(i) 101 (ii) 100 (iii) 1.9 (iv) None of these
11. Output of the following program is
a=”10”
ot

b=”20”
c=a+b
Br

print(c)
(i) “10”+”20” (ii) 30 (iii) ”10+20” (iv) 1020
12. Give the output
a,b=10,20
al

b,a=a,b
print(a,b)
(i) 10,20 (ii) 10,10 (iii) 20,10 (iv) 20,20
oy

13. Give the output


a=20
b=30
G

a,b=b+10,a+30
print(a,b)
(i) 30,60 (ii) 50,40 (iii) 60,50 (iv) 40,50

Score Plus Question Bank with CBSE Sample Paper and Model Test Papers in Computer Science-12 (Term I) 13
14. Give the output
a=5
b=20
a+=b
b-=a
print(a,b)

n
(i) 25,15 (ii) 25,5 (iii) 25,-5 (iv) 25,25
15. Give the output

ha
a=16
b=3
c=a/2**b
print(c)

as
(i) 512.0 (ii) 4.0 (iii) 2.0 (iv) 24.0

Answers

ak
1. (i) 2. (ii) 3. (iv) 4. (i) 5. (iii) 6. (iii) 7. (ii) 8. (iv) 9. (i) 10. (ii)
11. (iv) 12. (iii) 13. (iv) 14. (iii) 15. (iii)

5. Conditional Statement

Pr
Conditional Statement in Python performs different computations or actions depending on whether a specific Boolean
constraint evaluates to true or false. Conditional statements are handled by IF statements in Python.
(a) IF statement: In Python if statement is a statement which is used to test specified condition. The if statement
executes only when specified condition is true.
rs
The general Python syntax for a simple if statement is
if condition :
indentedStatementBlock
he

If the condition is true, then do the indented statements. If the condition is not true, then skip the indented
statements.
(b) if else statement:
The if statement accepts an expression and then executes the specified statemets If the condition is true. If the
condition is not true, then executes the specified statements indented below else.
ot

The syntax of the if...else statement is −


if expression:
Br

statement(s)
else:
statement(s)
(c) if-elif statement:
The elif statement allows to check multiple expressions for TRUE then execute a block of code as soon as one
al

of the conditions evaluates to TRUE.


syntax
if expression1:
oy

statement(s)
elif expression2:
statement(s)
G

elif expression3:
statement(s)
else:
statement(s)

14 Score Plus Question Bank with CBSE Sample Paper and Model Test Papers in Computer Science-12 (Term I)
54. Give the output
(i) Num = 6
Num = Num + 1
if Num > 5:
print(Num)
else:

n
print(Num+5)
Ans. 7

ha
(ii) N = 20
N = N + 1
if N<21:

as
print(N+10)
else:
print(N+15)
Ans. 36

ak
(iii) Age,Relatxation=24,6
ModiAge=Age-Relatxation
If ModiAge<18:
print(“Not eligible”)

Pr
else:
print(“Eligible”)
Ans. Eligible
55. The following code has some error(s). Rewrite the correct code underlining all the corrections made.
rs
(i) written=int(input(“enter written marks”))
interview=input(“enter interview marks”)
if (written<80 or interview <15)
he

print(“NotSelected”)
else:
print(“Selected”)
Ans. written=int(input(“enter written marks”))
ot

interview=int(input(“enter interview marks”))


if written<80 or interview <15:
print(“NotSelected”)
Br

else:
print(“Selected”)
(ii) marks = int(input(“enter marks”))
temperature = input(“enter temperature”)
al

if marks < 80 and temperature >= 40


print(“Not Good”)
else
oy

print(“OK”)
Ans. marks = int(input(“enter marks”))
temperature = int(input(“enter temperature”))
if marks < 80 and temperature >= 40:
G

print(“Not Good”)
else:
print(“OK”)

Score Plus Question Bank with CBSE Sample Paper and Model Test Papers in Computer Science-12 (Term I) 15
(iii) j=5
i= =j+5
if (i=j)
 print(“i and j are equal”)
else:
 print(“i and j are unequal”)

n
Ans.
j=5

ha
i=j+5
if i= =j:
print(“i and j are equal”)
else:

as
print(“i and j are unequal”)
56. Write the value of p after execution of the following code:
p=7

ak
q=2
if p>q:
p=p+2
p=p+1

Pr
Ans. 10
57. Rewrite the following code in python after removing all syntax error(s). Underline each correction done in the
code. [comptt 2020]

input(‘Enter a word’,W)
rs
if W = ‘Hello’

print(‘Ok’)
he

else:
print(‘Not Ok’)
Ans.
W=input(‘Enter a word’) //Error 1
ot

if W == ‘Hello’ : //Error 2,Error 3


print(‘Ok’)
else : //Error 4
Br

print(‘Not Ok’)
58. What is the difference between the following statements (i) and (ii)
(i) a = 5
(ii) if a = = 5:
al

x = 3
Ans. (i) variable a is being assigned the value 5
(ii) a is being checked for equality with 5
oy

OR
(i) assignment operator is used (ii) relational operator ‘= =’ is used.
59. What is the difference between statements (i) and (ii)
(i) t = 2
G

(ii) if t = = 2:
d = 3
Ans. (i) variable ‘t’ is being assigned the value 2 (ii) ‘t’ is being checked for equality with 2

16 Score Plus Question Bank with CBSE Sample Paper and Model Test Papers in Computer Science-12 (Term I)
OR
(i) assignment operator is used (ii) relational operator ‘==’ is used.

6. Loops
Loops are used to iterate over elements of a sequence, it is often used when a piece of code which you want to repeat
“n” number of time.

n
(a) For Loop

ha
A for loop is used for iterating over a sequence (i.e., is either a list, a tuple or a string).
With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.
Syntax:
for var in sequence:
statements(s)

as
else:
statements(s)
Here, var is the variable that takes the value of the item inside the sequence on each iteration.

ak
Else statement(s) will be executed when loop terminated normally(without break statement)
60. Give the output:
(i) for i in 1,2,3,4,5:

Pr
print(i)
Ans. 1
2
3
rs
4
5
(ii) for i in 1,3,10,15:
he

print(i)
Ans. 1
3
10
ot

15
(iii) for i in 1,3,10,15:
Br

print(i)
i=i+2
Ans. 1
3
10
al

15
61. Give the output
oy

(i) for i in range(1,5):


print(i)
Ans. 1
G

2
3
4

Score Plus Question Bank with CBSE Sample Paper and Model Test Papers in Computer Science-12 (Term I) 17
(ii) s=0
for i in range(1,10):
s=s+i
print(i,s,sep=”,”)
Ans. 9,45
62. Give the output

n
(i) for i in range(1,5):
print(i*i)

ha
Ans. 1
4
9
16

as
(ii) for i in range(5,25,5):
print(i*2)
Ans. 10

ak
20
30
40
(iii) for i in range(5,1,-1):

Pr
print(i*2)
Ans. 10
8
6
4
rs
(iv) for i in range(1,10,2):
print(i)
i=i+3
he

Ans. 1
3
5
7
ot

9
63. What will be displayed after the execution of the following loop?
Br

Total,End=5,15;
for Turn in range(1,End,2):
Total+=1;
print(Total)
print(Turn)
al

Ans. 12
13
oy

64. What will be displayed after the execution of the following loop?
sum,last=0,10
for C in range(1,last,2):
sum=sum+1;
G

print(sum)
print(C)
Ans. 5
9
18 Score Plus Question Bank with CBSE Sample Paper and Model Test Papers in Computer Science-12 (Term I)
65. Write the output that will be generated by the code given below:
for i in range(5,11,5):
t=i+3;
print(t)
Ans . 8

n
13

ha
66. What will be the values of variables ‘P’ and ‘Q’ after the execution of the following code?
Q=0;
for P in range(1,5,1):
Q+=P

as
Q=Q-1
print(P)
print(Q)

ak
Ans. 4
6
67. What will be the values of variables ‘m’ and ‘n’ after the execution of the following code?

Pr
n=5;
for m in range(5,10,1):
n+=m
rs
n=n-1
print(n)
print(m)
he

Ans. 35
9
68. What will be the value of A and B after execution of the following code?
ot

A = 100
for B in range(10, 13):
Br

A+=B
print(“A:”,A,”B:”,B) ;
Ans. A: 133 B: 12
69. What will be the value of num and num1 after execution of the following code?
al

num1=10
for num in range(100,102):
oy

num1=num+2
num1=num1-1
print(num)
G

print(num1)
Ans. 101
102

Score Plus Question Bank with CBSE Sample Paper and Model Test Papers in Computer Science-12 (Term I) 19
70. What will be the values of x and y after execution of the following code?
y=0;
for x in range(1,6):
y=x+1
y=y-1

n
print(x)
print(y)

ha
Ans. 5
5
71. Write the value that will be stored in variable sum after execution of following code?

as
sum=0
for i in range(9,5,-1):
if(i%3==0):
sum = sum + i

ak
else:
sum = sum - i
print(i,sum)

Pr
Ans. 6 0
72. What will be displayed after the following code is executed?
n=3;
p=1;
rs
if n < 0:
print(“Not Valid”)
else:
he

for i in range(1,n+1):
p = p* i;
print(n+p)
ot

Ans. 9
73. Write the value of C after executing the following code
R= 8;
Br

C = 0;
for P in range(1,R,3):
oddNum = P %2;
if oddNum == 1:
al

C= C+1
print(C)
oy

Ans. 2
(b) While loop
While loop repeats the sequence of actions many times until some condition evaluates to False
G

while condition:
block of statement(s)
The else part is executed if the condition in the while loop evaluates to False.

20 Score Plus Question Bank with CBSE Sample Paper and Model Test Papers in Computer Science-12 (Term I)
74. How many times will the following loop execute and give the output
z=7
sum=0;
while z<=12:
sum=sum+z

n
z=z+2
print(z)

ha
Ans. 3 times
11
75. How many times does the following while loop get executed?

as
K=5
L=36
while K<=L:
K+=6

ak
Ans. 6
76. How many times will the loop execute?
value1,value2=7,19;

Pr
while value1<=value2:
value1=value1+2
value2=value2-2
Ans. 4
rs
77. How many times will the following WHILE loop execute and what will be the value of sum after execution of
the following loop?
y,sum=7,0
he

while y<=15:
sum = sum + y
y = y+2
ot

print(sum)
Ans. loop will be executed 5 times and value of sum is 55
78. What will be the values of variables agg after execution of the following loops?
Br

a=9
agg=9
while(a>10):
agg+=a
al

a-=2
Ans. agg=9,
oy

79. What will be the values of variables sum after execution of the following loop?
v=6
sum=0
while v>3:
G

sum+=v;
v-=2
Ans. 10

Score Plus Question Bank with CBSE Sample Paper and Model Test Papers in Computer Science-12 (Term I) 21
80. What will be displayed after the execution of the following code?
G=1
while G<=5:
print(G+2)
G=G+1

n
Ans. 3
4

ha
5
6
7

as
81. What will be displayed after the execution of the following code?
z=4
while z<=8:

ak
print(z+3)
z=z+2
Ans. 7

Pr
9
11
82. Write the output that will be generated by the code given below:
i=7
rs
r=8
while i<=10:
print(r*i)
he

i=i+2
Ans. 56
72
ot

83. Observe the following code carefully and find which statement will never get executed in the code:
counter=1 #statement1
Br

while counter<=15: #statement2


if counter<15: #statement3
print(“Jump”) #statement4
else: #statement5
al

print(“Stop”) #statement6
counter+=4 #statement7
Ans. statement 6
oy

84. Find and write the output of the following python code: 1[SP 2019-20]
x = “abcdef”
i = “a”
G

while i in x:
print(i, end = “ “)
Ans. aaaaaa----- OR infinite loop

22 Score Plus Question Bank with CBSE Sample Paper and Model Test Papers in Computer Science-12 (Term I)
85. Write the value that will be stored in variable p after execution of following code. How many times will the
loop execute?
y,x,p = 3,5,0
while y > 0:
p = p + x

n
y = y-1
Ans. p=15

ha
3 times
86. What will be displayed after the following code is executed? Also write how many times will the loop execute.
a = 5;

as
b = 2;
while (b != 0):
r = a%b
a = b

ak
b = r
print(a)
Ans. 1

Pr
2 times
87. Write the value that will be stored in variable num and sum after execution of following code:
sum=0
num = -2
rs
while num < 1 :
sum = sum + num
he

num=num+1
Ans. num = 1 sum = -3
88. What will be displayed after the following code is executed?
ndigits = 0
ot

N = 35
while (N > 12):
Br

ndigits = ndigits + 1
N = N-10;
print(ndigits)
print(N)
al

Ans. 3
5
oy

89. What will be the final value of variable x after the following code is executed? [SP 18]
x=10
while x>1:
x=x//3
G

x=x+1
print(x)
Ans. 1

Score Plus Question Bank with CBSE Sample Paper and Model Test Papers in Computer Science-12 (Term I) 23
90. What will be the output displayed, when the following code is executed? [2018]
a=5
b = 2
while a < 20:
a = a + b

n
b = a - b
print(a,end=” “)

ha
Ans. 7 12 19 31
91. What will be the values of i and z after the following code is executed? [2018]
i = 0

as
z = 10
while i<10:
i=i+2
z=z-1

ak
print(i,z)
Ans. 10, 5
Theory

Pr
92. What do you understand by the term Iteration? [SP 2019-20]
Ans. Repeatation of statement/s finite number of times is known as Iteration.
93. Differentiate between break and continue statement with the help of an example. 2 [SP 18]
Ans. Break statement is used to terminate the execution of the loop, for example:
rs
for i in range(6):
if i = = 3:
he

break
print i
The output of the above code will be:
0
ot

1
2
Br

The loop terminates when i becomes 3 due to break statement whereas, continue statement is used to force the
next iteration while skipping the statements in the present iteration.
for i in range(6):
if i ==3:
continue
al

print i
The output of the above code will be
oy

0
1
2
G

4
5
continue statement forces next iteration when i becomes 3 , bypassing the print statement .Thus ,in the output 3
is missing.
24 Score Plus Question Bank with CBSE Sample Paper and Model Test Papers in Computer Science-12 (Term I)

You might also like