Summer 24
Summer 24
(Autonomous)
                                                 (ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
                                            SUMMER – 2024 EXAMINATION
                                    Model Answer – Only for the Use of RAC Assessors
                                                                                                             Page No: 1 | 19
                               MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
                                                        (Autonomous)
                                              (ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
              reverse (Optional): for reverse=True, it will sort the list descending. Default is
              reverse=False
              key (Optional): A function to specify the sorting criteria.
       d)     Write use of matplotlib package in python.                                                        2M
       Ans    Data abstraction: Data Abstraction is used to hide the internal functionality of the             1M
              function from the users. The users only interact with the basic implementation of the
              function, but inner working is hidden. User is familiar with that "what function                  (for
              does" but they don't know "how it does."                                                     definition of
                                                                                                               data
              Data hiding: Data hiding is a concept which underlines the hiding of data or information      abstraction
              from the user. Data hiding is a software development technique specifically used in            and data
              Object-Oriented Programming (OOP) to hide internal object details (data members).               hiding)
              Data hiding includes a process of combining the data and functions into a single unit to
              conceal data within a class by restricting direct access to the data from outside the class.
g) What is dictionary? 2M
                                                                                                       Page No: 2 | 19
                               MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
                                                       (Autonomous)
                                             (ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
 2.           Attempt any THREE of the following:                                                         12 M
                                                      Figure of Indentation
                     •    Example:
                                 if True:
                                    print "True"
                                 else:
                                    print "False"
                      • Thus, in Python all the continuous lines indented with same number of spaces
                         would form a block.
                  ii) Variables:
                      • Python Variable is containers that store values.
                      • We do not need to declare variables before using them or declare their type.
                      • A variable is created the moment we first assign a value to it.
                      • A Python variable is a name given to a memory location.
                      • It is the basic unit of storage in a program.
                      • Variable Naming Rules in Python
                         1) Variable name should start with letter(a-z ,A-Z) or underscore (_).
                             Example: age, _age, Age
                         2) In variable name, no special characters allowed other than underscore (_).
                             Example: age_, _age
                         3) Variables are case sensitive.
                             Example: age and Age are different, since variable names are case
                             sensitive.
                         4) Variable name can have numbers but not at the beginning.
                             Example: Age1
                         5) Variable name should not be a Python reserved keyword.
                      • Example:
                                                                                                   Page No: 3 | 19
                               MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
                                                             (Autonomous)
                                                (ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
                                x = 5 # x is of type integer
                                y = "John" # y is of type string
       b)     Print the following pattern using loop:                                                    4M
                                                    1010101
                                                     10101
                                                       101
                                                         1
       Ans                 p = '1'                                                                       4M
                           q = '0'                                                                    (for any
                           j=0                                                                         correct
                           k=4                                                                        program
                           while k >= 1:                                                             with proper
                             print(" "*j + (k-1)*(p+q) +p+ " "*j)                                       logic)
                             k = k-1
                             j=j+1
       c)     Write python program to perform following operations on set.                               4M
