C.S CH 2 &3 Preeti Arora Exercises PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

AND,

operators
Gates use the binary
Logic

OBJECTIVE TYPE QUESTIONS


blanks.
significant bit.
1. Fill in the bit is called the..
the left-most

(a) In binary number system, of a number.


. .

known as
number is also
. . .

(b) Base of a
of. .
.. symbols.
system is composed
(c) The decimal code which is used in computers to
the most widely used alphanumeric
code is
(d)
. .

(letters, numbers and symbols).


translate text
universal coding standard adopted by all new...
(e) Unicode is a new
and the operation is
(f) The . . . operation is Boolean multiplication
Boolean addition.
A statement is said to be a. if it has a definite value, which is either True or False.
(g)
(h) . . gate has only one input and it complements an input signal.
(i) The values which are stored in binary variables are known as . .

G) A . .. is a representation of a Boolean function or expression containing all possible


combinations of input values and their result in a tabular format.
the folowing statements are True or False.
,State whether
has only two symbols, 0 and 1.
Binary data representation
lal
UNICODE are the three internal storage representations.
hl ASCIl, ISCII and
number system.
representation of an octal
c) (128), is the
are the same in both the types of ASCI, i.e., ASCI1-7 and ASCI-8,
dl The first 128 characters
ASCI-8.
be represented in
512 characters can
(e) multilingual plain text.
a consistent way of encoding
(f) Unicode provides
Hexadecimal number.
ABC is a valid
(g) tractional part nas digits raised to both positive and negative
integer and a
I A number with both
number system.
of 2 in a decimal
powers system.
Decimal number
to 90 in
is equivalent 7-is called octal number system.
(i) (52),, uses eight digits-0,
1, 2, 3, 4, 5, 6, and
an

system that
() A number
universal gates.
NAND and
NOR are called
(K)
Choice Questions
(MCQs):
3. Multiple
information?
What is
(a)
(i) Processed data

Collection of programs
(i) instructions
Collection of
(ii)
None of these
(iv)
number system? (iv) 16
What is the base
of binary
(b) (ii) 2
(i) 8 in) 10
number system? (iv) 16
of decimal
(c) What is the base (ii) 8
(i) 10
() 2
octal number system? (iv) 16
(d) What is the base of (ii) 8
(ii) 10
() 2
number system:
(e) (43), into binary
Convert
(ii) (110101),
(iv) (110010),
(i) (101011),
(ii) (110011),
number system:
into decimal (iv) 22
(f) Convert (10010), (ii) 20
(i) 28 (i) 18
form of ISCII?
)What is the full Interchange
Code for Information
i)International Standard
for Information Interchange
(i) Indian Standard Code
Information Interchange
International Script Code for
(ii)
(iv) None of these
of?
Hexadecimal number system is composed (iv) 16 symbols
(h) (ii) 8 symbols
(i) 10 symbols
(i) 2 symbols
number?
Which of the following is not a binary (iv) 000
0) (ii) 11E
(i) 101
(i) 1111
and letters as symbols?
numbers (iv) Hexadecimal
Which numbering system uses (ii) Octal
) Decimal (i) Binary

(k) ldentify the logical statement: coffee.


(ii) I want to have
0) 15+4=19 I love dogs.
(i) Should I wear the mask or not
(iv)

