Test your knowledge with this easy-level Python interview quiz covering Python basics, data structures, SQL, APIs, machine learning concepts, and behavioral questions. Perfect for candidates preparing for technical interviews and seeking to master fundamental Python and tech interview topics.
In Python, what is the main difference between the operators 'is' and '==' when comparing two variables?
Explanation: 'is' verifies whether two variables refer to the same object in memory, making it an identity comparison. In contrast, '==' compares the actual values of the objects, not their locations. The remaining options are incorrect because they either reverse the definitions or introduce unrelated concepts like comparing string lengths or specific data types.
Which of these types is an example of an immutable object in Python?
Explanation: A tuple is immutable, meaning its elements cannot be changed after creation. Lists, dictionaries, and sets are mutable, allowing items to be added, removed, or changed. The distractors—list, dictionary, and set—all support in-place modification, so they are not immutable.
How does Python implement a heap, and in what scenario is it most useful?
Explanation: Python typically implements heaps as binary heaps stored within lists, making them suitable for efficiently managing priority queues. Arrays, dictionaries, and sets are implemented differently and serve other purposes such as linear search, look-ups, and membership testing. Therefore, those options are less appropriate when discussing heaps.
What is one major difference between linear search and binary search algorithms?
Explanation: Binary search is efficient but only works on sorted data, whereas linear search can work with unsorted sequences but is usually slower for large datasets. The other options are incorrect because recursion is not required for linear search, and binary search cannot work with unsorted lists. Linear search does not use hash tables, nor does binary search specifically rely on arrays.
Which method can help detect SQL injection vulnerabilities in Python database queries?
Explanation: Parameterized queries and prepared statements prevent SQL injection by keeping user input separate from the SQL command. Concatenating user input makes code vulnerable, print statements do not detect vulnerabilities, and encrypting fields does not directly address injection attacks. These distractors provide less secure or unrelated approaches.
Which strategy is commonly used to prevent overfitting in a machine learning model?
Explanation: By splitting data into training and validation sets, you can monitor how well a model generalizes and mitigate overfitting. Only training on the full dataset may lead to overfitting, and ignoring feature tuning is not a preventive action. Reducing training data size does not necessarily prevent overfitting, and can even reduce accuracy.
When designing a URL shortener, what is a typical way to generate short, unique keys?
Explanation: Base62 encoding produces short and unique identifiers using alphanumeric characters, which are compact and easy to use. Sequential numbers are easy to guess and less secure, while encrypting the whole URL is inefficient for shortening. Appending random letters may not ensure uniqueness or predictability.
How does Python primarily manage memory allocation and deallocation for objects?
Explanation: Python automatically manages memory using reference counting and a garbage collector to remove objects that are no longer in use. Manual memory management is not typical in Python, global variable tracking alone doesn't address memory management, and using memory leaks is an error, not a feature.
What makes a deep copy different from a shallow copy in Python?
Explanation: A deep copy duplicates the object and all objects contained within it, ensuring no shared references. A shallow copy only copies the outer object, meaning changes to nested items may affect both. The other options wrongly describe how copy operations work or misstate their effects.
Given the list items = [2, 4, 6, 8], what does items[-1] return?
Explanation: The index -1 in Python lists refers to the last element, so items[-1] returns 8. The options 4 and 2 correspond to other indices, and IndexError is only returned if the index is out of range, which is not the case here.
Which of the following can be used as a key in a Python dictionary?
Explanation: Dictionary keys in Python must be immutable and hashable; strings qualify as both. Lists, dictionaries, and sets are mutable and cannot be used as dictionary keys, which is why they would cause a TypeError if attempted.
Which statement is used in Python to handle exceptions and prevent a program from crashing?
Explanation: The try-except block is used to catch and handle exceptions in Python, allowing the program to recover gracefully. Loop-while and if-else are for control flow and not for exception handling, while define-end is not valid Python syntax.
In Python, what is the primary use of a lambda function?
Explanation: Lambda functions are concise, anonymous functions useful for simple, short tasks. They are not used for class methods, cannot span multiple lines, and are unrelated to generating configuration files. The other options either misuse lambdas or describe unrelated tasks.
What does the STAR method stand for in behavioral interview answers?
Explanation: The STAR method helps structure answers around Situation, Task, Action, and Result, making responses clear and organized. The other phrases are incorrect expansions or do not reflect common interview frameworks.
What does the following code return: [x * 2 for x in range(3)]?
Explanation: This list comprehension returns [0, 2, 4] by multiplying each integer from 0 to 2 by 2. Options [1, 3, 5] and [2, 4, 6] do not fit the calculation, and [0, 2, 3] includes an incorrect final value.
Which symbol is used to write a single-line comment in Python?
Explanation: Python uses the '#' character for single-line comments. The '//' and '/*' symbols are used in other languages, while 'u003C!--' is for comments in markup languages, not Python.