InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
What is the difference between Python Arrays and lists? |
Answer»
|
|
| 2. |
Explain how can you make a Python Script executable on Unix? |
| Answer» | |
| 3. |
What is slicing in Python? |
Answer»
|
|
| 4. |
What is docstring in Python? |
Answer»
|
|
| 5. |
What are unit tests in Python? |
Answer»
|
|
| 6. |
What is break, continue and pass in Python? |
||||||
Answer»
|
|||||||
| 7. |
What is __init__? |
|
Answer» __init__ is a contructor method in Python and is automatically called to allocate memory when a new object/instance is created. All classes have a __init__ method associated with them. It helps in distinguishing METHODS and attributes of a class from LOCAL variables. # class definitionclass Student: def __init__(self, fname, lname, age, section): self.firstname = fname self.lastname = lname self.age = age self.section = section# creating a new objectstu1 = Student("SARA", "Ansh", 22, "A2") |
|
| 8. |
What is the use of self in Python? |
|
Answer» Self is used to represent the INSTANCE of the class. With this keyword, you can access the ATTRIBUTES and methods of the class in python. It BINDS the attributes with the given ARGUMENTS. self is used in different places and often thought to be a keyword. But unlike in C++, self is not a keyword in Python. |
|
| 9. |
What are global, protected and private attributes in Python? |
Answer»
|
|
| 10. |
What are modules and packages in Python? |
|
Answer» Python packages and Python modules are two mechanisms that allow for MODULAR programming in Python. Modularizing has several advantages -
Modules, in general, are simply Python files with a .py EXTENSION and can have a set of functions, classes, or variables defined and implemented. They can be imported and initialized once using the import statement. If partial functionality is needed, import the requisite classes or functions using from foo import bar. Packages allow for hierarchial structuring of the module namespace using dot notation. As, modules help avoid clashes between global variable names, in a similar manner, packages help avoid clashes between module names. Note: You can technically import the package as well, but alas, it doesn't import the modules within the package to the local namespace, thus, it is practically useless. |
|
| 11. |
What is pass in Python? |
|
Answer» The pass KEYWORD represents a null operation in Python. It is generally used for the purpose of filling up empty blocks of code which may execute during runtime but has yet to be WRITTEN. Without the pass statement in the following code, we may RUN into some ERRORS during code execution. def myEmptyFunc(): # do nothing passmyEmptyFunc() # nothing happens## Without the pass keyword# File "<STDIN>", line 3# IndentationError: expected an indented block |
|
| 12. |
What are the common built-in data types in Python? |
||||||||||||||||||||||||||||||||||
|
Answer» There are several built-in data types in Python. Although, Python doesn't require data types to be defined explicitly during variable declarations type errors are LIKELY to occur if the knowledge of data types and their compatibility with each other are NEGLECTED. Python provides type() and isinstance() functions to check the type of these variables. These data types can be grouped into the following categories-
Note: The standard library also includes fractions to store rational numbers and decimal to store floating-point numbers with user-defined precision.
Note: The standard library also includes additional types for processing:
A mapping object can MAP hashable values to random objects in Python. Mappings objects are mutable and there is currently only one standard mapping type, the dictionary.
Note: set is mutable and thus cannot be used as key for a dictionary. On the other hand, frozenset is immutable and thus, hashable, and can be used as a dictionary key or as an element of another set.
|
|||||||||||||||||||||||||||||||||||
| 13. |
What are lists and tuples? What is the key difference between the two? |
|
Answer» Lists and Tuples are both sequence data types that can store a collection of objects in Python. The objects stored in both sequences can have DIFFERENT data types. Lists are represented with square brackets ['sara', 6, 0.19], while tuples are represented with parantheses ('ansh', 5, 0.97). |
|
| 14. |
What is Scope in Python? |
|
Answer» Every OBJECT in Python functions within a scope. A scope is a block of code where an object in Python remains relevant. Namespaces uniquely IDENTIFY all the objects inside a program. However, these namespaces also have a scope defined for them where you could use their objects without any prefix. A few EXAMPLES of scope CREATED during code execution in Python are as follows:
Note: Local scope objects can be synced with global scope objects using keywords such as global. |
|
| 15. |
What is PEP 8 and why is it important? |
|
Answer» PEP stands for Python Enhancement PROPOSAL. A PEP is an official design document providing information to the Python community, or describing a new FEATURE for Python or its processes. PEP 8 is ESPECIALLY IMPORTANT since it documents the style guidelines for Python Code. Apparently contributing to the Python open-source community REQUIRES you to follow these style guidelines sincerely and strictly. |
|
| 16. |
What is an Interpreted language? |
|
Answer» An Interpreted LANGUAGE EXECUTES its statements line by line. Languages such as Python, Javascript, R, PHP, and Ruby are prime examples of Interpreted languages. Programs written in an interpreted language runs directly from the source code, with no INTERMEDIARY COMPILATION step. |
|
| 17. |
What is a dynamically typed language? |
|
Answer» Before we understand a dynamically TYPED language, we should learn about what typing is. Typing refers to type-checking in programming languages. In a strongly-typed language, such as Python, "1" + 2 will result in a type error since these languages don't allow for "type-coercion" (IMPLICIT conversion of data types). On the other hand, a weakly-typed language, such as Javascript, will simply output "12" as result. Type-checking can be DONE at two stages -
Python is an INTERPRETED language, executes each statement line by line and thus type-checking is done on the fly, during execution. Hence, Python is a Dynamically Typed Language. |
|
| 18. |
What is Python? What are the benefits of using Python |
|
Answer» Python is a high-level, interpreted, general-purpose programming LANGUAGE. Being a general-purpose language, it can be used to build ALMOST any type of application with the right tools/libraries. Additionally, python supports objects, modules, threads, exception-handling, and automatic memory management which HELP in modelling real-world problems and building applications to solve these problems. Benefits of using Python:
Improve your chances of cracking the PYTHON interview Majority ELEMENT EasyAsked in BEST Time to Buy and Sell Stocks EasyAsked in Merge two sorted lists EasyAsked in View All Practice Questions |
|