2.35
SOLVED QUESTIONS=
1.
What do you understand by number
system?
Ans. Number system is the technique to represent numbers in the computer system architecture. Who
data is fed into the Computer, it is converted into numeric format by ASCIl (American standard Coda
hen t
Information Interchang where each type of data is represented by its numerical equivalent. The v
of each digit in such number is determined
value
a by three things:
The digit itself
The position of the digit in the number
The base or radix of the number
Examples of number systems are: Binary, octal, decimal and hexadecimal number systems.

2. Convert
(38.625),,
into its binary equivalent.
Ans. First convert 38 into a binary number, which is:

38 Remainder
Top
19 0
9

2
1
Bottom
Hence, (38),, = (100110),

Now, convert 0.625 into binary fraction, which is:

Integer Fraction

0.625x 2 1.250 1 0.250

0.250 x 2 0.500 0.500


0.500x 2 1.000 1 0.000
Therefore, (0.625)0 (0.101),
Hence, (38.625) (100110.101),
3. Convert (1101), into decimal number.
Ans. Binary number is 1101.
So, 1101 (1 x 2) + (1 x 2)+ (0 x 2) + (1 x 2)
=
(1 x 8) + (1 x 4) + (0 x 2) + (1 x 1)
= 8+4+0 +1

Answer is: (13)0


4. Convert
(EF.B1),, (?)h =

Ans. =E F. B1
= Ex 16'+ Fx 16°. Bx 16+1 x 162
= 14 x 16 +15 x 1.11 x (1/16) +1 x (1/256)
224+15.(0.6875)+ (0 .00390625)
239 +0.69140625
= 239.69140625

Answer is: (239.69140625)


into decimal.
5. Convert
(2C9),,
16'+12 x 16'+9x16
2
Ans.
16+9x1
2x256 +12
x

512+192+9

(713)0
Answer is: (713),
into hexadecimal.
6. Convert (423),,
Ans. Remainder A Top
423
16
16 26

16 1 10-A
0 1 Bottom
remainders moving from MSB to LSB. We have (1A7)..
Now, write the
Answer is: (1A7),
conversion:
7. Do the following
() (ABCD), to (?),
Ans. (1010101111001101),
(ii) (3567), to (?),

Ans. (6757,
8. Draw a truth table and logic circuit diagram of NAND gate.

Ans.
B (AB

0 1
0 1

11 o
A

B-
(AB)
9. Give the dual of (A.1)+(B.(C+0)).
Ans. (A+0).(B+(C.1))
10. The following is
message encoded in ASCII code. What is the
a
message?
1001000 1000101 1001100 1010000
Ans. Convert each 7-bit code
into its hexadecimal equivalent. The result is:
48 45 4C 50
Now locate these values in ASCIl code table and determine the character
HELP represented by each. The result is:

11. What is Unicode? How is it


useful?
Ans. Unicode a new universal coding standard
adopted by all new
Onsortium which is a non-profit organization. Unicode provides platforms.
It is promoted by Unicode
a unique number for
every character
espective of the
platform, program and the language. It is a character coding
e
Worldwide system designed to support
interchange, processing and display of the written texts of diverse
languages

Data ni
numbers.
12. Explain octal and hexadecimal
choice for representing digital circuit numbers in a form that is more
is r
Ans. Octal (base 8) was previously popular
a
numbers are represented as:
is sometimes abbreviated as oct. Octal
compact than binary. Octal
14, 15, 16, 17, 20, 21 and so on.
0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13,
currently the most popular choice for representing digital numbers in circuit
Hexadecimal (base 16) is a

Hexadecimal numbers are sometimes represented by precedino


form that is more compact than binary.
sometimes abbreviated as hex. Hexadecimal ic
Hexadecimal is
the value with '0x', as in Ox1B84.
represented as:
and
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F,
so on.

13. Perform the following conversions:


() (514), = (?)0

(i) (220), = (?),

(ii) (76F),= (?)0


(iv) (4D9), = (?)0

(v) (11001010), (?) =

(vi) (1010111), =(?),

Ans. () (514), (332),


=

(ii) (220), = (10010000),

(ii) (76F),s = (1903),

(iv) (4D9) =(1241),


(v) (11001010), = (202),
(vi) (1010111), = (87),0

14. Express the following decimal numbers into hexadecimal numbers.


(i) 548 (i) 4052
(ii) 58 (iv) 100.25
(i) (4052),. =(FD4),
Ans. (i) (548), (224)6
=

(ii) (58),, = (3A)16 (iv) (100.25), = (64.4),

15. Express the following hexadecimal numbers into equivalent decimal numbers:
(i) 4A2 (i) 9E1A
(ii) 6BD (iv) 6C.34
(i) (i) (9E1A), = (40474)0
Ans. (442),, (1186),0
=

(ii) (6BD), = (1725),o (iv) (6C.34) = (108.203125)n

16. What is the advantage of preparing digital content in Indian language using UNICODE font?
Ans. The most important advantage of UNICODE is that we need not add/install these fonts in our system.
These are rendered automatically in the system.
17. Fill the blanks provided:

Decimal Binary Octal


0 000
001

011 3

100
101 5

110 6
Sinary Octal

vanables
Sviean
8Nhatare
0 yeratie s aiss known as binary variable or logical variabile that takes its values lrom the Boolean
take dny one bnary valued quantity out of the two possible values, le,
ra Sooean vanable can
true tase. One bit repr sents one Boolean variable. In electrical circuit, it the signal passe
yes m
10
reoresents i ese tsrepresented through 0
statements?
1 What are Sooiean
Statement it it has d definite value which is either false or true
Astatement is said to be a Booesnylogica

20 What s truth table


truth tabie is a representation of a Boolean function or expression containing all possible combinations
1S A
their resuits in 3 tabular format.
of input values and

21. What are logic gates?


output. Logic Gates use the binary
Ans. A Gate is a cireuit which takes
one or more inputs and generates an

operators AND. OR and NOT

22 Veriiy the following using truth table:


X-Y2=(X-Y).(X-2)
Truth Table
Ans.X-YZ=(X-Y).(X-Z)
zY.z X+Y.Z(X+Y)(X+Z)(X*Y).(X+Z)

1
Ihe values for
highlighted columns are the same. Hence, L.H.S. =
R.H.S.
23. Verify the following using truth table:
a) X.X=0
(b) X*1=1
Ans.
a) X.X=0 (b) X+1=1

X
X.X' X+1
0

Data Rans
24. (a) Convert (122), into binary number
Ans.
2 122 Rernainder
Top
2 61
2 30
15
2
2
2
0
Bottom
Therefore, (122) (11110101,
into octal number.
(b) Convert (122)
Ans.
Remainder
8122 Top
815
81
0 Bottom

(122, (172),
=

Therefore,
number.
hexadecimal

C) Convert (122), into


Ans.
16 122 Remainder Top
10-A
16 7
0 Bottom

Therefore,(122),,-17A) Boolean expression


the following
corresponding to
circuit diagram
25. Draw a
(A +B)(B+C)

Ans.

A
B (4+8)(6C)

(CBSE D2014)
below:
for the logic circuit shown
26. Obtain the Boolean expression

Ans. F-A'.B+(C+D'
27. Write the equivalent Boolean expression for the following Logic Circuit: (CBSE D 2011)

FP,aR)= P.Q'+ P.R


Ans.
to?
is the following circuit equivalent
28. Which gate

D O-Output

(c) NAND (d) NOR


(b) OR
(a) AND

