HIGH Chances Pyton Qustion Answer in 2024
HIGH Chances Pyton Qustion Answer in 2024
Based on the consistent patterns in the provided question papers, below are important questions divided into
MCQs, SAQs (Short Answer Questions), and Long Answer Questions. These questions have a high
probability of appearing in the December 2024 semester.
python
Copy code
my_dict = {"name": "Alice", "age": 25}
python
Copy code
with open("file.txt", "w") as file:
file.write("Hello, World!") # Writing to a file
with open("file.txt", "r") as file:
content = file.read() # Reading from a file
print(content)
python
Copy code
x = 10 # Global
def my_function():
x = 5 # Local
print(x)
my_function()
print(x)
python
Copy code
num = 7
if num > 1:
for i in range(2, num):
if num % i == 0:
print("Not Prime")
break
else:
print("Prime")
else:
print("Not Prime")
python
Copy code
lst = [1, 2, 3, 4, 5]
print(lst[1:4]) # Output: [2, 3, 4]
print(lst[::-1]) # Output: [5, 4, 3, 2, 1]
1. What is the output of list(range(2, 6))? 2 is the start and 6 is the stop
value
o (a) [2, 3, 4, 5] (Answer)
o (b) [2, 3, 4, 5, 6]
o (c) (2, 4, 6)
o (d) [2, 5]
2. How do you remove an item from a list by its value?
o (a) pop()
o (b) remove() (Answer) remove is the list operation
o (c) delete()
o (d) discard()
3. Which of the following is a Python tuple?
o (a) {1, 2, 3}
o (b) [1, 2, 3]
o (c) (1, 2, 3) (Answer) tuple is inilize the parenthesis bracket "()"
o (d) None of these
4. What will be the output of print("diploma"[2:-1])?
o (a) plo
o (b) iplom (Answer) start in 2 index is i
o (c) diploma and -1 is stop index in last
o (d) Error
5. Which function converts a string to uppercase?
o (a) lower()
o (b) upper() (Answer)
o (c) capitalize()
o (d) None of these
6. Which operator has the lowest precedence in Python?
o (a) *
o (b) **
o (c) or (Answer) or in the low precedencs
o (d) and
python
Copy code
with open("file.txt", "r") as f:
content = f.read()
print(content)
o Writing Files:
python
Copy code
with open("file.txt", "w") as f:
f.write("Hello, world!")
python
Copy code
for i in range(5):
print(i)
python
Copy code
x = 0
while x < 5:
print(x)
x += 1
python
Copy code
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
print(factorial(5)) # Output: 120
python
Copy code
num = 121
if str(num) == str(num)[::-1]:
print("Palindrome")
else:
print("Not Palindrome")
python
Copy code
num = 29
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
print("Not Prime")
break
else:
print("Prime")
3. Reverse a String.
python
Copy code
s = "hello"
print(s[::-1]) # Output: "olleh"
python
Copy code
num = 123
total = sum(int(digit) for digit in str(num))
print(total) # Output: 6
python
Copy code
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
print(a, end=" ")
a, b = b, a + b
fibonacci(5) # Output: 0 1 1 2 3
Additional High-Chance Questions with Answers
3
MCQs with Answers
python
Copy code
class Person:
def __init__(self, name):
self.name = name
obj = Person("Alice")
print(obj.name) # Output: Alice
python
Copy code
def decorator(func):
def wrapper():
print("Before function")
func()
print("After function")
return wrapper
@decorator
def my_function():
print("Hello!")
my_function()
python
Copy code
import copy
a = [[1, 2], [3, 4]]
shallow = copy.copy(a)
deep = copy.deepcopy(a)
python
Copy code
a = [1, 2]
b = [1, 2]
print(a is b) # False
print(a == b) # True
1. Write a Python program to calculate the sum of the squares of a list of numbers.
python
Copy code
numbers = [1, 2, 3, 4]
squares_sum = sum(x**2 for x in numbers)
print("Sum of squares:", squares_sum)
# Output: Sum of squares: 30
python
Copy code
lst = [1, 2, 3]
lst[0] = 10 # Changes the first element
print(lst) # Output: [10, 2, 3]
python
Copy code
tup = (1, 2, 3)
# tup[0] = 10 # Error: Tuples are immutable
python
Copy code
def reverse_string(s):
reversed_str = ""
for char in s:
reversed_str = char + reversed_str
return reversed_str
python
Copy code
num = 10
if num % 2 == 0:
print("Even")
else:
print("Odd")
python
Copy code
s = "hello world"
vowels = "aeiou"
count = sum(1 for char in s if char.lower() in vowels)
print("Vowel count:", count) # Output: Vowel count: 3
python
Copy code
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
print(a, end=" ")
a, b = b, a + b
fibonacci(6) # Output: 0 1 1 2 3 5
python
Copy code
def factorial(n):
result = 1
for i in range(1, n + 1):
result *= i
return result
print(factorial(5)) # Output: 120
python
Copy code
print(2 ** 3 + 5 // 2) 2^3=8
5/2=2
o (a) 11 (Answer) 8+2=10
o (b) 9
o (c) 8
o (d) 10 ans
6. What is the purpose of __name__ == '__main__' in Python?
o (a) To define the main function
o (b) To check if the script is run as the main program (Answer)
o (c) To import other modules
o (d) To terminate a program
python
Copy code
class Parent:
def greet(self):
print("Hello from Parent!")
class Child(Parent):
def greet(self):
super().greet() # Calls Parent's greet()
print("Hello from Child!")
obj = Child()
obj.greet()
# Output:
# Hello from Parent!
# Hello from Child!
python
Copy code
print(isinstance(5, int)) # True
print(isinstance("Hello", str)) # True
python
Copy code
lst = [3, 1, 2]
print(sorted(lst)) # [1, 2, 3]
print(lst) # [3, 1, 2]
lst.sort()
print(lst) # [1, 2, 3]
python
Copy code
print('a' in 'apple') # True
print(5 not in [1, 2, 3]) # True
python
Copy code
lst = [3, 5, 7, 2, 8]
print("Largest element:", max(lst)) # Output: Largest element: 8
2. Explain Python's exception handling mechanism with an example.
o Exception handling uses try, except, and optionally finally blocks to manage errors
gracefully.
Example:
python
Copy code
try:
x = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero.")
finally:
print("Execution completed.")
# Output:
# Cannot divide by zero.
# Execution completed.
python
Copy code
from collections import Counter
s = "hello"
count = Counter(s)
print(count) # Output: Counter({'l': 2, 'h': 1, 'e': 1, 'o': 1})
python
Copy code
def func(a, b=2, *args, **kwargs):
print(a, b, args, kwargs)
func(1, 3, 4, 5, x=10, y=20)
# Output: 1 3 (4, 5) {'x': 10, 'y': 20}
python
Copy code
d = {'a': 3, 'b': 1, 'c': 2}
sorted_d = dict(sorted(d.items(), key=lambda x: x[1]))
print(sorted_d) # Output: {'b': 1, 'c': 2, 'a': 3}
python
Copy code
import math
print(math.gcd(12, 18)) # Output: 6
python
Copy code
s1 = "hello world"
s2 = "world"
print(s2 in s1) # Output: True
python
Copy code
year = 2024
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print("Leap Year")
else:
print("Not a Leap Year")
4. Reverse a Number
python
Copy code
num = 12345
reversed_num = int(str(num)[::-1])
print(reversed_num) # Output: 54321
python
Copy code
celsius = 25
fahrenheit = (celsius * 9/5) + 32
print(fahrenheit) # Output: 77.0
python
Copy code
print(5 > 3 and 3 < 2)
o (a) True
o (b) False (Answer)
o (c) None
o (d) Error
6. Which of the following is a mutable data type?
o (a) tuple
o (b) string
o (c) list (Answer)
o (d) frozenset
7. What will range(5) return?
o (a) [0, 1, 2, 3, 4]
o (b) (0, 1, 2, 3, 4)
o (c) A range object (Answer)
o (d) None
python
Copy code
nums = [1, 2, 3]
result = list(map(lambda x: x**2, nums))
print(result) # [1, 4, 9]
python
Copy code
with open("file.txt", "r") as f:
content = f.read()
python
Copy code
import copy
a = [[1, 2], [3, 4]]
shallow = copy.copy(a)
deep = copy.deepcopy(a)
python
Copy code
def add(*args):
return sum(args)
print(add(1, 2, 3)) # Output: 6
python
Copy code
def print_info(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")
print_info(name="Alice", age=25)
python
Copy code
dict1 = {"a": 1, "b": 2}
dict2 = {"c": 3, "d": 4}
merged_dict = {**dict1, **dict2}
print(merged_dict) # Output: {'a': 1, 'b': 2, 'c': 3, 'd': 4}
3. Explain the concept of list comprehension with an example.
o List comprehension is a concise way to create lists.
Example:
python
Copy code
squares = [x**2 for x in range(5)]
print(squares) # Output: [0, 1, 4, 9, 16]
python
Copy code
lst = [10, 20, 4, 45, 99]
largest = max(lst)
lst.remove(largest)
second_largest = max(lst)
print(second_largest) # Output: 45
python
Copy code
def factorial(n):
if n == 0:
return 1
return n * factorial(n - 1)
print(factorial(5)) # Output: 120
python
Copy code
a = [1, 2, 3]
b = [2, 3, 4]
intersection = list(set(a) & set(b))
print(intersection) # Output: [2, 3]
python
Copy code
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
python
Copy code
num = 5
for i in range(1, 11):
print(f"{num} x {i} = {num * i}")
python
Copy code
lst = [1, 2, 3, 4]
print(lst == sorted(lst)) # Output: True
python
Copy code
from collections import Counter
lst = [1, 2, 2, 3, 3, 3]
freq = Counter(lst)
print(freq) # Output: Counter({3: 3, 2: 2, 1: 1})
Additional Tips
Focus on MCQs about basic syntax, operators, methods, and built-in functions.
Master SAQs on file handling, list comprehensions, and Python-specific concepts like __init__ and
super().
Practice programming questions, especially involving loops, recursion, and common data structure
manipulations.
Be familiar with Django basics like MVC architecture, creating a project, and working with models.
These questions, along with the earlier sets, ensure comprehensive preparation for the December 2024
semester exams.