Python Interview Questions – Set 04

How to overload constructors or methods in Python?

Python’s constructor: _init__ () is the first method of a class. Whenever we try to instantiate an object __init__() is automatically invoked by python to initialize members of an object. We can’t overload constructors or methods in Python. It shows an error if we try to overload.

class student:
def __init__(self,name):
self.name = name
def __init__(self, name, email):
self.name = name
self.email = email

# This line will generate an error
#st = student(“rahul”)

# This line will call the second constructor
st = student(“rahul”, “rahul@gmail.com”)
print(st.name)
Output:
rahul

What is pickling and unpickling in Python?

The Python pickle is defined as a module which accepts any Python object and converts it into a string representation. It dumps the Python object into a file using the dump function; this process is called pickling.

The process of retrieving the original Python objects from the stored string representation is called as Unpickling.

What are the different file processing modes supported by Python?

Python provides three modes to open files. The read-only, write-only, read-write and append mode. ‘r’ is used to open a file in read-only mode, ‘w’ is used to open a file in write-only mode, ‘rw’ is used to open in reading and write mode, ‘a’ is used to open a file in append mode. If the mode is not specified, by default file opens in read-only mode.

  • Read-only mode : Open a file for reading. It is the default mode.
  • Write-only mode: Open a file for writing. If the file contains data, data would be lost. Other a new file is created.
  • Read-Write mode: Open a file for reading, write mode. It means updating mode.
  • Append mode: Open for writing, append to the end of the file, if the file exists.

What is the shortest method to open a text file and display its content?

The shortest way to open a text file is by using “with” command in the following manner:

with open(“file-name”, “r”) as fp:
fileData = fp.read()
#to print the contents of the file
print(fileData)

What is the usage of enumerate () function in Python?

The enumerate() function is used to iterate through the sequence and retrieve the index position and its corresponding value at the same time.

For i,v in enumerate([‘Python’,’Java’,’C++’]):
print(i,v)
0 Python
1 Java
2 C++
# enumerate using an index sequence
for count, item in enumerate([‘Python’,’Java’,’C++’], 10):

What are the advantages of Python?

  • Interpreted
  • Free and open source
  • Extensible
  • Object-oriented
  • Built-in data structure
  • Readability
  • High-Level Language
  • Cross-platform
  • Interpreted: Python is an interpreted language. It does not require prior compilation of code and executes instructions directly.
  • Free and open source: It is an open source project which is publicly available to reuse. It can be downloaded free of cost.
  • Portable: Python programs can run on cross platforms without affecting its performance.
  • Extensible: It is very flexible and extensible with any module.
  • Object-oriented: Python allows to implement the Object Oriented concepts to build application solution.
  • Built-in data structure: Tuple, List, and Dictionary are useful integrated data structures provided by the language.

What is a generator in Python?

In Python, the generator is a way that specifies how to implement iterators. It is a normal function except that it yields expression in the function. It does not implements __itr__ and next() method and reduce other overheads as well.

If a function contains at least a yield statement, it becomes a generator. The yield keyword pauses the current execution by saving its states and then resume from the same when required.

What is lambda function in Python?

The anonymous function in python is a function that is defined without a name. The normal functions are defined using a keyword “def”, whereas, the anonymous functions are defined using the lambda function. The anonymous functions are also called as lambda functions.

What is swapcase() function in the Python?

It is a string’s function which converts all uppercase characters into lowercase and vice versa. It is used to alter the existing case of the string. This method creates a copy of the string which contains all the characters in the swap case. If the string is in lowercase, it generates a small case string and vice versa. It automatically ignores all the non-alphabetic characters. See an example below.

string = “IT IS IN LOWERCASE.”
print(string.swapcase())

string = “it is in uppercase.”
print(string.swapcase())
it is in lowercase.
IT IS IN UPPERCASE.

What is the usage of help() and dir() function in Python?

Help() and dir() both functions are accessible from the Python interpreter and used for viewing a consolidated dump of built-in functions.

Help() function: The help() function is used to display the documentation string and also facilitates us to see the help related to modules, keywords, and attributes.

Dir() function: The dir() function is used to display the defined symbols.