All Question paper with solution mean Bachelorexam.com

UNIT-4: SIEVE OF ERATOSTHENES & FILE I/O | Python programming Important questions with solution

In this session, we are discussing UNIT-4: SIEVE OF ERATOSTHENES & FILE I/O | Python programming Important questions with solution Hope this session will help you in your python programming exam. so best of luck.

Dudes 🤔.. You want more useful details regarding this subject. Please keep in mind this as well.

Important Questions For Python Programming: 
*Unit-01     *Unit-02    
*Unit-03    *Unit-04 
*Unit-05    *Short-Q/Ans
*Question-Paper with solution 21-22 

Q1. Discuss file I/O in Python. How to perform open, read, write, and close into a file? Write a Python program to read a file line-by-line store it into a variable.

Ans.

File I/O:

  1. A file in a computer is a location for storing some related data.
  2. It has a specific name.
  3. The files are used to store data permanently on to a non-volatile memory (such as hard disks).
  4. As we know, the Random Access Memory (RAM) is a volatile memory type because the data in it is lost when we turn off the computer. Hence, we use files for storing of useful information or data for future reference.

Open, read, write and close into a file:

Open into a file

  1. Python has a built-in open() function to open files from the directory. 
  2. Two arguments that are mainly needed by the open() function are:
    1.  File name: It contains a string type value containing the name of the file which we want to access.
    2. Access_mode: The value of access_mode specifies the mode in which we want to open the file, i.e., read, write, append etc.
  3. Syntax:



close into a file

  1. When the operations that are to be performed on an opened file are finished, we have to close the file in order to release the resources. 
  2. Python comes with a garbage collector responsible for cleaning up the unreferenced objects from the memory, we must not rely on it to close a file.
  3. Proper closing of a file frees up the resources held with the file. 
  4. The closing of file is done with a built-in function close(). 
  5. Syntax:

write  into a file

  1. After opening a file, we have to perform some operations on the file. Here we will perform the write operation.
  2. In order to write into a file, we have to open it with w mode or a mode, on any writing-enabling mode.
  3. We should be careful when using the w mode because in this mode overwriting persists in case the file already exists.
  4. For example:

    The given example creates a file named test.txt if it does not exist, and overwrites into it if it exists. If we open the file, we will find the following content in it.

    Output:
    Writing to the file line 1
    Writing to the file line 2
    Writing to the file line 3
    Writing to the file line 4

Reading into a file



  1. In order to read from a file, we must open the file in the reading mode (r mode).
  2. We can use read (size) method to read the data specified by size.
  3. If no size is provided, it will end up reading to the end of the file.
  4. The read() method enables us to read the strings from an opened file. 
  5. Syntax :
    file object. read ([size])
    For example:
    # open the file

Q2. Discuss exceptions and assertions in Python. How to

handle exceptions with try-finally? Explain five built-in exceptions with example? 

Ans. 

Exception: 

  1. While writing a program, we often end up making some errors. There are many types of error that can occur in a program.
  2. The error caused by writing an improper syntax is termed syntax error or parsing error; these are also called compile time errors.
  3. Errors can also occur at runtime and these runtime errors are known as exceptions.
  4. There are various types of runtime errors in Python.
  5. For example, when a file we try to open does not exist, we get a FileNotFoundError. When a division by zero happens, we get a ZeroDivisionError. When the module we are trying to import does not exist, we get an ImportError.
  6. Python creates an exception object for every occurrence of these run- time errors.
  7. The user must write a piece of code that can handle the error.
  8. If it is not capable of handling the error, the program prints a trace back to that error along with the details of why the error has occurred.

Assertions: 

  1. An assertion is a sanity-check that we can turn on or turn off when we are done with our testing of the program. An expression is tested, and if the result is false, an exception is raised.
  2. Assertions are carried out by the assert statement.
  3. Programmers often place assertions at the start of a function to check for valid input, and after a function call to check for valid output. 
  4. An AssertionError exception is raised if the condition evaluates to false. 
  5. The syntax for assert is: assert Expression [, Arguments]
  6. If the assertion fails, Python uses ArgumentExpression as the argument for the AssertionError.

