Learn/ 10th Computer Science/ Unit 4 /Short Questions

Unit 4: Control Structures in Python — Short Questions

10th Class Computer Science · Unit 4: Unit 4: Control Structures in Python

Decision Making

1.What is a decision-making structure in programming?

Decision making in programming allows the program to choose different actions based on conditions. This is similar to how we make decisions in real life. Python provides a variety of conditional statements to implement decision-making.

2.What are decision-making structures in Python?

In Python, decision-making structures refer to control flow constructs that allow a program to make decisions based on conditions. These structures determine the execution path of the code.

3.What are the main types of decision-making structures in Python?

The main types of decision-making structures in Python are given below:
1 if Statement
2. if-else Statement
3. Nested Conditions

4.What is an if statement and its syntax?

The if statement lets us make decisions based on conditions. If the condition is true, it runs a block of code. In Python, indentation defines the structure block or scope of your code. Proper indentation is mandatory. Incorrect indentation may cause an error (Indentation Error).
Syntax
if condition:
code to run if the condition is true

5.What is an if-else statement and its syntax?

The if-else statement allows us to execute one block of code if a condition is true and another block if the condition is false. It is also known as two-way decision-making structure.
Syntax
if condition:
code to run if the condition is true
else:
code to run if the condition is false

6.What are nested conditions in Python?

Nested conditions occur when one condition is placed inside another condition. They are used when you need to check multiple related conditions, allowing more precise control over the program's flow.

Looping Constructs

7.What is a looping structure in programming?

A looping structure in programming allows a set of instructions to be executed repeatedly until a specific condition is met. It helps reduce code repetition and is commonly used when performing repetitive tasks.

8.How many types of Looping Structure?

There are two types of loops, which are given below:

  • while loop
  • for loop
9.Define while loop.

A while loop runs as long as a condition is true. It checks the condition before each iteration and stops running when the condition is no longer true.

10.How does a 'while' loop differ from a 'for' loop?

while repeats as long as a condition is true (e.g., counting to 10), while 'for' iterates over a sequence (e.g., list items).

11.Define for loop.

A for loop repeats a block of code for a specific number of times. It is commonly used to iterate over a sequence (like a list, tuple, or string).

12.What is the range() function used in for loops?

In Python, the range() function is used to generate a sequence of numbers. It is commonly used in for loops to repeat actions for a specific number of times.
Syntax:

  • range(stop)
  • range(start, stop)
  • range(start, stop, step)

Short Questions

13.What are the use of end statement in Python?

In Python, the end statement is not a separate command, but rather an optional parameter of the print() function. By default, every print() ends with a newline character, but you can change what gets printed at the end using the end parameter.

14.How are strings represented in Python? Give example.

Strings in Python are surrounded by either single quote ' ' or double quotes " " Both are treated the same. Example:
name1='Arshad'
name2="Faisal"
print(name1) # Output: Arshad
print(name2) # Output: Faisal

15.What is a nested loop?

Nested loops are loops inside other loops. For each iteration of the outer loop, the inner loop runs completely. We use nested loops when we need to perform repetitive tasks within tasks.

Libraries in Python

16.What are Python libraries?

In Python, libraries are like toolboxes full of useful tools that help you solve different problems without having to build everything from scratch.

17.How do you import and use libraries in Python?

Libraries are like pre-built toolkits that you can use without having to write all the code yourself.
Example: Random library
import random
number=random.randint(1,10)
print("The random number is:", number)

18.Why should you be careful when importing libraries in Python, and where can you find more information?

When importing libraries in Python, you should only import what you need to avoid unnecessary memory usage and keep your code efficient. You can explore more Python libraries and their documentation at: https://docs.python.org/3/

Lists in Python

19.What is a list in Python?

In Python, a list is a versatile data structure that can hold a collection of items. You can create, access, and modify lists easily.

20.How do you create a list? Give an example.

A list is created by placing items inside square brackets [ ], separated by commas. Lists can contain items of different types, such as numbers, strings, or even other lists.
Example
fruits = ["Mango", "Apple", "Banana"]
print(fruits)
Output: ['Mango', 'Apple', 'Banana']

21.How do you access items in a list? Give an example.

You can access items in a list by referring to their index, starting from 0.
Example
fruits = ["Mango", "Apple", "Banana"]
print(fruits[1])

22.How do you modify items in a list? Give an example.

You can modify list items by accessing them via their index and assigning a new value.
Example
fruits=["Mango","Apple","Banana"]
fruits[0]="Orange"
fruits.append("Pineapple")
print(fruits)
Output: ['Orange', 'Apple', 'Banana','Pineapple']

23.What are some common operations/methods on lists?

Methods and Operations on Lists

  • append(item): Adds an item to the end of the list.
  • remove(item): Deletes the first time that item appears in the list.
  • sort(): Arranges the list in order, from smallest to biggest or ascending order.
  • reverse(): Flips the list so the last item comes first.
24.Write the example of adding and sorting list of students.

students = ["Ahmed", "Sara", "Ali"]
students.append("Hina")
students.sort()
print(students)
Output: ['Ahmed','Ali','Hina', 'Sara']