None of these
(e)
Ans. (d) NOR
Boolean expression:
that can implement the following
29. ldentify the logic gates (ii) PQR+QR
(ii) X+Y.Z'
) A.B
A.B It requires AND gate.
(i)
-

Ans.
X+Y.Z'- It requires OR, AND and NOT gates.
(i) AND and NOT gates.
PQR+QR I t requires
OR,
(ii)
UNSOLVED QUESTIONS=
number.
number into binary (d) 62.325
1. Convert decimal (c) 25.80
(b) 44
(a) 52
number.
number into octal (d) 132
2. Convert decimal (c) 61
(b) 540
(a 911
hexadecimal number.
number into
3. Convert decimal (c) 206
(d) 752
(b) 3619
a) 132
number.
binary number into decimal (d) 101101
4. Convert (c) 101010.011
(b) 111101
a) 10111
decimal number.
5. Convert octal number into (c) 142
(d) 205
(b) 321
(a) 75
number.
into decimal
6. Convert hexadecimal number (d) 132
(c) 271
(b) 138
(a) A2 encoding
scheme?
different from UTF-32
scheme. How is it
Discuss UTF-8 encoding
8. What are Boolean constants?
NOT operators.
Boolean operators? Explain AND, OR,
S.Whatare
system in computers
0. What is the use of Hexadecimal number

2.
11. Verify the following using truth table:
(i) a.(a-b) =a
(ii) X.(Y Z)
=X.Y-X.z
2.Obtain the Boolean expression for the logic circuit shown belowe

13. State De Morgan's law and prove it with a truth table


14. What is the advantage of preparing a digital content in Indian language using UNICODE font?
15. Encode the word 'COMPUTER using ASCIl and convert the encoded value into binary values

16. Expand ASCIl and ISCI


17. Draw a logic circuit of the following Boolean expression:
(i) (A.B)-(C-D-(8.D)
(i) (A+8)/8c-D)
18. Write binary equivalent of the following octal numbers:
(a) 2306 (b) 5610 (c) 742 (d) 65.203
19. Write binary representation of the following hexadecimal numbers:
(a) 4026 (b) BCA1 (c) 98E (d) 132.45

20. Perform the following conversions from decimal number to other number systems:
(a) (54) (?), (b) (120), (P) =
()(76),, =
(P) d) (889), =
()
(e) (789), (?) () (108), (P)
21. Express the following binary numbers into their equivalent octal numbers:
a) 100101111 (b) 111011010 (c) 1010011 (d) 10011101
22. Express the following binary numbers into their equivalent hexadecimal numbers:
(a) 1111101101100011 (b) 100000101011100
(c) 1000111010100011 (d) 111011111
23. Express the following octal numbers into their equivalent decimal numbers.
(a) 145 (b) 6760 (c) 455 (d) 10.75
24. The hexadecimal number system uses 16 literals (0-9, A- F). Write down its base value.
25. Explore and list the steps required to type in an Indian language using UNICODE.
26. Convert the following base of number system:
(a) (1010100) (b) (3674), (.=

(c) (266) (. (d) (9F2),=


27. Write the name of following law and verify using truth table:
a(b+c) = ab + ac

28. Draw the logic circuit for the following Boolean expression:
(X+Y).Z +WN

2.42
Computer Science with Python-XI
OBJECTIVE TYPE QUESTIONS=
1. Fill in the blanks.
(a) The smallest individual unit in a program is known as a . . .

programming language is known as


having special meaning reserved by a . . .

(b) A word

(c) A literal is a by quotes.


sequence of characters surrounded
tokens that trigger same computation/action when applied to variables.
(d) are

(e A. ... is a reserved named location that stores values.

(6) To determine the type of an object we use.


.. ... function.
(g) The input() always returns a value of. . . type.
(h) Blocks of codes are represented through
(i) In. typing, a variable can hold values of different types at different times.
G) In Python, the floating point numbers have precision of digits
. . .

(k) Operators that act upon two operands are referred to as operators.

4.40 Computer Science with Python-X


******* . . truncates fractional remainders and gives only the whole part as the result.
rror does not stop execution but the program behaves incorrectly and produces
A .
undesired/wrong output.

ins to access a variable or file that doesn't exist is a kind of....


******* CTTO.

State whether the e following statements are True or False.

aPthon supports
Unicode coding standard.
identifier must be a keyword of Python.
(b) An
variable initialization is string literal,
c) Defaut
is a valid statement.
(d) 3C=A
iel The input() always returns a value of an integer type.
T h e print() without any value or name or expression prints a blank line.
Boolean type is a sub-type of integer.
(g)In Python,
in Python.
(n)AISSCE-2020 is a valid string
0) InPython, Integer data type has a fractional part.
means "there's nothing here".
(0) Nuil literal in Python
(k) Python supports multiple assignment to multiple variables.
is used to determine the data type of variable.
) id() function a

3. Multiple Choice Questions (MCQs):


(a) Which of the following are the fundamental building blocks of a Python program?
(i) ldentifiers (i) Constants (ii) Punctuators (iv) Tokens

(b) ldentifier name cannot be composed of special characters other than . **'****

(i) # i) Hyphen (-) (i) $ (iv) Underscore ()


(c) Python takes *** *** ***
indented spaces after the function declaration statement by default.
i) 5 (ii) 6 (ii) 4 (iv) 3

(d) Single line comments in Python begin with . . *****S y m b o l .

(i) # (ii) (iii) (iv) %


