0% found this document useful (0 votes)
0 views5 pages

I MSc Important IA1 questions

The document provides an overview of fundamental Python concepts including comments, variable assignments, data types, operators, functions, conditionals, loops, and object-oriented programming principles. It explains key terms such as modules, namespaces, encapsulation, inheritance, and recursion, along with examples for clarity. Additionally, it covers file handling, functional programming, and the use of built-in functions, emphasizing the importance of modularity and code reuse.

Uploaded by

youdunomebruh47
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views5 pages

I MSc Important IA1 questions

The document provides an overview of fundamental Python concepts including comments, variable assignments, data types, operators, functions, conditionals, loops, and object-oriented programming principles. It explains key terms such as modules, namespaces, encapsulation, inheritance, and recursion, along with examples for clarity. Additionally, it covers file handling, functional programming, and the use of built-in functions, emphasizing the importance of modularity and code reuse.

Uploaded by

youdunomebruh47
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1. What is a comment in Python?

A comment is text in code ignored by the interpreter, used for documentation.

2. Which symbol is used for single-line comments in Python?


The # symbol is used.

3. What is a variable assignment?


Assigning a value to a variable, e.g., x = 10.

4. What are identifiers in Python?


Names given to variables, functions, or classes.

5. Give an example of a built-in data type.


Example: int, float, list, dict.

6. What are standard type operators?


Operators like +, -, *, /, %, ==.

7. Which function returns the length of a sequence?


len () function.

8. Give an example of a floating-point number.


3.14

9. Which function is used to convert a string into an integer?


int () function.

10. What are conditionals in Python?


Decision-making statements like if, elif, else.

11. What is a loop?


A control structure used to repeat a block of code.

12. Write one example of a sequence type.


Example: tuple = (1, 2, 3).

13. What is a set in Python?


An unordered collection of unique elements.

14. Which keyword is used to define a function?


The def keyword.

15. What is recursion?


A function calling itself directly or indirectly.

16. Define IDLE.


Integrated Development Environment is available when Python is installed in the system.

17. Define Shell and Shell prompt.


Shell refers to the Python interactive shell, is the Python interpreter, immediately evaluates the
code and displays any results or errors. Chevron (>>> symbol) prompt is the interpreter uses to
indicate that is ready to receive the user input.

18. Define Module.


A *module* in Python is a file containing Python code, such as functions, classes, or variables, that
can be imported and reused in other Python programs

19. What is namespace ?


Modules help prevent naming conflicts by encapsulating code within their own namespaces. A
namespace is a container that holds a set of identifiers (names) and ensures that all the names are
unique within that context. In Python, namespaces are implemented as dictionaries mapping names
to objects.

20. Define encapsulation, inheritance, polymorphism and abstraction.


1. *Encapsulation*: Bundling data and methods that operate on that data within one unit (class), and
restricting access to some components. ([Wikipedia][5])
2. *Inheritance*: Creating new classes from existing ones, inheriting attributes and behaviors.
3. *Polymorphism*: Allowing different classes to be treated as instances of the same class through a
common interface.
4. *Abstraction*: Hiding complex implementation details and showing only the necessary features of
an object.

Five Marks Questions


1. Explain variable scope in Python.
Variable scope refers to the region where a variable is accessible. Local variables are accessible
only inside functions, while global variables are accessible throughout the program.

2. Differentiate between lists and tuples.


Lists are mutable (can be changed), whereas tuples are immutable (cannot be modified). Both are
sequence types but tuples are faster and memory efficient.

3. Explain the use of built-in functions with examples.


Built-in functions simplify tasks, e.g., len("abc") returns 3, max([1,2,3]) returns 3. They avoid writing
extra code.

4. What are modules in Python?


A module is a file containing Python code (functions, classes, variables). They promote reusability
and can be imported using import module_name.

5. Explain functional programming in Python.


Functional programming treats functions as first-class objects, allowing them to be passed as
arguments. Functions like map(), filter(), and reduce() support this style.
6. Explain literals in Python with examples.
Literals are fixed values directly embedded in source code representing constant data. Python
supports various literal types:

Numeric literals: 10, 3.14, 5+2j

String literals: 'Hello', "Python"


