0% found this document useful (0 votes)
23 views

Module 3 Worksheet

Uploaded by

rudra.24bai10485
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Module 3 Worksheet

Uploaded by

rudra.24bai10485
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Module 3 – Worksheet 1

Multiple Choice Questions

Q.No.1. choose the python statements which gives the different output form other
statements
a) for a in range(0,10):
print(a)
b) for b in [0,1,2,3,4,5,6,7,8,9]:
print(b)
c) for c in [0,1,2,3,4,5, 6,7,8,9,10]:
print(c)
d) for d in range(0,10,1):
print(d)
Answer:

Q.No.2. choose the correct output for the following python code

for i in range(0,3,-1):
print("Welcome")

a) No output
b) Welcome Welcome Welcome
c) WelcomeWelcomeWelcome
d) Welcome

Answer:

Q.No.3. Identify the correct python code in if statement

a) if x>=45 :
b) if (x >= 45)
c) if (x => 45)
d) if x >= 45

Answer:

Q.No.4. 'G' + 'R' if '1'.isdigit() else 'N' + 'B' choose the output of the above expression

a) GR
b) NB
c) GRNB
d) GR1

Answer:
Q.No.5. What type of iteration is used in While Loop
a) discriminant
b) indefinite
c) indeterminate
d) definite
Answer:

Q.No.6. Which of the following Python code snippets correctly swaps the values of
two variables, a and b?

a) a = b b = a

b) temp = a a = b b = temp

c) a, b = b, a

d) swap(a, b)

Answer:

Q.No.7. What is the purpose of the range() function in Python when used for
counting?

a. To generate a list of numbers in a specified range

b. To calculate the length of a list

c. To iterate over a sequence of numbers

d. To count the occurrences of a specific element in a list

Answer:

Q.No.8 What happens if you attempt to calculate the factorial of a negative number in
Python?
a) It calculates the factorial without any issues.

b) Python raises a ValueError.

c) It returns 0.

d) It returns 1.

Answer:

Q.No.9. What is the Fibonacci Sequence?

a) A series of prime numbers

b) A sequence of numbers where each number is the sum of the two preceding ones

c) A sequence of odd numbers

d) A sequence of even numbers

Answer:

Q.No.10. Which function can be used to convert a character to its corresponding


integer value without using ASCII codes?

A) char_to_int(char)

B) int(char)

C) convert(char)

D) num(char)

Answer:

Fill in the Blanks


Q.No.1. ________________keyword can be used instead of if in conditional
statement?
Answer:

Q.No.2 . ______________ is the output of the following statements in python.

x = "TIME"

for i in range(len(x)):

print(i)

Answer:

Q.No.3. If the condition will not become _____________ then a loop becomes infinite
loop
Answer:

Q.No.4. ____________ will be the output of the following python program


N=6
while N <= 6:
if N < 6:
N=N+1
print(N)
Answer:

Q.No.5. ____________ will be the output of the following python program


True = False
while True:
print(True)
break
Answer:

Q.No.6. To find the sum of all elements in a list named "numbers," you can use the
expression ____________.

Hint: It involves using the sum function and passing the list as an argument.

Answer:
Q.No.7. The base case for the factorial function is typically defined as follows:
factorial(0) equals __________.

Hints: The factorial of 0 is a mathematical constant.

Answer:

Q.No.8. The Python function chr() is used to convert an ____________ to its


corresponding character.

Hint: It's a numerical value.

Answer:

Q.No.9. The concept of summation is closely related to the concept of ____________


in mathematics.

Hint: It refers to the continuous addition of terms.

Answer:

Q.No.10. The result of the expression 5 > 10 is ____.

Hints: It compares the values on both sides of the greater-than symbol.

Answer:

Module 3 – Worksheet 2
Rearrange the Python Code

Q.No.1. Print the counts


