0% found this document useful (0 votes)
5 views14 pages

Python 2

The document contains a series of objective and theoretical questions related to Python programming, focusing on conditional statements, loops, and code rewriting. It includes explanations for correct answers and code outputs, along with examples of how to implement various programming concepts. Additionally, it addresses common errors and provides solutions for specific coding tasks.

Uploaded by

srnimkar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
5 views14 pages

Python 2

The document contains a series of objective and theoretical questions related to Python programming, focusing on conditional statements, loops, and code rewriting. It includes explanations for correct answers and code outputs, along with examples of how to implement various programming concepts. Additionally, it addresses common errors and provides solutions for specific coding tasks.

Uploaded by

srnimkar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 14

Objective Type Questions

Question 1

Which of the following are invalid conditional statements of Python?

1. IF

2. Test if

3. if-else

4. if-elif

5. if

6. if-elif-else

Answer

IF and Test if

Reason — IF and Test if are invalid conditional statements of Python.

Question 2

Which of the following is/are not a valid Python loop ?

1. for

2. while

3. iter

4. repeat

Answer

iter, repeat

Reason — iter and repeat are not valid Python loop. for and while are valid
loop statements.

Question 3

Which of the following is used to define a block of code (e.g., body of if or


body of a loop) in Python ?

1. { }
2. ( )

3. indentation

4. Quotation

Answer

indentation

Reason — Indentation is used to define a block of code in Python.

Question 4

What is the output of following code ?

if None :

print "Up"

else:

print "Down"

1. Up

2. Down

3. No output

4. Up Down

Answer

Down

Reason — Since None is treated as false, the if condition results in false. The
block of if will be ignored and the else block will be executed and Down will
be printed on the output screen.

Question 5

What will be the output of following code ?

for i in range(1) :

print i

1. 0

2. 1
3. No output

4. Error in code

Answer

Reason — The range(n) function generates a list as [0, 1, 2..., n - 1]. Thus,
the range(1) function will generate a list as [0]. Thus the for loop will execute
only one time and print 0 on the output terminal.

Question 6

What will the output of following code ?

for i in range(1, 0) :

print i

1. 0

2. 1

3. No output

4. Error in code

Answer

No output

Reason — The function range(1, 0) will return return an empty list [] as no


number falls in the arithmetic progression beginning with 1 and ending with
0. Thus, the for loop will not execute even once and hence, no output will be
generated on the output screen.

Question 7

What is the output of following code ?

while 3 >= 3 :

print 3

1. 3 is printed once

2. 3 is printed three times

3. 3 is printed infinitely until program is closed


4. Error in code

Answer

3 is printed infinitely until program is closed

Reason — Since the given condition (3 >= 3) will always remain true, the
while loop will execute indefinitely and keep on printing 3 on the output
screen till the program is closed.

Theoretical Questions

Question 1

What are selection statements ? How are they useful ?

Answer

A statement that carries out a set of instructions depending upon a


condition's result is known as a selection or conditional statement. Python
provides selection statements in the form of if and if-else statements.

These statements enable a program to carry out a set of instructions


depending upon a condition's result. For example,

num = 5

if num >= 0 :

print "Positive"

else :

print "Negative"

Question 2

What are looping statements ? How are they useful ?

Answer

A statement that allows a set of instructions to be performed repeatedly is


known as a looping or iteration statement. Python provides looping
statements in the form of for and while loop.

These statements enable a program to perform a set of instructions


repeatedly. For example,

num = 3
while num > 0 :

print num

num = num - 1

Question 3

Differentiate between if and if-else statements.

Answer

Difference between if and if-else statements in Python:

S.
No if if-else
.

if statement allows us to execute a if-else statement allows us to execute diffe


1. code block when the condition is blocks based on whether the condition is T
True. False.

if statement does nothing in case if-else statement executes the statements


2.
condition evaluates to False. below else when the condition is False.

Question 4

Differentiate between for and while statements.

Answer

Difference between for and while statements in Python:

S.
No for while
.

for loop is used when the number of while loop is used when the number of i
1.
iterations are known beforehand. are not fixed and depends on a specific

for loop is used for iterating over a while loop is used for repeated executio
2.
sequence or a range. on a condition.

3. Syntax: Syntax:
S.
No for while
.

for <variable> in <sequence> : while <logicalexpression> :


statements_to_repeat statements_to_repeat

Question 5(a)

Rewrite the following code fragments using for loop

while num > 0 :

print num % 10

num = num/10

Answer

l = [1]

for x in l:

l.append(x + 1)

if num <= 0:

break

print (num % 10)

num = num/10

Question 5(b)

Rewrite the following code fragments using for loop