Handle exceptions:

  1. Whenever an exception occurs in Python, it stops the current process and passes it to the calling process until it is handled.
  2. If there is no piece of code in our program that can handle the exception, then the program will crash.
  3. For example, assume that a function X calls the function Y, which in turn calls the function Z, and an exception occurs in Z. If this exception is not handled in Z itself, then the exception is passed to Y and then to X. If this exception is not handled, then an error message will be displayed and our program will suddenly halt.
    1. Try…except:
      1. Python provides a try statement for handling exceptions.
      2. An operation in the program that can cause the exception is placed in the try clause while the block of code that handles the exception is placed in the except clause.
      3. The block of code for handling the exception is written by the user and it is for him to decide which operation he wants to perform after the exception has been identified.
      4. Syntax:
    2. Try finally :
      1. The try statement in Python has an optional finally clause that can be associated with it.
      2. The statements written in the finally clause will always be executed by the interpreter, whether the try statement raises an exception or not.
      3. With the try clause, we can use either except or finally, but not both.
      4. We cannot use the else clause along with the final clause.

Five built-in exceptions:

  1. exception LookupError: This is the base class for those exceptions that are raised when a key or index used on a mapping or sequence is invalid or not found. The exceptions raised are:
    1. KeyError 
    2. IndexError
  2. TypeError: TypeError is thrown when an operation or function is applied to an object of an inappropriate type.
  3. exception ArithmeticError: This class is the base class for those built-in exceptions that are raised for various arithmetic errors such as:
    1. OverflowError
    2. ZeroDivisionError
    3. Floating PointError 
    4. For example:
  4. exception AssertionError: An AssertionError is raised when an assert statement fails.
  5. exception AttributeError: An AttributeError is raised when an attribute reference or assignment fails such as when a non-existent attribute is referenced.

Q3. Define Class Object with Examples?

Ans. 

Class:

  1. A class can be defined as a blue print or a previously defined structure from which objects are made.
  2. Classes are defined by the user; the class provides the basic structure for an object.
  3. It consists of data members and method members that are used by the instances of the class.
  4. In Python, a class is defined by a keyword Class.
  5. Syntax: class class_name;
  6. For example: Fruit is a class, and apple, mango and banana are its objects. Attribute of these objects are color, taste, etc.

In the given example, we have created a class Student that contains two methods: fill_details and print_details. The first method fill_details takes four arguments: self, name, branch and year. The second method print_details takes exactly one argument: self.

Objects:

  1. An object is an instance of a class that has some attributes and behavior. 
  2. The object behaves according to the class of which it is an object.
  3. Objects can be used to access the attributes of the class.
  4. The syntax of creating an object in Python is similar to that for calling a function.
  5. Syntax :
    obj_name = class_name()
    For example:
    s1 = Student ()
    In the given example, Python will create an object s1 of the class student.



Q4. Explain data encapsulation with example.

Ans. 

  1. In Python programming language, encapsulation is a process to restrict the access of data members. This means that the internal details of an object may not be visible from outside of the object definition.
  2. The members in a class can be assigned in three ways i.e., public, protected and private.
  3. If the name of a member is preceded by single underscore, it is assigned as a protected member.
  4. If the name of a member is preceded by double underscore, it is assigned as a private member.
  5. If the name is not preceded by anything then it is a public member.
NameNotationBehavior
varnamePublicCan be accessed from anywhere
_varnameProtectedThey are like the public member buy they cannot be directly accessed from outside
__varnamePrivate They cannot be seen and accessed from outside the class

Q5.  What do you mean by multiple inheritance? Explain in detail.

Ans. 

  1. In multiple inheritance, a subclass is derived from more than one base class.
  2. The subclass inherits the properties of all the base classes.
  3. In Fig. subclass C inherits the properties of two base classes A and B.
  4. Syntax

Python Programming Important Links:

LabelLink
Subject SyllabusSyllabus
Short QuestionsShort-question
Important Unit-1Unit-1
Important Unit-2Unit-2
Important Unit-3Unit-3
Important Unit-4Unit-4
Important Unit-5Unit-5
Question paper – 2021-222021-22

AKTU Important Links | Btech Syllabus

Link NameLinks
Btech AKTU CircularsLinks
Btech AKTU SyllabusLinks
Btech AKTU Student DashboardStudent Dashboard
AKTU RESULT (One VIew)Student Result

Important Links-Btech (AKTU) | Python Programming syllabus

LabelLinks
Btech InformationInfo Link
Btech CSECSE-LINK
Quantum-PageLink
Python Programming SyllabusPython Programming Syllabus

4 thoughts on “UNIT-4: SIEVE OF ERATOSTHENES & FILE I/O | Python programming Important questions with solution”

Leave a Comment