Home | | Computer Science 12th Std | Python Control Structures: Book Back Questions and Answers

Chapter: 12th Computer Science : Chapter 6 : Core Python : Control Structures

Python Control Structures: Book Back Questions and Answers

Choose the best answer, Answer the following questions

Computer Science : Core Python : Control Structures

Evaluation


Part – I

Choose the best answer 1 Marks


1.How many important control structures are there in Python?

A) 3   

B) 4

C) 5   

D) 6


2.elif can be considered to be abbreviation of

A) nested if     

B) if..else     

C) else if  

D) if..elif


3.What plays a vital role in Python programming?

A) Statements

B) Control

C) Structure

D) Indentation


4.Which statement is generally used as a placeholder?

A) continue

B) break

C) pass

D) goto


5.The condition in the if statement should be in the form of

A) Arithmetic or Relational expression

B) Arithmetic or Logical expression

C) Relational or Logical expression

D) Arithmetic


6.Which is the most comfortable loop?

A) do..while

B) while

C) for

D) if..elif


7.What is the output of the following snippet?

i=1

while True:

if i%3 ==0: break

print(i,end='')

i +=1

A) 12

B) 123

C) 1234

D) 124


8.What is the output of the following snippet?

T=1

while T:

print(True)

break

A) False

B) True

C) 0

D) no output


9.Which amongst this is not a jump statement ?

A) for

B) goto

C) continue

D) break


10. Which punctuation should be used in the blank?

if <condition>_

statements-block 1

else:

statements-block 2

A) ;

B) :

C) ::

D) !


Part –II

Answer the following questions       2 Marks


1. List the control structures in Python.

Ans. (i) Sequential

(ii) Alternative or Branching

(iii) Iterative or Looping


2. Write note on break statement.

Ans. (i) The break statement terminates the loop containing it. Control of the program flows to the statement immediately after the body of the loop.

(ii) When the break statement is executed, the control flow of the program comes out of the loop and starts executing the segment of code after the loop structure.

(iii) If break statement is inside a nested loop (loop inside another loop), break will terminate the innermost loop.


3. Write is the syntax of if..else statement

Ans. Syntax :

if <condition>:

statements-block 1

else:

statements-block 2


4. Define control structure.

Ans. A program statement that causes a jump of control from one part of the program to another is called control structure or control statement.


5. Write note on range () in loop

Ans. range() generates a list of values starting from start till stop-1,

range (start,stop,[step])

Where,

start - refers to the initial value

stop - refers to the final value

step - refers to increment value, this is optional part.


Part –III

Answer the following questions       3 Marks


1. Write a program to display

A

A  B

A  B  C

A  B  C  D

A  B  C  D  E

Ans. For i in range (65, 70):

For j in range (65, i + 1):

print (chr(j), end = ' ')

print (end='\n')

i+ = 1


2. Write note on if..else structure.

Ans. (i) When we need to construct a chain of if statement(s) then ’elif’ clause can be used instead of ’else’.

(ii) Syntax:

if <condition-l>:

statements-block 1

elif <condition-2>:

statements-block 2

else:

statements-block n

(iii) In the syntax of if..elif..else mentioned above, condition-1 is tested if it is true then statements-block 1 is executed, otherwise the control checks condition-2, if it is true statements-block2 is executed and even if it fails statements-block n mentioned in else part is executed.


3. Using if..else..elif statement write a suitable program to display largest of 3 numbers.