i = 100

while (i > 0) :

print i

i -= 3

Answer

for i in range(100, 0, -3) :

print (i)
Question 6

What is following code doing ? What would it print for input as 3 ?

n = input( "Enter an integer:" )

if n < 1 :

print "invalid value"

else :

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

print i * i

Answer

The code will print the square of each number from 1 till the number given as
input by the user if the input value is greater than 0. Output of the code for
input as 3 is shown below:

Enter an integer:3

Question 7(a)

Rewrite following code fragments using while loops

min = 0

max = num

if num < 0 :

min = num

max = 0

# compute sum of integers from min to max

for i in range(min, max + 1):

sum += i

Answer
min = 0

max = num

if num < 0 :

min = num

max = 0

# compute sum of integers from min to max

i = min

while i <= max:

sum += i

i += 1

Question 7(b)

Rewrite following code fragments using while loops

for i in range(1, 16) :

if i % 3 == 0 :

print i

Answer

i=1

while i < 16:

if i % 3 == 0 :

print (i)

i += 1

Question 8(i)

Write a short program to print the following series :

1 4 7 10 ........... 40.

Answer

Solution

for i in range(1, 41, 3) :


print(i, end = ' ')

Output

1 4 7 10 13 16 19 22 25 28 31 34 37 40

Question 8(ii)

Write a short program to print the following series :

1 -4 7 -10 .......... -40

Answer

Solution

x=1

for i in range(1, 41, 3) :

print(i * x, end = ' ')

x *= -1

Output

1 -4 7 -10 13 -16 19 -22 25 -28 31 -34 37 -40

Question 9(a)

Predict the output of the following code fragments :

count = 0

while count < 10:

print "Hello"

count += 1

Answer

Output

Hello

Hello

Hello

Hello

Hello
Hello

Hello

Hello

Hello

Hello

Explanation

The while loop executes 10 times so "Hello" is printed 10 times.

Question 9(b)

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

Explanation

x y Output Remarks

10 0 10 0 1st Iteration

9 1 10 0 2nd Iteration
x y Output Remarks

91

10 0
8 2 91 3rd Iteration
82

10 0
91
7 3 4th Iteration
82
73

10 0
91
6 4 82 5th Iteration
73
64

Question 10

What will be output of the following code if the user enters Principal amount
as 20000 and Time as 10 years.

#Considering Python 2.7

P = input("Enter Principal amount:")

T = input("Enter Time:")

if T > 10:

SI = P*T*10/100

else:

SI = P*T*15/100

print("Simple Interest = ", SI)

Answer

Output

Enter Principal amount:20000


Enter Time:10

Simple Interest = 30000

Explanation

This Python program calculates the Simple Interest (SI) based on the given
Principal amount (P) and Time (T) values. For time value of more than 10
years, the program takes the rate of interest as 10% and for 10 years or less
it takes the rate of interest as 15%.

Question 11

Write a Python script that asks the user to enter a length in centimetres. If
the user enters a negative length, the program should tell the user that the
entry is invalid. Otherwise, the program should convert the length to inches
and print out the result. There are 2.54 centimetres in an inch.

Answer

Solution

len = int(input("Enter length in cm: "))

if len < 0:

print("Invalid input")

else:

inch = len / 2.54

print(len, "centimetres is equal to", inch, "inches")

Output

Enter length in cm: 150

150 centimetres is equal to 59.05511811023622 inches

Question 12

Write a program that asks the user for two numbers and prints Close if the
numbers are within .001 of each other and Not close otherwise.

Answer

Output

Enter first number: 10.12345


Enter second number: 10.12354

Close

Question 13

Write a short program to print first n odd numbers in descending order.

Answer

Solution

n = int(input("Enter n: "))

x=n*2-1

for i in range(x, 0, -2) :

print(i)

Output

Enter n: 5

Application Oriented Questions

Question 1

Consider the following code fragment :

while

n=3

print n == 3

(a) What will be the output of above code ?

(b) What is the reason behind this output ?

Answer
(a) The given code will generate a syntax error.

(b) The condition of while and colon after that is missing. Thus a syntax error
is generated.

Question 2

Consider the following code fragment :

n=3

while n == 3

print n

n=n-1

(a) What will be the output of above code?

(b) What is the reason behind this output ?

(c) What change/corrections will you make to above code to make it print
some output ?

Answer

(a) The given code will generate a syntax error.

(b) The syntax of while statement has a colon after the condition which is
missing in the given code. Thus, a syntax error is generated.

(c) The following changes must be made to the code to print some output:

n=3

while n == 3 :

print (n)

n=n-1

The code given above will generate the following output:

You might also like