Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

Python Interview Questions and Answer Part – 1

What Python function is used to open files?

  • open_file()
  • file()
  • new_file()
  • open() (Ans)

What does the pass keyword do?

  • Passes control to another function
  • Continues to the next iteration of a loop
  • it is a no-op (Ans)
  • Raises an exception

What are with-blocks used for?

  • Writing to files
  • Defining functions
  • Resource management (Ans)
  • Looping over sequences

Which one of the following is true?

  • If present, the module docstring must begin on the first non-empty line in the module
  • The first statement in the module must be an import statement
  • If present, the shebang comment must be the first non-empty line in the module (Ans)

What does the in operator do for lists?

  • Inserts an element in to the list
  • Runs a function for each element in the list
  • Duplicates the list
  • Checks for membership of a value (Ans)

What is the standard package that provides support for building and installing additional modules into a Python installation?

  • fixture
  • pkgutils
  • palindrome
  • distutils (Ans)

Where must keyword arguments come when calling a function?

  • Before positional arguments
  • After the closing parenthesis
  • They can appear anywhere
  • After positional arguments (Ans)

Python source files can contain multiple classes.

  • False
  • True (Ans)

The control flow construct for nested conditionals uses which form:

  • elif: (Ans)
  • else if:
  • elsif:
  • elseif:

What does a comprehension’s filtering predicate do?

  • Prevents certain values from going into the collection (Ans)
  • Modifies the values going into the collection
  • Allows sets to contain duplicates
  • Determines the actual type of collection constructed

Can classes have docstrings?

  • Yes (Ans)
  • No

Why is the first command line argument in sys.argv[1]

  • Because Python lists use one-based indexing
  • Because the program filename is in sys.argv [0] (Ans)
  • Because the program module name is in sys.argv[0]

What does the LEGB acronym stand for?

  • Local, Enclosing, Global, Built-in (Ans)
  • Lookup, Enable, Gateway, Backup
  • Link, Extension, Generic, Binary
  • Local, Embedded, Global, Built-in

The REPL always displays the value of the entered expression.

  • False (Ans)
  • True

How do you define a test function on a TestCase?

  • Manually add it to the test_functions list
  • Start the function name with test_ (Ans)
  • Add the word test-function to the function’s docstring
  • End the function name with _test

How do you replace an element in a list?

  • the square-bracket operator (Ans)
  • the cons() method
  • the replace() method
  • you can’t because lists are immutable

What does the global keyword do?

  • Declares a function as global
  • Defines a global variable
  • Allows assignment to globals in a function (Ans)
  • Exports functions from a namespace

Copying a list does a deep copy of the list’s.

  • True
  • False (Ans)

How is iteration termination signalled in Python?

  • With a special end token
  • By raising an IndexError exception
  • By raising the StopIteration exception (Ans)
  • By calling a user-provided callback function

Why should you follow existing patterns when choosing exception types?

  • To improve program performance
  • To simplify integration of your code (Ans)
  • To avoids SyntaxErrors
  • To avoid documenting your APIs

When is top level module code executed?

  • Only the first time a module is imported (Ans)
  • Every time the module is imported

What is the Python 3.3 tool for creating virtual environments?

  • create_env
  • virtenv
  • pyvenv (Ans)
  • makeenv

Which pdb function will stop program execution?

  • pdb.set_trace() (Ans)
  • pdb.break()
  • pdb.debug()
  • pdb.halt()
Rajesh Kumar
Follow me
Latest posts by Rajesh Kumar (see all)