CSV File Quiz Class 12

πŸ“Š 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✍️ Writing CSVπŸ“– Reading CSVπŸ“₯ Stack BasicsπŸ”Ό Stack Operations🧠 Stack Applications⭐ Board-Style Programs
Q1
πŸ“Š csv Module

How do you import the csv module in Python?

  1. import csv_module
  2. from csv import module
  3. import csv
  4. 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.

Q2
✍️ Writing CSV

Which method of the csv module is used to write a single row into a CSV file?

  1. csv.writer().writerow()
  2. csv.writerows()
  3. csv.writer()
  4. 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.

Q3
πŸ“– Reading CSV

In the context of CSV files, what does the reader() method do?

  1. Reads the entire file as a single string
  2. Reads data from a binary file
  3. Reads data from a CSV file, row by row
  4. 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.

Q4
✍️ Writing CSV

What operation does the writerow() method of the csv module perform?

  1. Writes a single character to a CSV file
  2. Writes a single row to a CSV file
  3. Writes a single column to a CSV file
  4. 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.

Q5
πŸ“Š csv Module

How can you open a CSV file for writing in Python?

  1. file = open(“example.csv”, “r”)
  2. file = open(“example.csv”, “w”)
  3. file = open(“example.csv”, “rb”)
  4. 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.)

Q6
πŸ“₯ Stack Basics

What is the primary purpose of a stack data structure?

  1. To organize data in a tabular form
  2. To store and retrieve elements in a Last In, First Out (LIFO) order
  3. To perform mathematical calculations
  4. 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.

Q7
πŸ”Ό Stack Operations

Which operation is used to add an element to a stack?

  1. Insert
  2. Push
  3. Append (only, never any other term)
  4. 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”.)

Q8
πŸ”Ό Stack Operations

In a stack, what does the “pop” operation involve?

  1. Removing the top element
  2. Adding an element to the top
  3. Checking the top element without removing it
  4. 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.

Q9
πŸ“₯ Stack Basics

Which of the following correctly creates an empty stack in Python using a list?

  1. stack = list() or stack = [] β€” both are equally correct
  2. stack = {}
  3. stack = set()
  4. 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.)

Q10
πŸ”Ό Stack Operations

What happens when you try to pop an element from an empty stack (using list.pop() directly)?

  1. The program terminates silently with no message
  2. An error occurs (IndexError β€” this is the stack underflow condition)
  3. The stack remains unchanged, nothing happens
  4. 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.

Q11
πŸ”Ό Stack Operations

In a stack, what is the purpose of the “peek” operation?

  1. To add an element
  2. To remove an element
  3. To check the top element WITHOUT removing it
  4. 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.

Q12
πŸ“₯ 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)?

  1. O(1)
  2. O(n)
  3. O(log n)
  4. 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.

Q13
🧠 Stack Applications

Which of the following is a genuine real-world application of a stack data structure?

  1. Sorting elements in ascending order
  2. Evaluating arithmetic expressions and checking balanced parentheses
  3. Storing elements in a random, unordered way
  4. 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.

Q14
πŸ”Ό Stack Operations

Which of the following correctly clears all elements from a stack (list) in Python?

  1. Only stack.clear() works; stack = [] does not clear a stack
  2. Only stack = [] works; stack.clear() does not exist
  3. Both stack.clear() and stack = [] result in an empty stack (though clear() empties in place, while stack = [] creates a brand-new list object)
  4. 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.)

Q15
πŸ“₯ Stack Basics

What does the “is_empty” check typically verify for a stack?

  1. If the stack is full
  2. If the stack is NOT empty
  3. If the stack has zero elements (is empty)
  4. 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.

Q16
✍️ Writing CSV

Which method of the csv module writes MULTIPLE rows to a CSV file in a single call?

  1. csv.writerow()
  2. csv.writer().writerows()
  3. csv.write_multiple()
  4. 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.

Q17
πŸ“– Reading CSV

How can you read all rows of a CSV file directly into a list of lists?

  1. rows = csv.reader(file)
  2. rows = list(csv.reader(file))
  3. rows = file.readlines()
  4. 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).

Q18
πŸ“Š csv Module

How can you use a different separator (like a semicolon) instead of a comma when writing a CSV file?

  1. It’s not possible; CSV always uses commas only
  2. Pass the delimiter parameter: csv.writer(file, delimiter=”;”)
  3. Use csv.writer(file).set_delimiter(“;”)
  4. 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.

Q19
⭐ 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)))
  1. [‘Name’, ‘Age’, ‘Riya’, ’16’, ‘Aman’, ’17’]
  2. [[‘Name’, ‘Age’], [‘Riya’, ’16’], [‘Aman’, ’17’]]
  3. ‘Name,Age\nRiya,16\nAman,17\n’
  4. 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']].

Q20
⭐ 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)
  1. [10, 20, 30], then 30, then 30 [10, 20]
  2. [10, 20, 30], then 10, then 10 [20, 30]
  3. [30, 20, 10], then 30, then 30 [20, 10]
  4. 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].

Jitendra Singh
βœ” Verified Educator

Jitendra Singh

Founder of CBSEPython.in

I help CBSE Class 9–12 students learn Python, Information Technology, Artificial Intelligence and Computer Science through easy notes, quizzes, MCQs and sample papers.

Read More About Me β†’