[go: up one dir, main page]

0% found this document useful (0 votes)
212 views13 pages

Class 12 Pyq Chapter 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
212 views13 pages

Class 12 Pyq Chapter 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

CLASS 12:: COMPUTER SCIENCE:: CHAPTER 1:: REVISION TOUR 1

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Short Answer Questions/Conceptual Questions

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:

Keywords — Examples are import, for, in, while, etc.

Identifiers — Examples are MyFile, _DS, DATE_9_7_77, etc.

Literals — Examples are "abc", 5, 28.5, etc.

Operators — Examples are +, -, >, or, etc.

Punctuators — ' " # () etc.

QUESTION 2

How are keywords different from identifiers?

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:

String literals, Numeric literals, Boolean literals,Special literal None,Literal collections

QUESTION 4

State True or False : "Variable declaration is implicit in Python."

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

name = "Ravi" # name is implicitly declared as a string.

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

Price*Qty ⇒ Contains special character *

class ⇒ It is a keyword

4thCol ⇒ Begins with a digit

Question 6

Find the invalid identifier from the following :

MyName

True

2ndName

My_Name

Answer

The invalid identifier are:

2ndName ⇒ Begins with a digit

True ⇒ It is a keyword

Question 7

Which of the following is an invalid datatype in Python ?


Set , None , Integer , Real

Answer

Real

Reason — Real is not a standard built-in data type in Python.

Question 8

Identify the valid arithmetic operator in Python from the following:

<

**

and

Answer :: **

Reason — Let's go through each option and see if its valid arithmetic operator or not:

? — The question mark is not a valid arithmetic operator in Python.

< — It is a relational operator.

** — It is an arithmetic operator.

and — It is a logical operator.

Question 9

How are string-literals represented and implemented in Python?

Answer

A string-literal is represented as a sequence of characters surrounded by quotes (single, double or


triple quotes). String-literals in Python are implemented using Unicode.

Question 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

Which of the following are valid operators in Python:

**

*/

like

||

is

between

in

Answers

The valid operators are:

** ⇒ Exponentiation operator

is ⇒ Identity operator

^ ⇒ Bitwise XOR operator

in ⇒ Membership operator

Question 12

What is an expression and a statement?

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

What are variables? How are they important for a program?

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)

What are data types? How are they important?

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)

Write the names of any four data types available in Python.

Answer

The names of any four data types in Python are:

Integer

String

List

Tuple

Question 19

How many integer types are supported by Python? Name them.

Answer

Two integer types are supported by Python. They are:

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.

Mutable types in Python are:

Lists , Dictionaries , Sets


Immutable types in Python are:

Integers , Floating-Point numbers , Booleans , Strings , Tuples

Question 21

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:

* 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

Explain the use of the pass statement. Illustrate it with an example.

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

The corrected code is shown below:

To = 30 # Correction 1

for K in range(0,To): # Correction 2

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 2 — Semi-colon was missing in the for loop syntax.

Correction 3 — if statement should be in lower case.

Correction 4 — else statement should be in lower case.

Correction 5 — Full stop should not be there at the end of print function.

TYPE B: APPLICATION BASED QUESTIONS


Question 1

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.

#Read the limit

limit = float(input("Enter the limit"))

max_price = 0

# Read the next price

next_price = float(input("Enter a price or 0 to stop:"))

while next_price > 0 :

<write your code here>

#Read the next price

<write your code here>

if max_price > 0:

<write your code here>

else :

<write your code here>

Answer

#Read the limit

limit = float(input("Enter the limit"))

max_price = 0

# Read the next price

next_price = float(input("Enter a price or 0 to stop:"))

while next_price > 0 :

if next_price < limit and next_price > max_price:

max_price = next_price

#Read the next price

next_price = float(input("Enter a price or 0 to stop:"))

if max_price > 0:
print("Largest Price =", max_price)

else :

print("Prices exceed limit of", limit);

Question 2b

Predict the output of the following code fragments:

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

Predict the output of the following code fragments:

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

Predict the output of the following code fragments:

x = 10

y=5

for i in range(x-y * 2):

print (" % ", i)

Answer

This code generates No Output.

Explanation

The x-y * 2 in range(x-y * 2) is evaluated as below:

x-y*2

⇒ 10 - 5 * 2

⇒ 10 - 10 [∵ * has higher precedence than -]

⇒0

Thus range(x-y * 2) is equivalent to range(0) which returns an empty sequence — [ ].

Question 2i

Predict the output of the following code fragments:

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

Predict the output of the following code fragments:

x = [1,2,3]

counter = 0

while counter < len(x):

print(x[counter] * '%')

for y in x:

print(y * '* ')

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

Predict the output of the following code fragments:

x ='apple, pear, peach, grapefruit'

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

Loop executes for 5 times.

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.

You might also like