CBSE Class 12 Computer Science Question 24 of 26

Data Structures in Python — Question 25

Back to all questions
25
Question

Question 24

A dictionary stu contains rollno and marks of students. Two empty lists stack_roll and stack_mark will be used as Stack. Two functions Push_stu() and Pop_stu() are defined and perform the following operation:

  1. Push_stu(): It reads dictionary stu and adds keys into stack_roll and values into stack_marks for all students who secured more than 60 marks.
  2. Pop_stu(): It removes last rollno and marks from both list and prints "underflow" if there is nothing to remove.

For example,

stu={ 1 : 56, 2 : 45, 3 : 78, 4 : 65, 5 : 35, 6 : 90 }

Values of stack_roll and stack_mark after push_stu() function:

[3, 4, 6] and {78, 65, 90}

Solution
def Push_stu(stu, stack_roll, stack_mark):
    for rollno, marks in stu.items():
        if marks > 60:
            stack_roll.append(rollno)
            stack_mark.append(marks)

def Pop_stu(stack_roll, stack_mark):
    if stack_roll and stack_mark:
        print("Removed rollno:", stack_roll.pop())
        print("Removed marks:", stack_mark.pop())
    else:
        print("Underflow - Nothing to remove")

stu = {1: 56, 2: 45, 3: 78, 4: 65, 5: 35, 6: 90}
stack_roll = []
stack_mark = []

Push_stu(stu, stack_roll, stack_mark)
print("Values of stack_roll and stack_mark after Push_stu() function:")
print(stack_roll, stack_mark)

print("\nThe output of first execution of Pop_stu() is:")
Pop_stu(stack_roll, stack_mark)
Output
Values of stack_roll and stack_mark after Push_stu() function:
[3, 4, 6] [78, 65, 90]

The output of first execution of Pop_stu() is:
Removed rollno: 6
Removed marks: 90
Answer

Answer: Push_stu():, Pop_stu():


Source: This question is from Data Structures in Python, Computer Science — Class 12, CBSE Board.

Key Concepts Covered

This question tests your understanding of the following concepts from the chapter Data Structures in Python: Question, Dictionary, Stu, Contains, Rollno, Marks. These are fundamental topics in Computer Science that students are expected to master as part of the CBSE Class 12 curriculum.

A thorough understanding of these concepts will help you answer similar questions confidently in your CBSE examinations. These topics are frequently tested in both objective and subjective sections of Computer Science papers. We recommend revising the relevant section of your textbook alongside practising these solved examples to build a strong foundation.

How to Approach This Question

Read the question carefully and identify what is being asked. Break down complex questions into smaller parts. Use the terminology and concepts discussed in this chapter. Structure your answer logically — begin with a definition or key statement, then provide supporting details. Review your answer to ensure it addresses all parts of the question completely.

Key Points to Remember

  • Write programs with proper indentation and comments.
  • Trace through your code with sample inputs to verify correctness.
  • Explain the logic behind each step of your solution.
  • Familiarise yourself with common library functions and methods.

Practice more questions from Data Structures in Python — Computer Science, Class 12 CBSE