Python

Here’s the updated list of 50 Python interview questions, now including how Python and Java interact:


Basic Python Concepts

  1. What is Python?

    • Python is an interpreted, high-level, dynamically-typed programming language known for its readability and ease of use.

  2. What are Python’s key features?

    • Interpreted, dynamically-typed, object-oriented, open-source, has extensive libraries, and supports multi-paradigm programming.

  3. How do you declare a variable in Python?

    x = 10
    name = "Alice"
    
  4. What is the difference between = and == in Python?

    • = is an assignment operator, == is a comparison operator.

  5. What data types are available in Python?

    • int, float, complex, bool, str, list, tuple, set, dict, NoneType.

  6. How do you check the data type of a variable?

    type(x)  # Returns the data type of x
    
  7. How do you take user input in Python?

    name = input("Enter your name: ")
    
  8. What is type casting?

    • Converting one data type into another.

    int("10")  # Converts string to integer
    
  9. How do you perform string concatenation?

    first_name = "John"
    last_name = "Doe"
    full_name = first_name + " " + last_name
    
  10. What is the difference between is and ==?

  • is checks object identity, == checks value equality.


Control Flow (Loops & Conditions)

  1. How do you write an if-else statement in Python?

if x > 0:
    print("Positive")
else:
    print("Negative")
  1. How do you write a for loop?

for i in range(5):
    print(i)
  1. How do you write a while loop?

x = 0
while x < 5:
    print(x)
    x += 1
  1. What is the difference between break and continue?

  • break exits the loop entirely; continue skips the current iteration.

  1. How do you use the pass statement?

  • A placeholder for future code.

if x > 0:
    pass

Functions

  1. How do you define a function in Python?

def greet(name):
    return "Hello " + name
  1. What is a default parameter in a function?

def greet(name="Guest"):
    return "Hello " + name
  1. What is the difference between return and print in a function?

  • return sends a value back; print displays it.

  1. **What are *args and kwargs in functions?

def func(*args, **kwargs):
    print(args, kwargs)
  1. What is recursion?

  • A function calling itself.

def factorial(n):
    return 1 if n == 0 else n * factorial(n - 1)

Lists, Tuples, and Dictionaries

  1. How do you create a list?

my_list = [1, 2, 3, 4, 5]
  1. How do you add an element to a list?

my_list.append(6)
  1. How do you remove an element from a list?

my_list.remove(2)  # Removes first occurrence of 2
  1. What is the difference between a tuple and a list?

  • Tuples are immutable; lists are mutable.

  1. How do you create a dictionary?

my_dict = {"name": "Alice", "age": 25}
  1. How do you access a dictionary value?

my_dict["name"]
  1. How do you add a key-value pair to a dictionary?

my_dict["city"] = "New York"

Strings and File Handling

  1. How do you get the length of a string?

len("hello")
  1. How do you convert a string to uppercase?

"hello".upper()
  1. How do you split a string?

"hello world".split()
  1. How do you read a file in Python?

with open("file.txt", "r") as file:
    content = file.read()
  1. How do you write to a file?

with open("file.txt", "w") as file:
    file.write("Hello, world!")

Object-Oriented Programming (OOP)

  1. How do you define a class?

class Person:
    def __init__(self, name):
        self.name = name
  1. What is self in Python classes?

  • Represents the instance of the class.

  1. How do you create an object of a class?

p = Person("Alice")
  1. What is inheritance in Python?

class Employee(Person):
    pass
  1. What is method overriding?

class Parent:
    def show(self):
        print("Parent")
class Child(Parent):
    def show(self):
        print("Child")

Error Handling

  1. How do you handle exceptions in Python?

try:
    x = 1 / 0
except ZeroDivisionError:
    print("Cannot divide by zero")
  1. What is the purpose of the finally block?

  • Executes regardless of exception occurrence.


Advanced Topics

  1. What is a lambda function?

square = lambda x: x * x
  1. What is list comprehension?

squares = [x*x for x in range(10)]
  1. How do you sort a list in Python?

sorted_list = sorted(my_list)
  1. What is a generator in Python?

def my_generator():
    yield 1
    yield 2

Integrating Python and Java

  1. How do Python and Java interact?

  • Python and Java can communicate using subprocesses (ProcessBuilder), Jython, Py4J, or shared files, databases, and APIs.

  1. How can Java run a Python script?

  • Using ProcessBuilder:

ProcessBuilder pb = new ProcessBuilder("python", "script.py");
  1. How can Python call a Java method?

  • Using Py4J:

from py4j.java_gateway import JavaGateway
gateway = JavaGateway()
print(gateway.entry_point.add(5, 3))
  1. What is Jython?

  • Jython allows running Python inside the Java Virtual Machine (JVM).

  1. How can Python and Java share data?

  • Using JSON, databases, REST APIs, or sockets.

  1. Why would you use both Java and Python?

  • Java is great for performance-heavy applications, while Python is used for scripting, automation, and AI.

  1. 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! 🚀