i) Numpy
ii) Pandas
       Ans        i) Numpy                                                                                2M
                                                                                                       (for each
                                                                                                 Page No: 4 | 19
                               MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
                                                       (Autonomous)
                                             (ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
                     •   NumPy is the fundamental package for scientific computing with Python.            correct
                         NumPy stands for "Numerical Python". It provides a high-performance             explanation)
                         multidimensional array object, and tools for working with these arrays.
                     •   An array is a table of elements (usually numbers), all of the same type,
                         indexed by a tuple of positive integers and represented by a single variable.
                         NumPy's array class is called ndarray. It is also known by the alias array.
                     •   In NumPy arrays, the individual data items are called elements. All elements
                         of an array should be of the same type. Arrays can be made up of any
                         number of dimensions.
                     •   In NumPy, dimensions are called axes. Each dimension of an array has a
                         length which is the total number of elements in that direction.
                     •   The size of an array is the total number of elements contained in an array in
                         all the dimension. The size of NumPy arrays are fixed; once created it cannot
                         be changed again.
                     •   Numpy arrays are great alternatives to Python Lists. Some of the key
                         advantages of Numpy arrays are that they are fast, easy to work with, and
                         give users the opportunity to perform calculations across entire arrays.
                     Figure shows the axes (or dimensions) and lengths of two example arrays;
                     (a) is a one-dimensional array and (b) is a two-dimensional array.
                  ii) Panda
                      • Pandas is a powerful and versatile library that simplifies the tasks of data
                         manipulation in Python.
                      • Pandas is well-suited for working with tabular data, such as spreadsheets or
                         SQL tables.
                      • The Pandas library is an essential tool for data analysts, scientists, and
                         engineers working with structured data in Python.
                      • It is built on top of the NumPy library which means that a lot of the
                         structures of NumPy are used or replicated in Pandas.
                      • The data produced by Pandas is often used as input for plotting functions in
                         Matplotlib, statistical analysis in SciPy, and machine learning algorithms in
                         Scikit-learn.
                                                                                                     Page No: 5 | 19
                                   MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
                                                        (Autonomous)
                                              (ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
 3.           Attempt any THREE of the following:                                                       12 M
dict1.update(dict2);
                                                                                                 Page No: 6 | 19
                                MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
                                                        (Autonomous)
                                              (ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
              Output: 12321 is a palindrome.
       c)     Write a python program to create a user defined module that will ask your                         4M
              program name and display the name of the program.
d) List data types used in python. Explain any two with example. 4M
       Ans    Python has various standard data types that are used to define the operations possible on       2 M for
              them and the storage method for each of them. Data types in Python programming                   types,
              includes:
                                                                                                             2 M for two
              1. Numbers: Represents numeric data to perform mathematical operations.
                                                                                                               proper
              2. String: Represents text characters, special symbols or alphanumeric data.                    examples
              3. List: Represents sequential data that the programmer wishes to sort, merge etc.
              4. Tuple: Represents sequential data with a little difference from list.
              5. Dictionary: Represents a collection of data that associate a unique key with each value.
              6. Boolean: Represents truth values (true or false).
              Example1: Tuple Data Type
              • Tuple is an ordered sequence of items same as list. The only difference is that tuples are
              immutable. Tuples once created cannot be modified.
              • Tuples are used to write-protect data and are usually faster than list as it cannot change
              dynamically. It is defined within parentheses ( ) where items are separated by commas ( ,
              ).
                                                                                                       Page No: 7 | 19
                                   MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
                                                           (Autonomous)
                                                 (ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
                  •      A tuple data type in python programming is similar to a list data type, which also
                         contains heterogeneous items/elements.
              Example: For tuple.
              a=(10,'abc',1+3j)
              print(a)
              (10, 'abc', (1+3j))
              Example 2:List Data Type
              List is an ordered sequence of items. It is one of the most used datatype in Python and is
              very flexible.
              • List can contain heterogeneous values such as integers, floats, strings, tuples, lists and
              dictionaries but they are commonly used to store collections of homogeneous objects.
              • The list datatype in Python programming is just like an array that can store a group of
              elements and we can refer to these elements using a single name.
              • Declaring a list is pretty straight forward. Items separated by commas ( , ) are enclosed
              within brackets [ ].
              Example:
              For list.
              first=[10, 20, 30]
              # homogenous values in list
              second=["One","Two","Three"]
              Print(first)
              [10, 20, 30]
              Print(second)
              ['One', 'Two', 'Three']
                                                                                                        Page No: 8 | 19
                                MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
                                                           (Autonomous)
                                                (ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
                        The implication of iterations is            The implication of iterations is              correct
                 2
                        Time-consuming                              comparatively Faster                          points.
                        Unexpected changes and errors are           Because tuples don’t change they are
                 6
                        more likely to occur                        far less error-prone.
                        Example:                                    Example:
                 8
                        Employee = [‘A’, ‘B’, ‘C’]                  Employee = (1001, 1002, 1003)
       b)     Write a python program takes in a number and find the sum of digits in a                             4M
              number.
       c)     Write a program function that accepts a string and calculate the number of                           4M
              uppercase letters and lower case letters.
upperlower(Str)
              Output -
              Enter a String -: Welcome to Python programing
              The number of lowercase characters is: 23
              The number of uppercase characters is: 2
