Python VVIMP Answers - Campusify
2 Marks Questions
1. List features of Python.
- 1. Easy to learn and use.
- 2. Interpreted and dynamically typed language.
- 3. Extensive standard library support.
- 4. Supports multiple programming paradigms like OOP, procedural, and functional.
2. Compare list and Tuple.
- List: Mutable, defined with [].
- Tuple: Immutable, defined with ().
3. Define class and object.
- Class is a blueprint for objects.
- Object is an instance of a class.
4. List different modes of opening file in Python.
- 'r' - read, 'w' - write, 'a' - append, 'x' - create, 'b' - binary mode, '+' - read and write.
5. Enlist any four data structures used in Python.
- List, Tuple, Set, Dictionary.
6. Write syntax for a method to sort a list.
- list.sort()
7. What is data obstruction and data hiding?
- Abstraction hides complex details.
- Data hiding restricts access to internal object details.
4 Marks Questions
1. Building Blocks
- Identifiers: names used for variables, functions, etc.
- Keywords: reserved words like if, else.
- Indentation: used for block definition.
- Variables: store data values.
- Comments: notes in code using #.
- Data types: int, float, str, etc.
2. Conditional statements, operators, loops
- if, elif, else for decision making.
- Operators: +, -, *, / etc.
- Loops: for and while loops.
3. Continue, pass, break, else
- continue: skip iteration.
- break: exit loop.
- pass: placeholder.
- else: used with loops or try blocks.
4. Membership, assignment, identity operators
- Membership: in, not in.
- Assignment: =, +=, -=.
- Identity: is, is not.
5. List, Dictionary, Set, Tuple
- List: Ordered, mutable.
- Dictionary: Key-value pairs.
- Set: Unordered, no duplicates.
- Tuple: Ordered, immutable.
6. Local and Global variables
- Local: inside functions.
- Global: defined outside any function.
7. File modes
- 'r': read, 'w': write, 'a': append, 'b': binary, 'x': exclusive creation.
8. Exception handling
- try, except, else, finally blocks handle errors.
- Allows graceful error management.
9. User defined exception
- Create class inheriting Exception.
- Raise with raise keyword.
10. Numpy package
- Used for numerical computations.
- Supports arrays, matrices, math functions.
11. 'Self' parameter
- 'self' refers to instance of class.
- Used in method definitions.
12. Module use and example
- Module: file containing Python code.
- Define functions and variables.
- Use with import.
13. Matplotlib and Pandas
- Matplotlib: plotting library.
- Pandas: data analysis and manipulation.
14. User defined function
- Defined using def keyword.
- Reusable blocks of code.
15. File operation functions
- open(), read(), write(), close() etc.
6 Marks Questions
1. Class and Inheritance
- Class: blueprint for objects.
- Inheritance: acquiring properties from base class.
- Example: class Child(Parent):
2. File operations
- Read from file A and write to file B using open() and read/write methods.
3. Method overloading and overriding
- Overloading: same method name, different parameters.
- Overriding: re-defining base class method in derived class.
4. User-defined package
- Create a directory with __init__.py.
- Add modules inside.
- Import in other programs.
5. Module program
- Create module file with functions.
- Import and call functions.
6. Palindrome program
- Check if reverse of string/number equals original.
7. Command line arguments
- Use sys.argv to get arguments.
- Convert and add numbers.
8. Largest and smallest in list
- Use max() and min() functions.
9. Sum of digits
- Loop through digits using modulo and integer division.
10. Output based on slicing and lists
- Slicing strings and using list append operations.
Extras
- Random numbers: use numpy.random.
- Zero division: handle with try-except.
- format(): 'Hello {}'.format(name).
- Factorial: use loop or recursion.
- Remove key from dict: pop(), del.
- Greatest of 4 numbers: use max().