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
Logical operators and truth tables23 questions
Modifying a list: assignment and append21 questions
Accessing list items by index24 questions
Variable scope and functions calling functions (standard knowledge)23 questions
Slicing strings23 questions
Function parameters and return values23 questions
Assignment and compound assignment operators23 questions
Variable naming rules30 questions
Slicing lists and sequences24 questions
Loops with range: sums and patterns20 questions
Operator precedence rules24 questions
Conditions that combine comparison and logic26 questions
Default parameter values22 questions
break, continue, nested loops and loop counts (standard knowledge)20 questions
The if-elif-else statement28 questions
Exception handling: concepts23 questions
Integrated programs: functions with loops and lists24 questions
return versus print in functions23 questions
First program and the print function25 questions
Case sensitivity and name errors24 questions
Variables: storing and changing values23 questions
while loops: sums, counters and class activities21 questions
Negative indexing and slicing23 questions
Addition, subtraction, multiplication and division traces25 questions
List methods: remove, sort and reverse23 questions
Arithmetic operators: symbols and meanings24 questions
Floor division and modulus traces29 questions
Functions with lists: maximum, total and counting23 questions
The if statement24 questions
List operations: concatenation, repetition and length23 questions
The for loop over sequences21 questions
Identifying the error raised by a statement29 questions
Integrated programs: classes, files, strings and exceptions23 questions
Exponentiation and powers24 questions
The if-else statement27 questions
File handling: opening, reading and writing29 questions
Exercise-style programs: sign check and odd numbers23 questions
The while loop21 questions
Tracing the ToyCar class23 questions
Input and output with input() and print()22 questions
Comparison operators30 questions
Class activities: BMI and arithmetic programs20 questions
Control structures: decisions and loops20 questions
Reserved keywords and meaningful names25 questions
Defining and calling functions26 questions
Classes and objects: concepts24 questions
Data types: int, float, str and bool21 questions
Strings as sequences: indexing27 questions
String operations: joining, repeating and length24 questions
The range() function27 questions
Lists versus tuples21 questions
Writing and tracing other classes (standard knowledge)22 questions
Importing and using libraries and modules25 questions
Parentheses and the order of evaluation25 questions
Type conversion and mixed data types25 questions
Exercise-style expression evaluation22 questions
Packages and module structure22 questions
Tracing try and except25 questions
Modular programming and the main function21 questions
Translating mathematical expressions to Python24 questions
Testing and debugging24 questions
Reviewing functions, modules and classes in questions27 questions
Python comments24 questions
String methods (standard knowledge)23 questions
Tuples: creation, indexing and immutability24 questions
Python introduction and applications24 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).