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

pythan-PTT II Question Bank With Answer 1

The document discusses Python concepts like built-in functions, lambda functions, classes, inheritance, modules, file handling, exceptions, variables and functions. It provides examples and explanations of each concept. It also asks questions to test understanding.

Uploaded by

Fazal Qureshi
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)
48 views

pythan-PTT II Question Bank With Answer 1

The document discusses Python concepts like built-in functions, lambda functions, classes, inheritance, modules, file handling, exceptions, variables and functions. It provides examples and explanations of each concept. It also asks questions to test understanding.

Uploaded by

Fazal Qureshi
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/ 10

PYTHON PTT –II QUESTION BANK

1) Explain following built-in functions.

a. all() : Returns True if all items in an iterable object are true

b. hasattr() : Returns True if the specified object has the specified attribute
(property/method).

c. oct() : Converts a number into an octal

d. issubclass() : Returns True if a specified class is a subclass of a specified


object.

2) Explain Lambda function with example.

• A lambda function is often called as anonymous function.

• As the name suggests it is defined without a name.

• 'lambda' keyword is used to define anonymous function. Lambda function


has following format:

Syntax:

Lambda arguments : expression.

Example:

C = lamhda b:b**3

print('The result of iambda function isl',C(6"8))

The result lambda function is: 36.0

3) State the use of parameter “self” in python class.


• self represents the instance of the class.
• By using the “self” we can access the attributes and methods of the class
in python.
• It binds the attributes with the given arguments.
• Self is always pointing to the current object.
• Self must be provided the first parsmeter to the instance of the class.
Example:

class car():

# init method or constructor


def __init__(self, model, color):
self.model = model
self.color = color

def show(self):
print("Model is", self.model )
print("color is", self.color )

# both objects have different self which


# contain their attributes
audi = car("audi a4", "blue")
ferrari = car("ferrari 488", "green")

audi.show() # same output as car.show(audi)


ferrari.show() # same output as car.show(ferrari)

# Behind the scene, in every instance method


# call, python sends the instances also with
# that method call like car.show(audi)
Output
Model is audi a4
color is blue
Model is ferrari 488
color is green
4) Explain difference between composition & Inheritance

5) Write a short note on method overriding in python.

• Method overriding is defined as having a method with the same name in


the child class as well as in the parent class'

• The definition of the method is different in parent and child classes but the
name stays the same.
6) Explain Concept of Module In detail.

It contains a python codes

Every file in python which is save with .py extension is module.

We can import the functions, classes defined in a module to another module


using the import statement in some other Python source file.

There is a multiple module provided by python:

Math, random, RegEx/re, Matplotlib and Pyplot,

Syntax:

Import module_name

Example:
Import math as mp
Mp.floor(1.5)
2

7) What are different modes of opening file?

In order to open a file, python has provided a built in function called as open()
function.

This function return the file object. While opening a file we can specify the
mode in which we wan open file.

Syntax:

f = open(“file_name”)
8) Explain any two standard library.

a library is a collection of precompiled codes that can be used later on in a


program for some specific well-defined operations.

the commonly used libraries:

• Matplotlib: This library is responsible for plotting numerical data. And


that’s why it is used in data analysis. It is also an open-source library
and plots high-defined figures like pie charts, histograms, scatterplots,
graphs, etc.

• Numpy: The name “Numpy” stands for “Numerical Python”. It is the


commonly used library. It is a popular machine learning library that
supports large matrices and multi-dimensional data. It consists of in-
built mathematical functions for easy computations. Even libraries like
TensorFlow use Numpy internally to perform several operations on
tensors.

• PyGame: This library provides an easy interface to the Standard


Directmedia Library (SDL) platform-independent graphics, audio, and
input libraries. It is used for developing video games using computer
graphics and audio libraries along with Python programming language.

9) Write a program to demonstrate try with multiple except blocks to


handle multiple exceptions.
10) Define and explain local and Global variables with suitable examples.

python also supports concept of local and global variable. By default every
variable declared has global scope. If a variable is defined within a function,
then the scope is local.
Local Variable :
Local variables are those which are initialized inside a function and belongs only
to that particular function. It cannot be accessed anywhere outside the function.
Ex.,
def f():
# local variable
s = "I love Geeksforgeeks"
print("Inside Function:", s)

# Driver code
f()
print(s)
Global Variable :
The global variables are those which are defined outside any function and which
are accessible throughout the program i.e. inside and outside of every function
Ex.,
# This function uses global variable s
def f():
print("Inside Function", s)

# Global scope
s = "I love Geeksforgeeks"
f()
print("Outside Function", s)
11) Explain need & Types of Function.
• To improve the readability of the source code.
• To provides the facility of the reusability of the code, same function can be
used in any program rather than writing the same code from scratch.
• Function provides modular programming hence debugging of the code
would be easier, as errors are easy to be traced.
• Reduces the size of the code, duplicate set of statements are replaced by
function calls.
Types of Function :
1) User Defined Function :
These types of functions are defined by the user as per their requirement.
2) Built-In Function :
These functions are provided by the python. As the name suggests these are
built-in you can Directly use it.
e.g. print0 function in python

12) Write a program to demonstrate Multilevel Inheritance.


When a derived class is inherited from another derived class then it is called
as multilevel inheritance.
Example :

13) Explain Data Encapsulation with example.

Encapsulation is the object oriented programing concept.


Encapsulation refers to binding of the data together.
Encapsulation in Python describes the concept of bundling data
and methods within a single unit.
So, for example, when you create a class, it means you are implementing
encapsulation.
A class is an example of encapsulation as it binds all the data members (instance
variables) and methods into a single unit.
14) What is package and how to create package.
15) Explain data abstraction with example

You might also like