PRIP 2.2 Sumita Arora Solutions | Class 12 Computer Science

Here are PRIP 2.2 Sumita Arora Solutions for class 12 Computer Science. To view Sumita Arora solutions for all chapters, visit here.

Q.1: Read the following pieces of code. Determine whether they will be executed successfully or not. If yes, show the result that would be printed. If not, explain why not.       
(a) myDictionary1={ a:1, b:2, c:3}                                   
       myDictionary1[d] = 4 
       print(myDictionary1)
(b) myDictionary2 = { 10 : 1, 20 :2, 30 : 3 }   
        print(myDictionary2[1])

Answer:

a)myDictionary1={ a:1, b:2, c:3}                                   
myDictionary1[d] = 4 
print(myDictionary1)

In dictionaries keys should be in ' ' or ” ” (quotation marks)
Error:  
NameError: name 'a' is not defined

Correct Code: 
myDictionary1={ 'a':1, 'b':2, 'c':3}                                   
myDictionary1['d'] = 4 
print(myDictionary1)

b)
myDictionary2 = { 10 : 1, 20 :2, 30 : 3 }   
print(myDictionary2[1])

in Dictionaries to access value associated with some key 
valid key should be present in square brackets
i.e.myDictionary2[key]
as there is no key 1 in dictionary this program will give 
Error: KeyError: 1
Q.2:What would be the output of following code?        
mydict = {"cat":12, "dog":6, "elephant":23} 
        mydict["mouse"] = mydict["cat"] + mydict["dog"] 
        print (mydict["mouse"])

Answer:

Output:
18

Explanation:

values associated with both keys "cat" and "dog" are added
and new value is stored with key "mouse" in mydict
Q.3: What would be the output of following code?
mydict = {"cat":12, "dog":6,"elephant":23, "bear":20}
        answer = mydict.get("cat")//mydict.get("dog")
        print (answer)

Answer:

Output:
2

Explanation:

// is used for floor division 
value associated with key "cat" is divided by value associated with key "dog"
12//2 = 2
Q.4: What would be the output of following code ?        
mydict = {"cat":12, "dog":6, "elephant":23, "bear": 20}
        print("dog" in mydict)

Answer:

Output:
True

Explanation:
"dog" in mydict - verifies that key "dog" is present in dictionary or not
as mydict has key "dog" it returns True
hence output is true
Q.5: What would be the output of following code ?      
 mydict = {"cat":12, "dog":6, "elephant":23, "bear":20)
 print(23 in mydict)

Answer:

Output:
False

Explanation:

x in mydict - checks if x is present in keys available in mydict
hence 23 in mydict returns False
Also values are associated with keys hence can't directly checked if present or not
Correct Code:
mydict = {"cat":12, "dog":6, "elephant":23, "bear":20)
print(23 in mydict.values())

In dictionary values() method returns a list of all the values present in a given dictionary.
Q.6: What would be the output of following code?        
total = 0 
        mydict = {"cat":12, "dog":6, "elephant":23, "bear":20} 
        for akey in mydict:
            if len(akey) > 3:
                total = total + mydict[akey]
        print(total)

Answer:

Output: 
43

Explanation:
for akey in mydict: #for loop goes through all keys present in mydict
if len(akey) > 3: # checks if length of key is > 3
total = total + mydict[akey] # value associated with akey added to total
print(total) #print the value of total
Q.7: Write code to create a Python dictionary (the dict type). Add two entries to the dictionary Associate the key ‘name’ with the value ‘Jivin’, and associate the key ‘phone’ with ‘85000-00003’. Add two more names and their phones in the dictionary after getting input from user. Then ask the user a name and print its corresponding phone.

Answer:

Code:
1. list is created to store all dictionaries
2. each dictionary contains name and phone keys with corresponding values

mydictList = [{"name":'Jivin', "phone":'85000-00003'}]

for i in range(2):              # for taking input from user 2 times
    name  = input("Enter Name: ") # input name
    phone = input("Enter Phone:") # input phone
    tempDict = dict()             # create empty dictionary
    tempDict['name'] = name       # associate name value to key 'name' in tempDict
    tempDict['phone'] = phone     # associate phone value to key 'phone' in tempDict
    mydictList.append(tempDict)   # append the newly created dictionary to list

search_name = input("input name to get phone:") # input search name from user
  
for d in mydictList:                            # iterate through every dictionary in list 
    if(d['name']==search_name):                 # check if value associated with key 'name' is required one
        phone = d['phone']                      # if yes get the value associated with key 'phone'    
        print("Phone associated with name {} is {}".format(name,phone)) # print name and phone


