0% found this document useful (1 vote)
37 views7 pages

Unit-Ii Question Bank

The document discusses control structures and strings in Python. It covers sequential, selection, and iterative structures, and provides examples of if/else statements, for loops, and while loops. String concepts like comparison, slicing, and indexing are also explained through examples.
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 (1 vote)
37 views7 pages

Unit-Ii Question Bank

The document discusses control structures and strings in Python. It covers sequential, selection, and iterative structures, and provides examples of if/else statements, for loops, and while loops. String concepts like comparison, slicing, and indexing are also explained through examples.
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/ 7

1

UNIT II
CONTROL STRUCTURES AND STRINGS
Course Objective – To understand the control structures and string concepts.

Course Outcome – To develop program using control structure and simple string
processing.

Bloom’s Taxonomy Level – K1, K2, K3


PART A

S.No. Question BTL


1. What are control structures? What are the different control structures K1
used in Python?
Control Structures are the blocks that analyze variables and choose directions
in which to go based on given parameters. They can alter the control flow of
a program execution.
The primary types of control structures used in Python:
i. Sequential structure – default mode of execution
ii. Selection structure - used for decisions and branching
iii. Repetition structure - used for looping, i.e., repeating a piece of code
multiple times
2. What is sequential structure? K1
This control structure represents the linear and sequential execution of code,
where statements are executed one after another in the order they appear. This
is the simplest type of programming control structure and forms the
foundation of most programs.
Eg.
a = 20
b = 10
c=a*b
print(f"The product of {a} and {b} = {c}”)

OUTPUT
The product of 20 and 10 = 200
3. What is selection structure? List the different selection control K1
statements in Python.
This control structure enables a program to choose between two or more paths
of execution based on specific conditions. Several conditions are tested and
instructions are executed based on which condition is true. The statements
used for selection structure are also called as decision control statements or
branching statements. They are
• if statement
• if-else statement
• if – elif - else ladder statement
2

• nested if statement
4. Write a python program that accepts two numbers, finds and displays K3
the bigger one of them.
A = int(input("Enter the first number"))
B = int(input("Enter the second number"))
if(A>B):
print(A, "is bigger")
else:
print(B, "is bigger")

Output
Enter the first number80
Enter the second number90
90 is bigger
5. Write a Python program that allows a withdrawal transaction. Assume K3
an available balance amount of Rs. 10,000.
bal = 10000
amt = int(input(“Enter the amount to withdraw: ”))
if (amt >= bal):
print(“Insufficient balance. Withdrawal not possible.”)
else:
bal -= amt
print(“Your balance amount = Rs. ”, bal)

Output:
Enter the amount to withdraw: 8000
Your balance amount = Rs. 2000
6. What is iterative structure? List the different iteration statements in K2
Python.
This control structure allows certain blocks of code to be executed repeatedly
as long as a condition remains true. Iteration statements in python include the
following:
• for loop
• while loop
7. Write the syntax and usage of for loop. K2
A for loop is a control flow statement that executes code repeatedly for a
particular number of iterations. In this control flow statement, the keyword
used is for. The ‘for’ loop is used when the number of iterations is already
known. It has three forms of usage.
(i) for var in range([start],end,[step] ):
statement block

(ii) for var in list:


statement block
3

(iii) for var in list:


statement block 1
else:
statement block 2
Usage:
• When it is used with range( ) function the iterator variable var takes
values from start_value to stop_value – 1.
• When it is used with list / tuple, the iterator variable var takes values,
one by one from the list / tuple.
• Used to repeat a set of statements for a finite number of times.
• Used to iterate through a string, list and tuple.
8. Write the syntax of while loop and its usage. K1
It is a loop that executes a single statement or a group of statements for the
given true condition. The keyword used to represent this loop is "while". A
"while" loop is used when the number of iterations is unknown. It has two
forms of usage.
(i) while condition:
statement block

(ii) while condition:


statement block 1
else:
statement block 2
Usage:
• Used to repeat a set of statements till a condition remains True.
• Used to repeat a set of statements for a finite number of times.
• Used to iterate through a string, list and tuple.
9. What is pass statement? K2
‘pass’ is a null statement. It is like a comment, but it is not ignored by the
interpreter. When the user does not know what code to write, user simply
places a ‘pass’ at that line. It can be placed where empty code is not allowed,
like in loops, function definitions, class definitions, or in if statements. It can
be used as a placeholder for future code.
Syntax: pass
10. Can loop statements have else clause? If yes, when will it be executed? K2
Yes. Python allows the else keyword to be used with the ‘for’ and ‘while’
loops too. The else block appears after the body of the loop. The statements
in the else block will be executed after all iterations are completed.
Eg.
n=4
for i in range(1, n):
print(i)
4

else:
print(“PYTHON”)
11. Differentiate between break and continue statements. K3
Break Continue
The break statement is used to The continue statement is used to
terminate the execution of the stop the current iteration of the loop
nearest enclosing loop in which it and continues with the next iteration
appears.
Syntax: break Syntax: continue
It causes a forward jump in program It causes a backward jump in
execution. program execution.
Example Example
i=1 for i in range(1, 10):
while(i <= 10): if (i % 5 = = 0):
print(i, end='') continue
if (i==5): print(i, end='')
break print("Done")
i=i+1
print("Done") Output: 1 2 3 4 6 7 8 9 Done

Output: 1 2 3 4 5 Done
12. State the differences between continue and pass statement with an K3
example.
Continue Pass
The continue statement is used to The pass statement is used when a
ignore the remaining statements statement is required syntactically.
in the current iteration of the loop
and moves the control back to the
start of the loop.
It stops the current iteration. It does nothing.
Syntax: continue Syntax: pass
Example Example
for i in range(1, 10): for i in range(1, 10):
if (i % 5 = = 0): if (i % 5 = = 0):
continue pass
print(i, end='') print(i, end='')
print("Done") print("Done")
Output: 1 2 3 4 6 7 8 9 Done
Output: 1 2 3 4 6 7 8 9 Done
5

13. Point out the errors in the following codes: K2


(i) for i in range(1, 10, 2.0):
print(i)
(ii) while j <= 10:
print(j)
j++
(i) TypeError: 'float' object cannot be interpreted as an integer – float values
cannot be used in range function.
(ii) 2 errors:
(a) SyntaxError: invalid syntax - ++ is not an operator in Python
14. Explain string comparison with an example. K3
The comparison operator works on string to check if two strings are equal.
>>>PIN=‘1234‘
>>>if PIN==‘1234’:
print(“Correct PIN”)
Correct PIN
15. What is meant by string slicing? Give examples. K2
A substring of a string is obtained by taking a slice. The operator [n:m] returns
the part of the string from the nth character to the mth character, including the
first but excluding the last.
Example:
>>>book=‘Problem Solving and Python Programming‘
>>>print(book[0:7])
Problem
>>>print(book[21:27])
Python
16. Mention the use of the split function in Python. K2
The use of split function in Python is that it breaks a string into shorter strings
using the defined separator. It gives a list of all words present in the string
Syntax: string.split(separator, maxsplit)
separator – Optional parameter - Specifies the separator to use when splitting
the string. By default any whitespace is a separator
maxsplit – Optional parameter - Specifies how many splits to do. Default
value is -1, which is "all occurrences"
Eg.
txt = "hello world"
x = txt.split(" ")
print(x)

Output: ['hello', 'world']


17. What is a Negative Indexing in Python? K2
Negative Indexing is used to in Python to begin slicing from the end of the
string i.e. the last. Slicing in Python gets a sub-string from a string. The slicing
range is set as parameters i.e. start, stop, and step.
6

Eg.
myStr = 'Python Programming!'
print("String = ", myStr)
print("String after slicing (negative indexing) = ", myStr[-4 : -1])

Output:
String after slicing (negative indexing) = ing
18. What are the two operators that are used in string functions? K2
The 2 operators used in string functions are ‘in’ and ‘not in’. They can be used
to test whether a substring is a member of a string or not.
>>>‘t‘ in ‘Python’
True
>>>‘p‘ in ‘Python’
False
>>>‘thon‘ not in ‘Python’
False
>>>‘then‘ not in ‘Python’
True
19. What is the use of str.upper() and str.lower() functions in string?? K2
The functions str.upper() and str.lower() will return a string with all the letters
of original string converted to upper or lower case letters.
>>>ss = ‘Python Programming’
>>>print(ss.upper())
PYTHON PROGRAMMING
>>>print(ss.lower())
python programming
20. List few string methods and their purpose, used in Python. K3
Method Description
capitalize() Converts the first character to upper case
find() Searches the string for a specified value and returns the
position of where it was found
index() Searches the string for a specified value and returns the
position of where it was found.
isalpha() Returns True if all characters in the string are in the alphabet
isdigit() Returns True if all characters in the string are digits
isnumeric() Returns True if all characters in the string are numeric
islower() Returns True if all characters in the string are lower case
isupper() Returns True if all characters in the string are upper case
lower() Converts a string into lower case
upper() Converts a string into upper case
replace() Returns a string where a specified value is replaced with a
specified value
strip() Returns a trimmed version of the string
7

PART B

S. No Question BTL
1. Explain in detail about the different conditional control (16 Marks) K2
statements / Selection Control statements / decision making
with an example.
2. What are the different looping construct available in python? (16 Marks) K2
Explain each of them in detail with relevant example.
3. Write in detail about the different unconditional statements in (16 Marks) K2
Python. Give example program for each.
4. a) Design a simple calculator using if …elif construct. (16 Marks) K3
b) Write a Python program to check the prime number using
while loop.
5. a) Write a python program to print factorial of a given number (16 Marks) K3
b) Implement a Python program to find the sum of n even
natural numbers.
6. a) Develop a program to find the largest among three (16 Marks) K3
numbers.
b) Write a python program to find the given year is leap or
not.
7. Discuss about the following operations related to strings in K2
Python. (8 Marks)
(a) String comparison (8 Marks)
(b) String slicing
(c) String splitting
(d) String stripping
8. Explain the different string manipulation functions available (16 Marks) K2
in Python with example.

You might also like