Solve worksheet

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Solve Worksheet Chapter 7,8 & 11

Chapter: 7
A. Fill in the blanks :

1. ____________ construct is used in situations where a user can decide among multiple conditions.

Answer: `if-elif-else`.

2. ____________An "if" statement must end with a

Answer: colon (`:`).

3. The statement X += 1 is equivalent to ____________.

Answer: `X = X + 1`.

4. The statement 5%2 will evaluate to ____________ and 5/2 will evaluate to ____________.

Answer: `1`, `2.5`.

5. ____________ is used to exit the Python Shell.

Answer: `exit()` or `quit()`.


B. Spot and correct the errors in the following programs :

1. Original Code:
Corrected Code:
```python
```python
Value=40
Value = 40
If Value>=25
if Value >= 25:
print("Smaller Value)
print("Smaller Value")
print "Program Over"
print("Program Over")
```
```

2. Original Code: Corrected Code:

```python ```python

A=10,B=20 A = 10

if (15==A) B = 20

print("A";A,"B=";B) if 15 == A:

``` print("A =", A, "B =", B)

```

Corrected Code:
3. Original Code:
```python
```python
A = input('Enter a number: ')
input('Entera number, A)
print('The number is', A)
Print ('The number is' A)
```
```

4. Original Code:
Corrected Code:
```python
```python
Num1=input()
Num1 = int(input("Enter first number: "))
Num2=int(input())
Num2 = int(input("Enter second number: "))
print(Num1+Num2)
print(Num1 + Num2)
```
C. Write Python programs :

1. Accept the length and the breadth of a rectangle 2. Accept three numbers from the user and display
from the user and calculate its area and perimeter. the largest number.
```python
length = float(input("Enter the length of the rectangle: ")) num1 = float(input("Enter the first number: "))
breadth = float(input("Enter the breadth of the rectangle: ")) num2 = float(input("Enter the second number: "))
area = length breadth num3 = float(input("Enter the third number: "))
perimeter = 2 (length + breadth) largest = max(num1, num2, num3)
print("area=",area)
print("perimeter=",perimeter) print("largest=",largest)

3. Accept a number from the user and check if it is an odd or even number.

```python

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

if num % 2 == 0:
print("number is an even number.")

else:
print("number is an odd number.")
D. Answer the following questions :

1. Give any two features of the Python language.


• Feature 1: Easy to Learn and Use
Python has a simple and clean syntax, making it beginner-friendly. It allows users to write code in fewer lines
compared to other programming languages.
Example:
print("Hello, World!") # This prints a simple message
• Feature 2: Dynamically Typed Language
In Python, you don't need to declare variable types explicitly. The type is determined at runtime.
Example:
x = 10 # x is treated as an integer
x = "Hello" # Now x is treated as a string
print(x) # Output: Hello
________________________________________

2. Explain the difference between = and == with an example.


• = (Assignment Operator): It is used to assign a value to a variable.
• == (Equality Operator): It is used to compare two values and returns True if they are equal, otherwise False.
Example:
x = 5 # Assigns the value 5 to the variable x
y = 10

print(x == y) # Compares x and y. Output: False (5 is not equal to 10)

x=y # Assigns the value of y to x


print(x) # Output: 10

________________________________________

3. What are relational operators? Give examples.


Relational operators are used to compare two values. They return a boolean value (True or False) depending
on the condition.
Relational Operators:
• >: Greater than
• <: Less than
• >=: Greater than or equal to
• <=: Less than or equal to
• ==: Equal to
• !=: Not equal to
Example:
a = 20
b = 15

print(a > b) # True, because 20 is greater than 15


print(a < b) # False, because 20 is not less than 15
print(a >= b) # True, because 20 is greater than or equal to 15
print(a != b) # True, because 20 is not equal to 15
4. Identify which of the following are valid/invalid variable names. Provide reasons for invalid names.
a. Num%: Invalid
Reason: Variable names cannot contain special characters like %.
Example:
# Invalid
# Num% = 100 # This will cause a syntax error
________________________________________

b. First Name: Invalid


Reason: Variable names cannot contain spaces. Use an underscore _ instead.
Example:
# Invalid
# First Name = "John" # This will cause a syntax error

# Valid
First_Name = "John"
________________________________________
print(First_Name) # Output: John

c. Value1: Valid
Reason: Variable names can contain letters, digits, and underscores, but cannot start with a digit.
Example:
Value1 = 50
print(Value1) # Output: 50
________________________________________
d. Int: Valid
Reason: While it is valid, using Int (or other built-in names) as a variable name is discouraged because it may
create confusion or overwrite built-in functions.
Example:
Int = 5
print(Int) # Output: 5
________________________________________
e. Stock: Valid
Reason: It follows all the rules for valid variable naming.

________________________________________
Example:
Stock = 100
print(Stock) # Output: 100

5. How do you accept data from a user? Give the syntax with an example.
To accept input from a user in Python, you use the input() function. It always returns the input as a string, so you
may need to convert it to another data type (e.g., int, float) if needed.
Syntax:
variable = input("Enter your prompt: ")
Example:
# Accepting a string input
name = input("Enter your name: ")

# Accepting an integer input


age = int(input("Enter your age: "))

# Printing the inputs


print("Hello, " + name + "! You are " + str(age) + " years old.")
Output (when the user provides input):
Enter your name: Alice
Enter your age: 25
Hello, Alice! You are 25 years old.
Chapter: 11

A. Fill in the blanks (Questions and Answers):

1. You can speed up your computer by running the ____________ utility.


Answer: Disk cleanup.
2. The restarting of a computer is called ____________.
Answer: Rebooting.

3. Many printer models have a built-in ____________ option.


Answer: Troubleshooting.

4. You check the power supply points for ____________ related problems.
Answer: Hardware.

5. Disk defragmenter is available in ____________.


Answer: Windows Accessories.
B. True or False (Questions and Answers):

1. Troubleshooting is the identification of a problem in a system caused by a failure of some kind.


Answer: True.

2. It is not important to take proper care of your computer.


Answer: False.

3. The three areas to troubleshoot are: hardware, software, and operating system.
Answer: True.

4. If your Windows restarts without warning, then there is a problem in the computer hardware.
Answer: True.

5. Proper maintenance will increase the speed and the life of your PC.
Answer: True.

C. Identify the troubleshooting areas for each of the following problems (Questions and Answers):

1. Windows stops responding.


Answer: Software or operating system.

2. The system crashes when you are working with a specific software.
Answer: Software.

3. Power failure in the computer.


Answer: Hardware.

4. You are unable to install a program.


Answer: Software.

5. Windows restarts without warning.


Answer: Hardware.

6. Windows starts in a safe mode.


Answer: Operating system.

D. Answer the following questions :

1. Give a brief explanation of troubleshooting.


Answer:
Troubleshooting is the process of identifying, analyzing, and resolving issues in a system. It
involves systematically diagnosing problems related to hardware, software, or the operating
system to ensure the smooth functioning of a computer.
2. Why is it important to take proper care of a computer?
Answer:
Proper care of a computer helps to:

o Avoid hardware failures and costly repairs.

o Maintain smooth operation of software.

o Extend the computer's lifespan.

o Enhance overall performance and reliability.

3. What are the areas to troubleshoot?


Answer:

o Hardware: Issues like power failures or faulty connections.

o Software: Errors or bugs in applications.

o Operating System: Problems in system files or configurations.

4. Write about two common problems related to software that you might have experienced.
Answer:

o Application not responding: When a program freezes or crashes, often due to


insufficient system resources or bugs.

o Installation failure: Occurs when a program cannot be installed due to incompatibility,


corrupted files, or insufficient disk space.

5. Give troubleshooting steps for an error message displayed on the monitor.


Answer:

o Read and note down the exact error message.

o Restart the system and check if the issue is resolved.

o Search online for the specific error message to find solutions.

o Update or reinstall the problematic software or driver.

o If unresolved, contact technical support or a professional technician.


Chapter: 8

A. Fill in the blanks :

1. The ________ statement skips the rest of the loop statements and causes the next iteration of the
loop to take place.

Answer: `continue`.

2. The ________ statement enables a program to skip over a part of the ________.

Answer: `break`, loop.

3. The ________ part of the while loop is executed only when the while loop completes all its iterations.

Answer: `else`.

4. Condition is checked for True or False, and the statements are executed only if the condition is
________.

Answer: `True`.

5. The ________ loop is helpful when the number of iterations is not known prior to the execution of the
loop.

Answer: `while`.

B. Convert the following loops as directed (5 marks each):

1. Convert `while` to `for`: ```


Original Code: Converted Code:
```python ```python
x=5 for x in range(5, 10, 2):
while x < 10: print(x + 10)
print(x + 10) ```
x += 2
```
2. Convert `for` to `while`:
Converted Code:
Original Code:
```python
```python
x=5
for x in range(5, 20, 5):
while x < 20:
print(x)
print(x)

x += 5

```

C. Give the output for the following codes (5 marks each):

1. Code:

```python

Num = 0 Output:

while Num < 10: `1234`

Num += 1

if Num == 5:

break

print(Num, end=)

```

Output:
2. Code:
S
```python
t
for val in String:
r
if val == l:
i
break
n
print(val)
g
print(Over)
Over
D. Answer the following questions (5 marks each):

1. What is the significance of using iterations in Python?

Answer:

Iterations are used to repeat a block of code multiple times, making the program efficient and concise.
They help automate repetitive tasks, such as traversing data structures like lists or ranges. Loops like `for`
and `while` enable decision-making and control flow in programs. Iterations also make it easy to perform
calculations or process data sets. Python's loops are versatile and can handle nested or conditional
structures.

2. Explain the `range()` function with the help of an example.

Answer:

The `range()` function generates a sequence of numbers. It accepts up to three parameters: start
(inclusive), stop (exclusive), and step (increment).

Example:

```python

for i in range(1, 6, 2):

print(i)

```

Output: `1 3 5`.

3. Explain the difference between `for` and `while` loops.

Answer:

- A `for` loop is used when the number of iterations is known beforehand.

- A `while` loop is used when the condition for repetition is dynamic or not known beforehand.

- `for` loops iterate over sequences like lists or ranges, while `while` loops repeat as long as a condition
evaluates to `True`.

- `for` loops are concise and easier to read for finite iterations.

- `while` loops are better for conditional or infinite looping scenarios.


4. What are jump statements?

Answer:

Jump statements alter the flow of loops in Python. The three main jump statements are:

- `break`: Exits the loop prematurely when a condition is met.

- `continue`: Skips the rest of the current iteration and proceeds to the next iteration.

- `pass`: Acts as a placeholder and does nothing.

These statements help in controlling loop execution dynamically.

You might also like