0% found this document useful (0 votes)
5 views

Python Question Bank

The document is a question bank from Mahendra Institute of Technology covering various programming concepts, particularly in Python and SQL. It includes questions on class attributes, polymorphism, file handling, exceptions, databases, and GUI programming. Each question is followed by a detailed explanation or example to aid understanding.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Python Question Bank

The document is a question bank from Mahendra Institute of Technology covering various programming concepts, particularly in Python and SQL. It includes questions on class attributes, polymorphism, file handling, exceptions, databases, and GUI programming. Each question is followed by a detailed explanation or example to aid understanding.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

MAHENDRA INSTITUTE OF TECHNOLOGY

(AUTONOMOUS)

Question Bank

Answer the following. PART –B (2 marks)


1. State the difference between Class Attributes and Instance Attributes.
CLASS ATTRIBUTES INSTANCE ATTRIBUTES

Defined within the class block but outside Defined within methods, typically
of methods the __init__ constructor

Shared among all instances of the class Specific to each instance of the class

Accessed using the class name or any


Accessed using an instance of the class
instance

Changing affects all instances of the class Changing affects only the specific instance

2. What is polymorphism?
The polymorphism means having many forms. In programming, polymorphism means the
same function name (but different signatures) being used for different types. The key difference is
the data types and number of arguments used in function .
3. Write the uses of _init_method.
__init__ method in Python is used to initialize objects of a class. It is also called a constructor.
constructors are used to initialize the object’s state.
The task of constructors is to initialize(assign values) to the data members of the class when an
object of the class is created.
4. Define Aggregation.
Aggregation is a concept in which an object of one class can own or access another independent
object of another class.
 It represents Has-A’s relationship.
 It is a unidirectional association i.e. a one-way relationship. For example, a department can
have students but vice versa is not possible and thus unidirectional in nature.
 In Aggregation, both the entries can survive individually which means ending one entity
will not affect the other entity.
5. How to open a new file in Python?

Opening a file creates a file object. In this example, the variable f refers to the new file object. >>> f
= open("test.dat","w") >>> print f The open function takes two arguments. The first is the name of
the file, and the second is the mode. Mode "w" means that we are opening the file for writing.

6. Which method is used to read the contents of a file which is already created?
The read method reads data from the file. With no arguments, it reads the entire contents of the file:
>>> text = f.read()

>>> print text Now is the time to close the file.

7. What is a text file? Give an example for a text file.

A text file is a file that contains printable characters and whitespace, organized into lines separated by
newline characters. To demonstrate, we‘ll create a text file with three lines of text separated by newlines:
>>> f = open("test.dat","w")

>>> f.write("line one\nline two\nline three\n")

>>> f.close()

8. What is an exception? Explain with few examples.

Whenever a runtime error occurs, it creates an exception. Usually, the program stops and Python
prints an error message.

Forexample, dividing by zero creates an exception:

>>> print 55/0

ZeroDivisionError: integer division or modulo So does accessing a nonexistent list item:

>>> a = [] >>> print a[5]

9. Explain pickling and how import pickle works.

Pickling is so called because it ―preserves‖ data structures. The pickle module contains the
necessary commands. To use it, import pickle and then open the file in the usual way: >>> import
pickle >>> f = open("test.pck","w")

10. Write a simple program which illustrates Handling Exceptions.

x=int(input(―Please enter a number:‖))

break

except ValueError:

print(―Oops! That was no valid number. Try again…‖)

11. List some few common Exception types and explain when they occur.

 ArithmeticError- Base class for all errors that occur for numeric calculations.
• OverflowError- Raised when a calculation exceeds maximum limit for a numeric type.
• ZeroDivisionError- Raised when division or modulo by zero takes o place.
• ImportError- Raised when an import statement fails.
• IndexError- Raised when an index is not found in a sequence.
• RuntimeError- Raised when a generated error does not fall into any category.

12. What is Database?


A database is an organized collection of data stored in a computer system and usually
controlled by a database management system (DBMS). The data in common databases is
modeled in tables, making querying and processing efficient. Structured query language
(SQL) is commonly used for data querying and writing.

13. Write the SQL Query to create a Table in database.

The CREATE TABLE statement is used to create a new table in a database.

Syntax
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
);
14. Define SQLite Manager.
SQLite is a relational database management system contained in a C programming library. In
contrast to many other database management systems, SQLite is not a client–server database engine.
Rather, it is embedded into the end program. SQLite is an open source SQL database that stores data
to a text file on a device.
15. List the various data manipulation commands in SQL.
SQL commands are essential for managing databases effectively. These commands are divided
into categories such as Data Definition Language (DDL), Data Manipulation Language (DML),
Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language
(TCL).
16. What do you mean by GUI programming?
A Python GUI, or Graphical User Interface, is a visual interface that allows users to interact
with a software application. It provides users with a set of graphical elements, such as
buttons, checkboxes, dropdown lists, and sliders, to perform various tasks and access
different functionalities of the application.

17. How to delete a row in table?


import mysql. connector package.
Create a connection object using the mysql. connector. ...
Create a cursor object by invoking the cursor() method on the connection object created
above.
 Then, execute the DELETE statement by passing it as a parameter to the execute() method.
18. Write the benefits of Tkinter library.

 Layered approach. The layered approach used in designing Tkinter gives Tkinter all of the
advantages of the TK library. ...
 Accessibility. Learning Tkinter is very intuitive, and therefore quick and painless. ...
 Portability. ...
 Availability.
19. Enlist the advantages of using SQL in python.
 Faster Query Processing
 No Coding Skills
 Standardized Language
 Portable
 Interactive Language
20. How do you write a SELECT statement to retrieve data from a table?

The SELECT statement is used to select data from a database.

SELECT Customer Name, City FROM Customers;

ANSWER THE FOLLOWING (PART-C)


1. Discuss about the idea of inheritance with suitable python code.
2. Explain the concept of polymorphism in python.

3. Describe about Python Files, its types, functions and operations that can be performed on files

with examples.

4. Demonstrate the use of Exception Handling in Python.

5. Explain the uses of packages and module with suitable example.


6. Illustrate the uses of SQL statements for data manipulation

7. Demonstrate the working process of python in Database.


8. Discuss the steps involved in creating simple GUI using Tkinter

You might also like