Header Ads

  • Breaking Now

    Short Q&A for Python - II

    Q1. What are four main types of namespaces in Python?
    A: The four main types of namespaces in Python are defined as below:

    1. Global,
    2. Local,
    3. Module and
    4. Class namespaces.

    Q2. Explain the difference between local and global namespaces.
    A: The local namespaces are created within a function, whenever that function is called. Global name spaces are created when the program starts.

    Q3. While defining a class, what does the __ init_O function do?
    A:__ init_O function overrides any initialization from an inherited class, and is called when the class is instantiated.

    Q4. What is a lambda operator?
    A: A lambda operator is used to create anonymous functions and mostly used in cases where one wishes to pass functions as parameters. or assign them to variable names.

    Q5. How does a function return values?
    A: Functions return values using the return statement.

    Q6. When would you use triple quotes as a delimiter?
    A:Triple quotes ‘’”” or ‘“ are string delimiters that span multiple lines in Python. Triple quotes are usually used when spanning multiple lines, or enclosing a string that has a mix of single and double quotes contained therein.

    Q7. What happens when a function doesn’t have a return statement? Is this valid?
    A: Yes, it is valid. The function will return a None object. The end of a function is defined by a block of code being executed not by any explicit keyword.

    Q8. Explain the use of break and continue in Python looping.
    A: The break statement stops the execution of the current loop. and transfers control to the next block. The continue statement ends the current block’s execution and jumps to the next iteration of the loop.

    Q9. When do you use a continue statement in a for loop?
    A: The intent of continue statement is, “I’m done processing this specific item, move on to the next item.” So the moment when processing of a particular item is complete; in order to move on to next, without executing further processing in the block.

    Q10. When do you use a break statement in a for loop?
    A: Whenever in a loop, the purpose of looping is served, break statement is used. For example. after finding an item in a list searched for, there is no need to keep looping. The moment the break statement is encountered, moving on to the next block of code takes place.

    Q11. How do you use GUI that comes with Python to test your code?
    A:That is just an editor and a graphical version of the interactive shell.There is no automated testing.

    Q12. Why is that none of my threads are not running? How can I make it work?
    A: The moment main thread exits, all threads are killed. When main thread runs too quickly, gives threads no time to do any work.
    A simple fix is to add a sleep to the end of the program that’s long enough for all the threads to finish.

    Post Top Ad

    Post Bottom Ad