top of page
  • Facebook
  • Twitter
  • Instagram

Top Python Interview Questions

Mobiprep handbooks are free downloadable placement preparation resources that can help you ace any company placement process. We have curated a list of the 40 most important MCQ questions which are asked in most companies such as Infosys, TCS, Wipro, Accenture, etc. The placement handbooks also have detailed explanations for every question ensuring you revise all the questions with detailed answers and explanations.

Question

17

Explanation

How do you create text files in python?

We can create the text files by using the syntax:
Variable name=open (“file.txt”, file mode)
For ex: f= open ("hello.txt","w+")
> We declared the variable f to open a file named hello.txt. Open takes 2 arguments, the file that we want to open and a string that represents the kinds of permission or
operation we want to do on the file
> Here we used "w" letter in our argument, which indicates write and the plus sign that means it will create a file if it does not exist in library.

Question

18

Explanation

List and describe file modes in python.

> 'r' This is the default mode. It Opens file for reading.
> 'w' This Mode Opens file for writing. If file does not exist, it creates a new file. If file exists it truncates the file.
> 'x' Creates a new file. If file already exists, the operation fails.
> 'a' Open file in append mode. If file does not exist, it creates a new file.
> 't' This is the default mode. It opens in text mode.
> 'b' This opens in binary mode.
> '+' This will open a file for reading and writing (updating)

Question

19

Explanation

What do you mean by *args **kwargs?

We can use *args when we are not sure how many args we are going to pass to a function. We can also use if we want to pass a stored list or tuple of arguments to a function.
**kwargs is used when we don’t know how many keyword arguments will be passed to a function, or it can be used to pass the values of a dictionary as keyword arguments.
The *args and **kwargs are a convention.You can also use *hello and **world.

Question

20

Explanation

What is the purpose of PYTHONSTARTUP, PYTHONCASEOK and PYTHONHOME environment variable?

1. PYTHONSTARTUP: It contains the path of an initialization file containing Python source code. It is executed every time you start the interpreter. It is named as .pythonrc.py in Unix and it contains commands that load utilities or modify PYTHONPATH.
2. PYTHONCASEOK: It is used in Windows to instruct Python to find the first case-insensitive match in an import statement. Set this variable to any value to activate it.
3. PYTHONHOME: It is an alternative module search path. It is usually embedded in the PYTHONSTARTUP or PYTHONPATH directories to make switching module libraries easy.

bottom of page