def count_elements(input_list):
for element in input_list:
if element in element_counts:
element_counts[element] = 1
else:
element_counts[element] += 1
for element, count in element_counts.items():
print(f"{element}: {count}")
input_list = [1, 2, 3, 1, 2, 1, 4, 5, 4, 3, 2, 1]
count_elements(input_list)
element_counts = {}

Hint :

● Dictionary for Counts: Use a dictionary to store the counts of each element.
The elements will be the keys, and the counts will be the values.
● Iterating through the List: Use a loop to iterate through each element in the
input list.
● Checking Existence: Check whether an element is already in the dictionary.
● Updating Counts: If the element is already in the dictionary, increment its count;
otherwise, add it to the dictionary with a count of 1.
● Printing Counts: Finally, iterate through the dictionary and print the elements
along with their counts.

Answer:
Q.No.2. Fibonacci sequence
fib_sequence = [0, 1]
def fibonacci(n):
n = int(input("Enter the number of terms in the Fibonacci sequence: "))
for i in range(2, n):
fib_sequence.append(next_term)
return fib_sequence
result = fibonacci(n)
next_term = fib_sequence[i-1] + fib_sequence[i-2]
print(f"The Fibonacci sequence with {n} terms is: {result}")

Hint : The fibonacci function takes an input n, representing the number of terms in
the sequence. The sequence starts with [0, 1], and then the loop generates the next
terms by summing the last two terms. The loop runs from the third term (index 2) up to
the nth term. The result is a list containing the Fibonacci sequence up to the specified
number of terms.

Answer:
Q.No.3. Factorial Computation

if n == 0 or n == 1:
result = 1
else:
return 1
for i in range(2, n + 1):
number = 5
def factorial(n):
result = factorial(number)
return result
result *= i
print(f"The factorial of {number} is: {result}")

Hint : This code defines a function factorial that takes a non-negative integer n as
input and returns its factorial. It uses a simple iterative approach with a for loop to
calculate the factorial.

