Home → Resource

10th – Computer Science – Chapter 4 – Control Structures in Python

MCQs

  1. What is the output of the following code? count = 0; for i in range(2): for j in range(3): count += 1; print(count)
    a) 4 b) 5 c) 6 d) 7
  2. What does list.remove() remove?
    a) All items b) The first occurrence of the specified item c) The last item d) Duplicates only
  3. Which Python module contains the mean() function?
    a) random b) math c) statistics d) datetime
  4. What type of error does wrong indentation in Python cause?
    a) SyntaxError b) IndentationError c) TypeError d) ValueError
  5. Which statement is used to run code when a condition is false?
    a) if b) elif c) else d) while
  6. Which method adds an item at the end of a list in Python?
    a) insert() b) append() c) add() d) extend()
  7. What is an infinite loop?
    a) A loop that runs exactly 10 times b) A loop that never terminates c) A loop that is inside another loop d) A loop that uses range()
  8. What is the function of range() in Python?
    a) Generates a list of numbers b) Creates a sequence of numbers c) Calculates the sum of numbers d) Prints a range of numbers
  9. Which keyword is used to start a while loop in Python?
    a) loop b) repeat c) while d) do
  10. Which library is used for generating random numbers?
    a) Datetime b) statistics c) random d) math
  11. What is range() used for?
    a) To create a list of numbers b) To generate a sequence of numbers c) To calculate the range of values d) To sort numbers
  12. Which debugging technique involves adding output statements to the code?
    a) Unit testing b) Print statements c) Error messages d) Integration testing
  13. How do you import the random module in Python?
    a) use random b) import random c) load random d) require random
  14. What does the continue statement do inside a loop?
    a) Ends the loop b) Exits the program c) Repeats the same iteration d) Skips the rest of the current iteration and moves to the next
  15. What does the break statement do inside a loop?
    a) Skips the current iteration b) Restarts the loop c) Pauses the loop d) Exits the loop immediately
  16. How do you access the second element of a list named ‘fruits’?
    a) fruits[2] b) fruits[1] c) fruits(1) d) fruits.get(2)
  17. Which of the following describes a nested loop?
    a) A loop with no condition b) A loop placed inside another loop c) A loop that runs only once d) A loop that uses while only
  18. Which keyword is used to define a function in Python?
    a) function b) func c) define d) def
  19. Why are control structures used in programming?
    a) Only to make code longer b) To manage decision-making and repetition of tasks c) To create variables d) To import libraries
  20. How can you stop print() from adding a newline?
    a) Use end=”” b) Use newline=False c) Use break d) Use continue
  21. What does the len() function return?
    a) The first element b) The number of items in a list or string c) The last element d) The sum of all items
  22. For what purpose does open(‘file. txt ’, ’ r ’) open a file?
    a) Reading b) Writing c) Deleting d) Appending
  23. What does the ‘elif’ keyword mean?
    a) else if – checks another condition if the previous was false b) end of loop c) equal loop d) exit loop if
  24. What is exception handling in Python used for?
    a) Speeding up code b) Handling runtime errors gracefully c) Compressing code d) Increasing memory
  25. Which values does range(0, 5) generate?
    a) 0,1,2,3,4,5 b) 0,1,2,3,4 c) 1,2,3,4,5 d) 1,2,3,4
  26. What does list.sort() do in Python?
    a) Removes duplicates b) Returns the length c) Reverses the list d) Sorts the list in place
  27. What is the term for testing individual functions separately?
    a) Unit testing b) Integration testing c) Regression testing d) Functional testing
  28. What is the function of list.append()?
    a) Removes the first item b) Adds an item to the end c) Sorts the list d) Reverses the list
  29. Which values does range(2, 10, 3) generate?
    a) 2, 5, 8 b) 2, 4, 6, 8 c) 3, 6, 9 d) 2, 3, 4, 5
  30. Which of the following best describes a class in Python OOP?
    a) A type of loop b) A blueprint for creating objects c) A module d) A function

