Question 31 (1)
Hem [*2 for n in range(10)
Print item
Prediction: This will result in a syntax error.
Justification: The syntax is incorrect. It seems like the intention might have been to define a list
comprehension but it’s improperly formatted. The correct syntax should involve proper list
comprehension or function definition.
Question 31 (2)
Keyword = ‘acinubedfg’
Print(keyword[3] + keyword[3:])
Prediction: Output will be ‘inubedfg’.
Justification: keyword[3] refers to the character at index 3, which is ‘i’. Keyword[3:] slices the
string from index 3 to the end, yielding ‘inubedfg’. Therefore, concatenating them gives
‘inubedfg’.
Question 31 (3)
a = [1, 2, 3]
b = [1, 2, 3]
print(a is b)
c=a
print(a is c)
for i in (1, 2, 3):
pass
Prediction:
Print(a is b) will output False.
Print(a is c) will output True.
Justification: a and b are two different lists with the same content, so a is b evaluates to False
(they do not reference the same object). C references the same object as a, hence a is c evaluates
to True.
Question 31 (4)
count = 1
def doThis():
global count
count += 1
doThis()
print(count)
Prediction: Output will be 2.
Justification: The doThis function modifies the global variable count, incrementing it by 1.
Initially, count is 1, so after calling doThis(), it becomes 2.
Question 31 (5)
x = set()
x = {1, 2, 3, 4, 4, 5}
for i in x:
print(i)
Prediction: The output will be a series of numbers: 1, 2, 3, 4, 5 in no particular order.
Justification: The set data structure automatically removes duplicates. Hence, even though 4 is
repeated in the initialization, it will appear only once when iterated over.
Question 31 (6)
a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
a[0][1] + a[2][2:1]
Prediction: This will raise an error.
Justification: a[2][2:1] attempts to slice from index 2 to 1, which is an invalid operation as it will
yield an empty list. Therefore, trying to add an integer to an empty list will result in a TypeError.
Question 32
Let’s go through these one by one.
item = [n*2 for n in range(10)] print(item)
This creates a list comprehension that multiplies each number in the range from 0 to 9 by 2. The
result is: \[ \text{item} = [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] \] Prints: \[ \text{[0, 2, 4, 6, 8, 10, 12,
14, 16, 18]} \]
keyword = ‘aeioubcdfg’ print(keyword[:3] + keyword[3:])
This prints the entire string as keyword[:3] grabs the first three characters, and keyword[3:] grabs
the rest: \[ \text{keyword} = ‘aeioubcdfg’ \] Prints: \[ \text{aeioubcdfg} \]
a = [1, 2, 3] b = a c = [1, 2, 3] print(a is b) print(a == c) print(a is c)
a is b checks if a and b are the same object in memory. Since b is assigned to a, this is True.
a == c checks if a and c have the same content, which is True.
a is c checks if a and c are the same object. They are not, so it is False.
Prints: \[ \text{True} \] \[ \text{True} \] \[ \text{False} \]
x = set() x = {1, 2, 3, 4, 4, 5} s = 0 for i in x: s = s + i print(s)
The duplicate value 4 in the set {1, 2, 3, 4, 4, 5} is ignored.
The set becomes {1, 2, 3, 4, 5}.
Summing these values: s = 1 + 2 + 3 + 4 + 5 = 15.
Prints: \[ \text{15} \]
Suppose a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] Evaluate the expression a[0][:1] + a[2][2:]
a[0][:1] gets the first sublist [1] from [1, 2, 3].
a[2][2:] gets the third element [9] from [7, 8, 9].
Question 33
def prime_factors(n):
factors = []
# Check for number of 2s that divide n
while n % 2 == 0:
[Link](2)
n = n // 2
# n must be odd at this point, check for odd factors
for i in range(3, int(n**0.5) + 1, 2):
while n % i == 0:
[Link](i)
n = n // i
# If n is still a prime number greater than 2
if n > 2:
[Link](n)
return factors
# Example usage:
num = int(input(“Enter a number: “))
result = prime_factors(num)
print(f”Prime factors of {num} are: {result}”)
Question 34
def find_first_day_above_average(temperatures):
# Loop through the list of temperatures starting from the second day
for i in range(1, len(temperatures)):
# Calculate the average of the temperatures up to the previous day
previous_average = sum(temperatures[:i]) / i
# Check if the current day’s temperature is higher than the previous average
if temperatures[i] > previous_average:
Return i # Return the index of the first day meeting the condition
# If no day meets the condition, return -1
return -1
# Example usage
temperatures = [32, 34, 30, 40, 38, 45]
result = find_first_day_above_average(temperatures)
print(result) # Output should be 3
Question 35
def is_palindrome(s):
length = len(s)
for i in range(length // 2):
if s[i] != s[length – i – 1]:
return False
return True
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
If n % i == 0:
return False
return True
def count_magical_strings(strings):
magical_count = 0
for s in strings:
if is_palindrome(s) and is_prime(len(s)):
magical_count += 1
return magical_count
input_strings = [“radar”, “level”, “world”, “deified”, “noon”, “abc”, “a”, “bob”]
magical_count = count_magical_strings(input_strings)
print(“Number of magical strings:”, magical_count)
Question 36
x = int(input(“enter the time of each song:”))
y = int(input(“enter the time of journey:”))
one_playtime=3*x
no_of_times=n//one_playtime
rem = n%one_playtime
a = b = c = no_of_times
if (rem//x==1):
a +=1
elif (rem//x==2):
a +=1
b +=1
print(“time for which A song is played:”,a)
print(“time for which B song is played:”,b)
print(“time for which C song is played:”,c)
Question 37
Import random
def roll_dice():
return [Link](1,6), [Link] (1,6)
def calculate_sum(dice):
return sum(dice)
def game():
print (“Welcome to the dice game”)
#First roll
dice = roll_dice()
roll_sum = calculate_sum (dice)
print(f”You rolled : {dice} ( Sum : {roll_sum})”)
# Check the outcome of the first roll
if roll_sum in [7, 11]:
print(“You win!”)
elif roll_sum in [2, 3, 12]:
print(“Craps! You lose.”)
else:
point = roll_sum
print(f”Your point is set to {point}. Keep rolling to make your point!”)
# Continue rolling until the player either makes their point or rolls a 7
whiile True:
dice = roll_dice()
roll_sum = calculate_sum(dice)
print(f”You rolled: {dice} (Sum: {roll_sum})”)
if roll_sum == point:
print(“Congratulations! You made your point and win!”)
break
elif roll_sum == point:
print(“You rolled a 7. You lose.”)
break
if _name_ = = “_main_”:
game()
Question 38
def encypt_func(txt, s):
result = “”
# transverse the plain txt
for i in range(len(txt)):
char = txt[i]
# encypt_func uppercase characters in plain txt
if ([Link]()):
result += chr((ord(char) + s – 64) % 26 + 65)
# encypt_func lowercase characters in plain txt
else:
result += chr((ord(char) + s – 96) % 26 + 97)
return result
# check the above function
Txt = “CEASER CIPHER EXAMPLE”
s=4
print(“Plain txt : “ + txt)
print(“Shift pattern : “ + str(s))
print(“Cipher: “ + encypt_func(txt, s))
Question 39
a = 12
b=1
c=1
if a + b > c and a + c > b and b + c > a:
print(“Yes”)
else:
print(“No”)
Question 40
# Total number of families
total_families = 960
# Percentages
cow_families_percent = 60 / 100
buffalo_families_percent = 30 / 100
both_families_percent = 15 / 100
# Applying inclusion-exclusion principle
families_with_cow_or_buffalo = cow_families_percent + buffalo_families_percent –
both_families_percent
# Families that have neither cow nor buffalo
families_neither = 1 – families_with_cow_or_buffalo
# Calculating the number of families that have neither
families_neither_count = families_neither * total_families
# Output the result
print(f”Number of families that do not have a cow or a buffalo: {int(families_neither_count)}”)