1. What is the output of the following code?
2. Which of the following functions creates an array with random values between 0 and 1?
A)
B)
C)
D)
3. Write a function that takes a 2D NumPy array and returns the sum of all elements in each row.
4. What will be printed by this code?
5. Explain the difference between
6. How do you efficiently reshape a 1D array of 100 elements into a 10x10 matrix?
7. What is the result of
8. Write a program to generate a 3D array of shape (2, 3, 4) filled with random integers between 0 and 9.
9. What happens when you use
10. Which method can be used to find the indices of non-zero elements in a NumPy array?
11. What is the output of this code?
12. Describe how broadcasting works in NumPy with an example.
13. Write a function that normalizes each column of a 2D NumPy array using z-score normalization.
14. What is the purpose of
15. What does
16. How would you perform element-wise multiplication of two arrays of different shapes using broadcasting?
17. Write a program to compute the dot product of two large 2D arrays without using loops.
18. What is the difference between
19. How can you efficiently remove duplicate rows from a 2D NumPy array?
20. Explain the use of
#NumPy #AdvancedPython #DataScience #ScientificComputing #PythonLibrary #NumericalComputing #ArrayProgramming #MachineLearning #PythonDeveloper #CodeQuiz #HighLevelNumPy
By: @DataScienceQ 🚀
import numpy as np
a = np.array([1, 2, 3])
b = a + 1
a[0] = 99
print(b[0])
2. Which of the following functions creates an array with random values between 0 and 1?
A)
np.random.randint() B)
np.random.randn() C)
np.random.rand() D)
np.random.choice()3. Write a function that takes a 2D NumPy array and returns the sum of all elements in each row.
4. What will be printed by this code?
import numpy as np
x = np.array([1, 2, 3])
y = x.view()
y[0] = 5
print(x)
5. Explain the difference between
np.copy() and np.view().6. How do you efficiently reshape a 1D array of 100 elements into a 10x10 matrix?
7. What is the result of
np.dot(np.array([1, 2]), np.array([[1], [2]]))?8. Write a program to generate a 3D array of shape (2, 3, 4) filled with random integers between 0 and 9.
9. What happens when you use
np.concatenate() on arrays with incompatible shapes?10. Which method can be used to find the indices of non-zero elements in a NumPy array?
11. What is the output of this code?
import numpy as np
arr = np.arange(10)
result = arr[arr % 2 == 0]
print(result)
12. Describe how broadcasting works in NumPy with an example.
13. Write a function that normalizes each column of a 2D NumPy array using z-score normalization.
14. What is the purpose of
np.fromfunction() and how would you use it to create a 3x3 array where each element is the sum of its indices?15. What does
np.isclose(a, b) return and when is it preferred over ==?16. How would you perform element-wise multiplication of two arrays of different shapes using broadcasting?
17. Write a program to compute the dot product of two large 2D arrays without using loops.
18. What is the difference between
np.array() and np.asarray()?19. How can you efficiently remove duplicate rows from a 2D NumPy array?
20. Explain the use of
np.einsum() and provide an example for computing the trace of a matrix.#NumPy #AdvancedPython #DataScience #ScientificComputing #PythonLibrary #NumericalComputing #ArrayProgramming #MachineLearning #PythonDeveloper #CodeQuiz #HighLevelNumPy
By: @DataScienceQ 🚀
1. What is the output of the following code?
2. Which of the following functions is used to create an array with values spaced at regular intervals?
A)
B)
C)
D) All of the above
3. Write a function that takes a 1D NumPy array and returns a new array where each element is squared, but only if it’s greater than 5.
4. What will be printed by this code?
5. Explain the difference between
6. How would you efficiently compute the outer product of two vectors using NumPy?
7. What is the result of
8. Write a program to generate a 5x5 matrix filled with random integers from 1 to 100, then find the maximum value in each row.
9. What happens when you use
10. Which method can be used to flatten a multi-dimensional array into a 1D array without copying data?
11. What is the output of this code?
12. Describe how
13. Write a function that calculates the Euclidean distance between all pairs of points in a 2D array of coordinates.
14. What is the purpose of
15. How do you perform matrix multiplication using
16. Write a program to filter out all elements in a 2D array that are outside the range [10, 90].
17. What does
18. How can you efficiently transpose a large 3D array of shape (100, 100, 100) using
19. Explain the concept of "views" vs "copies" in NumPy and give an example where a view leads to unexpected behavior.
20. Write a function that computes the covariance matrix of a dataset represented as a 2D NumPy array.
#NumPy #AdvancedPython #DataScience #InterviewPrep #PythonLibrary #ScientificComputing #MachineLearning #CodingChallenge #HighLevelNumPy #PythonDeveloper #TechnicalInterview #DataAnalysis
By: @DataScienceQ 🚀
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = a.T
b[0, 0] = 99
print(a)
2. Which of the following functions is used to create an array with values spaced at regular intervals?
A)
np.linspace() B)
np.arange() C)
np.logspace() D) All of the above
3. Write a function that takes a 1D NumPy array and returns a new array where each element is squared, but only if it’s greater than 5.
4. What will be printed by this code?
import numpy as np
x = np.array([1, 2, 3])
y = x.copy()
y[0] = 5
print(x[0])
5. Explain the difference between
np.meshgrid() and np.mgrid in generating coordinate matrices.6. How would you efficiently compute the outer product of two vectors using NumPy?
7. What is the result of
np.sum(np.eye(3), axis=1)?8. Write a program to generate a 5x5 matrix filled with random integers from 1 to 100, then find the maximum value in each row.
9. What happens when you use
np.resize() on an array with shape (3,) to resize it to (5,)?10. Which method can be used to flatten a multi-dimensional array into a 1D array without copying data?
11. What is the output of this code?
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
result = arr[[0, 1], [1, 2]]
print(result)
12. Describe how
np.take() works and provide an example using a 2D array.13. Write a function that calculates the Euclidean distance between all pairs of points in a 2D array of coordinates.
14. What is the purpose of
np.frombuffer() and when might it be useful?15. How do you perform matrix multiplication using
np.matmul() and @ operator? Are they always equivalent?16. Write a program to filter out all elements in a 2D array that are outside the range [10, 90].
17. What does
np.nan_to_num() do and why is it important in numerical computations?18. How can you efficiently transpose a large 3D array of shape (100, 100, 100) using
np.transpose() or swapaxes()?19. Explain the concept of "views" vs "copies" in NumPy and give an example where a view leads to unexpected behavior.
20. Write a function that computes the covariance matrix of a dataset represented as a 2D NumPy array.
#NumPy #AdvancedPython #DataScience #InterviewPrep #PythonLibrary #ScientificComputing #MachineLearning #CodingChallenge #HighLevelNumPy #PythonDeveloper #TechnicalInterview #DataAnalysis
By: @DataScienceQ 🚀
What are the implications of using
Answer:
Using
For example:
While `__slots__` improves memory efficiency—especially in classes with many instances—it must be used carefully, particularly when dealing with inheritance or when dynamic attribute assignment is needed.
#Python #AdvancedPython #MemoryOptimization #Performance #OOP #PythonInternals
By: @DataScienceQ 🚀
__slots__ in Python classes, and how can it affect memory usage, performance, and inheritance? Answer:
Using
__slots__ in Python classes allows you to explicitly declare the attributes a class can have, which reduces memory usage by preventing the creation of a dict__dict__ for each instance. This results in faster attribute access since attributes are stored in a fixed layout rather than a dictionary. However, __slots__ restricts the ability to add new attributes dynamically, disables certain fedictike __dict__ and __weakref__, and complicates multiple inheritance because of potential conflicts between slot definitions in parent classes.For example:
class Point:
__slots__ = ['x', 'y']
def __init__(self, x, y):
self.x = x
self.y = y
p = Point(1, 2)
# p.z = 3 # This will raise an AttributeError
While `__slots__` improves memory efficiency—especially in classes with many instances—it must be used carefully, particularly when dealing with inheritance or when dynamic attribute assignment is needed.
#Python #AdvancedPython #MemoryOptimization #Performance #OOP #PythonInternals
By: @DataScienceQ 🚀
What is the difference between
Answer:
For example:
Use `@classmethod
#Python #AdvancedPython #OOP #ClassMethods #StaticMethods #PythonInternals
By: @DataScienceQ 🚀
@classmethod and @staticmethod in Python, and when should each be used? Answer:
@classmethod receives the class (cls) as its first argument and is used to define methods that operate on the class itself rather than instances. It can modify class state or create alternative constructors. @staticmethod, on the other hand, does not receive any implicit first argument (neither self nor cls) and behaves like a regular function bound to the class namespace. It cannot access or modify class or instance state.For example:
class MyClass:
count = 0
def __init__(self):
MyClass.count += 1
@classmethod
def get_count(cls):
return cls.count
@staticmethod
def helper_method(x):
return x * 2
print(MyClass.get_count()) # 0 initially
obj = MyClass()
print(MyClass.get_count()) # 1
print(MyClass.helper_method(5)) # 10
Use `@classmethod
for factory methods or operations affecting the class, and @staticmethod` for utility functions logically related to the class but independent of its state.#Python #AdvancedPython #OOP #ClassMethods #StaticMethods #PythonInternals
By: @DataScienceQ 🚀
What is the purpose of
Answer:
The
For example:
By overriding
#Python #AdvancedPython #Metaclasses #OOP #PythonInternals #CustomClassCreation
By: @DataScienceQ 🚀
__prepare__ in Python metaclasses, and how does it influence the creation of class dictionaries? Answer:
The
__prepare__ method is a class method defined in a metaclass that allows custom control over the namespace dictionary used when creating a new class. It is called before the class body executes and returns a dictionary-like object (e.g., dict, OrderedDict) that will serve as the class namespace. This enables metaclasses to define custom behaviors for attribute ordering, validation, or even use non-standard data structures.For example:
class OrderedMeta(type):
@classmethod
def __prepare__(cls, name, bases, **kwargs):
return OrderedDict()
class MyClass(metaclass=OrderedMeta):
a = 1
b = 2
print(list(MyClass.__dict__.keys())) # ['a', 'b'] - ordered
By overriding
__prepare__, you can ensure that class attributes are stored in a specific order or with additional constraints, making it powerful for frameworks requiring predictable attribute behavior.#Python #AdvancedPython #Metaclasses #OOP #PythonInternals #CustomClassCreation
By: @DataScienceQ 🚀
Advanced Python Interview Preparation Test (20 Questions)
1. Which of the following is NOT a valid way to create a dictionary in Python?
A)
B)
C)
D)
2. What will be the output of the following code?
3. Write a Python function that takes a list of integers and returns a new list containing only the even numbers using a list comprehension.
4. Explain the difference between
5. Which decorator is used to define a class method in Python?
A)
B)
C)
D)
6. What does the
A) Accepts keyword arguments
B) Accepts any number of positional arguments
C) Accepts a single argument
D) Accepts only integer values
7. What will be the output of the following code?
8. Write a generator function that yields the Fibonacci sequence up to a given number n.
9. Describe how the GIL (Global Interpreter Lock) affects multithreading in Python.
10. What is the purpose of the
11. Which of the following statements about Python's garbage collector is true?
A) It uses reference counting exclusively
B) It uses both reference counting and a cyclic garbage collector
C) It only runs when memory is low
D) It is disabled by default
12. What will be the output of the following code?
13. Implement a context manager using the
14. Explain what a metaclass is in Python and give an example of its use.
15. Which of the following is true about Python’s
A) It allows for true parallel execution
B) It enables cooperative multitasking
C) It requires threading for I/O operations
D) It cannot handle CPU-bound tasks
16. What will be the output of this code?
17. Write a Python program that reads a file line by line and counts the number of lines starting with a specific prefix (e.g., "ERROR").
18. What is the significance of the
19. How does Python handle exceptions in generators?
20. Given a list of dictionaries, write a one-liner using
#PythonInterview #AdvancedPython #ProgrammingTest #CodingChallenge #PythonExperts
By: @DataScienceQ 🚀
1. Which of the following is NOT a valid way to create a dictionary in Python?
A)
{} B)
dict() C)
{} = dict D)
dict(a=1, b=2) 2. What will be the output of the following code?
def func(x, y=[]):
y.append(x)
return y
print(func(1))
print(func(2))
3. Write a Python function that takes a list of integers and returns a new list containing only the even numbers using a list comprehension.
4. Explain the difference between
__str__ and __repr__ methods in Python classes.5. Which decorator is used to define a class method in Python?
A)
@staticmethod B)
@classmethod C)
@property D)
@abstractmethod 6. What does the
*args parameter do in a function definition? A) Accepts keyword arguments
B) Accepts any number of positional arguments
C) Accepts a single argument
D) Accepts only integer values
7. What will be the output of the following code?
import copy
a = [1, 2, [3, 4]]
b = copy.deepcopy(a)
b[2][0] = 'x'
print(a)
8. Write a generator function that yields the Fibonacci sequence up to a given number n.
9. Describe how the GIL (Global Interpreter Lock) affects multithreading in Python.
10. What is the purpose of the
with statement in Python? Provide an example.11. Which of the following statements about Python's garbage collector is true?
A) It uses reference counting exclusively
B) It uses both reference counting and a cyclic garbage collector
C) It only runs when memory is low
D) It is disabled by default
12. What will be the output of the following code?
x = [1, 2, 3]
y = x
y[0] = 4
print(x)
13. Implement a context manager using the
contextlib module that prints "Entering" when entered and "Exiting" when exited.14. Explain what a metaclass is in Python and give an example of its use.
15. Which of the following is true about Python’s
asyncio library? A) It allows for true parallel execution
B) It enables cooperative multitasking
C) It requires threading for I/O operations
D) It cannot handle CPU-bound tasks
16. What will be the output of this code?
def outer():
x = 10
def inner():
nonlocal x
x += 5
return x
return inner()
print(outer())
17. Write a Python program that reads a file line by line and counts the number of lines starting with a specific prefix (e.g., "ERROR").
18. What is the significance of the
__slots__ attribute in a Python class?19. How does Python handle exceptions in generators?
20. Given a list of dictionaries, write a one-liner using
sorted() and lambda to sort the list by the value of the key 'age' in descending order.#PythonInterview #AdvancedPython #ProgrammingTest #CodingChallenge #PythonExperts
By: @DataScienceQ 🚀
Question:
How can you securely execute a dynamic shell command in Python using
---
Answer:
The above code demonstrates secure execution of dynamic shell commands by avoiding
#Python #OSModule #Security #ShellInjection #Subprocess #Sandboxing #SecureCode #AdvancedPython
By: @DataScienceQ 🚀
How can you securely execute a dynamic shell command in Python using
os module while preventing shell injection, handling environment variables, and ensuring the process is isolated with limited privileges? Provide a detailed example demonstrating all these aspects.---
import os
import subprocess
import tempfile
import shutil
import sys
from pathlib import Path
# Secure execution of dynamic shell commands
def secure_execute(cmd: str, cwd: str = None, env: dict = None):
# Validate input to prevent shell injection
if not isinstance(cmd, str) or not cmd.strip():
raise ValueError("Command must be a non-empty string.")
# Split command into safe components (avoid shell=True)
try:
args = cmd.split()
if not args:
raise ValueError("Invalid command format.")
# Sanitize arguments to avoid path traversal or injection
for arg in args:
if any(c in arg for c in [';', '&', '|', '>', '<', '`', '$']):
raise ValueError(f"Malicious character detected in command: {arg}")
# Use temporary directory for isolation
temp_dir = tempfile.mkdtemp(prefix="secure_exec_")
try:
# Set minimal environment
safe_env = {
'PATH': '/usr/bin:/bin',
'HOME': temp_dir,
'USER': 'sandbox_user',
}
if env:
safe_env.update(env)
# Run command with restricted privileges
result = subprocess.run(
args,
cwd=cwd,
env=safe_env,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
timeout=30,
preexec_fn=os.setuid(1000), # Drop to unprivileged user
universal_newlines=True,
check=False
)
return {
'stdout': result.stdout,
'stderr': result.stderr,
'returncode': result.returncode,
'success': result.returncode == 0
}
finally:
shutil.rmtree(temp_dir, ignore_errors=True)
except Exception as e:
return {'error': str(e)}
# Example usage
if __name__ == "__main__":
# Simulate a dynamic command from user input
user_input = "ls -la /tmp"
result = secure_execute(user_input, cwd="/")
print(result)
Answer:
The above code demonstrates secure execution of dynamic shell commands by avoiding
shell=True, splitting the command safely, validating input to prevent injection, isolating execution via a temporary directory, dropping privileges using os.setuid(), and restricting environment variables. This approach prevents common vulnerabilities like shell injection and privilege escalation.#Python #OSModule #Security #ShellInjection #Subprocess #Sandboxing #SecureCode #AdvancedPython
By: @DataScienceQ 🚀
❤3
Q: How can you implement a thread-safe, connection-pooling mechanism using Python's
A:
#Python #SQLite #Database #Multithreading #ThreadSafety #ConnectionPooling #AtomicTransactions #SchemaMigration #Concurrency #Programming #AdvancedPython
By: @DataScienceQ 🚀
sqlite3 with concurrent.futures.ThreadPoolExecutor, while ensuring atomic transactions and handling database schema migrations dynamically? Provide a complete example with error handling and logging.A:
import sqlite3
import threading
import logging
from concurrent.futures import ThreadPoolExecutor, as_completed
from contextlib import contextmanager
import os
import time
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Database path
DB_PATH = "example.db"
# Schema definition
SCHEMA = """
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL
);
"""
# Connection pool with threading
class DatabaseConnectionPool:
def __init__(self, db_path, max_connections=5):
self.db_path = db_path
self.max_connections = max_connections
self._connections = []
self._lock = threading.Lock()
def get_connection(self):
with self._lock:
if self._connections:
return self._connections.pop()
else:
return sqlite3.connect(self.db_path)
def release_connection(self, conn):
with self._lock:
if len(self._connections) < self.max_connections:
self._connections.append(conn)
else:
conn.close()
def close_all(self):
with self._lock:
for conn in self._connections:
conn.close()
self._connections.clear()
@contextmanager
def get_db_connection(pool):
conn = pool.get_connection()
try:
yield conn
except Exception as e:
conn.rollback()
logger.error(f"Database error: {e}")
raise
finally:
pool.release_connection(conn)
def execute_transaction(pool, query, params=None):
with get_db_connection(pool) as conn:
cursor = conn.cursor()
cursor.execute(query, params or ())
conn.commit()
def create_user(pool, name, email):
query = "INSERT INTO users (name, email) VALUES (?, ?)"
try:
execute_transaction(pool, query, (name, email))
logger.info(f"User {name} created.")
except sqlite3.IntegrityError:
logger.warning(f"Email {email} already exists.")
def fetch_users(pool):
query = "SELECT id, name, email FROM users"
with get_db_connection(pool) as conn:
cursor = conn.cursor()
cursor.execute(query)
return cursor.fetchall()
def schema_migration(pool, new_schema):
with get_db_connection(pool) as conn:
cursor = conn.cursor()
cursor.executescript(new_schema)
conn.commit()
logger.info("Schema migration applied.")
# Example usage
if __name__ == "__main__":
# Initialize pool
pool = DatabaseConnectionPool(DB_PATH)
# Apply schema
schema_migration(pool, SCHEMA)
# Simulate concurrent user creation
names_emails = [("Alice", "[email protected]"), ("Bob", "[email protected]")]
with ThreadPoolExecutor(max_workers=4) as executor:
futures = [
executor.submit(create_user, pool, name, email)
for name, email in names_emails
]
for future in as_completed(futures):
try:
future.result()
except Exception as e:
logger.error(f"Task failed: {e}")
# Fetch results
users = fetch_users(pool)
logger.info(f"Users: {users}")
# Cleanup
pool.close_all()
#Python #SQLite #Database #Multithreading #ThreadSafety #ConnectionPooling #AtomicTransactions #SchemaMigration #Concurrency #Programming #AdvancedPython
By: @DataScienceQ 🚀
Question:
How can you use Python’s
Answer:
To efficiently handle both I/O-bound (e.g., network requests, file I/O) and CPU-bound (e.g., data processing, math operations) tasks in Python, you should combine
Here’s an example:
Explanation:
-
-
-
- Mixing both ensures optimal resource usage: async for I/O, multiprocessing for CPU.
Best practices:
- Use
- Use
- Avoid mixing
- Use
#Python #AsyncIO #Concurrency #Multithreading #Multiprocessing #AdvancedPython #Programming #WebDevelopment #Performance
By: @DataScienceQ 🚀
How can you use Python’s
asyncio and concurrent.futures to efficiently handle both I/O-bound and CPU-bound tasks in a single application, and what are the best practices for structuring such a system?Answer:
To efficiently handle both I/O-bound (e.g., network requests, file I/O) and CPU-bound (e.g., data processing, math operations) tasks in Python, you should combine
asyncio for I/O-bound work and concurrent.futures.ThreadPoolExecutor or ProcessPoolExecutor for CPU-bound tasks. This avoids blocking the event loop and maximizes performance.Here’s an example:
import asyncio
import time
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
import aiohttp
import requests
# Simulated I/O-bound task (e.g., API call)
async def fetch_url(session, url):
try:
async with session.get(url) as response:
return await response.text()
except Exception as e:
return f"Error: {e}"
# Simulated CPU-bound task (e.g., heavy computation)
def cpu_intensive_task(n):
return sum(i * i for i in range(n))
# Main function using asyncio + thread/process pools
async def main():
# I/O-bound tasks with asyncio
urls = [
"https://httpbin.org/json",
"https://httpbin.org/headers",
"https://httpbin.org/status/200"
]
# Use aiohttp for concurrent HTTP requests
async with aiohttp.ClientSession() as session:
tasks = [fetch_url(session, url) for url in urls]
results = await asyncio.gather(*tasks)
print("I/O-bound results:", results)
# CPU-bound tasks with ProcessPoolExecutor
with ProcessPoolExecutor() as executor:
# Run CPU-intensive work in separate processes
futures = [executor.submit(cpu_intensive_task, 1000000) for _ in range(3)]
cpu_results = [future.result() for future in futures]
print("CPU-bound results:", cpu_results)
# Run the async main function
if __name__ == "__main__":
asyncio.run(main())
Explanation:
-
asyncio handles I/O-bound tasks asynchronously without blocking the main thread. -
aiohttp is used for efficient HTTP requests. -
ProcessPoolExecutor runs CPU-heavy functions in separate processes (bypassing GIL). - Mixing both ensures optimal resource usage: async for I/O, multiprocessing for CPU.
Best practices:
- Use
ThreadPoolExecutor for light I/O or blocking code. - Use
ProcessPoolExecutor for CPU-intensive work. - Avoid mixing
async and blocking code directly — always offload CPU tasks. - Use
asyncio.gather() to run multiple coroutines concurrently. #Python #AsyncIO #Concurrency #Multithreading #Multiprocessing #AdvancedPython #Programming #WebDevelopment #Performance
By: @DataScienceQ 🚀
❤1