π CSV File & Stack Data Structure Quiz β Class 12 Computer Science (Code 083)
Practice 20 CBSE Class 12 Computer Science (Code 083) MCQs covering two topics in one set: CSV files (the csv module, writer(), writerow(), writerows(), reader()) and the stack data structure (LIFO, push, pop, peek, underflow, time complexity, and applications like balanced-bracket checking).
This set includes 2 board-style predict-the-output questions with full working code. Every result below has been verified by actually running the code in Python. Tap any question to reveal the answer with a clear explanation.
π csv Module
How do you import the csv module in Python?
- import csv_module
- from csv import module
- import csv
- from module import csv
Show Answer & Explanation
β Answer: (c) import csv
import csv is the correct, standard way to bring in Python’s built-in CSV-handling module.
βοΈ Writing CSV
Which method of the csv module is used to write a single row into a CSV file?
- csv.writer().writerow()
- csv.writerows()
- csv.writer()
- csv.write()
Show Answer & Explanation
β Answer: (a) csv.writer().writerow()
You first create a writer object with csv.writer(file), then call .writerow(row) on it to write one row at a time.
π Reading CSV
In the context of CSV files, what does the reader() method do?
- Reads the entire file as a single string
- Reads data from a binary file
- Reads data from a CSV file, row by row
- Reads the entire file as one combined list of characters
Show Answer & Explanation
β Answer: (c) Reads data from a CSV file, row by row
csv.reader(file) creates a reader object that lets you iterate through the CSV file row by row, with each row returned as a list of string values.
βοΈ Writing CSV
What operation does the writerow() method of the csv module perform?
- Writes a single character to a CSV file
- Writes a single row to a CSV file
- Writes a single column to a CSV file
- Writes a single line without any separators
Show Answer & Explanation
β Answer: (b) Writes a single row to a CSV file
writerow() takes a list (or any iterable) of values and writes them as one row, automatically separating them with commas.
π csv Module
How can you open a CSV file for writing in Python?
- file = open(“example.csv”, “r”)
- file = open(“example.csv”, “w”)
- file = open(“example.csv”, “rb”)
- file = open(“example.csv”, “wb”)
Show Answer & Explanation
β Answer: (b) file = open(“example.csv”, “w”)
“w” opens the file for writing in text mode β the same mode used for regular text files, since CSV is fundamentally a text format. (Board answers typically add newline="" too, to avoid extra blank lines on Windows, though that’s a separate parameter from the mode itself.)
π₯ Stack Basics
What is the primary purpose of a stack data structure?
- To organize data in a tabular form
- To store and retrieve elements in a Last In, First Out (LIFO) order
- To perform mathematical calculations
- To sort data alphabetically
Show Answer & Explanation
β Answer: (b) To store and retrieve elements in a Last In, First Out (LIFO) order
A stack follows LIFO (Last In, First Out) β the most recently added element is always the first one to be removed, like a stack of plates.
πΌ Stack Operations
Which operation is used to add an element to a stack?
- Insert
- Push
- Append (only, never any other term)
- Add
Show Answer & Explanation
β Answer: (b) Push
Adding an element to a stack is called a push operation. (In Python, this is typically implemented using the list’s .append() method, but the operation’s name in stack terminology is βpushβ.)
πΌ Stack Operations
In a stack, what does the “pop” operation involve?
- Removing the top element
- Adding an element to the top
- Checking the top element without removing it
- Searching for a specific element
Show Answer & Explanation
β Answer: (a) Removing the top element
pop removes and returns the element currently at the top of the stack β the most recently pushed one.
π₯ Stack Basics
Which of the following correctly creates an empty stack in Python using a list?
- stack = list() or stack = [] β both are equally correct
- stack = {}
- stack = set()
- stack = ( )
Show Answer & Explanation
β Answer: (a) stack = list() or stack = [] β both are equally correct
Both stack = list() and stack = [] create a genuinely empty list, which is the standard way to implement a stack in Python β they’re functionally identical. (This question is corrected from the original, which marked only stack = [] as correct, even though list() produces the exact same result.)
πΌ Stack Operations
What happens when you try to pop an element from an empty stack (using list.pop() directly)?
- The program terminates silently with no message
- An error occurs (IndexError β this is the stack underflow condition)
- The stack remains unchanged, nothing happens
- The bottom element is removed instead
Show Answer & Explanation
β Answer: (b) An error occurs (IndexError β this is the stack underflow condition)
Calling .pop() on an empty list raises an IndexError: pop from empty list β this is what βstack underflowβ refers to conceptually, and it’s exactly why real stack implementations check if len(stack) == 0 before popping.
πΌ Stack Operations
In a stack, what is the purpose of the “peek” operation?
- To add an element
- To remove an element
- To check the top element WITHOUT removing it
- To reverse the order of all elements
Show Answer & Explanation
β Answer: (c) To check the top element WITHOUT removing it
peek lets you look at the top element (commonly stack[-1] in a Python list) without actually removing it from the stack β useful when you need to know what’s on top before deciding whether to pop it.
π₯ Stack Basics
What is the time complexity of the “push” and “pop” operations on a stack implemented using a Python list (operating on the end of the list)?
- O(1)
- O(n)
- O(log n)
- O(nΒ²)
Show Answer & Explanation
β Answer: (a) O(1)
Both .append() (push) and .pop() (pop) operating on the end of a Python list run in constant, O(1) time β this is exactly why the end of the list (not the beginning) is used to implement a stack efficiently.
π§ Stack Applications
Which of the following is a genuine real-world application of a stack data structure?
- Sorting elements in ascending order
- Evaluating arithmetic expressions and checking balanced parentheses
- Storing elements in a random, unordered way
- Searching for an element using binary search
Show Answer & Explanation
β Answer: (b) Evaluating arithmetic expressions and checking balanced parentheses
Stacks are classically used for evaluating expressions (like converting infix to postfix) and checking balanced brackets β every opening bracket is pushed, and closing brackets are matched by popping, which naturally enforces the LIFO order they need to close in.
πΌ Stack Operations
Which of the following correctly clears all elements from a stack (list) in Python?
- Only stack.clear() works; stack = [] does not clear a stack
- Only stack = [] works; stack.clear() does not exist
- Both stack.clear() and stack = [] result in an empty stack (though clear() empties in place, while stack = [] creates a brand-new list object)
- Neither method actually works
Show Answer & Explanation
β Answer: (c) Both stack.clear() and stack = [] result in an empty stack (though clear() empties in place, while stack = [] creates a brand-new list object)
stack.clear() empties the existing list object in place. stack = [] instead creates a new empty list and rebinds the name stack to it β the old list object still exists elsewhere if anything else was referencing it. Both leave you with an empty stack, but they behave subtly differently. (This question is corrected from the original, which only accepted one of these as correct.)
π₯ Stack Basics
What does the “is_empty” check typically verify for a stack?
- If the stack is full
- If the stack is NOT empty
- If the stack has zero elements (is empty)
- If the stack has reached a certain fixed size
Show Answer & Explanation
β Answer: (c) If the stack has zero elements (is empty)
An is_empty check (commonly written as len(stack) == 0 in Python) verifies whether the stack currently has no elements at all β typically used as a safety check before attempting a pop.
βοΈ Writing CSV
Which method of the csv module writes MULTIPLE rows to a CSV file in a single call?
- csv.writerow()
- csv.writer().writerows()
- csv.write_multiple()
- csv.appendrows()
Show Answer & Explanation
β Answer: (b) csv.writer().writerows()
writerows(list_of_rows) takes a list where each item is itself a row (a list of values), and writes all of them at once β more convenient than calling writerow() repeatedly in a loop.
π Reading CSV
How can you read all rows of a CSV file directly into a list of lists?
- rows = csv.reader(file)
- rows = list(csv.reader(file))
- rows = file.readlines()
- rows = csv.load(file)
Show Answer & Explanation
β Answer: (b) rows = list(csv.reader(file))
csv.reader(file) returns an iterator, not a list by itself β wrapping it with list() consumes that iterator and gives you a genuine list of rows (each row itself a list of string values).
π csv Module
How can you use a different separator (like a semicolon) instead of a comma when writing a CSV file?
- It’s not possible; CSV always uses commas only
- Pass the delimiter parameter: csv.writer(file, delimiter=”;”)
- Use csv.writer(file).set_delimiter(“;”)
- Manually replace every comma in the string afterward
Show Answer & Explanation
β Answer: (b) Pass the delimiter parameter: csv.writer(file, delimiter=”;”)
csv.writer(file, delimiter=”;”) lets you customize the separator character used between values β useful for regions or systems where commas are used as decimal points instead.
β Board-Style Programs
What does the following program print?
import csv
with open("t.csv", "w", newline="") as f:
writer = csv.writer(f)
writer.writerows([["Name", "Age"], ["Riya", "16"], ["Aman", "17"]])
with open("t.csv", "r") as f:
print(list(csv.reader(f)))- [‘Name’, ‘Age’, ‘Riya’, ’16’, ‘Aman’, ’17’]
- [[‘Name’, ‘Age’], [‘Riya’, ’16’], [‘Aman’, ’17’]]
- ‘Name,Age\nRiya,16\nAman,17\n’
- Error, writerows() only accepts a single row
Show Answer & Explanation
β Answer: (b) [[‘Name’, ‘Age’], [‘Riya’, ’16’], [‘Aman’, ’17’]]
writerows() writes all three rows at once. Reading them back and wrapping with list() gives a list of lists, where each inner list is one row’s values: [['Name', 'Age'], ['Riya', '16'], ['Aman', '17']].
β Board-Style Programs
What does the following stack program print, in order?
stack = [] stack.append(10) stack.append(20) stack.append(30) print(stack) top = stack[-1] print(top) popped = stack.pop() print(popped, stack)
- [10, 20, 30], then 30, then 30 [10, 20]
- [10, 20, 30], then 10, then 10 [20, 30]
- [30, 20, 10], then 30, then 30 [20, 10]
- Error, lists cannot be used as stacks
Show Answer & Explanation
β Answer: (a) [10, 20, 30], then 30, then 30 [10, 20]
After three pushes, the stack is [10, 20, 30]. Peeking with stack[-1] shows the top element, 30, without removing it. Popping then removes and returns that same top element (30), leaving the stack as [10, 20].