Ans. a = int (input ("Enter number 1")

 b = int (input (" Enter number 2")

c = int (input (" Enter number 3")

if a > b and a > c:

put (" A is greatest")

elif b > a and b > c:

print ("B is greatest")

else:

print ("C is greatest")

 

4. Write the syntax of while loop.

Ans. Syntex:

while <condition>:

statements block 1

[else:

statements block2]


5. List the differences between break and continue statements.

Ans. The break statement terminates the loop containing it and control reaches after the body of the loop where as continue statement skips the remaining part of a loop and start with next iteration.


Part –IV

Answer the following questions       5 Marks


1. Write a detail note on for loop

Ans. (i) for loop is the most comfortable loop. It is also an entry check loop. The condition is checked in the beginning and the body of the loop(statements-block 1) is executed if it is only True otherwise the loop is not executed.

(ii) Syntax:

for counter_variable in sequence:

statements-block 1

[else: # optional block

statements-block 2]

(iii) The counter_variable mentioned in the syntax is similar to the control variable that we used in the for loop of C++ and the sequence refers to the initial, final and increment value. Usually in Python, for loop uses the range() function in the sequence to specify the initial, final and increment values. range() generates a list of values starting from start till stop-1.

(iv) The syntax of range() is as follows: range (start,stop,[step])

Where,

start - refers to the initial value

stop - refers to the final value

step - refers to increment value, this is optional part.

Examples for range():

range (1,30,1)- will start the range of values from 1 and end at 29

range (2,30,2)- will start the range of values from 2 and end at 28

range (30,3,3)-will start the range of values from 30 and end at 6

range(20) -will consider this value 20 as the end value(or upper limit) and starts the range count from 0 to 19 (remember always range() will work till stop -1 value only)

Example:

#Program to illustrate the use of for loop - to print single digit even number

for i in range (2,10,2):

print (i, end=’')

Output:

2 46 8


2. Write a detail note on if..else..elif statement with suitable example.

Ans. (i) When we need to construct a chain of if statement(s) then ’elif clause can be used instead of ’else’.

(ii) Syntax :

if <condition-l>:

statements-block 1

elif <condition-2>:

statements-block 2

else:

statements-block n

(iii) In the syntax of if..elif..else mentioned above, condition-1 is tested if it is true then statements-block 1 is executed, otherwise the control checks condition-2, if it is true statements-block2 is executed and even if it fails statements-block n mentioned in else part is executed.



(iv) ’elif clause combines if..else-if..else statements to one if..elif...else, ’elif can be considered to be abbreviation of’else if. In an 'if statement there is no limit of'elif clause that can be used, but an ’else’ clause if used should be placed at the end.

(v) Example : #Program to illustrate the use of nested if statement

Average : Grade

>=80 and above : A

>=70 and <80 : B

>=60 and <70 : C

>=50 and <60 : D

Otherwise        :   E

ml=int (input("Enter mark in first subject:"))

m2=int (input("Enter mark in second subject:"))

avg= (ml+m2)/2

if avg>=80:

print ("Grade: A")

elif avg>=70 and avg<80:

print ("Grade: B")

elif avg>=60 and avg<70:

print ("Grade : C")

elif avg>=50 and avg<60:

print ("Grade: D")

else:

print ("Grade: E")

Output 1:

Enter mark in first subject: 34

Enter mark in second subject: 78

Grade: D

Output 2 :

Enter mark in first subject: 67


3. Write a program to display all 3 digit odd numbers.

Ans. for i in range (101, 100, 2):

 print (i, end = " ")


4. Write a program to display multiplication table for a given number.

Ans. n = int (input ("Enter the number")

for i in range (1, 13):

print (n, 'x', i," = ", n * i)


HANDS ON EXPERIENCE

 

1. Write a program to check whether the given character is a vowel or not.

Ans. Program :

ch = input ("Enter a character")

if ch in ('a, 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U'):

print (ch, 'is a vowel')

 

2. Using if..else..elif statement check smallest of three numbers.

Ans. Program :

a = in + (input ("Enter number 1")

b = in + (input("Enter number 2")

c = in + (input ("Enter number 3")

if a < b and a < c :

print ("a is smallest")

elif b < a and b< c:

print ("b is smallest")

else

print ("c is smallest")

 

3. Write a program to check if a number is Positive, Negative or zero.

Ans. Program :

x = in + (input("aEnter a number")

if x>0:

print (x, "is a positive number")

elif x < 0:

print (x, "is a negative number")

else

print (x, "is zero")

 

4. Write a program to display Fibonacci series 0 1 12 3 4 5 (upto n terms)

Ans. Program to display Fibonacci series :

a, b, c = 0, 1,0

n = in + (input("Enter number of terms"))

print (a, end = ")

print (b, end = ")

for I in range (3, n+1):

c = a+b

print (c, end = ")

a = b

b = c

 

5. Write a program to display Sum of natural numbers, upto n.

Ans. Program to display sum of natural numbers upto n :

s = 0

n = in + (input("Enter number of terms"))

for i in range (1, n+1)

s = s + i

print ("sum = ", s)


6. Write a program to check if the given number is a palindrome or not.

Ans. Program :

n = int (input("Enter the number"))

s, d, x = 0, 0, n

while (n ! = 0):

d = n%10

s = (s* 10)+d

n = n//10

 if s = = x:

print ("The given number is palindrome")

else:

print ("The given number is not palindrome")

 

7. Write a program to print the following pattern

* *  * * *

* * * *

* * *

* *

*

Ans. Program :

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

for j in range (1, i + 1):

print (', * ', end =")

print (end = '\n')

 i + = 1

 

8. Write a program to check if the year is leap year or not.

Ans. Program :

y = int (input ("Enter the year"))

If y % 400 = = 0 or y% 4 = = 0 or y% 100 = = 0:

print ("The given year is a leap year")

else:

print ("The given number is not a leap year")


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
12th Computer Science : Chapter 6 : Core Python : Control Structures : Python Control Structures: Book Back Questions and Answers |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.