Short Questions

(i) Name three list methods and their purposes.
(ii) How do you create a list in Python and add items to it?
(iii) What does list.index() return?
(iv) How do you import a library in Python?
(v) What is a nested loop? Give a practical example of its use.
(vi) What is the difference between regression testing and integration testing?
(vii) How can we access an element from the list?
(viii) Explain the use of the range() function in a for loop.
(ix) What are the four types of testing discussed in the chapter?
(x) What is an infinite loop, and how can it be avoided?
(xi) What is the difference between a while loop and a for loop?
(xii) What is unit testing and why is it important?
(xiii) What are the three parameters of the range() function?
(xiv) What is random.randint() used for?
(xv) What is OOP (Object-Oriented Programming) in Python?
(xvi) What is the difference between list.pop() and list.remove()?
(xvii) What is the difference between the append and remove methods in Python lists?
(xviii) What is the difference between break and continue?
(xix) How does range() work with start, stop, and step parameters?
(xx) What is the purpose of indentation in Python?
(xxi) What is a module in Python, and how do you import one?
(xxii) How does the end parameter in print() work? Give an example.
(xxiii) What is functional testing in programming?
(xxiv) What is the pdb debugger used for in Python?
(xxv) How do you access the third element in a list?
(xxvi) How do you read a file in Python? Write the code.
(xxvii) How do you define and call a function in Python? Give a simple example.
(xxviii) Define debugging.
(xxix) How do you modify an existing item in a list?
(xxx) Name three list methods and explain what each does.
(xxxi) What is the purpose of the end parameter in print()?
(xxxii) What is exception handling,g and how is it written in Python?

Long Questions

  1. Write code to print stars (asterisks) in a rectangular pattern using nested loops with three rows and five columns.
  2. Write a Python program that: (a) Generates a list of 5 random numbers between 1 and 20. (b) Uses the statistics library to calculate and print the mean of the generated numbers.
  3. Write a Python program that: (a) Creates a list: [“Biryani”, “Nihari”, “Karahi”, “Seekh Kebabs”]. (b) Add “Quorma”. (c) Change “Karahi” to “Chicken Karahi”. (d) Remove “Nihari”.
  4. How do Python libraries enhance programming efficiency? Provide an example.
  5. Explain the different types of decision-making statements in Python with examples for each.
  6. Explain the importance of testing and debugging in programming. Discuss different debugging techniques.
  7. Write the output of the following code: print(“=== Multiplication Table (1-5) ===”); for i in range(2, 4): for j in range(1, 11): print(f”{i} x {j} = {i*j:2d}”, end=” “); print()
  8. Write a Python program using a loop that prints all the odd numbers between 1 and 20.
  9. Compare while loops and for loops.
  10. Describe how to work with Python lists, including creation and modification. Provide code examples for adding items, removing items, and accessing elements.

Answer Key

  1. c) 6
  2. b) The first occurrence of the specified item
  3. c) statistics
  4. b) IndentationError
  5. c) else
  6. b) append()
  7. b) A loop that never terminates
  8. b) Creates a sequence of numbers
  9. c) while
  10. c) random
  11. b) To generate a sequence of numbers
  12. b) Print statements
  13. b) import random
  14. d) Skips the rest of the current iteration and moves to the next
  15. d) Exits the loop immediately
  16. b) fruits[1]
  17. b) A loop placed inside another loop
  18. d) def
  19. b) To manage decision-making and repetition of tasks
  20. a) Use end=””
  21. b) The number of items in a list or string
  22. a) Reading
  23. a) else if – checks another condition if the previous was false
  24. b) Handling runtime errors gracefully
  25. b) 0,1,2,3,4
  26. d) Sorts the list in place
  27. a) Unit testing
  28. b) Adds an item to the end
  29. a) 2, 5, 8
  30. b) A blueprint for creating objects

Leave a Reply

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