Chap 2 Solved Exercise


Multiple Choice Questions

1. An action needed during Python installation to run from the command line easily:
a) Uncheck “Add Python to PATH”
b) Choose a different IDE
c) Check “Add Python to PATH”
d) Install only the IDE

2. A valid variable name in Python is:
a) variable1
b) 1variable
c) variable-name
d) variable name

3. Output of the following piece of code is:

age = 25
print("Age:", age)

a) Age: 25
b) 25
c) Age
d) age

4. The operator used for exponentiation in Python is:
a) ^
b) **
c) //
d) /

5. A loop used to iterate over a collection such as lists is:
a) while
b) for
c) do-while
d) repeat

6. A range() function used to generate a sequence of numbers:
a) Generates a list of numbers
b) Creates a sequence of numbers
c) Calculates the sum of numbers
d) Prints a range of numbers

7. A keyword used to define a function in Python is:
a) def
b) func
c) function
d) define

8. Output of the following code is:

temp = 25
humidity = 60
if temp > 30 and humidity < 50:
    print("Hot")
elif temp <= 25 and humidity > 70:
    print("Cool and breezy")
elif temp <= 20 and humidity > 30:
    print("Cool")
else:
    print("Moderate")

a) Cool
b) Warm
c) Moderate
d) Nothing

9. The operation used to combine lists in Python:
a) +
b) concat()
c) append()
d) merge()



1. Explain the purpose of using comments in Python.
Ans: Comments are used to explain code, make it easier to read, and disable code for testing/debugging.

2. Describe the difference between integer and float data types in Python. Provide an example.
Ans:

  • Integer: Whole numbers without decimal (e.g., age = 15).
  • Float: Numbers with decimal (e.g., price = 99.9).

3. Define operator precedence and give an example.
Ans: Operator precedence is the order in which operations are performed.
Example:

result = 3 + 5 * 2
print(result)  # Output: 13

4. How does the short-hand if-else statement differ from the regular if-else statement?
Ans:

  • Short-hand if-else: Written in a single line.
  • Regular if-else: Written in multiple lines.

5. Explain the use of the range() function in a for loop in Python.
Ans: The range() function generates a sequence of numbers, making it easier to loop through values.

6. Explain parameter with default values in Python functions.
Ans: A default parameter is a value assigned if no argument is passed.
Example:

def greet(name="Student"):
    print("Hello", name)

7. Explain why modular programming is useful in Python.
Ans:

  • Improved code organization
  • Code reusability
  • Easy debugging and maintenance
  • Collaboration and scalability
  • Simplified management

Q.8. Explain the difference between a class and an object in Python.
Ans:

  • Class:
    • A blueprint or template for creating objects.
    • Defines properties (attributes) and behaviors (methods) that the objects will have.
    • Created once.
    • Occupies no memory until an object is created.
  • Object:
    • An instance of a class, created using the class blueprint.
    • Represents individual data and can use the methods defined in class.
    • Created many times.
    • Occupies memory when created.

Example:

class Car:  
    def __init__(self, color, model):  
        self.color = color  
        self.model = model  

    def start(self):  
        return f"The {self.color} {self.model} is starting."
myCar = Car("Red", "Toyota")  
print(myCar.start())  
# Output: The Red Toyota is starting.



Long Questions

Q.3. Explain the concept of variables in Python.
Ans:
Variables are used to store values in a program.

  • Example: x = 10 assigns integer value 10 to variable x.
  • Variables can change values during program execution.

Q.4. Write a program that asks the user to input a number and checks whether it is positive, negative, or zero using an if-elif-else statement.

num = int(input("Enter a number: "))

if num > 0:
    print("The number is positive.")
elif num < 0:
    print("The number is negative.")
else:
    print("The number is zero.")

Q.5. Write a Python program using a while loop that prints all the odd numbers between 1 and 100. Also, count and print the total number of odd numbers.

# Initialize variables
count = 0
number = 1

# While loop to iterate through numbers 1 to 100
while number <= 100:
    # Check if the number is odd
    if number % 2 != 0:
        print(number, end=", ")
        count += 1
    number += 1

# Print the total number of odd numbers
print("\nTotal number of odd numbers between 1 and 100:", count)

Leave a Reply

Your email address will not be published. Required fields are marked *