Python interview questions

Python quiz questions

  • 1.

    How To Convert A List Into Other Data Types?

    Answer:

    Sometimes, we don’t use lists as is. Instead, we have to convert them to other types.

    View
  • 2.

    Why And When Do You Use Generators In Python?

    Answer:

    A generator in Python is a function which returns an iterable object. We can iterate on the generator object using the <yield> keyword. But we can only do that once because their values don’t persist in memory, they get the values on the fly.

    Generators give us the ability to hold the execution of a function or a step as long as we want to keep it. However, here are a few examples where it is beneficial to use generators.

    • We can replace loops with generators for efficiently calculating results involving large data sets.
    • Generators are useful when we don’t want all the results and wish to hold back for some time.
    • Instead of using a callback function, we can replace it with a generator. We can write a loop inside the function doing the same thing as the callback and turns it into a generator.
    View
  • 3.

    List Down Some Of The PDB Commands For Debugging Python Programs?

    Answer:

    Here are a few PDB commands to start debugging Python code.

    • Add breakpoint – <b>
    • Resume execution – <c>
    • Step by step debugging – <s>
    • Move to next line – <n>
    • List source code – <l>
    • Print an expression – <p>
    View
  • 4.

    How Do You Debug A Program In Python? Is It Possible To Step Through Python Code?

    Answer:

    Yes, we can use the Python debugger (<pdb>) to debug any Python program. And if we start a program using <pdb>, then it let us even step through the code.

    View
  • 5.

    How do you Use To Convert A Number To A String?

    Answer:

    For converting a number into a string, you can use the built-in function <str()>.  If you want an octal or hexadecimal representation, use the inbuilt function <oct()> or <hex()>.

    View
  • 6.

    What Is The Purpose Of Doc Strings In Python?

    Answer:

    In Python, documentation string is popularly known as doc strings. It sets a process of recording Python functions, modules, and classes.

    View
  • 7.

    What Are Different Methods To Copy An Object In Python?

    Answer:

    There are two ways to copy objects in Python.

    • copy.copy() function
      • It makes a copy of the file from source to destination.
      • It’ll return a shallow copy of the parameter.
    • copy.deepcopy() function
      • It also produces the copy of an object from the source to destination.
      • It’ll return a deep copy of the parameter that you can pass to the function.
    View
  • 8.

    Explain the <Self> Keyword Do?

    Answer:

    The <self> keyword is a variable that holds the instance of an object. In almost, all the object-oriented languages, it is passed to the methods as hidden parameter.

    View
  • 9.

    What Are The Optional Statements That Can Be Used Inside A <Try-Except> Block In Python?

    Answer:

    There are two optional clauses you can use in the <try-except> block.

    • The <else> clause
      • It is useful if you want to run a piece of code when the try block doesn’t create any exception.
    • The <finally> clause
      • It is useful when you want to execute some steps which run, irrespective of whether there occurs an exception or not.
    View
  • 10.

    Is There A Switch Or Case Statement In Python? If Not Then What Is The Reason For The Same?

    Answer:

    No, Python does not have a Switch statement, but you can write a Switch function and then use it.

    View

© 2017 QuizBucket.org