Ouput:

Enter Name: computer
Enter Phone:520
Enter Name: tutor
Enter Phone:140
input name to get phone:tutor
Phone associated with name tutor is 140
Q.8: Write code to create a Python dictionary (the dict type) similar to previous question.Add three names and their phones in the dictionary after getting input from user. 
The names should act as keys and phones as their values. 
Then ask the user name a and print its corresponding phone.

Answer:

Code:
mydictList = [] # innitial empty list 

for i in range(3):              # for taking input from user 3 times
    name  = input("Enter Name: ") # input name
    phone = input("Enter Phone:") # input phone
    tempDict = dict()             # create empty dictionary
    tempDict[name] = phone       # as we need name as key and phone as value associate phone value to key name in tempDict
    mydictList.append(tempDict)   # append the newly created dictionary to list

search_name = input("input name to get phone:") # input search name from user
  
for d in mydictList:                            # iterate through every dictionary in list 
    if(search_name in d):                 # check if search_name is present in d ( dictionary in list )
        phone = d[search_name]                      # if yes get the phone associated with search_name    
        print("Phone associated with name {} is {}".format(name,phone)) # print name and phone
        

Output:

Enter Name: computer
Enter Phone:150
Enter Name: tutor
Enter Phone:120
Enter Name: ankit
Enter Phone:100
input name to get phone:ankit
Phone associated with name ankit is 100
Q.9: Given a dictionary with 5 student names and their marks. Write a program that prints the dictionary contents in the ascending order of marks.

Answer:

Code:
#Declare dictionary with 5 records
studentDict = {'avinash':90, 'kishor':40, 'vishal':80, 'pawan':50, 'samruddhi':60}
student_lst = list(studentDict.items()) 
# studentDict.items() returns a list of tuples in which each tuple contains key and value

# applying bubble sort on list of tuples
length = len(student_lst)
for i in range(length):                                
    for j in range(length-i-1):            # with each iteration size rediced by one as we get one sorted element 
        if (student_lst[j][1] > student_lst[j + 1][1]): # comparing 2nd (marks) values with each other and swap if required
            temp = student_lst[j]  
            student_lst[j]= student_lst[j + 1]  
            student_lst[j + 1]= temp  

# convert student_lst to dictionary   
outputDict = dict(student_lst)  # dict() function converts list of  tuples to dictionary
print(outputDict)


Output:

{'kishor': 40, 'pawan': 50, 'samruddhi': 60, 'vishal': 80, 'avinash': 90}
Q.10: Carefully read the following description and try to identify the sorting technique it is talking about
(a) 
In this every element is compared with its next adjacent element and if the two elements are not in proper order, the two elements are swapped in place. If there are n elements in total, then n comparisons will place the largest element at the last position. Next time, beginning with first two elements and continuing with next elements, element at the 2nd last position. 1 comparisons will place the 2nd largest
(b) 
In this, we divide the list/array in two parts: 
    (a) sorted part (which has no element in the beginning), 
    (b) unsorted part (which has all the elements in the beginning). 
Every iteration is performed with an aim of putting the first element of unsorted part at the correct position in sorted part. Each such placement adds one element to sorted part and removes one element from unsorted part.

Answer:

a) bubble sort
b) insertion sort
Q.11: (a) Carefully go through following code of Bubble Sort technique. It is creating some problem during execution. Figure out the problem and correct it. Try writing efficient solution of the problem
Hint. Efficient solution is error free and has less number of operation taking place
  
  #arr is the list being sorted   
    n = len(arr) 
    for i in range(n):
        for j in range(n):

            if(arr[j] > arr[ j + 1]) 
                temp = arr[j]
                arr[j] = arr[ j + 1] 
                arr[j+1] = temp
    for i in range(n):
        print(arr[i], end = " ")

Answer:

Output:
IndexError: list index out of range
because comparison of arr[j] and arr[j+1]
for j = n, j+1 goes out of range 

Efficient Code:
for i in range(n-1): 
    # Last i elements are already in place 
    for j in range(0, n-i-1): # traverse the array from 0 to n-i-1 
            # Swap if the current element is greater than the next element 
        if arr[j] > arr[j+1] : 
            temp = arr[j]
            arr[j] = arr[j + 1] 
            arr[j+1] = temp

Clear Doubts with Computer Tutor
In case you’re facing problems in understanding concepts, writing programs, solving questions, want to learn fun facts | tips | tricks or absolutely anything around computer science, feel free to join CTs learner-teacher community: students.computertutor.in

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

You cannot copy content of this page