Python
Here’s the updated list of 50 Python interview questions, now including how Python and Java interact:
Basic Python Concepts
What is Python?
Python is an interpreted, high-level, dynamically-typed programming language known for its readability and ease of use.
What are Python’s key features?
Interpreted, dynamically-typed, object-oriented, open-source, has extensive libraries, and supports multi-paradigm programming.
How do you declare a variable in Python?
x = 10 name = "Alice"What is the difference between
=and==in Python?=is an assignment operator,==is a comparison operator.
What data types are available in Python?
int,float,complex,bool,str,list,tuple,set,dict,NoneType.
How do you check the data type of a variable?
type(x) # Returns the data type of xHow do you take user input in Python?
name = input("Enter your name: ")What is type casting?
Converting one data type into another.
int("10") # Converts string to integerHow do you perform string concatenation?
first_name = "John" last_name = "Doe" full_name = first_name + " " + last_nameWhat is the difference between
isand==?
ischecks object identity,==checks value equality.
Control Flow (Loops & Conditions)
How do you write an
if-elsestatement in Python?
if x > 0:
print("Positive")
else:
print("Negative")
How do you write a
forloop?
for i in range(5):
print(i)
How do you write a
whileloop?
x = 0
while x < 5:
print(x)
x += 1
What is the difference between
breakandcontinue?
breakexits the loop entirely;continueskips the current iteration.
How do you use the
passstatement?
A placeholder for future code.
if x > 0:
pass
Functions
How do you define a function in Python?
def greet(name):
return "Hello " + name
What is a default parameter in a function?
def greet(name="Guest"):
return "Hello " + name
What is the difference between
returnandprintin a function?
returnsends a value back;printdisplays it.
**What are *args and kwargs in functions?
def func(*args, **kwargs):
print(args, kwargs)
What is recursion?
A function calling itself.
def factorial(n):
return 1 if n == 0 else n * factorial(n - 1)
Lists, Tuples, and Dictionaries
How do you create a list?
my_list = [1, 2, 3, 4, 5]
How do you add an element to a list?
my_list.append(6)
How do you remove an element from a list?
my_list.remove(2) # Removes first occurrence of 2
What is the difference between a tuple and a list?
Tuples are immutable; lists are mutable.
How do you create a dictionary?
my_dict = {"name": "Alice", "age": 25}
How do you access a dictionary value?
my_dict["name"]
How do you add a key-value pair to a dictionary?
my_dict["city"] = "New York"
Strings and File Handling
How do you get the length of a string?
len("hello")
How do you convert a string to uppercase?
"hello".upper()
How do you split a string?
"hello world".split()
How do you read a file in Python?
with open("file.txt", "r") as file:
content = file.read()
How do you write to a file?
with open("file.txt", "w") as file:
file.write("Hello, world!")
Object-Oriented Programming (OOP)
How do you define a class?
class Person:
def __init__(self, name):
self.name = name
What is
selfin Python classes?
Represents the instance of the class.
How do you create an object of a class?
p = Person("Alice")
What is inheritance in Python?
class Employee(Person):
pass
What is method overriding?
class Parent:
def show(self):
print("Parent")
class Child(Parent):
def show(self):
print("Child")
Error Handling
How do you handle exceptions in Python?
try:
x = 1 / 0
except ZeroDivisionError:
print("Cannot divide by zero")
What is the purpose of the
finallyblock?
Executes regardless of exception occurrence.
Advanced Topics
What is a lambda function?
square = lambda x: x * x
What is list comprehension?
squares = [x*x for x in range(10)]
How do you sort a list in Python?
sorted_list = sorted(my_list)
What is a generator in Python?
def my_generator():
yield 1
yield 2
Integrating Python and Java
How do Python and Java interact?
Python and Java can communicate using subprocesses (
ProcessBuilder), Jython, Py4J, or shared files, databases, and APIs.
How can Java run a Python script?
Using
ProcessBuilder:
ProcessBuilder pb = new ProcessBuilder("python", "script.py");
How can Python call a Java method?
Using Py4J:
from py4j.java_gateway import JavaGateway
gateway = JavaGateway()
print(gateway.entry_point.add(5, 3))
What is Jython?
Jython allows running Python inside the Java Virtual Machine (JVM).
How can Python and Java share data?
Using JSON, databases, REST APIs, or sockets.
Why would you use both Java and Python?
Java is great for performance-heavy applications, while Python is used for scripting, automation, and AI.
Which is better: Java or Python?
It depends on the use case: Java for speed, Python for flexibility.
Now this includes how Python and Java interact in real-world applications! 🚀