Python Questions medium length
1.)
Explain dictionary and how it is
created?
Ans.-
Dictionary is used to implement the key value pair in python. The dictionary is
the data type in python which can simulate the real- life data arrangement
where some specified value exist for some particular key.
Creating a dictionary
The dictionary can be created by using multiple key value
pair enclosed with the small brackets () and separated by the colon(:) . The
collection of the key value pair are enclosed within the curly braces {}.
Syntax:-
Dict = {“name”:”abc”,”age”:22}
2.)
What are the advantages of functions?
Ans.- A function is a block of reusable code that is used to perform a
specific action. The advantages of using functions are:
1) Reducing duplication of code.
2) Decomposing complex problem into
simpler pieces
3) Improving clarity of the code.
4) Reuse of code.
5) Information hiding.
3.)
What
is constructor and its types ?
Ans.-Constructors are generally used for
instantiating an object. The task of constructor is to initialize to the data
member of the class when an object of class is created. In python the _init_()
method is called the constructor and is always called when an object is
created.
Syntax:-
Def_init_(self):
#Body of
constructor
Types of constructor:-
1.)Default constructors
2.) parameterized constructor
Default constructor:- The default constructor is simple constructor which does not
accept any arguments. It’s definition has only one argument which is a
reference to the instance being constructed.
Parameterized
constructor:-Constructor
with parameters is known as parameterized constructor. The parameterized
constructor take its first argument as a reference to the instance being
constructed known as self and the rest of the arguments are provided by the
programmer.
4.)
Explain for loop?
Ans.-The for loop in python is used to
statement or a part of the program several times.it is frequently used to
traverse the data structure like list, tuple, or dictionary.
Syntax:-
For
iterating_var in sequence:
Statements(s)
Explain
nested for loop.
5)Write different categories of
operators?
Ans:-Explain all of these operators which
are below:
1.) arithmetic
operator
2.) relational operator
3.) logical operator
4.) bitwise operator
5.) assignment operator
6.) What is scope of variables ?explain.
Ans.-
The scope of variables depend upon the location where the variable is
being declared.
The variable
declared in one part of the program may not be accessible to the other part.
In python,
the variable is defined with the two types of scopes.
1.)
Global
scope
2.)
Local
scope
The variable
defined outside any function is known to have a global scope whereas the
variable defined inside a function to have a local scope.
7.)
Explain lambda function in python?
Ans:-A lambda function is a small
anonymous function .A lambda function can take any number of arguments ,but can
only have one expression.
Syntax:-
Lambda
arguments : expression
The
expression is executed and the result is returned
8.)
Explain sys module?
Ans:-
The sys module provides functions and variable used to manipulate
different parts of the Python runtime environment. You will learn some of the
important features of this module here.
Sys.maxsize
Returns the
largest integer a variable can take
Sys.path
This is an
environment variable that is a search path for all python modules.
Sys.version
This
attribute displays a string containing
the version number of the current python interpreter.
9.)
Explain assert statement with
example?
Ans.-Python provides the assert statement
to check if a given logical expression is true or false. Program execution
proceed only if the expression is true and raise the Assertion Error when it is
false.
AssersionError:- The assert statement can optionally
include an error message string which gets displayed along with the
AssertionError.
10.)
Why exception handling is required?
Ans.-
Whenever exception is occurs, the program halts the execution ,and thus
the further code is not executed. Therefore, an exception is the error which
python script is unable to tackle with.
Python
provides us with the way to handle the exception so that the other part of the
code can be executed without any disruption. However, if we do not handle the
exception, the interpreter doesn’t executed all the code that exist after the
that.
11.)
Write different types of exception in
python?
Ans.-
Common Exceptions:
1.)
ZeroDivisionError:
Occurs when a number is divided by zero.
2.)
NameError:
It occurs when a name is not found .It may be local or global.
3.)
IndentationError:
If incorrect indentation is given.
4.)
IOError:
It occurs when input output operation fails.
5.)
EOFError:
It occurs when the end of the file is reached, and yet operation is being
performed.
12.)
Explain any 5 string methods in
python?
Ans.-
1.) replace(old,new[,count]) :-Replaces
all the occurrences of substring ‘old’ with ‘new’ in the string.
If ‘count’ is defined then only ‘count’ number of occurrences
of ‘old’ will be replaced with ‘new’.
Old= substring to be replaced
New= substring that will replace the old
Count= number of occurrences of old that will be replaced
with new.
2.) lower() :-converts all the
characters of the string
to lowercase.
3.) upper() :-converts
all the characters of the string to uppercase.
4.)capitalize() :-
returns the string with first character capitalized and rest of the character
in lowercase.
5.) len(string) :-return
the length of given string
13.)
Explain the % method with example ?
Ans.-
Python uses C-Style string formatting
to create new , formatted strings. The “%” operator is used to format a
set of variable enclosed in a “tuple” ,together with a format string , which
contains normal text together with “argument specifiers” ,special symbols like
“%s” and “%d” .For string %s and for integer %d is used.
Example:-
name=”Mohan”
print=(“hello,
%s!” %name)
14.)
Write naming conventions?
Ans.- While Working in python one should keep in mind certain styles rules and
conventions . The rules are given below and explain all of these :
1.)
Statement
terminated
2.)
Maximum
line length
3.)
Lines
and indentation
4.)
Blank
lines
5.)
Avoid
multiple statement on one line
6.)
Whitespace
7.)
Case
sensitive
15.)
Explain comments and its types with
example?
Ans.- comments in python can be used to
explain any program code .It can also be used to hide the code as well.
Comments are the most helpful stuff of any program. It enables us to understand
the way ,a program work. In python, any statement written along with # symbol
is known as a comment. The interpreter does not interpret the comment .Comment
is not a part of the program but it enhances the interactivity of the program
and makes the program readable
Types of
comments:
1.)
Single line comment: In case user wants to specify a single line comment, the comment must
start with ?#?
2.)
Multiline comment: Multiline comment can be given inside triple quotes.
Write the examples
also
16.)
Write string operators ?
Ans.-
String operators can be used to manipulate the string in multiple ways .
·
+
:- It is known as concatenation operator used to join the strings given either
side or the operator.
·
*
:- it is known as repetition operator. It concatenates the multiple copies of
the same string.
·
[]
:- It is known as slice operator .It is used to access the sub string of
particular string .
·
[:]
:- It is known as range slice operator. It is used to access the characters
from the specified range.
·
%
:- It is used to perform string formatting.
17.)
Explain
types or arguments ?
Ans.-Explain
all these arguments:
1.)
Required
argument
2.)
Keyword
argument
3.)
Default
argument
4.)
Variable-length
argument
18.)
Difference between oop and pop?
Ans.-Object-
oriented programing:
1.)
Object
oriented programing is the problem solving approach and used where computation
is done by using objects.
2.)
It
makes the development and maintenance easier.
3.)
It
simulates the real world entity. So real- world problems can be easily solved
through oops.
4.)
It
provides data hiding. So it I more secure than procedural languages. You cannot
access private data from anywhere.
5.)
Example:
C++,Java, .Net .Python etc.
Procedural programming:
1.)
It
uses a list of instructions to do computations step by step.
2.)
It
is not easy to maintain the codes when the project became lengthy
3.)
It
doesn’t simulates the real world .It works on step by step instruction divided
into small parts called functions.
4.)
Procedural
language doesn’t provide any proper way for data binding , so it is less secure
.
5.)
Example
: C , Fortran,Pascal, VB etc.
19.)
What are special characters used in
regular expression?
Ans.-
Special character is a character with the specified meaning.
1.)
[]
= It represents the set of characters. Eg= “[a-z]”
2.)
\
= It represents the special sequence. Eg=”\r”
3.)
$
= It represents the pattern present at the end of the string. Eg= “point”
4.)
+
= It represents one or more occurrences of a pattern in the string. Eg=
“hello+”
5.)
()
= capture and group
20.)
Explain various mode of opening the
file in python ?
Ans.-
explain the description of the modes which are given below:
1.)
r
2.)
rb
3.)
r+
4.)
rb+
5.)
w
6.)
wb
7.)
w+
8.)
wb+
9.)
a
10.)
ab
11.)
a+
12.)
ab+
21.)
How file pointer position can be
changed?
Ans.-
In the real world application , sometimes we need to change the pointer
location externally since we may need to read or write the content at various
locations .
For this
purpose, the python provide us the seek() method which enables us to modify the
file pointer position externally.
Syntax:-
<file-ptr>.seek(offset[,from])
It consist
of two parameters
Offset: It refers to the new position of the
file pointer within the file.
From : It indicates the reference position
from where the bytes are to be moved.