Answer:
Q.No.4. Find the Sum of even numbers.
m = int(input(“Enter the number:”)
total_sum = 0
if i % 2 == 0:
for i in range(m):
total_sum += i
print("Sum of even numbers up to", m, "is:", total_sum)
Hint : Sample Input1 : 10
Sample Output1 : 20
Answer:
Q.No.5. Check the number is palindrome or not
result = is_palindrome(input_word)

def is_palindrome(word):
reversed_word = word[::-1]
if word == reversed_word:
return True
else:
return False

input_word = "racecar"

if result:
print(input_word, "is a palindrome.")
else:
print(input_word, "is not a palindrome.")

Hint : Sample Input1 : racecar


Sample Output1 : racecar is a palindrome

Answer:
Programming Questions

Q.No 1 : A shop is offering 10 % Discount on all items. Given Price P of an item, print
the discounted price rounded up to two decimal places.

Hint : Sample Input1 Sample Input2


100 175
Sample Output1 Sample Output2
90.00 157.50

Answer:

Q.No 2 :Two numbers A and B are passed as input. A number N is also passed as
the input. The program must print the numbers from A to B (inclusive of A and B)
which are not divisible by N.
Hint : Sample Input1
3
20
5
Sample Output1
3 4 6 7 8 9 11 12 13 14 16 17 18 19

The numbers 5,10,15,20 are left out as they are divisible by 5.


Answer:

Q.No 3: Two numbers A and B are passed as input. The program must print the odd
numbers from A to B (inclusive of A and B) interlaced with the even numbers from B
to A
Hint : Sample Input1
5
11
Sample Output1
5 10 7 8 9 6 11

Explanation
The Odd numbers from 5 to 11 are 5 7 9 11
The even numbers from 11 to 5 (that is in reverse direction) are 10 8 6
So these numbers are interlaced to produce 5 10 7 8 9 6 11

Answer:
Q.No 4 : Balu was kind to beggars. Balu daily donates 50% of the amount he has
when a beggar request him. Input for the program is the amount M1 left in Balu hand
and the number of beggars B1 who received the amount are passed as the input. The
program must print the money Balu had in the starting of the day
Hint : Sample Input1
100
2
Sample Output1
400

Balu donated to 2 beggars.


When Balu encountered Second beggar he had 100 *2 = 200
When Balu encountered First beggar he had 200 *2 = 400
Answer:

Q.No 5 : The Managing Director(MD) of the company has planned to encourage the
staff members coming to the office on time. MD was announced the consecutive
incentives form the starting of the week Monday to end of the week Saturday. The
reward will be given as Rs.500 that is more than the punctuality incentive than the
previous day. The starting day Incentive (Icen) is passed as the first input. The
second input N is passed as the number of days the staff came on time to the office
and the output should be p which is the total amount of incentive he got in the
particular week.
Hint : Sample Input1
1000
3
Sample Output1
4500

On First Day he receives : Rs. 1000, second day : Rs. 1500, Third Day : 2000.
So total : Rs. 4500.
Answer:
Module 3 – Worksheet 3

Debugging the Python Code

Q.No 1 :Rectify the error(s). So that the program must accept three integers X, Y,
Z as the input and print the output based on the following condition.
If all the integers are in sorted order then print as ascending order
If all the integers are sorted in descending order then print as descending order
Else print as random order.
X = input()
Y = input()
Z = input()
If X<=Y<=Z :
print(“Ascending Order”)
else if X>=Y>=Z :
print(“Descending Order”)
else:
print(“Random Order”)
Answer:
Q.No 2 : The character Ch was passed as the input. Based on the input ch the
following should be printed.
The value of the ch can be one of the following – Y y N n
If the input is y or Y the output should be Yes
If the input is n or N the output should be No
Otherwise the output should be invalid
Ch = input()
If ch.Upper == ‘y’:
Print(“Yes”)
If ch.upper ==’N’
Print(“No”)
Else:
Print(“Invalid”)
Answer:

Q.No 3 : M1,N1,X1 these three values are passed as the input to the program. The
program should print the output as the numbers divisible by X1 from N1 to M1.
M1 = input()
N1 = input()
X1 = input()
for n in range(N1-1,M1,1):
if n/X1 ==0:
pri
t(no, end=’ ‘);
Hint: Sample Input1
2
40
7
Sample Output1
35 28 21 14 7
Answer:

Q.No 4 : M1 was passed as the input to the program. The program should print the
total sum of the odd digits in the given input
M1 = input()
Oddsum = 0
While m1>0
Udig = M1%10
If Udig%2 == 1:
Oddsum += Udig
M1 = M1%10
print(Oddsum)
Hint: Sample Input1 Sample Input2
224 4165
Sample Output1 Sample Output2
0 6
Answer:
Q.No 5 : M1, M2 are passed as the input to the program. Third input is N1. The
program should print the numbers from M1 to M2, inclusive of M1 and M2 which
are not divisible by N1
M1 = input()
M2 = input()
N1 = input()
for n in range(M1, M2-1,-1):
if n%N1 ==0:
print(n, end=’ ‘);

Hint: Sample Input1


2
20
4
Sample Output1
2 3 5 6 7 9 10 11 13 14 15 17 18 19
Answer:

Q.No.6.
fact=1
num=int(input("enter the number"))
if num==0:
print(1)
elif num<0:
print("not possible")
if num>0:
for i in range(1,num+1):
fact=fact*i
print(fact, end=' ')
What will be the output of given program if the user input is 3?

Answer:
Q.No.7.
fact=1
num=int(input("enter the number"))
if num==0:
print(0)
elif num<0:
print("not possible")
if num>0:
for i in range(1,num+1):
fact=fact*i
print(fact, end=' ')
Is this factorial program logic correct? Will it give you correct output for all the inputs?

Answer:

You might also like