Python Programming

Computer Science, HSSC-Punjab First Year. This chapter is split into 73 topics and has 1,753 practice questions. The topics covered are listed below, followed by 10 free sample questions from the chapter.

Topics in this chapter

  1. Logical operators and truth tables 23 questions
  2. Modifying a list: assignment and append 21 questions
  3. Accessing list items by index 24 questions
  4. Variable scope and functions calling functions (standard knowledge) 23 questions
  5. Slicing strings 23 questions
  6. Function parameters and return values 23 questions
  7. Assignment and compound assignment operators 23 questions
  8. Variable naming rules 30 questions
  9. Slicing lists and sequences 24 questions
  10. Loops with range: sums and patterns 20 questions
  11. Operator precedence rules 24 questions
  12. Conditions that combine comparison and logic 26 questions
  13. Default parameter values 22 questions
  14. break, continue, nested loops and loop counts (standard knowledge) 20 questions
  15. The if-elif-else statement 28 questions
  16. Exception handling: concepts 23 questions
  17. Integrated programs: functions with loops and lists 24 questions
  18. return versus print in functions 23 questions
  19. First program and the print function 25 questions
  20. Case sensitivity and name errors 24 questions
  21. Variables: storing and changing values 23 questions
  22. Lists: creating and understanding 23 questions
  23. Setting up Python and choosing an IDE 25 questions
  24. Chained conditional expressions (exercise style) 27 questions
  25. Programming basics and the four steps 23 questions
  26. Evaluating expressions with precedence 30 questions
  27. Short-hand if-else (conditional expression) 24 questions
  28. Slicing with a step 26 questions
  29. while loops: sums, counters and class activities 21 questions
  30. Negative indexing and slicing 23 questions
  31. Addition, subtraction, multiplication and division traces 25 questions
  32. List methods: remove, sort and reverse 23 questions
  33. Arithmetic operators: symbols and meanings 24 questions
  34. Floor division and modulus traces 29 questions
  35. Functions with lists: maximum, total and counting 23 questions
  36. The if statement 24 questions
  37. List operations: concatenation, repetition and length 23 questions
  38. The for loop over sequences 21 questions
  39. Identifying the error raised by a statement 29 questions
  40. Integrated programs: classes, files, strings and exceptions 23 questions
  41. Exponentiation and powers 24 questions
  42. The if-else statement 27 questions
  43. File handling: opening, reading and writing 29 questions
  44. Exercise-style programs: sign check and odd numbers 23 questions
  45. The while loop 21 questions
  46. Tracing the ToyCar class 23 questions
  47. Input and output with input() and print() 22 questions
  48. Comparison operators 30 questions
  49. Class activities: BMI and arithmetic programs 20 questions
  50. Control structures: decisions and loops 20 questions
  51. Reserved keywords and meaningful names 25 questions
  52. Defining and calling functions 26 questions
  53. Classes and objects: concepts 24 questions
  54. Data types: int, float, str and bool 21 questions
  55. Strings as sequences: indexing 27 questions
  56. String operations: joining, repeating and length 24 questions
  57. The range() function 27 questions
  58. Lists versus tuples 21 questions
  59. Writing and tracing other classes (standard knowledge) 22 questions
  60. Importing and using libraries and modules 25 questions
  61. Parentheses and the order of evaluation 25 questions
  62. Type conversion and mixed data types 25 questions
  63. Exercise-style expression evaluation 22 questions
  64. Packages and module structure 22 questions
  65. Tracing try and except 25 questions
  66. Modular programming and the main function 21 questions
  67. Translating mathematical expressions to Python 24 questions
  68. Testing and debugging 24 questions
  69. Reviewing functions, modules and classes in questions 27 questions
  70. Python comments 24 questions
  71. String methods (standard knowledge) 23 questions
  72. Tuples: creation, indexing and immutability 24 questions
  73. Python introduction and applications 24 questions

Sample questions: Python Programming

10 sample questions with the correct answer and an explanation. Try to answer each one before reading the explanation.

Q1. fruits = ["Apple", "Banana", "Cherry", "Date", "Elderberry"] print(fruits[::-1]) What is the output of the Python code above?

  • A. ['Banana', 'Apple']
  • B. ['Apple', 'Banana', 'Cherry', 'Date', 'Elderberry']
  • C. ['Elderberry', 'Cherry', 'Apple']
  • D. ['Elderberry', 'Date', 'Cherry', 'Banana', 'Apple'](correct answer)

Answer: D.The step controls the stride between selected items (a negative step walks backward); the slice gives ['Elderberry', 'Date', 'Cherry', 'Banana', 'Apple'].

