Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
7 views3 pages

PU Board Style Answers

The document compares various Python concepts including SyntaxError and ValueError, explains the raise and assert statements, and discusses multiple except clauses. It also differentiates between text and binary files, details the pickle module, and describes file pointer methods tell() and seek(). Additionally, it defines stack and queue data structures, lists their applications, and provides algorithms for bubble sort and insertion sort, along with file reading and writing methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

PU Board Style Answers

The document compares various Python concepts including SyntaxError and ValueError, explains the raise and assert statements, and discusses multiple except clauses. It also differentiates between text and binary files, details the pickle module, and describes file pointer methods tell() and seek(). Additionally, it defines stack and queue data structures, lists their applications, and provides algorithms for bubble sort and insertion sort, along with file reading and writing methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Compare SyntaxError and ValueError

Definition: A SyntaxError occurs when the rules of Python syntax are violated, and it is
detected before the program runs. A ValueError occurs during execution when a function
receives a value of the correct type but with an inappropriate value.
Explanation: SyntaxError prevents the program from running until the syntax is corrected.
Example: Missing a colon after an 'if' statement. ValueError happens at runtime, such as
attempting to convert the string 'abc' into an integer. SyntaxError relates to incorrect code
structure, while ValueError relates to invalid data values.

2. Explain the concept of raise statement

Definition: The raise statement in Python is used to generate an exception manually.


Explanation: It allows a programmer to stop the normal flow of execution and indicate that
an error condition has occurred. For example, 'raise ValueError("Invalid age")' will
immediately raise a ValueError exception. This helps enforce rules and validate data
during execution.

3. Explain the concept of assert statement

Definition: The assert statement is used to test an expression, and if the condition is false,
it raises an AssertionError.
Explanation: Assertions are primarily used for debugging purposes to ensure that certain
conditions are true during program execution. For example, 'assert x > 0, "x must be
positive"' will raise an error if x is not positive.

4. Explain the concept of multiple except clauses

Definition: Multiple except clauses allow handling of different exception types separately
within a single try block.
Explanation: This makes error handling more specific and organized. Each except block is
matched with a particular exception type. Example: catching ValueError and
ZeroDivisionError in separate except blocks ensures appropriate error messages for
different issues.

5. Differentiate between text file and binary file

Definition: A text file stores data as characters readable by humans, while a binary file
stores data in computer-readable binary format.
Explanation: Text files can be opened with any text editor and store information in
encoded characters (like ASCII or UTF-8). Binary files store data as sequences of bytes,
such as images or executable programs, and cannot be directly read without decoding.

6. Explain the pickle module in detail


Definition: The pickle module in Python is used for serializing and deserializing Python
objects.
Explanation: Serialization converts a Python object into a byte stream for storage or
transmission. Deserialization restores the byte stream back into the original Python object.
This is useful for saving program state, data persistence, and inter-process
communication.

7. Write about tell() and seek()

Definition: The tell() method returns the current file pointer position, while the seek()
method moves the file pointer to a specified position.
Explanation: tell() helps determine where the next read or write will occur in the file.
seek(offset) allows moving to a specific byte position in the file for random access reading
or writing.

8. Define Stack. What is the principle involved in it?

Definition: A stack is a linear data structure that follows the Last In First Out (LIFO)
principle.
Explanation: In a stack, the last element inserted is the first to be removed. Common
operations are push (insert) and pop (remove). It is used in function calls, expression
evaluation, and undo mechanisms.

9. Define Queue. What is the principle involved in it?

Definition: A queue is a linear data structure that follows the First In First Out (FIFO)
principle.
Explanation: In a queue, the first element inserted is the first to be removed. Common
operations are enqueue (insert) and dequeue (remove). It is used in scheduling, order
processing, and buffering.

10. Write any 5 applications of Stack

Definition: Stack applications are practical uses of the LIFO data structure in computing.
Explanation: Common applications include: function call management, expression
evaluation, syntax parsing, undo operations in text editors, and depth-first search in
graphs.

11. Write any 5 applications of Queue

Definition: Queue applications are practical uses of the FIFO data structure in computing.
Explanation: Common applications include: print spooling, CPU scheduling, order
processing, breadth-first search in graphs, and message queue management.
12. Write an algorithm for bubble sort

Definition: Bubble sort is a simple sorting algorithm that repeatedly compares and swaps
adjacent elements if they are in the wrong order.
Explanation: The process continues until no swaps are required, indicating the list is
sorted. It is inefficient for large datasets due to its O(n²) time complexity.

13. Write an algorithm for insertion sort

Definition: Insertion sort is a simple sorting algorithm that builds the final sorted array one
item at a time.
Explanation: It works by taking each element and inserting it into its correct position
among the already sorted elements. It is efficient for small datasets and has a time
complexity of O(n²) in the worst case.

14. Define: read(), readline(), readlines()

Definition: The read() method reads the entire file or specified number of bytes as a string.
readline() reads one line from the file. readlines() reads all lines and returns them as a
list.
Explanation: These methods are used for different purposes depending on whether
complete file data or partial data is needed.

15. Define: write(), writeline(), writelines()

Definition: The write() method writes a single string to a file. writeline() is used in some
contexts to write one line with a newline character. writelines() writes a list of strings to a
file.
Explanation: These methods are used for storing text data into files.

16. Explain the concept of serialization and deserialization

Definition: Serialization is the process of converting a Python object into a byte stream,
while deserialization is the process of converting the byte stream back into the original
object.
Explanation: These processes are used for storing data to files, sending data over
networks, and saving program states. The 'pickle' module is commonly used for this
purpose in Python.

You might also like