Showing posts with label basics of python interview questions. Show all posts
Showing posts with label basics of python interview questions. Show all posts

Multiple-Choice Questions (MCQs) on Python coding suitable for freshers

 Multiple-Choice Questions (MCQs) on Python coding suitable for freshers:

Multiple-Choice Questions (MCQs) on Python coding suitable for freshers


1. What does the following code output?

   ```
   print(3 * 'abc')
   ```

   a) `abcabcabc`

   b) `abc3`

   c) `9`

   d) Error


2. Which of the following statements is true about Python indentation?

   a) It is optional

   b) It is for readability only

   c) It is mandatory for indicating block of code

   d) It is used for commenting


3. What is the output of the following code?

   ```

   a = [1, 2, 3]

   b = a

   b.append(4)

   print(a)

   ```

   a) `[1, 2, 3]`

   b) `[1, 2, 3, 4]`

   c) `[1, 2, 3, 4, 4]`

   d) Error


4. Which of the following is NOT a valid Python variable name?

   a) `my_var`

   b) `2var`

   c) `_var`

   d) `var_2`


5. What will be the output of the following code?

   ```

   def add(a, b=2):

       return a + b

   print(add(3))

   ```

   a) `5`

   b) `6`

   c) `3`

   d) Error


6. What does the `range(5)` function in Python represent?

   a) A list of numbers from 0 to 4

   b) A list of numbers from 1 to 5

   c) A generator that yields numbers from 0 to 4

   d) A generator that yields numbers from 1 to 5


7. What is the correct way to open a file named "example.txt" for writing in Python?

   a) `file = open("example.txt", "w")`

   b) `file = open("example.txt", "r")`

   c) `file = open("example.txt", "a")`

   d) `file = open("example.txt", "rw")`


8. What will be the output of the following code?

   ```

   my_string = "Hello, World!"

   print(my_string[7:])

   ```

   a) `World!`

   b) `W`

   c) `o, World!`

   d) `o`


9. Which of the following is used to comment multiple lines in Python?

   a) `//`

   b) `#`

   c) `/* */`

   d) `''' '''`


10. What is the output of the following code?

    ```

    print(9 // 2)

    ```

    a) `4.5`

    b) `4`

    c) `5`

    d) `4.0`


Answers:

1. a) `abcabcabc`

2. c) It is mandatory for indicating block of code

3. b) `[1, 2, 3, 4]`

4. b) `2var`

5. a) `5`

6. c) A generator that yields numbers from 0 to 4

7. a) `file = open("example.txt", "w")`

8. a) `World!`

9. d) `''' '''`

10. b) `4`


10 interview questions on Python along with their answers:


1. What is Python?

   Python is a high-level, interpreted programming language known for its simplicity and readability. It supports multiple programming paradigms like procedural, object-oriented, and functional programming.


2. What are the key features of Python?

   Python boasts several key features including:

   - Easy-to-read syntax

   - Dynamic typing

   - Automatic memory management

   - Extensive standard library

   - Cross-platform compatibility


3. What is PEP 8?

   PEP 8 is the Python Enhancement Proposal that provides guidelines and best practices for writing Python code. It covers topics such as code layout, naming conventions, and programming style.


4. What is the difference between lists and tuples in Python?

   Lists and tuples are both sequences in Python, but the main difference is that lists are mutable (can be changed) while tuples are immutable (cannot be changed). Lists are defined with square brackets `[]`, whereas tuples are defined with parentheses `()`.


5. What is the difference between `==` and `is` in Python?

   In Python, `==` is used to check if the values of two objects are equal, while `is` is used to check if the objects themselves are the same (i.e., they have the same memory address).


6. What is a decorator in Python?

   A decorator is a design pattern in Python that allows behavior to be added to functions or methods dynamically. Decorators are functions that take another function as an argument and extend its behavior without modifying its code directly.


7. Explain the concept of list comprehension.

   List comprehension is a concise way to create lists in Python. It allows you to generate a new list by applying an expression to each item in an existing iterable (such as a list, tuple, or range) and filtering the items based on a condition.


8. What is the purpose of the `__init__` method in Python classes?

   The `__init__` method is a special method in Python classes that is automatically called when a new instance of the class is created. It is used to initialize the object's attributes and perform any necessary setup.


9. What are modules in Python?

   Modules in Python are files containing Python code that define functions, classes, and variables. They allow you to organize your code into reusable units and are imported using the `import` statement.


10. What is the difference between `__str__` and `__repr__` in Python?

    Both `__str__` and `__repr__` are special methods used to represent objects as strings, but they serve different purposes. `__str__` is called by the `str()` function and is meant to provide a readable representation of an object for end-users, while `__repr__` is called by the `repr()` function and is meant to provide an unambiguous representation of an object for developers.