Problem Solving and Python Programming: UNIT V: Files, Modules, Packages

Two Marks Questions with Answers

Files, Modules, Packages | Problem Solving and Python Programming

Engineering Python : UNIT V : Files, Modules, Packages : Two Marks Questions with Answers

Two Marks Questions with Answers

Q. 1 What is file ?

Ans. : File is a named location on the disk to store information. It is a mechanism for persistent storage.

 

Q. 2 What is the syntax for file open method ?

Ans. : In python there is a built in function open() to open a file.

Syntax

File_object=open(file_name,mode)

Where File_object is used to as a handle to the file.

Example

F = open("test.txt")

Here file named test.txt is opened and the file object is in variable F

 

Q. 3 What are different modes of file operation ?

Ans. :

Mode : Purpose

‘r’ - Open file for reading

'w’ - Open file for writing. If the file is not created, then create new file and then write. If file is already existing then truncate it.

‘x’ - Open a file for creation only. If the file already exists, the operation fails.

‘a’ - Open the file for appending mode. Append mode is a mode in which the data is inserted at the end of existing text. A new file is created if it does not exist.

‘ť -  Opens the file in text mode

‘b’ - Opens the file in binary mode

‘+’ - Opens a file for updation i.e. reading and writing,

 

Q. 4 Explain the seek and tell methods used in python file handling

Ans. : The seek method is used to change the file position. Similarly the tell method returns the current position.

Fobj.seek(0) #will set the position at the beginning of the file

Fobj.seek(1) #will set the position at current location

Fobj.seek(2) # will set the position at the end of the file

 

Q. 5 What is the difference between readline and readlines methods ?

Ans. :

The readline method reads a single line from the file while the readlines read all the lines written in the file at a time.

 

Q. 6 Explain the use of format operator.

Ans. : The format operator is specified using % operator. For example - if we want to display integer value then the format sequence must be %d, similarly if we want to display string value then the format sequence must be %s and so on.

 

Q. 7 What are command line arguments in python ?

Ans. : The sys.argv is a list in python, which contains the command-line arguments passed to the script. With the len(sys.argv) function you can count the number of arguments.

 

Q. 8 Give examples of syntax errors.

Ans. : The python finds the syntax errors when it parses the source program. Commonly occurring syntax errors are -

(i) Putting a keyword at wrong place

(ii) Misspelling the keyword

(iii) Incorrect indentation

(iv) Forgetting the symbols such as comma, brackets, quotes

(v) Empty block.

 

Q. 9 What is exception ?

Ans. : An exception is an event, which occurs during the execution of a program that disrupts the normal flow of the program.

 

Q. 10 Give the syntax of exception handling.

Ans. : The exception handling mechanism using the try...except...else blocks.

Syntax of try...except...else

try:

write the suspicious code here

except Exception 1:

If Exception 1 occurs then execute this code

except Exception 2:

If Exception 2 occurs then execute this code

….

….

else:

If there is no exception then execute this code.

 

Q. 11 What is ValueError exception ?

Ans. : This is a kind of exception that is raised when the built-in function for a data type has the valid type of arguments, but the arguments have invalid values specified.

 

Q. 12 What is TypeError ?

Ans. : This exception is raised when an operation or function is attempted that is invalid for the specified data type.

 

Q. 13 What is module ?

Ans. : The modules are basically the files having .py extension and containing some special functionalities.

 

Q. 14 What is package ?

Ans. :  Package is a specialized directory containing subpackages, modules and __init__.py file.

 

Q.15 What is the use of __init__.py file ?

Ans. : The primary use of _ _init__.py is to initialize python packages. Simply putting the subdirectories and modules inside a directory does not make a directory a package, what it needs is a _ _init__.py inside it. Then only python treats that directory as package.

 

Q. 16 What is the difference between module and python ?

Ans. :  

• The module is single file(or files) that are imported and then used. Whereas package is a collection of modules in directories.

There is no need of _ _init__.py file for module. The package must contain _ _init__.py file.

 

Q. 17 Write a Python script to display current date and time. AU: Jan.-18

Ans. :

import datetime

now = datetime.datetime.now()

print ("Current date and time :")

print (now.strftime("%Y-%m-%d %H:%M:%S"))

 

Q. 18 Write a note on modular design. AU : Jan.-18

Ans. : Modular design approach in python is a design technique which allows to keep the python code in separate files. The executable application will be created by putting all the modules together. It makes the application readable, reliable and maintainable.

 

Q. 19 Categorise the different types of errors arises during programming. Interpret the following python code.

>>> import os

>>> cwd = os.getcwd()

>>>print cwd

/home/dinsale

AU : May-19

Ans. : Types of errors - Refer section 5.3.

• The os stands for operating system. The first line is to import os module. This module provides functions for working with files and directories.

• The os.getcwd() returns the name of the current directory. The cwd stands for current working directory.

• In the variable cwd the name of the current working directory is returned.

• In above program execution environment, the current working directory I /home/dinsale

 

Q. 20 What is command line argument ? AU : May-19

Ans : The command line argument in Python are the input parameters passed to the script when executing them.

In python the sys module is used to use the command line arguments. We can use sys.argv for getting the list of command line arguments. The len(sys.argv) gives total number of command line arguments.

 

Problem Solving and Python Programming: UNIT V: Files, Modules, Packages : Tag: Engineering Python : Files, Modules, Packages | Problem Solving and Python Programming - Two Marks Questions with Answers