25.Write the example of removing a student from a list.

students = ["Ahmed", "Sara", "Ali","Hina","Sara"]
students.remove("Sara")
print(students)

26.Write the example of sorting a list of numbers.

numbers=[34,12,89,5,23]
numbers.sort()
print(numbers)
Output: [5,12,23,34,89]

27.Write the example of reversing a list.

fruits = ["Apple", "Banana", "Orange", "Mango"]
fruits.reverse()
print(fruits)
Output: ['Mango', 'Orange', 'Banana', 'Apple']

Testing and Debugging in Python

28.What is testing in Python programming?

Testing is the process of running your code with various inputs to check if it behaves as expected. The goal is to find and fix any issues before the code is used in real-world applications.

29.Write the main types of testing in Python.
  • Unit Testing: Tests individual parts of the code (like functions or classes) in isolation. Python's unittest module is commonly used.
  • Integration Testing: Checks how different parts of the code work together.
  • Functional Testing: Validates that the software behaves as expected from the user's perspective.
  • Regression Testing: Ensures that new changes don't break existing functionality.
30.What is debugging in Python?

Debugging is the process of finding and fixing errors (bugs) in your code. It involves identifying the root cause of problems and making the necessary changes.

31.Write some common debugging techniques in Python.
  • Print Statements: Adding print statements to check the values of variables at different stages of the code.
  • Debugging Tools: Using tools like pdb (Python Debugger) to step through the code, inspect variables, and understand the flow of execution.
  • Error Messages: Reading and interpreting error messages to locate the source of the problem.

Predict the Output

32.Predict the output:

x=7
if x%2==0:
print("Even")

Output Even

33.Predict the output:

x=3
if x>5:
print("Yes")
else:
print("No")

Output No

34.Predict the output:

x=10
if x%2==0:
print("Even")

Output Even

35.Predict the output:

x=9
if x%2==0:
print("Even")
else:
print("Odd")

Output Odd

36.Predict the output:

x=4
y=6
if x<y:
print("X")

Output X

37.Predict the output:

x=8
y=3
if x<y:
print("X")
else:
print("Y")

Output Y

38.Predict the output:

x=5
if x>2:
if x<10:
print("In Range")

Output In Range

39.Predict the output:

x=6
if x>5:
if x%2==0:
print("Even Big")

Output Even Big

40.Predict the output:

x=7
if x>5:
if x%2==0:
print("Even Big")
else:
print("Odd Big")

Output Odd Big

41.Predict the output:

x=3
if x>5:
print("A")
else:
if x>1:
print("B")

Output B

42.Predict the output:

x=0
if x>=1:
print("True")
else:
print("False")

Output False

43.Predict the output:

x=1
if x==1:
print("True")

Output True

44.Predict the output:

x=-1
if x>0:
print("Positive")
else:
print("Not Positive")

Output Not Positive

45.Predict the output:

x=5
if x==-5:
print("Equal")

Output Equal

46.Predict the output:

x=4
if x!=5:
print("Not Equal")

Output Not Equal

47.Predict the output:

x=10
if x>5:
if x>8:
print("High")
else:
print("Medium")

Output High

48.Predict the output:

x=6
if x>5:
if x>8:
print("High")
else:
print("Medium")

Output Medium

Find the Errors

49.Find the error:

x=5
if x>3
print("A")
Syntax Error: Missing colon: after if
Fix:
x=5
if x>3:
print("A")

50.Find the error:

x=2:
if x>3:
print("A")
Indentation Error: print not indented
Fix:
x=2
if x>3:
print("A")

51.Find the error:

x=7
if x>5
print("Yes")
else:
print("No")
Syntax Error & Indentation Error: missing colon and wrong indentation
Fix:
x=7
if x>5:
print("Yes")
else:
print("No")

52.Find the error:

x=10
if x%2=0:
print("Even")
Syntax Error: used `=` instead of `==`
Fix:
x=10
if x%2==0:
print("Even")

53.Find the error:

x=4
if x<5
print("Less")
else
print("More")
Syntax Error: missing colons after if and else
Fix:
x=4
if x<5:
print("Less")
else:
print("More")

54.Find the error:

x=5
if x>2:
if x<10:
print("In Range")
Syntax Error: missing colon after inner if
Fix:
x=5
if x>2:
if x<10:
print("In Range")

55.Find the error:

x=7
if x>5
if x%2==0:
print("Even Big")
else:
print("Odd Big")
Syntax Error: missing colon after outer if
Fix:
x=7
if x>5:
if x%2==0:
print("Even Big")
else:
print("Odd Big")

56.Find the error:

x=0
if x
print("True")
else:
print("False")
Syntax Error: Missing colons after 'if'
Fix:
x=0
if x==0:
print("True")
else:
print("False")

57.Find the error:

x=-1
if x>0:
print("Positive")
else
print("Not Positive")
Syntax Error: missing colon after 'else'
Fix:
x=-1
if x>0:
print("Positive")
else:
print("Not Positive")

58.Find the error:

x=5
if x==5
print("Equal")
Syntax Error: missing colon and indentation error
Fix:
x=5
if x==5:
print("Equal")