(e) Which of the following does not represent a complex number ?
(ii) K = 2 + 3i (iv) K = 4 +3j
(i) K= 2 + 3 (i) K=complex(2,3)
() What is the order of precedence of arithmetic operators given below in Python?
1. Parenthesis 2. Exponential 3. Multiplication
4. Division 5. Addition 6. Subtraction
i) 1, 2, 3, 4, 5, 6 (ii) 2, 3, 4, 5, 6, 1 (ii) 1, 3, 2, 6, 4, 5 (iv) 4, 6, 5, 2, 3, 1
g)What will be the output ofthe following snippet?
% Y 2, 6
Y Y, x + 2
print (X, y)
i) 66 (ii) 4 4 (ii) 4 6 (iv) 6 4
Which of the following is not in Python character set.
i) Letters: A - Z or a -z (i) Digits: 0-9
(11) Whitespaces: blank space, tab, etc. (iv) Images: Vector
Each statement in Python is terminated by..***""***'"'** "'' '
(i) Semicolon (;) (ii) Colon () (ii) Comma () (iv) None of these
The extension of a Python file is given as
(i) .pt (ii) pwy (ii) py or .pyw (iv) yppy
E
reserved words used by Python interpreter to recognize the structure of a program are termed as
**** * *********
i) ldentifiers (ii) Literals
ii) Tokens (iv) Keywords
ython Programming Fundamentals
4.41
)What can be the maximum possible length of an identifier?
(i) 31 (i) 63 (ii) 79 (iv) Can be of
(m) Which of the following operators is floor division? anyl
() +
(i) (ii) // iv)
(n) Which of the following is an invalid statement?
() a=b=c=2 (ii) a,b, c=10,20, 30

(ii) a b c 20, 30, 40 (iv) a_b_c=20

SOLVED QUESTIONS=
1. What are the different components of a variable?
Ans. A variable has three main components:
(a) ldentity (memory address) of the variable.
(6) Type (data type) of the variable.
(c) Value of the variable.
2. Consider the following statements in Python interpreter and describe the output/statement required

(a) Print a message "Hello World"


(b) a = 10
b = 12
=
a + b
print (c)
(c) To retrieve the data type ofthe inputted string "Hello" stored inside the variable'a'.
(d) To describe the data type of variable 'b'.
(e) To retrieve the address of variables a and b.
(f) State the output:
>>>b 12
>> d = b

>>> d
>>b
>>> id (d)
>>> id (b)
(B)>>> a = "Hello"
>>> a * 10

(h) To display the value for a?, a and a


(i)>>> a = 15
>>> b = 4

>>>a/b
>>a//b
(g) To swap the values of two variables, a and b, using multiple assignment statements.
(k)> a, b, c,d=10,27, 4+5, 10-7
>>> al/d
>>>d**d
>>>b/c
>> a+b+c-d
Ans. (a) >>> print ("Hello World")
Hello World
(b) >>> a = 10
>>>b = 12

>> c = a + b

>>> print (c)


22
"|lT|"
(
y (a)

(d) y(D)

(c) id(a)
2193248

iI
(D)
1616896960

() d b
> d
12
>>>b
12
>id (d)
1616896960

>>> id (b)
1616896960
(g)>>>a - "Hello"

>>>a*10
'HelloHelloHelloHelloHelloHelloHelloHelloHelloHello'
(h)>>> a - 10

> > a**2

100
>> a**3

1000
>>> a* *4
10000

(i)>>> a =15
>>>b =4
>>> a/b
3.75
>>> al/b
3
) >>>a, b = 10, 12
>>> a, b = b, a
>>> a
12
>>>b
10

K>>>a,b, c, d=10, 27, 4+5, 10-7


>>>al/d
3
>>>d* *d
27
b/c
3.0
atbtc-d
43
3. Which of the following identifier names are invalid 3 d

i)1st_Room
i) Serial_no
(iv) Total Marks
(ii) Hundreds
(vi) total-Marks

(v) Total_Marks
(vii) True
(vi) Percentage
Ans. Following identifier names are invalid:

Reason symbo
S.No. Invalid ldentifier dentifier
cannot
contain
speia
number
(6) Serialno. cannot begin with
name
dentifier
symbol
special
(i) 1st_Room ldentifier
cannot
contain

(i) HundredS cannot


contain space
ldentiffer
Total Mariks special symbo!
(iv) ldentifier
cannot
contain

(vi) total-Marks cannot be a keyword


Identifier

(vii) True
variable?
and a
difference between
a keyword to the
Pytnon interpreter. Kevwor
4. What is the purpose
and meaning have been detne
they
that has a specific ror which
special word
purpose
Keyword is a only for the
Ans.
used in ourprogram
can be
r e s e r v e d words that
else, def, true,
false, etc. reserved. These are defner
elif, not fixed/
For example, for, if, Variables a r e
user-defined name given
toa value.
underscore.
Iney m u s t besin
with eithera l g
Variable is the the symbol
have letters, digits and
the user but they can
name, result_1, etc.
or underscore.
For example, _age,

How is it useful? that pertorms a specific tas


5. What is a function? statements
within a program
named block of
Ans. A function
in Python is a within it, namely greet_msg().
has a function
following program
For example, the
progl.py
der greet_msg):
p r i n t" H e l l o there!!")