Q2. fruits = ["Apple", "Banana", "Cherry", "Date", "Elderberry"] print(fruits[4:0:-1]) What does the program above display?

  • A. ['Elderberry', 'Cherry', 'Apple']
  • B. ['Elderberry', 'Date', 'Cherry', 'Banana'](correct answer)
  • C. ['Banana', 'Apple']
  • D. ['Apple', 'Banana', 'Cherry', 'Date', 'Elderberry']

Answer: B.The step controls the stride between selected items (a negative step walks backward); the slice gives ['Elderberry', 'Date', 'Cherry', 'Banana'].

Q3. tup = ("Math", "Science", "English", "History", "Geography") print(tup[::-1]) Which output does the snippet above produce?

  • A. ('Science', 'Math')
  • B. ('Math', 'Science', 'English', 'History', 'Geography')
  • C. ('Geography', 'History', 'English', 'Science', 'Math')(correct answer)
  • D. ('Geography', 'English', 'Math')

Answer: C.The step controls the stride between selected items (a negative step walks backward); the slice gives ('Geography', 'History', 'English', 'Science', 'Math').

Q4. for i in range(1, 10):     if i == 4:         break     print(i) What does the program above display? (Each option lists the printed lines in order.)

  • A. 1, 2, 3(correct answer)
  • B. 1, 2, 3, 4
  • C. 1, 2, 3, 4, 5, 6, 7, 8, 9
  • D. 1, 2, 3, 5, 6, 7, 8, 9

Answer: A.break leaves the loop, continue skips to the next round, and nested loops multiply their counts; the output is 1, 2, 3 (standard knowledge).

Q5. for i in range(1, 6):     if i == 3:         continue     print(i) What is printed when the code above is run? (Each option lists the printed lines in order.)

  • A. 1, 2, 3, 4, 5
  • B. 5, 4, 2, 1
  • C. 1, 2, 4, 5(correct answer)
  • D. 1, 2, 4

Answer: C.break leaves the loop, continue skips to the next round, and nested loops multiply their counts; the output is 1, 2, 4, 5 (standard knowledge).

Q6. for i in range(3):     for j in range(2):         print(i, j) Which output does the snippet above produce? (Each option lists the printed lines in order.)

  • A. 0 0, 1 0, 0 1, 1 1, 0 2, 1 2
  • B. 0 0, 1 0, 2 0
  • C. 0 0, 0 1, 0 2, 1 0, 1 1, 1 2
  • D. 0 0, 0 1, 1 0, 1 1, 2 0, 2 1(correct answer)

Answer: D.break leaves the loop, continue skips to the next round, and nested loops multiply their counts; the output is 0 0, 0 1, 1 0, 1 1, 2 0, 2 1 (standard knowledge).

Q7. for i in range(1, 4):     for j in range(i):         print("*", end="")     print() What does the program above display? (Each option lists the printed lines in order.)

  • A. , *, **, ***
  • B. *, **, ***(correct answer)
  • C. *, *, *, *, *, *
  • D. ***, ***, ***

Answer: B.break leaves the loop, continue skips to the next round, and nested loops multiply their counts; the output is *, **, *** (standard knowledge).

Q8. for i in range(5):     if i % 2 == 0:         continue     print(i) What is printed when the code above is run? (Each option lists the printed lines in order.)

  • A. 0, 2, 4
  • B. 0, 1, 2, 3, 4
  • C. Nothing is printed
  • D. 1, 3(correct answer)

Answer: D.break leaves the loop, continue skips to the next round, and nested loops multiply their counts; the output is 1, 3 (standard knowledge).

Q9. total = 0 for i in range(1, 100):     total += i     if total > 10:         break print(total, i) Trace the code above and choose what it prints.

  • A. 21 6
  • B. 10 4
  • C. 4950 99
  • D. 15 5(correct answer)

Answer: D.break leaves the loop, continue skips to the next round, and nested loops multiply their counts; the output is 15 5 (standard knowledge).

Q10. for i in range(2):     print("a") else:     print("done") What is the output of the Python code above? (Each option lists the printed lines in order.)

  • A. a, a, a, done
  • B. a
  • C. a, a, done(correct answer)
  • D. a, done

Answer: C.break leaves the loop, continue skips to the next round, and nested loops multiply their counts; the output is a, a, done (standard knowledge).

Take a full practice exam on this chapter

Mix every topic of the chapter, get instant explanations, and track your accuracy over time.

Create a free account
An unhandled error has occurred. Reload 🗙