1. Computer Programs are categorized into __
a) Interactive Program and Batch Program
b) Interactive Program and Badge Program
c) Inactive Program and Batch Program
d) Inactive Program and Badge Program
Answer: a) Interactive Program and Batch Program
2. Python was first introduced in __
a) 1990
b) 1994
c) 1992
d) 2022
Answer: a) 1990 (Note: Python was created by Guido van Rossum and first released
in 1991, but 1990 is the closest option as development began in 19891990.)
3. Operator != is used for _____
a) Equal
b) Logical AND
c) Not equal
d) Logical OR
Answer: c) Not equal
4. We can design 2D geometrical shapes in Python, using __
a) Operators
b) Iterations
c) Variables
d) Turtle Graphics
Answer: d) Turtle Graphics
5. What will the following program do: for i in range(10,0,-1): print(i)
a) It will give error
b) It will run 9 times
c) It will run 10 times
d) It will run 11 times
Answer: c) It will run 10 times (Note: It prints numbers from 10 down to 1, which is
10 iterations.)
6. The interactive computer program accepts some input, which is from a user program like
__
a) Sub program
b) Web browser
c) Print job
d) Page break
Answer: b) Web browser
7. __ provide basic variable types such as str, integer, float, etc.
a) Print job
b) Page break
c) Python
d) Procedure
Answer: c) Python
8. The input() function treats the entered data as a set of __
a) Digits
b) Characters
c) Symbols
d) Arithmetic operators
Answer: b) Characters
9. Python provide the option of Modulo operator represented as __
a) =
b) **
c) !
d) %
Answer: d) %
10. "" is used to represent __**
a) Division
b) Floor division
c) Multiplication
d) Exponentiation
Answer: d) Exponentiation
11. If the program is unable to execute the next instruction, the program will reach the state
of __
a) Hang
b) Reboot
c) Shutdown
d) Sleep
Answer: a) Hang
12. We apply comparison with the help of __ statement.
a) For
b) While
c) If
d) But
Answer: c) If
13. In online IDEs, we can write and run our code without __ in our computer.
a) Installing
b) Creating
c) Performing
d) Detecting
Answer: a) Installing
14. IDE stands for __
a) Internet Development Environment
b) Illegal Development Environment
c) Integrated Development Environment
d) Installer Development Environment
Answer: c) Integrated Development Environment
15. Turtle graphics help us to design basic drawing and __
a) 1D shapes
b) 2D shapes
c) 3D shapes
d) 4D shapes
Answer: b) 2D shapes
16. AREPL means __
a) Read-Enter-Print-Loop
b) Read-Exit-Print-Loop
c) Read-Eval-Print-Loop
d) Read-Evolve-Print-Loop
Answer: c) Read-Eval-Print-Loop
17. To draw a triangle, we need to play with __
a) Angles
b) Vertices
c) Lines
d) Shapes
Answer: a) Angles
18. Bug is an unanticipated error which is generally __ in nature.
a) Analog
b) Digital
c) Logical
d) Legal
Answer: c) Logical
1. What are the applications of computer programming in daily life?
Answer: Computer programming is used in almost every aspect of daily life,
including:
Communication: Apps like WhatsApp, Instagram, and email.
Transportation: GPS navigation, traffic lights, and self-driving cars.
Healthcare: Medical imaging, patient records, and robotic surgery.
Finance: Online banking, ATMs, and stock trading.
Entertainment: Video games, streaming services (Netflix, YouTube), and
music apps.
Home Automation: Smart home devices (Alexa, Google Home).
2. Write code to take input a number from user and print its mathematical table on screen
from 1 to 10.
Answer:
python
num = int(input("Enter a number: "))for i in range(1, 11): print(num, "x", i, "=", num * i)3. Take an odd number as input from the user, check if it is odd, otherwise ask the user to
re-enter another odd number.
Answer:
python
while True: num = int(input("Enter an odd number: ")) if num % 2 != 0: print("You entered:", num) break else: print("That's not an odd number. Please try again.")4. Write down the main examples of Python based application.
Answer:
Web Development: Django, Flask (e.g., Instagram, Spotify).
Data Science & Machine Learning: NumPy, Pandas, TensorFlow.
Automation/Scripting: Automating repetitive tasks.
Game Development: Pygame.
Desktop GUI Applications: Tkinter, PyQt.
5. Differentiate between global and local variables with the help of suitable example.
Answer:
Global Variable: Defined outside of all functions. It can be accessed and
modified anywhere in the program.
Local Variable: Defined inside a function. It can only be accessed within
that function.
Example:
python
x = 10 # Global variabledef my_function(): y = 5 # Local variable print("Inside function: x =", x, "y =", y)my_function()print("Outside function: x =", x)# print(y) would cause an error because y is local1. Explain the applications of Python in different business and technical domains.
Answer: Python is a versatile language used across many domains:
Web Development: Frameworks like Django and Flask allow rapid
development of secure and scalable web applications.
Data Science and Analytics: Libraries like Pandas, NumPy, and Matplotlib
enable data manipulation, analysis, and visualization.
Machine Learning and AI: TensorFlow, PyTorch, and Scikit-learn are used
for building predictive models and AI systems.
Automation and Scripting: Python is used to automate repetitive tasks, such
as file management, web scraping, and sending emails.
Software Development: Used for building desktop applications (Tkinter,
PyQt) and testing (Selenium).
Finance: Used for quantitative analysis, algorithmic trading, and risk
management.
Scientific Research: Used in bioinformatics, physics, and astronomy for
simulations and data analysis.
2. What are the basic functions that List provides. Elaborate each of them with an example.
Answer: Lists in Python are mutable sequences. Key functions/methods include:
append(x) : Adds an item x to the end of the list.
Example: my_list = [1, 2]; my_list.append(3) -> [1, 2, 3]insert(i, x) : Inserts item x at index i .
Example: my_list = [1, 3]; my_list.insert(1,2) -> [1, 2, 3]remove(x) : Removes the first occurrence of item x .
Example: my_list = [1, 2, 3];my_list.remove(2) -> [1, 3]pop([i]) : Removes and returns the item at index i (default is last).
Example: my_list = [1, 2, 3]; my_list.pop() -> 3 (list becomes [1, 2] )
sort() : Sorts the list in ascending order.
Example: my_list = [3, 1, 2]; my_list.sort() -> [1, 2, 3]reverse() : Reverses the order of the list.
Example: my_list = [1, 2, 3];my_list.reverse() -> [3, 2, 1]3. Write a program for a Dice Rolling Race game for 2 players. Alternatively, generate a
random number for each player. If the number rolls up to 6, then the player gets another
roll. Rolls of iteration is summed up for the previous roll. The player who reaches 100 first,
wins. How to locate and select 'Debugger' in IDLE? Write steps by taking an example into
consideration.
Answer:
Dice Rolling Race Program:
python
import randomplayer1_score = 0player2_score = 0while player1_score < 100 and player2_score < 100: # Player 1 turn while True: roll = random.randint(1, 6) player1_score += roll print(f"Player 1 rolled {roll}. Total: {player1_score}") if roll != 6: break if player1_score >= 100: print("Player 1 wins!") break # Player 2 turn while True: roll = random.randint(1, 6) player2_score += roll print(f"Player 2 rolled {roll}. Total: {player2_score}") if roll != 6: break if player2_score >= 100: print("Player 2 wins!") breakSteps to use Debugger in IDLE:
1. Open your Python file in IDLE.
2. Go to the Debug menu at the top.
3. Select Debugger to open the Debug Control window.
4. In the Debug Control window, check the boxes for Source, Globals,
and Locals to see the code and variable values.
5. Go back to your code window and set a breakpoint by right-clicking on a
line and selecting Set Breakpoint.
6. Run your program (F5). It will pause at the breakpoint.
7. Use the Step button in the Debug Control window to execute your code line
by line and observe how variables change.
4. Write code to print the multiplication of first 10 odd numbers and first 10 even numbers
and find the difference of the two. [Hint: Use functions]
Answer:
python
def product_of_odds(): product = 1 count = 0 num = 1 while count < 10: product *= num num += 2 count += 1 return productdef product_of_evens(): product = 1 count = 0 num = 2 while count < 10: product *= num num += 2 count += 1 return productodd_product = product_of_odds()even_product = product_of_evens()difference = abs(odd_product - even_product)print("Product of first 10 odd numbers:", odd_product)print("Product of first 10 even numbers:", even_product)print("Difference:", difference)Mini Project 1: Number Guessing Game
Generate a Random number and ask the user to guess the number. If the user guesses right,
congratulate the user and inform about how many guesses it took till the right guess. If the
user guesses wrong, inform the user to try again. Additionally, also provide a hint about
whether the guess was on the higher or lower side. e.g., if the guess is between 1 to 20, you
may provide a user appropriate number of tries. e.g., if the guess is between 1 to 20, you
may provide 5 retries.
Answer:
python
import randomnumber = random.randint(1, 20)attempts = 0max_attempts = 5print("Guess a number between 1 and 20. You have 5 tries.")while attempts < max_attempts: guess = int(input("Enter your guess: ")) attempts += 1 if guess == number: print(f"Congratulations! You guessed it in {attempts}tries.") break elif guess < number: print("Too low! Try again.") else: print("Too high! Try again.")else: print(f"Sorry, you ran out of tries. The number was {number}.")Mini Project 2: Motorcycle Rent Calculator
Motorcycles can be rented either based on distance travelled or the time the motorcycle was
being used. In the first case, the rent is 5 km, while the rate of distance is Rs. 5/km. A client
travelled only 20 km but the possession of the motorcycle for 3 hours. This way, the bill of
transfer and the total rent is calculated based on the value that is greater. e.g., the total rent
in the above case is Rs. 400/-. However, for existing members, the lesser of the two charges
is treated as a discount. This way, if the client in above example was already a member,
only Rs. 300/- needs to be paid by him.
Answer:
python
def calculate_rent(distance_km, hours): distance_charge = distance_km * 5 time_charge = hours * 100 return max(distance_charge, time_charge), min(distance_charge,time_charge)distance = float(input("Enter distance travelled (km): "))hours = float(input("Enter hours used: "))is_member = input("Are you an existing member? (yes/no): ").lower()total_rent, discount = calculate_rent(distance, hours)if is_member == "yes": final_rent = total_rent - discount print(f"Total Rent: Rs. {total_rent}/-") print(f"Member Discount: Rs. {discount}/-") print(f"Final Amount to Pay: Rs. {final_rent}/-")else: print(f"Total Rent to Pay: Rs. {total_rent}/-")(These activities are from the book text, separated as requested.)
Activity 1
In context of Fig 3.10, rewrite code to swap the values of 2 variables without using
temporary variable.
Answer:
python
a = 5b = 10a, b = b, aprint("a =", a) # Output: a = 10print("b =", b) # Output: b = 5Activity 2
Take three inputs as day, month & year, e.g. your date of birth, store in three variables and
print in the form of:
13-09-2023
09-13-2023
2023-09-13
Answer:
python
day = input("Enter day (DD): ")month = input("Enter month (MM): ")year = input("Enter year (YYYY): ")print(f"{day}-{month}-{year}")print(f"{month}-{day}-{year}")print(f"{year}-{month}-{day}")Activity 3
Take input from the user which determines the height of the diamond and prints a hollow
diamond (◇).
Answer:
python
n = int(input("Enter the height of the diamond (odd number): "))# Upper halffor i in range(1, n + 1, 2): spaces = (n - i) // 2 print(" " * spaces + "*" + " " * (i - 2) + ("*" if i > 1 else""))# Lower halffor i in range(n - 2, 0, -2): spaces = (n - i) // 2 print(" " * spaces + "*" + " " * (i - 2) + ("*" if i > 1 else""))Activity 4
Take a random 2 digit number. Subtract it from 100. Calculate the difference. Print the
difference (magnitude only).
Answer:
python
import randomnum = random.randint(10, 99)print("Random number:", num)difference = abs(100 - num)print("Difference from 100:", difference)Activity 5
Take a number as input from the user, using a function, print a mathematical table.
Answer:
python
def print_table(num): for i in range(1, 11): print(f"{num} x {i} = {num * i}")number = int(input("Enter a number: "))print_table(number)Activity 6
Take input from the user which determines the height of the diamond and print a filled
diamond (*).
Answer:
python
n = int(input("Enter the height of the diamond (odd number): "))for i in range(1, n + 1, 2): spaces = (n - i) // 2 print(" " * spaces + "*" * i)for i in range(n - 2, 0, -2): spaces = (n - i) // 2 print(" " * spaces + "*" * i)Activity 7
Write a turtle graphics code to draw a Tic-Tac-Toe board.
Answer:
python
import turtle# Setupscreen = turtle.Screen()screen.title("Tic-Tac-Toe Board")t = turtle.Turtle()t.speed(0)t.pensize(3)# Draw the board# Vertical linest.penup()t.goto(-50, 150)t.pendown()t.goto(-50, -150)t.penup()t.goto(50, 150)t.pendown()t.goto(50, -150)# Horizontal linest.penup()t.goto(-150, 50)t.pendown()t.goto(150, 50)t.penup()t.goto(-150, -50)t.pendown()t.goto(150, -50)t.hideturtle()turtle.done()Activity 8
Take an odd number as input from the user and draw a 'X' with the help of a character like
'+'. The input number defines the size of 'X' to be displayed.
Answer:
python
n = int(input("Enter an odd number: "))for i in range(n): for j in range(n): if i == j or i + j == n - 1: print("+", end="") else: print(" ", end="") print()Activity 9
Write a program to calculate how many iterations it takes to subtract a small number from a
larger one, where both the numbers are positive integrals.
Answer:
python
larger = int(input("Enter the larger number: "))smaller = int(input("Enter the smaller number: "))iterations = 0while larger >= smaller: larger -= smaller iterations += 1print(f"Iterations taken: {iterations}")print(f"Remainder: {larger}")