")
rane:
name i n p u t ("YOur
print(name, greet_sT()) s t a t e m e n t s . So, for the
through their function call
reused anywhere
useful as they can be through functions it can
Functions are time it is needed; rather,
need not rewrite the code every
same functionality, one
be used again and again.

6. Find the error.


def minus (total, decremen )
decrement
total
-

=
Output
return output header line
need to add colon (:) at the end of the
colon missing at the end. So, we
Ans. The function header has
Thus, the corrected code will be
decrement)
def minus (total,
=
total -

decrement
output
return output

and immutable objects


7. Differentiate between mutable ed
Ans. Mutable object: Objects whose
values can be changed after they are created and assigned values are cdi
mutable For example, list, dictionary, etc.
objects.
created and assigned value
Immutableobject: Objects whose values cannot be changed after they are
called immutable objects. For example, int, float, string, tuple, etc.

4.44 Omputer Science with Python-


a Ritu is confused between 3*2 and 3**2. Help her to kno the difference between the tuo Espresons
The statement 32 multiplies 3 by 2 and produces the resuit 6 while 3**2 calculates 3 ra sedt
Ans.
power 2 and produces the result 9.
9. How many types of strings are supported in Python?

Python allows two string types:


Ans.
(1) Single line Strings-Strings that are terminated in a single line enclosed within single and cout e tue
(2) Multiline Strings-Strings storing multiple lines of text enciosed within three single or doubie cucts

10. Differentiate between explicit and implicit type conversion.

Ans. An implicit type conversion is a conversion performed by the compiler without prograrmmer nterve

An implicit conversion is applied generally whenever multiple data types are intermived in an expres

(mixed mode expression), so as not to lose inforrnation.


An explicit type conversion is user-defined conversion that forces an expression to a specific type. Tre
explicit type conversion is also known as Type Casting.

11. What are variable-naming conventions?

Ans. () A variable must start with a letter or underscore followed by any number of digits and/or letter
(ii) No reserved word or standard should be used as the variable name.

(ii) No special character (other than underscore) should be included in the variable name.

(iv) Case sensitivity should be taken care of.


12. What is None in Python?
Ans. Python has one special data type caled None. The None type is used to indicate something that has not yet

been created. It is a non-nuil, unidentified value.

13. Identify the types of the following literals:


23.789, 23789, True, {4: "four", 5: "five"}, True', "True", (1,2,3), False, "Faise", None, [100,200, 300]
Ans. 23.789 Floating point
23789 integer
True Boolean
4: four", 5: "five") Mapping (Dictionary)
True' String
"True" String
(1,2,3) Sequence (Tuple)
False Boolean
"False" String
None None
[100,200,300] Sequence (List)
14. Write the corresponding Python statements:
(0 Assign value 100 to variable A and value 200 to variable B.

(i) Assign value 10 to variables a, b, and c.


(n) Assign value 20 to x and delete 5 from x and assign the variable x to variable y.

Ans. (i) A=100


B=200
(i) a=b=c=10
(ii) x=20
x=x-5
Y=X

Python Programming Fundamentals 4.45


15. Find the output generated by the following code:

(1) x2 (2) x=8


3 y-2
xty +=y
print (x, y) y-=X

(Ans. 5 3) print (x,, y)


(Ans. 10 -8)

(3) a-5 (4) p-10


b-10 q-20
atatb p=q//3
b-atb t=ptq* *2
print (a,b) print (p, g)
(Ans. 20 300) (Ans. 60 480)

(5) p-5/2 (6) p-2/5


p*4 p*4
r=ptq r=pq
pt ptqtr pt=ptg-r
rt=ptqtr r*=p-g+r
-ptq*r qt=ptg
print (p,q, r) print (P, q, r)
(Ans. 27.5 -642.5 62.5) (Ans. 1.759999999999999 96 0.512)
16. What is the difference between an expression and a statement in Python?
Ans.
S. No.
Expression Statement
1. An expression is a combination of symbols, Statement is defined as any programming
operators and operands. instruction given in Python
per the syntax
as
2. An expression represents some value. Statement is given to execute a task.
3. The outcome of an expression is always a value. Statement may or may not return a value as
the result.
4. For example, 3* 7+ 10 is an expression. For example, print("Hello") is a statement.
17. Identify the error in the following Python statement:
>>>
print ("My name is", first_name)
Write the corrected statement also.
Ans. The above statement is trying to print the value of an undefined variable first_name. The correction is made
by defining the variable name before using it, i.e.,
>>first_name = 'Rinku'
>>>
print("My name is", first name)
18. The following code is not giving the desired
output. We want to input the value as 15 and ooto
output as 30. Could you pinpoint the problem?
num input ("Enter Number: ")
r e s u l t = num * 2

Print (result)
Ans. The problem is with input() function as it returns value as a 5 it will
string, so when we input the value 13
assign to num variable as string '15' and not as integer 15. So, when num variable is multiplied the
output is '1515' and not 30. To solve this problem, int() function is to be used while accepting tne Dy
Also, Print is not a legal statement of Python; it should be print. The corrected code is:
num =
int (input ("Enter Number: "))
result = num *

print (result)
Write a program to obtain temperature in Celsius and convert it into Fahrenheit using the formula:
19.
F C*9/5+32

#Celsius to Fahrenheit
Ans.
e l s = float (input ("Enter temp in Celsius : "))
cel

emperature
p r i n t ("Ten in Celsius is : ", cels)
*
9/5 + 32
f =cels
print ("Temperature in Fahrenheit is : ", £)
20, What will be the output produced by the following code?
name = "Neeru

age= 21
print (name, "you are", 21, "now but", )
print ("you will be", age + 1, "next year")

Ans. Output should be:


Neeru, You are 21 now but
you will be 22 next year

21. What will be the output of the following code? Explain.


X, y
=
2, 6
X, Y
=

Y, x + 2

print (x, y)
Ans. 6 4
Explanation: The first line of code assigns values 2 and 6 to variables x and y respectively.
The next line (second line) of code first evaluates the right-hand side, ie., y, x + 2, which is 6, 2+2, ie., 6,4;
And then assigns this to x, y, i.e., x, y = 6, 4; so x gets 6 and y gets 4.

The third line prints x and y so the output is 64.


the of item. Cost price and profit is to be
22. Write a Python code to calculate and display selling price an

accepted by user.
Ans. c o s t P r i c e = i n t (input ( ' E n t e r c o s t p r i c e : '))
profit = i n t (input ( ' E n t e r p r o f i t : '))
sellingPrice = costPrice + profit

print ('Selling Price: ', sellingPrice)

Output:
Enter cost price: 50
Enter profit : 12
Selling Price: 62
23. What output will be produced by the following code:
A, B, C, D = 9.2, 2.0, 4, 21

print (A/4)
print (A//4)
print (B**C)
print (A%C)
Ans. 2.3
2.0
16.0
1.1999999999999993
4 Write the Python code to convert the time given in minutes into hours and minutes.

Inin=int (input ("Enter time in minutes ") )


h=min// 60
m=min&60
print ("Hours=", h)
print ("Minutes=", m)
25. Evaluate (to true or false) each of the following expressions:
() 14= 14 (i) 14< 14
iv) -15 >=15
ii)-14>-15
Ans. i) True (i) False
(iv) False
(ii) True in Python and evaluaaluate the expre
statements

