Python interview questions

Python quiz questions

  • 1.

    Explain how to delete a file in Python?

    Answer:

    By using a command os.remove (filename) or os.unlink(filename)

    View
  • 2.

    Explain how can you make a Python Script executable on Unix?

    Answer:

    To make a Python Script executable on Unix, you need to do two things,

    • Script file’s mode must be executable and
    • the first line must begin with # ( #!/usr/local/bin/python)
    View
  • 3.

    How can you share global variables across modules?

    Answer:

    To share global variables across modules within a single program, create a special module. Import the config module in all modules of your application. The module will be available as a global variable across modules.

    View
  • 4.

    Mention what are the rules for local and global variables in Python?

    Answer:

    Local variables: If a variable is assigned a new value anywhere within the function’s body, it’s assumed to be local.

    Global variables: Those variables that are only referenced inside a function are implicitly global.

    View
  • 5.

    What is module and package in Python?

    Answer:

    In Python, module is the way to structure program. Each Python program file is a module, which imports other modules like objects and attributes.

    The folder of Python program is a package of modules.  A package can have modules or subfolders.

    View
  • 6.

    What is the difference between Xrange and range?

    Answer:

    Xrange returns the xrange object while range returns the list, and uses the same memory and no matter what the range size is.

    View
  • 7.

    How you can convert a number to a string?

    Answer:

    In order to convert a number into a string, use the inbuilt function str().  If you want a octal or hexadecimal representation, use the inbuilt function oct() or hex().

    View
  • 8.

    What is negative index in Python?

    Answer:

    Python sequences can be index in positive and negative numbers.   For positive index, 0 is the first index, 1 is the second index and so forth.  For negative index, (-1) is the last index and (-2) is the second last index and so forth.

    View
  • 9.

    How can you copy an object in Python?

    Answer:

    To copy an object in Python, you can try copy.copy () or copy.deepcopy() for the general case. You cannot copy all objects but most of them.

    View
  • 10.

    What is docstring in Python?

    Answer:

    A Python documentation string is known as docstring, it is a way of documenting Python functions, modules and classes.

    View
  • 11.

    What are generators in Python?

    Answer:

    The way of implementing iterators are known as generators.  It is a normal function except that it yields expression in the function.

    View
  • 12.

    In Python what is slicing?

    Answer:

    A mechanism to select a range of items from sequence types like list, tuple, strings etc. is known as slicing.

    View
  • 13.

    What is unittest in Python?

    Answer:

    A unit testing framework in Python is known as unittest.  It supports sharing of setups, automation testing, shutdown code for tests, aggregation of tests into collections etc.

    View
  • 14.

    In Python what are iterators?

    Answer:

    In Python, iterators are used to iterate a group of elements, containers like list.

    View
  • 15.

    What is pass in Python?

    Answer:

    Pass means, no-operation Python statement, or in other words it is a place holder in compound statement, where there should be a blank left and nothing has to be written there.

    View
  • 16.

    Why lambda forms in python does not have statements?

    Answer:

    A lambda form in python does not have statements as it is used to make new function object and then return them at runtime.

    View
  • 17.

    What is lambda in Python?

    Answer:

    It is a single expression anonymous function often used as inline function.

    View
  • 18.

    What is namespace in Python?

    Answer:

    In Python, every name introduced has a place where it lives and can be hooked for. This is known as namespace. It is like a box where a variable name is mapped to the object placed.  Whenever the variable is searched out, this box will be searched, to get corresponding object.

    View
  • 19.

    What are the built-in type does python provides?

    Answer:

    There are mutable and Immutable types of Pythons built in types Mutable built-in types

    • List
    • Sets
    • Dictionaries

    Immutable built-in types

    • Strings
    • Tuples
    • Numbers
    View
  • 20.

    What is Dict and List comprehensions are?

    Answer:

    They are syntax constructions to ease the creation of a Dictionary or List based on existing iterable.

    View

© 2017 QuizBucket.org