or
print('Lower case characters = %s' %lower, '\nUpper case characters = %s' %upper)
upperlower(string)
              Output-
              Enter a String -: Welcome to Python programming
              Lower case characters = 24
              Upper case characters = 2
       d)     Write a python program to create class student with roll-no. and display its                      4M
              contents.
                                                                                                       Page No: 10 | 19
                                 MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
                                                         (Autonomous)
                                               (ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
                def display_data(self):
                   print(f"Roll No: {self.rollno}")
              student1 = Student(10)
              student1.display_data()
              Output:
              Rollno = 10
                                                                                                       Page No: 11 | 19
                                 MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
                                                         (Autonomous)
                                               (ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
              Example:
              f = open("demofile.txt", "r")
              print(f.read())
Where the specified text will be inserted depends on the file mode and stream position.
              "a": The text will be inserted at the current file stream position, default at the end of the
              file.
              "w": The file will be emptied before the text will be inserted at the current file stream
              position, default 0.
              Syntax:
              file.write(byte)
              byte: The text or byte object that will be inserted.
              Example:
              Open the file with "a" for appending, then add some text to the file:
              f = open("demofile2.txt", "a")
              f.write("Hi!")
              f.close()
a) Write the output for the following if the variable course = a "Python" 6M
>>> course [ : 3 ]
>>> course [ 3 : 1 ]
>>> course [ 2 : 2 ]
                                                                                                        Page No: 12 | 19
                                MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
                                                        (Autonomous)
                                              (ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
              >>> course [-1]
course[3:1]
Ans: None
>>>course[2:2]
              Ans: a slice starting and ending at the same index, which results in an empty string. Slicing
              in this way doesn't include any characters between the specified indices.
>>>course[:]
Ans: Python
>>>course[-1]
Ans: n
>>> course[1]
Ans: y
# If you want to generate an array of random integers, you can specify the size
                                                                                                       Page No: 13 | 19
                               MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
                                                         (Autonomous)
                                               (ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
              CO diploma" and "I am with IF diploma' respectively. Call the method by
              creating an object of each of the three classes.
class CO(Diploma):
def getdiploma(self):
class IF(Diploma):
def getdiploma(self):
diploma_obj = Diploma()
co_obj = CO()
if_obj = IF()
                                                                                                Page No: 14 | 19
                                MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
                                                        (Autonomous)
                                              (ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
       Ans    Multiple inheritance is a feature in object-oriented programming where a class can               2 M-
              inherit attributes and methods from more than one parent class. This allows a class to        Explanation,
              combine and reuse code from multiple classes, promoting code reuse and modular                 4 M-any
                                                                                                              suitable
              design.
                                                                                                             example
              In Python, you can achieve multiple inheritance by specifying multiple parent classes in
              the definition of a child class.
Program:
class Person:
self.name = name
self.age = age
def display_person(self):
class Employee:
self.employee_id = employee_id
self.position = position
def display_employee(self):
                                                                                                       Page No: 15 | 19
                               MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
                                                       (Autonomous)
                                             (ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
                   self.department = department
def display_manager(self):
self.display_person()
self.display_employee()
print(f"Department: {self.department}")
manager.display_manager()
       b)     Write a Python program to create user defined exception that will check                    6M
              whether the password is correct or not.
self.message = message
super().__init__(self.message)
def check_password(input_password):
if input_password != correct_password:
else:
                                                                                                Page No: 16 | 19
                                  MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
                                                            (Autonomous)
                                                  (ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
                      print("Password is correct. Access granted.")
try:
check_password(user_password)
except InvalidPasswordException as e:
print(e)
                            •   When opened in this mode, attempting to write to the file will raise a
                                UnsupportedOperation error.
                            •   Example: open('file.txt', 'r')
                     2. Write Mode ('w'):
                            •   Opens the file for writing. If the file doesn't exist, it creates a new file.
                            •   If the file already exists, it truncates the file to zero length.
                            •   Example: open('file.txt', 'w')
                     3. Append Mode ('a'):
                            •   Opens the file for writing. If the file doesn't exist, it creates a new file.
                            •   If the file already exists, it appends data to the end of the file.
                            •   Example: open('file.txt', 'a')
                     4. Binary Mode ('b'):
                            •   Opens the file in binary mode. It's used in conjunction with other modes
                                like read ('rb'), write ('wb'), or append ('ab') to deal with binary files like
                                images, executables, etc.
                            •   Example: open('image.jpg', 'rb')
                     5. Text Mode ('t'):
                                                                                                                Page No: 17 | 19
                                MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
                                                          (Autonomous)
                                                (ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
                         • This is the default mode for text files (although you don't explicitly specify
                              it). It's used to handle text files and performs encoding/decoding of text
                              automatically.
                          •   Example: open('file.txt', 'rt')
                  6. Read and Write Mode ('r+'):
                          •   Opens the file for both reading and writing.
                          •   The file pointer is at the beginning of the file.
                          •   Example: open('file.txt', 'r+')
                  7. Write and Read Mode ('w+'):
                          •   Opens the file for both reading and writing.
                          •   If the file doesn't exist, it creates a new file.
                          •   If the file exists, it truncates the file to zero length.
                          •   Example: open('file.txt', 'w+')
               Detailed Explanation of Three Modes:
                                                                                                        Page No: 18 | 19
                                MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
                                                        (Autonomous)
                                              (ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
                         • It's useful when you want to add data to an existing file without overwriting
                              its contents.
                          •   Common use cases include logging, adding new entries to a file, or
                              maintaining a history of operations.
Page No: 19 | 19