Boolean literals: True, False

Special literal: None


Additional types include literals for collections like lists, tuples, dictionaries, and sets.

7 Explain relational operators in Python with examples.


Relational (comparison) operators compare values and return a Boolean (True or False):
== (equal), != (not equal), <, >, <=, >=
Examples:
5 < 10 # True
a == b # True if a equals b
x != y # True if x not equal to y

8. Explain operations commonly performed on lists.


Common list operations include:
Indexing and slicing: lst[0], lst[1:3]

Appending and inserting: lst.append(item), lst.insert(index, item)

Removing: lst.remove(value), lst.pop()

Concatenation and repetition: lst1 + lst2, lst * 3

Membership test: item in lst

Iterating: for item in lst: …

Comprehensions: [x*2 for x in lst if x>0]

9. Explain the concept of a program routine.


A routine (often called a function or procedure) is a named block of code designed to perform a
specific task. It improves modularity by:

Encapsulating functionality

Allowing reuse and easier maintenance

Accepting inputs (parameters) and optionally returning outputs (via return)


– (General definition; no specific web reference needed)

9. Explain the use of modules and namespaces in Python.


Modules are .py files containing reusable Python code (functions, classes, variables). They support
modularity and code reuse.

Namespaces are name-to-object mappings (implemented as dictionaries). Examples include:

Built-in namespace (e.g., abs, len)

Global namespace (module-level names)

Local namespace (names within functions)


Separate namespaces prevent naming conflict — e.g., different modules can define maximize without
collision.

10. Write a brief note on Recursive Functions.


A recursive function calls itself to solve a problem by breaking it down into smaller, similar sub-
problems. Each call moves toward a base case, which terminates recursion.
Example:

def factorial(n):
if n <= 1:
return 1
else:
return n * factorial(n - 1)
Recursion is elegant for tasks like tree traversal or mathematical series.

Ten Marks Questions

1. Explain different Python number types with examples.


Python supports three main number types: integers (whole numbers, e.g., 5), floating-point
numbers (decimal values, e.g., 3.14), and complex numbers (with real and imaginary parts, e.g.,
2+3j). Integers are used for counting, floats for precise calculations, and complex for scientific
computations.

2. Discuss conditionals and loops with examples.


Conditionals (if, elif, else) control program flow based on conditions. Loops (for, while) repeatedly
execute code until conditions are met. Example:

3. Explain Object-Oriented Programming concepts in Python.


OOP in Python uses classes and objects. Encapsulation bundles data and methods together,
Inheritance allows code reuse, and Polymorphism enables methods to behave differently in different
contexts. This makes programs modular and easier to maintain.

4. Describe functions in Python with different argument types.


Functions are defined using def and may take arguments. Types include positional arguments,
keyword arguments, default arguments, and variable-length arguments. Example:

5. Explain the two means of constructing multi-way selection in Python with examples.

if–elif–else ladder:

if x < 0:
print("Negative")
elif x == 0:
print("Zero")
else:
print("Positive")

match–case (Python 3.10+):

match command:
case "start":
start()
case "stop":
stop()
case _:
print("Unknown")
match–case is similar to switch-case constructs in other languages.

6. Explain calling value-returning functions with an example.


A value-returning function performs a task and returns a result:

def add(a, b):


return a + b

result = add(5, 3) # result is 8


You call it in expressions, use its return for further computation, and enable modular, reusable code.

7. Describe the process of reading and writing files.

In Python:

# Writing to a file
with open("data.txt", "w") as f:
f.write("Hello, world!\n")

# Reading from a file


with open("data.txt", "r") as f:
content = f.read()
lines = f.readlines()
Use modes: "r" (read), "w" (write), "a" (append), "b" (binary)

8. Explain the effective use of encapsulation and inheritance in Python.


Encapsulation bundles data and methods (in a class) and controls access using naming conventions
(_protected, __private). It enhances modularity and safeguards internal state.

Inheritance lets a child class derive attributes and methods from a parent class, facilitating code reuse
and extension:

class Parent:
def greet(self):
print("Hello")

class Child(Parent):
def greet(self):
super().greet()
print("Child here")
Both principles promote scalable, maintainable, and logical code structure

You might also like