Tricky Interview Questions (Python)

The reason why I put interview questions as the title of a lot of posts is because:
1. It makes you click on the post
2. It makes you feel that these are very important questions and you can nail an interview with it
3. These are actual interview questions asked in companies like Myntra, Flipkart, BookMyShow, WNS, Sapient, etc.
4. You have to practice to become perfect. You can practice here or anywhere else. But make sure you know all the questions given below.


Toh suru karte hain bina kisi bakchodi ke
Let’s start with the questions 😛

1. Which data type is mutable and ordered in Python?
List

2. Can a dictionary contain another dictionary?
Yes, a dictionary can contain another dictionary. In fact, this is the main advantage of using dictionary data type.

3. When to use list, set or dictionaries in Python?
A list keeps order, dict and set don’t: When you care about order, therefore, you must use list (if your choice of containers is limited to these three, of course;-).

dict associates with each key a value, while list and set just contain values: very different use cases, obviously.
set requires items to be hashable, list doesn’t: if you have non-hashable items, therefore, you cannot use set and must instead use list.

4. WAP where you first create an empty list and then add the elements.
basic_list = []
basic_list.append(‘Alpha’)
basic_list.append(‘Beta’)
basic_list.append(‘Gamma’)

5. What does this mean: *args, **kwargs? And why would we use it?
We use *args when we aren’t sure how many arguments are going to be passed to a function, or if we want to pass a stored list or tuple of arguments to a function. **kwargsis used when we don’t know how many keyword arguments will be passed to a function, or it can be used to pass the values of a dictionary as keyword arguments. The identifiers args and kwargs are a convention, you could also use *bob and **billy but that would not be wise.

6. What are negative indexes and why are they used?
The sequences in Python are indexed and it consists of the positive as well as negative numbers. The numbers that are positive uses ‘0’ that is uses as first index and ‘1’ as the second index and the process goes on like that.

7. Randomly shuffle the content of a list

8. Take a random sample of 20 elements and put it in a list

9. Take a list and sort it

10. Explain split() and sub() function from the “re” package
split() – uses a regex pattern to “split” a given string into a list
sub() – finds all substrings where the regex pattern matches and then replace them with a different string

11. What are the supported data types in Python?
The most important data types include the following:
1. Number
2. String
3. List
4.Tuple
5. Dictionary
6. Set

12. What is the function to reverse a list?
list.reverse()

13. How to remove the last object from the list?
list.pop(obj=list[-1])
Removes and returns last object or obj from list.

14. What is a dictionary?
A dictionary is one of the built-in data types in Python. It defines an unordered mapping of unique keys to values. Dictionaries are indexed by keys, and the values can be any valid Python data type (even a user-defined class). Notably, dictionaries are mutable, which means they can be modified. A dictionary is created with curly braces and indexed using the square bracket notation.

15. Python is an object oriented language. What are the features of an object oriented programming language?
OOP is the programming paradigm based on classes and instances of those classes called objects. The features of OOP are:
Encapsulation, Data Abstraction, Inheritance, Polymorphism.

16. What is the difference between append() and extend() method?
Both append() and extend() methods are the methods of list. These methods a re used to add the elements at the end of the list.
append(element) – adds the given element at the end of the list which has called this method.
extend(another-list) – adds the elements of another-list at the end of the list which is called the extend method.

17. Write a program to check if a string is a palindrome?
Palindrome is a string which is symmetric like. aba, nitin, nureses run, etc

Below is the code, write it down yourself 😛

18. Take a random list and plot a histogram with 3 bins.

19. What is the different between range () and xrange () functions in Python?
range () returns a list whereas xrange () returns an object that acts like an iterator for generating numbers on demand.

20. Guess the output of the following code
x = “Fox ate the pizza”
print(x[:7])

You can find Python interview questions on many websites, we will keep on updating this list. Time for some marketing, if you want to get some more interview questions on Python, then click below:-

100 Python Questions to crack Data Science/Analyst Interview

Keep practicing 🙂

XtraMous


Leave a Reply

Your email address will not be published. Required fields are marked *