Engineering Python : UNIT III : Control Flow, Functions, Strings : Anna University Two Marks Questions & Answers
Two Marks Questions with Answers
Q.
1 What is pass in python ?
Ans. : Pass means no operation statement. It
can be treated as placeholder in compound statement, where there should be
blank left
Q.
2 What are various control statements in python ?
Ans. : Various control statements in python
are - if, if...else, while, for statements
Q.
3 What is the use of range in python ?
Ans. : The range is used to represent the size
of the list or a sequence. It is commonly used in for loop to denote the
element from given range.
Q.
4 What is the use of ll operator in python ?
Ans. : Using // operator is used for
performing the division operation. The result will be rounded and only integer
value of the result will be displayed.
Q.
5 What are the rules for global and local variables ?
Ans. : Local variables : If a variable is
assigned a new value anywhere within the function's body, it's assumed to be
local, Global variables : Those variables that are only referenced inside a
function are implicitly global.
Q.
6 Is Python Case sensitive language ?
Ans. : Yes Python is a case sensitive
language.
Q.
7 What will be the output of s*3 if s="Ureka"
Ans. : The output will be UrekaUrekaUreka
Q.
8 What is the purpose of ** operator?
Ans. : The operator ** is exponent operator.
It calculates the power of given number. For example 2**3 = 8
Q.
9 What is purpose of break and continue statement ?
Ans. : • The break statement is for
terminating the loop statement and transfers execution to the statement
immediately following the loop. The continue statement causes the loop to skip
the remainder of its body and immediately retest its condition prior to
reiterating.
Q.
10 How will you check that in a string all the characters are numberic ?
Ans. : Using the isnumeric() function we can
check that in a string all the characters are numeric.
Q.
11 If we declare [10,20,30] then what is the output of 30. Justify
Ans. : True. It indicates that 30 is a member
of the given sequence.
Q.
12 What is the difference between pop() and remove() function
Ans. : The pop() will return remove last
element of the list and remove() will remove any desired element from the list.
Q.
13 How to represent string in Python ?
Ans. : The string is represented using either
double quotes or single quotes.
Q.
14 What are the operating systems on which the Python program runs ?
Ans. : The python is platform can run on
Windows, Mac, Linux and so on. It is a platform independent language.
Q.
15 What is a Boolean value ?
Ans. : A Boolean value is either true or false. It is
named after the British mathematician, George Boole, who first formulated
Boolean algebra - some rules for reasoning about and combining these values.
This is the basis of all modern computer logic.
Q.
16 What are the Python language supports the types of operators ?
Ans. : Arithmetic operators • Comparison
(Relational) operators • Assignment operators • Logical operators • Bitwise
operators Membership operators • Identity operators
Q.17
What is the meaning of iteration ?
Ans. : Computers are often used to automate
repetitive tasks. Repeating identical or similar tasks without making errors is
something that computers do well and people do poorly. Repeated execution of a
set of statements is called iteration.
Q.
18 What are the python supports the control statements?
Ans. :
Q.
19 State about logical operators available in python language with example.
Ans :
Logical Operators
There
are three types of logical operators and, or, not.
For
example
>>>
a= Ture
>>>
b= False
>>>
a and b
False
>>>
a or b
True
>>>
not a
False
>>>
Q. 20 Comment with an example on the use of local and global variable with the same identifier.
Local and Global Scope
•
The global variables are those variables that are declared and defined outside
the function and can be used inside the function.
•
The local variables are those variables that are declared and defined inside a
function.
•
A global variable is one that can be accessed anywhere. A local variable is the
opposite, it can only be accessed within its frame.
•
The difference between the global and local is that global variables can be
accessed locally, but not modified locally inherently.
•
For example : In the following program, variable a is global variable.
def
fun():
print(a)
#global
scope
a
= 10 fun()
Output
10
Now
consider following program, in which we try to change the value declared
outside the function
def
f():
print(a)
a
= 100 #Due to this statement the
error is raised
#
Global scope
a
= 10
f()
print(a)
To
make the above program work, we need to use global keyword. We only need to use
global keyword in a function if we want to do change that variable.
The
corrected version of above program is as follows:
def
f():
global
a
print(a)
a
= 100
#
Global scope
a
= 10
f()
print(a)
Output
10
100
Q. 21 Define recursive function
Ans : Recursion is a property in which one
function calls itself repeatedly in which the values of function parameter get
changed on each call.
Q.
22 Present the flow of execution for a while statement.
Ans :
The
while statement is popularly used for representing iteration.
Syntax
while
test_condition:
body
of while
Flowchart
for while statement is as given below
The
flow of execution is specified as follows –
1.
Using the condition determine if the given expression is true or false.
2.
If the expression is false then exit the while statement
3.
If the expression is true then execute the body of the while and goback to step
1 in which again the condition is checked.
For
example
while
i< = 10:
i
= i + 1
•
The body of a while contains the statement which will change the value of the
variable used in the test condition. Hence finally after performing definite
number of iterations, the test condition gets false and the control exits the
while loop.
•
If the condition never gets false, then the while body executes for infinite
times. Then in this case, such while loop is called infinite loop.
•
There is another version of while statement in which else is used.
Problem Solving and Python Programming: UNIT III: Control Flow, Functions, Strings : Tag: Engineering Python : Control Flow, Functions, Strings | Engineering Python Programming - Anna University Two Marks Questions & Answers
Problem Solving and Python Programming
GE3151 1st Semester | 2021 Regulation | 1st Semester Common to all Dept 2021 Regulation