26. Write to the following


already having meaningful
valueci.on
logical expressions correspor are
middle, last
(assuming variables num1, num2, num3, first,
a) The of 20 and -10 is less than 12.
sum

(b) num3 is not more than 24. num..


num1 and the string last'.
(C) 6.75 is between the values of integers smaller than
first' and
the string
'middle' is larger than
n e string
(e) List Stationery is empty.
Ans. Expressions:
(a) 20+(-10)< 12
24)
(6) not (num3>
and (6.75 num2)
(c)(6.75> numl)
> first) and (middle < last) ==
0
(d)(middle
(stationery)
not leap years unless thevaare
len
stationery
==
[ ] OR by 100 are
(e) divisible
that years is a leap year ornat
divisible by 4, except and print whether it not.
27. A year is a leap year if it is that asks the
user for a year

by 400. Write a program wish: "))


also divisible Year Number you
E n t e r the
= i n t (input ("Please ! 0 :
Ans. y e a r and years100
year$4
==
0
==
0 or
i f year$400
Year")
"is a Leap
print (year,

else Year")
"is Not a Leap Calculate and
print (year, pencil from the
user.
and quantity of
program
that accepts cost price
28. Write a Python

display total price.


("Enter c o s t price of pencil : '))
Ans. costprice
=
int (input
"Enter quantity: '))
gty
=

int (input (
totalprice=costprice* qty
is: ", totalprice)
print ("Total price and angle store the length
of the
wall. Let variables length
29. Assume that a ladder is put
upright against a program to
as it leans against
the wall. Write a Python
forms with the ground
ladder and the angle that it values of length and angle:
reached by the ladder on the wall for the following
compute the height
(b) 20feet and 0 degree
a) 16 feet and 75 degrees
(c) 24 feet and 45 degrees (d) 24feet and 80 degrees

Ans. import math


length =
int (input ("Enter length of ladder: "))
the

int (input ("Enter the angle in degree:


"))
angind =

anginr math.radians (angind)


sin = math. sin (anginr)

height round (length sin, 2)


*
=

print ("The height reached by ladder ", height) e


user
to
their name and age. Print a message addressed
30. Write a program that asks the user to enter
that tells the user the year in which they will turn 100 years old.
Ans. nm = input ("Enter Your Name")

age = int (input ("Enter Your age") )


yr =
(100 age) #Taking 2022 as Current Year
2022 +

print (nm, ":You will turn 100 in", yr)


ntiate between Syntax and Run-time errors. Give
3 yntax Error:Violation
Violat
an
of formal rules of a programming language such as
appropriate example.
elling a keyword or incorrect
A n s .

indentati etc., results in syntax error.

Forexample,

=5
len( ' h e l l o ' )
"<stdin>": line 1
File
ntaxError : can't ass1gn to function call
Run-time Error: These errors are generated during a program executi due to resource limitation such as
ivision by zero, trying to access an identifier or file which doesn't exists, etc. Python has provision of
hk
cheo Doints to handle these errors known as Exception Handling.
For example,
a=10

b=0
ca/b

Would result in
7eroDivisionError: division by zero
29 Fxolain the concept of R-value and L-value with example.
Anc. In a normal expression, the variable to the left side of an assignment operator refers to L-value (left-value)
refers to
because it resides in memory and is addressable, and an expression after the assignment operator
R-value (right-value). It does not represent an object occupying some identifiable location in memory.
For example, X=1 #X is an L-value
Y=20 #Y is an L-value
Z=X+Y #X+Y is an R-value
33. Write the output of the following:

(a) numl =
4
num2= numl +1
num1 = 2 print (numl, num2)

num2 =
2, 6
(b) numl,
numl, num2 =
num2, numl + 2

print (numl, num2)


(c) numl, num2 = 2, 3
num2 numl, num3 + 1
num3,
=

print (numl, num2, num3)

Ans. (a) 2, 5
(b) 6, 4
'num3' is not defined
c) NameError: name

numbers using a third variable.


34. Write a program to swap two
Ans. a int (input ("Enter First Number") )|
Number"))
b int (input ("Enter Second
=

are")
print ("Be fore Swapping Values
print ("Val ue of First Variable
a is", a)
b is", b)
print ("Value of Second Variable
C a
a b

b C
print ("After Swapping Values are")
Variable a is", a)
Print ("Value of First
Variable b is", b)
print ("Value of Second
35. Write third
a program to swap two numbers without using a
variabie
Ans.
int (input ("Enter First Number ")
D int (input (" Enter Second Number"))

print( Before Suapping Values are")


print (""Value of First Variable a is", à

print("yalue of Second Variable b is", D)

print ("After Suapping Values are ")


print("Value of First Variable a
is" a)

print("Value of Second Variable b is", b)

OR
int (input ("Enter First Number"))
b = int (input ("Enter Second Nurmber"))
print ("Before Swapping Values are")
print ("Value of First Variable a is", a)

print ("Value of Second Variable b is", b)


a, b = b, a
print ("After Swapping Values are")
print ("Value of First Variable a is", a)
b is", b)
print ("Value of Second Variable
36. Give the output for the following expressions:
(3<18) and not 8<18
i) (5<10) and (10<5) or

(i) A, B, C=9, 12, 3


X=A-B/3+C*2-1
Y=A-B/ (3+C) * (2-1)

Z-A- (B/ (3+C) *2) -1

print ("X=", X)
print ("Y=", Y)
print ("Z=", Z)
Ans. (i) False
(ii) X=10.0
Y7.0

Z=4.0

UNSOLVED QUESTIONS=

1. Write Python command/instruction/statement to display your name.

command to display your school name, class and section, separated by ""
2. Write Python
3. Evaluate the following expressions manually:
(i) (2+3) **3-6/2 (i) (2+3)
*
5//4 + (4 + 6)/ 2
(ii) 1 2 + (3 * 4 - 6 ) / 3 (iv) 12 +(3**4-6// 2
(v) 12 * 3 %5+2 * 6//4 (vi) 12 % 5 *3+(2*6)//4 nually
calculator and verify the results that you get man
4. Evaluate the above expressions by using IDLE as a

reason for each:


5. Identify invalid variable names from the following, giving
Group, if, total marks, S.l, volume, tot_strength, #tag,
tags, 9a, for
YI
the output
ut
of the llowing code:
Find
6. (1) x=3 (2) x=-2
y=x+2
Y=2

x+=y x+=Y
print (x, y) Y-=X
print (x, y)
(3) a=5 (4) p=10
b-2*a q-20
at=a+b
p*=q/3
b*=a+b gt=p+q*2
print (a, b) print (P,q)
p=582 (6) p-21//5
(5)
p**4 p4
r=p//q r=p*q
pt=ptqtr pt ptq-r
rt=p+q+r r*=p-qtr
g-=p+q*r qt=ptg
print (P, q, r) print (p, q, r)
Write Python expressions equivalent to the following arithmetic/algebraic expressions:

(2)+9 (3) 3+
2

(4) a +2 (5) 8-6+ Sum


-var (6) ut+ot
b 7

8. Write Python expressions to represent the following situations:


(a) Add remainder of 8/3 to the product of 8 and 3.
(b) Find the square root of the sum of 8 and 43.
(c) Find the sum of the square roots of 8 and 43.
(d) Find the integral part of the quotient when 100 is divided by 32.
9. What are operators? Give some examples of unary and binary operators.
10. What is an expression and a statement?
11. What all components can a Python program contain?
12. What are variables? How are they important for a program?
13. What is Dynamic Typing feature in Python?
14. Write a function called calculate_area() that takes base and height as an input argument and returns an
area of a triangle as an output. The formula used is:
Area of a Triangle = 4*base*height

15. Modify the above function to take a third parameter called shape type. Shape type should be either triangle
or rectangle. Based on the shape, it should calculate the area. Formula used:
Area of a Rectangle length*width.
16.
Write function called print_pattern
a
pattern if the input number is 3:
that takes integer number as argument and prints the following

***

If input is 4, then it should print:


**

***
****

. Write a function that takes


amount-in-dollars and dollar-to-rupee conversion price; it then returns the
amount converted to
rupees.
18. What would the following code do?
a = b = 70

19. What is the in the


error following code?
2,p 6
20. Find out the error(s) in the following code fragments?
(1) temperature = 90
Print temperature
(2) a = 30
b = a + b

print (a And b)
(3) a, b, C =2, 8, 9
print (a, b, c)
C, b, a t a, b, C
print (a ; b; c)
(4)x = 24
4 x
(5) Print ("X =" X)
21. What will be the output produced by the following code fragment(s)?
(1) X = 10
X X + 10
X = X - 5

print (X)
X, Y =X 2, 22
print (X, Y)
(2) first = 2
second = 3
third = first * second

print (first, second, third)


third = second * first

print (first, second, third)


(3) side = int (input ('side '))

#side given as 7
area = side * side

print (side, area)


22. "Comments useful and are easy way to enhance
an
readability and understandability of a
program
Elaborate with examples.
23. Find errors in the following code fragments.
(1) a = 3

print (a)
b 4
print (b)
S atbb

print (s)
(2) Name = "Prateek"
Age = 26

Print ("your name & age are", Name + Age)


(3) A 3
S A + 10
A "New"
Q A10
output,
the
Give

24.
x 40

x20, Ytx

print (x, y)

output.
the
Give
5,
=
20, 60
y
X *, Y
*

10, x + 10
X, Y
y,
print(x, y)

the output.
Give
26. 13
b
=
12,
a,
a/2
C,
b a *2,
(a, b, c)
print
output.
Give the
27.
b, c
=
10, 20, 30
a,
a +3, b -4
9 r =C~5,
P,
P , g, r)
print('a and b,
the following code fragment. (The input entered as XI)
98 Find errors in
("Enter your class:"))
c
int (input

print ("Your class is", c)


code fragment.
errors in the following
29. Find
("Enter your class")
cl- input
in class" cl-1)
("Last year you were
print
radius of a circle and prints its area.
Python program that accepts
30. Write a
marks.
marks in 5 subjects and outputs average
Write Python program that accepts
31.
to find the area of a triangle.
32. Write a program
a number and print its
first five multiples.
33. Write a program to input in the
class, age of a student and then print the details, firstly
to read details like name,
34. Write a program
same line and
then in separate lines. of
variables with the sums
variables and swap first two
to read three numbers in three
35. Write a program
and third numbers, respectively.
and second, second
first

CASE-BASED/SOURCE-BASED INTEGRATED QUESTIONS=


Industries
in 500 shares of Agro
He has invested 545000
Awasthi is in share trading business. calculate his average
1. Mr. Upendra Ltd. Write a program to
300 shares of LTP Associates (P)
Ltd. and 357550 in
investment cost per share.

Ans. C / U s e r s / p r e e t i / A p p D a t a / L o c a l / P r o g r a m s / P y t h o n / P y t h o n 3 7 - 3 2 / p r

prog shares investment.py


FeEda Femat Run Options Window Help invested in Agro Industries:"))
Amount
("Enter
agro_amt=float (input of share3 purchased:)}
(Eater number
dgroshares=int (input
Anount
invested 2n DP Indus tri es:"))
(input("Enter
amtfloat shares purchased:
"))
P (input("Enter
nuber of
P_Bhares=int
total _amt=agro_amt 1tp_amt
tOtal_shares=agro_shares+1tp_shares

avgcost_share-total_amt/total_shares

print(- invested :", tota_amt)


amount
Print(Total :",total_3hares) , a v g c o s t _ s h a r e )
print(otal shares bough share
made per
prlnt("Average
investment
L 17 Cot
A
sLds he ebg Ogten Winda tey

R9TANTI C1/Uaea/preeti/Appbata/local/progr ana/


ython/Python 37- 12/prog oharen lnvestmont PY
Enter Anount invested in Agro Industrlen 1545000
tler uidet of shates purchasedi 500
Enter Amount Inve sted in TP Induntrlesi 351550
tnter number of shares purchasedi 300

Total amount invested 902550,0


Total shares bought000
Average nventnont mado per share 1120,1875

2. EX500 is an India based Cargo company that deals in the export and
selling of various pond.
country as well as abroad. To do business analysis of day-to-day transactions,
they have hireddaa programrt
nh
to develop a computerized system for the purp0se.
Write a Python code that accepts cost of goods sold
(cogs), revenue generated (re), operating.coete t
prints gross profit (gp), net profit (np) and net profit percentage (npp). [Net profit
Revenu =

yenue og-
Ans.
rogevenselpy-CAewpreeti/AppDtalocayProgramy/yhanvty
Pldia lema hun Options Vlndow Hely
0
PEOgram to calculate Gross profit, net profit and
e t profit percentage
cog nt (input f g90ds 3o1d ))
gint(input ( 9nerated "))
Oc int (input (
9P Ig cogsGroBsprofit
np g P OC Net protit
npp np/rg*100 Net profit percentage
print(
print( np)
print( npp)

n 14 Cto
Pyhon 370 Shel
Fe Ld hDebu Optens Vinden p
hore intormation.

RESTART: C:/Users/preeti/AppData/local/progra
ms/Python/Python 37-32/pr09 revenue1.pY
Lnter cost of goods sold: 100000
Enter revenue generated : 150000
Enter operating cost 12000
Gross profit is 50000
Het profit is 3800
Net profit percentage is 25.333333333333336

In11 Cel4

3. Gurukul Vidyapeeth is a reputed institution in the field of academics and extracuricular activities and involved
in the overall growth of every child. Every year, with the commencement of the new sesion, it hikes fee by 10%
for all the students which was done manually till now and required enormous efforts on the part of office staft.
To solve this problem, develop a Python program that automatically calculates this 10% fee hike every year
after obtaining the basic fee amount from the user and displays it for the parents of students enrolled witn
the institute.
Ans.
Laprog seeike.oy Chers/prenti/AppData/Loc-
File f4t fnmat Run Opions Windon Hdp
9Program to calculate 108 fee hike
&mount int(input("Enter Arnount '))
1ee_hike 0.1"amount
total_fee amount fee_hike
print"Fee Iy paY ',total_fee)
Pyrthon 3:70 Shel
Fle lda heli Dehup oplions Window Halp

RESTART C:/Users/preeti/hppData/Loca
1/Prograns/Python/Python37-32/prog_fee
hike.py
Enter Amount 120000
Fee to pay now: 132000.0

YI

You might also like