The IGNOU BCS-094 Solved Question Paper PDF Download page is designed to help students access high-quality exam resources in one place. Here, you can find ignou solved question paper IGNOU Previous Year Question paper solved PDF that covers all important questions with detailed answers. This page provides IGNOU all Previous year Question Papers in one PDF format, making it easier for students to prepare effectively.
- IGNOU BCS-094 Solved Question Paper in Hindi
- IGNOU BCS-094 Solved Question Paper in English
- IGNOU Previous Year Solved Question Papers (All Courses)
Whether you are looking for IGNOU Previous Year Question paper solved in English or ignou previous year question paper solved in hindi, this page offers both options to suit your learning needs. These solved papers help you understand exam patterns, improve answer writing skills, and boost confidence for upcoming exams.
IGNOU BCS-094 Solved Question Paper PDF

This section provides IGNOU BCS-094 Solved Question Paper PDF in both Hindi and English. These ignou solved question paper IGNOU Previous Year Question paper solved PDF include detailed answers to help you understand exam patterns and improve your preparation. You can also access IGNOU all Previous year Question Papers in one PDF for quick and effective revision before exams.
IGNOU BCS-094 Previous Year Solved Question Paper in Hindi
Q1. (a) Write a Python program which take marks of a class test of a student and display “Good” if marks are greater than 30 and display “Need improvement” otherwise. (b) What is Object Oriental Programming ? Explain the concept of object and class with example. Write code to explain how a class is defined in Python. (c) What is the need of software testing ? Explain the difference between functional and non-functional testing. (d) What is syntax error ? Explain how syntax error may be avoided in Python with the help of an example. (e) What is data encapsulation ? Compare public, private and protected access specifiers used in Python.
Ans.
(a) एक छात्र के क्लास टेस्ट के अंकों के आधार पर प्रदर्शन प्रदर्शित करने के लिए पाइथन प्रोग्राम:
यह प्रोग्राम उपयोगकर्ता से अंक इनपुट के रूप में लेता है और फिर यह जांचने के लिए एक if-else स्टेटमेंट का उपयोग करता है कि अंक 30 से अधिक हैं या नहीं।
“`python # उपयोगकर्ता से अंक इनपुट करने के लिए कहना try: marks = int(input(“Enter the marks obtained in the class test: “)) # यह जांचने के लिए कि अंक 30 से अधिक हैं या नहीं if marks > 30: print(“Good”) else: print(“Need improvement”) except ValueError: print(“Invalid input. Please enter a valid number for marks.”) “` प्रोग्राम का स्पष्टीकरण:
input()फ़ंक्शन का उपयोग उपयोगकर्ता से इनपुट प्राप्त करने के लिए किया जाता है, जिसे एक स्ट्रिंग के रूप में लौटाया जाता है।int()फ़ंक्शन का उपयोग स्ट्रिंग को एक पूर्णांक (integer) में बदलने के लिए किया जाता है।- if कंडीशन
marks > 30यह जांचती है कि छात्र के अंक 30 से अधिक हैं या नहीं। यदि यह सत्य है, तो “Good” प्रिंट होता है। - यदि if कंडीशन असत्य है, तो else ब्लॉक निष्पादित होता है और “Need improvement” प्रिंट होता है।
- अमान्य इनपुट (जैसे टेक्स्ट) को संभालने के लिए try-except ब्लॉक का उपयोग किया गया है।
(b) ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग (OOP)
ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग (OOP) एक प्रोग्रामिंग प्रतिमान (paradigm) है जो “ऑब्जेक्ट्स” की अवधारणा पर आधारित है। ये ऑब्जेक्ट्स डेटा (विशेषताएँ या attributes) और कोड (प्रक्रियाएं या methods) दोनों को समाहित कर सकते हैं। OOP का मुख्य उद्देश्य वास्तविक दुनिया की संस्थाओं को प्रोग्रामिंग में मॉडल करना है, जिससे कोड अधिक मॉड्यूलर, पुन: प्रयोज्य (reusable) और रखरखाव (maintainable) में आसान हो जाता है। क्लास (Class) और ऑब्जेक्ट (Object) की अवधारणा:
- क्लास (Class): एक क्लास एक ब्लूप्रिंट या टेम्पलेट है जिससे ऑब्जेक्ट बनाए जाते हैं। यह उन विशेषताओं (attributes) और व्यवहारों (methods) को परिभाषित करता है जो एक विशेष प्रकार के सभी ऑब्जेक्ट्स में होंगे। उदाहरण के लिए, एक
Carक्लास में रंग, मॉडल और वर्ष जैसी विशेषताएँ और स्टार्ट, स्टॉप और ड्राइव जैसे व्यवहार हो सकते हैं। - ऑब्जेक्ट (Object): एक ऑब्जेक्ट एक क्लास का एक इंस्टेंस (instance) होता है। यह एक वास्तविक दुनिया की इकाई है जिसमें क्लास द्वारा परिभाषित अवस्था (state) और व्यवहार (behavior) होता है। उदाहरण के लिए, यदि
Carएक क्लास है, तो “लाल मारुति स्विफ्ट” और “नीली होंडा सिटी” उस क्लास के ऑब्जेक्ट होंगे।
पाइथन में क्लास की परिभाषा:
पाइथन में, class कीवर्ड का उपयोग करके एक क्लास को परिभाषित किया जाता है। “`python # ‘Car’ नामक एक क्लास को परिभाषित करना class Car: # यह एक कंस्ट्रक्टर मेथड है def __init__(self, color, model): self.color = color # ऑब्जेक्ट की विशेषता (attribute) self.model = model # ऑब्जेक्ट की विशेषता # ऑब्जेक्ट के विवरण को प्रदर्शित करने के लिए एक मेथड def display_details(self): print(f”This is a {self.color} {self.model}.”) # ‘Car’ क्लास का एक ऑब्जेक्ट बनाना my_car = Car(“Red”, “Maruti Swift”) # ऑब्जेक्ट के मेथड को कॉल करना my_car.display_details() # आउटपुट: This is a Red Maruti Swift. “`
(c) सॉफ्टवेयर टेस्टिंग की आवश्यकता और प्रकार
सॉफ्टवेयर टेस्टिंग की आवश्यकता: सॉफ्टवेयर टेस्टिंग एक ऐसी प्रक्रिया है जिसमें किसी सॉफ्टवेयर एप्लिकेशन का मूल्यांकन यह जांचने के लिए किया जाता है कि यह निर्दिष्ट आवश्यकताओं को पूरा करता है या नहीं। इसकी आवश्यकता निम्नलिखित कारणों से होती है:
- गुणवत्ता सुनिश्चित करना: यह सुनिश्चित करता है कि सॉफ्टवेयर उत्पाद विश्वसनीय, कुशल और उपयोगकर्ता के अनुकूल है।
- त्रुटियों (Defects) का पता लगाना: यह विकास चक्र में जल्दी से जल्दी बग्स और त्रुटियों की पहचान करने और उन्हें ठीक करने में मदद करता है, जिससे बाद में लागत कम हो जाती है।
- ग्राहक संतुष्टि: एक अच्छी तरह से परीक्षण किया गया सॉफ्टवेयर बेहतर प्रदर्शन करता है और उपयोगकर्ताओं की अपेक्षाओं को पूरा करता है, जिससे ग्राहक संतुष्टि बढ़ती है।
- सुरक्षा: यह एप्लिकेशन में कमजोरियों का पता लगाकर उसे सुरक्षित रखने में मदद करता है।
फंक्शनल और नॉन-फंक्शनल टेस्टिंग के बीच अंतर:
आधार
फंक्शनल टेस्टिंग (Functional Testing)
नॉन-फंक्शनल टेस्टिंग (Non-Functional Testing)
उद्देश्य
यह जांचता है कि सॉफ्टवेयर क्या करता है। यह एप्लिकेशन की कार्यात्मकताओं (functionalities) का परीक्षण करता है।
यह जांचता है कि सॉफ्टवेयर कैसे काम करता है। यह प्रदर्शन, उपयोगिता और विश्वसनीयता जैसे पहलुओं का परीक्षण करता है।
फोकस
व्यावसायिक आवश्यकताओं (business requirements) का सत्यापन।
उपयोगकर्ता अनुभव (user experience) और सिस्टम प्रदर्शन का मूल्यांकन।
उदाहरण
यूनिट टेस्टिंग, इंटीग्रेशन टेस्टिंग, रिग्रेशन टेस्टिंग, API टेस्टिंग।
परफॉर्मेंस टेस्टिंग, लोड टेस्टिंग, सिक्योरिटी टेस्टिंग, यूजबिलिटी टेस्टिंग।
प्रकार
इसे ब्लैक-बॉक्स टेस्टिंग के रूप में किया जा सकता है।
यह प्रदर्शन, भार और तनाव का मूल्यांकन करता है।
(d) सिंटैक्स एरर (Syntax Error)
एक सिंटैक्स एरर एक प्रोग्रामिंग भाषा के व्याकरण (grammar) या नियमों का उल्लंघन है। जब पाइथन इंटरप्रेटर कोड को पार्स (parse) करने का प्रयास करता है और उसे एक ऐसा स्टेटमेंट मिलता है जो भाषा के नियमों का पालन नहीं करता है, तो वह एक सिंटैक्स एरर उत्पन्न करता है। ये एरर प्रोग्राम के निष्पादन (execution) से पहले ही पहचानी जाती हैं। उदाहरण: “`python # सिंटैक्स एरर का उदाहरण x = 10 if x > 5 # यहाँ कोलन (:) छूट गया है print(“x is greater than 5”) “` उपरोक्त कोड में, if स्टेटमेंट के अंत में कोलन (:) नहीं है, जो पाइथन सिंटैक्स का उल्लंघन है। इसे चलाने पर, इंटरप्रेटर एक SyntaxError: expected ‘:’ दिखाएगा। पाइथन में सिंटैक्स एरर से कैसे बचें:
- कोड एडिटर/IDE का उपयोग करें: आधुनिक कोड एडिटर जैसे VS Code, PyCharm आदि में सिंटैक्स हाइलाइटिंग और लिंटिंग (linting) की सुविधा होती है। वे टाइप करते समय ही सिंटैक्स एरर को उजागर कर देते हैं।
- कोड को ध्यान से पढ़ें: सबमिट करने या चलाने से पहले अपने कोड की समीक्षा करें। कोष्ठक (parentheses), कोलन (colons), और इंडेंटेशन जैसी सामान्य गलतियों की जाँच करें।
- इंडेंटेशन का ध्यान रखें: पाइथन में इंडेंटेशन सिंटैक्स का हिस्सा है। गलत इंडेंटेशन
IndentationErrorका कारण बन सकता है, जो एक प्रकार का सिंटैक्स एरर है। - छोटे हिस्सों में कोड करें और परीक्षण करें: एक बार में पूरा प्रोग्राम लिखने के बजाय, छोटे, प्रबंधनीय हिस्सों में कोड लिखें और प्रत्येक हिस्से का परीक्षण करें। इससे त्रुटियों को ढूंढना और ठीक करना आसान हो जाता है।
(e) डेटा इनकैप्सुलेशन (Data Encapsulation)
डेटा इनकैप्सुलेशन ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग का एक मौलिक सिद्धांत है। इसका अर्थ है डेटा (विशेषताएँ) और उस डेटा पर काम करने वाले तरीकों (methods) को एक ही इकाई, यानी एक क्लास के भीतर एक साथ बांधना। यह बाहरी दुनिया से ऑब्जेक्ट के डेटा को सीधे एक्सेस से बचाता है और केवल क्लास के तरीकों के माध्यम से ही एक्सेस की अनुमति देता है। यह “डेटा हाइडिंग” की अवधारणा को लागू करने में मदद करता है। पाइथन में एक्सेस स्पेसिफायर्स की तुलना:
पाइथन में, जावा या C++ की तरह सख्त public , private , या protected कीवर्ड नहीं होते हैं, लेकिन यह नामकरण परंपराओं (naming conventions) का उपयोग करके समान अवधारणाओं को लागू करता है।
- पब्लिक (Public):
- परंपरा: कोई विशेष उपसर्ग नहीं (जैसे,
self.name)। - पहुंच: पब्लिक सदस्य क्लास के अंदर, बाहर और सब-क्लास में कहीं से भी एक्सेस किए जा सकते हैं। यह पाइथन में डिफ़ॉल्ट व्यवहार है।
- उदाहरण:
my_object.name
- परंपरा: कोई विशेष उपसर्ग नहीं (जैसे,
- प्रोटेक्टेड (Protected):
- परंपरा: एक अंडरस्कोर उपसर्ग (जैसे,
self._balance)। - पहुंच: यह एक संकेत है कि विशेषता केवल क्लास के भीतर और उसकी सब-क्लासेस में उपयोग के लिए है। हालांकि, पाइथन इसे तकनीकी रूप से लागू नहीं करता है; आप अभी भी इसे बाहर से एक्सेस कर सकते हैं, लेकिन ऐसा करना एक खराब प्रथा माना जाता है।
- उदाहरण:
my_object._balance(तकनीकी रूप से संभव, लेकिन अनुशंसित नहीं)।
- परंपरा: एक अंडरस्कोर उपसर्ग (जैसे,
- प्राइवेट (Private):
- परंपरा: दो अंडरस्कोर उपसर्ग (जैसे,
self.__account_number)। - पहुंच: प्राइवेट सदस्यों को केवल उनकी अपनी क्लास के भीतर से ही एक्सेस किया जा सकता है। पाइथन इसे नेम मैंगलिंग (name mangling) नामक एक तकनीक का उपयोग करके लागू करता है। यह विशेषता का नाम बदलकर
_ClassName__attributeNameकर देता है, जिससे इसे बाहर से सीधे एक्सेस करना मुश्किल हो जाता है। - उदाहरण: आप
my_object.__account_numberको सीधे एक्सेस नहीं कर सकते। आपको इसे एक्सेस करने के लिएmy_object._ClassName__account_numberका उपयोग करना होगा।
- परंपरा: दो अंडरस्कोर उपसर्ग (जैसे,
Q2. (a) What is unit test in Python ? Explain the concepts used in unit test framework. (b) What is exception ? Explain the difference between built-in exception and user defined exception. Also explain how exceptions are handled in Python the example code.
Ans.
(a) पाइथन में यूनिट टेस्ट (Unit Test)
यूनिट टेस्ट एक सॉफ्टवेयर टेस्टिंग विधि है जिसमें सॉफ्टवेयर एप्लिकेशन के अलग-अलग हिस्सों या “यूनिट्स” का परीक्षण किया जाता है। एक “यूनिट” कोड का सबसे छोटा परीक्षण योग्य हिस्सा होता है, जो आमतौर पर एक फ़ंक्शन, मेथड या क्लास होता है। यूनिट टेस्टिंग का मुख्य उद्देश्य यह सत्यापित करना है कि कोड की प्रत्येक यूनिट अपेक्षा के अनुसार सही ढंग से काम करती है।
पाइथन में, unittest नामक एक अंतर्निहित (built-in) फ्रेमवर्क है जो यूनिट टेस्ट बनाने और चलाने के लिए एक मानकीकृत तरीका प्रदान करता है। यूनिट टेस्ट फ्रेमवर्क में प्रयुक्त अवधारणाएं: पाइथन का unittest फ्रेमवर्क निम्नलिखित प्रमुख अवधारणाओं पर आधारित है:
- टेस्ट केस (Test Case): यह परीक्षण की मूल इकाई है। यह
unittest.TestCaseक्लास का एक सब-क्लास बनाकर बनाया जाता है। इस क्लास के भीतर, आप व्यक्तिगत परीक्षण तरीकों को परिभाषित करते हैं। प्रत्येक परीक्षण मेथड का नामtest_से शुरू होना चाहिए। - टेस्ट फिक्स्चर (Test Fixture): एक टेस्ट फिक्स्चर उन क्रियाओं का प्रतिनिधित्व करता है जो एक या अधिक परीक्षणों को चलाने से पहले और बाद में की जानी चाहिए। इसमें परीक्षण के लिए आवश्यक संसाधनों को सेट करना (जैसे डेटाबेस कनेक्शन बनाना या अस्थायी फाइलें बनाना) और परीक्षण के बाद उन्हें साफ करना शामिल है।
unittestमें, यहsetUp()औरtearDown()मेथड्स का उपयोग करके किया जाता है।setUp()प्रत्येक टेस्ट मेथड से पहले औरtearDown()प्रत्येक टेस्ट मेथड के बाद चलता है। - टेस्ट सूट (Test Suite): एक टेस्ट सूट टेस्ट केस, अन्य टेस्ट सूट या दोनों का एक संग्रह है। इसका उपयोग उन परीक्षणों को समूहित करने के लिए किया जाता है जिन्हें एक साथ निष्पादित किया जाना चाहिए। आप विभिन्न मॉड्यूलों से परीक्षणों को एक साथ बंडल करने के लिए टेस्ट सूट का उपयोग कर सकते हैं।
- टेस्ट रनर (Test Runner): यह एक घटक है जो परीक्षणों के निष्पादन को व्यवस्थित करता है और उपयोगकर्ता को परिणाम प्रदान करता है। यह परीक्षणों को चलाता है, परिणामों को एकत्र करता है और उन्हें एक रिपोर्ट के रूप में प्रारूपित करता है, जिसमें विफलताओं और त्रुटियों का विवरण होता है।
- एसर्शन (Assertions): टेस्ट केस के भीतर, आप यह जांचने के लिए एसर्शन मेथड्स का उपयोग करते हैं कि कोई शर्त सही है या नहीं। यदि एसर्शन विफल हो जाता है, तो परीक्षण को विफल (failed) के रूप में चिह्नित किया जाता है। उदाहरणों में
assertEqual(),assertTrue(),assertIn(), औरassertRaises()शामिल हैं।
(b) एक्सेप्शन (Exception) और इसका हैंडलिंग
एक्सेप्शन (Exception) क्या है?
एक एक्सेप्शन एक त्रुटि है जो प्रोग्राम के सामान्य निष्पादन प्रवाह को बाधित करती है। यह तब होता है जब पाइथन स्क्रिप्ट में कुछ अप्रत्याशित या असाधारण होता है, जैसे कि शून्य से भाग देना, एक ऐसी फ़ाइल खोलने का प्रयास करना जो मौजूद नहीं है, या गलत डेटा प्रकार का उपयोग करना। जब कोई एक्सेप्शन होता है, तो पाइथन एक एक्सेप्शन ऑब्जेक्ट बनाता है और प्रोग्राम के प्रवाह को रोक देता है, जब तक कि उसे संभाला न जाए। बिल्ट-इन और यूजर-डिफाइंड एक्सेप्शन के बीच अंतर:
आधार
बिल्ट-इन एक्सेप्शन (Built-in Exception)
यूजर-डिफाइंड एक्सेप्शन (User-defined Exception)
परिभाषा
ये पाइथन इंटरप्रेटर में पहले से परिभाषित एक्सेप्शन हैं। वे मानक त्रुटियों को कवर करते हैं।
ये प्रोग्रामर द्वारा विशिष्ट एप्लिकेशन आवश्यकताओं के लिए बनाए गए कस्टम एक्सेप्शन हैं।
उदाहरण
ValueError , TypeError , ZeroDivisionError , FileNotFoundError , IndexError .
एक एप्लिकेशन में InvalidAgeError या InsufficientBalanceError जैसे कस्टम एक्सेप्शन हो सकते हैं।
निर्माण
ये पाइथन द्वारा स्वचालित रूप से उत्पन्न होते हैं जब कोई संबंधित त्रुटि होती है।
इन्हें Exception क्लास या इसके किसी सब-क्लास से विरासत (inherit) लेकर बनाया जाता है।
उपयोग
सामान्य प्रोग्रामिंग त्रुटियों को संभालने के लिए उपयोग किया जाता है।
एप्लिकेशन-विशिष्ट त्रुटि स्थितियों को अधिक स्पष्ट और पठनीय तरीके से संभालने के लिए उपयोग किया जाता है।
पाइथन में एक्सेप्शन हैंडलिंग:
पाइथन में एक्सेप्शन को try…except ब्लॉक का उपयोग करके संभाला जाता है।
tryब्लॉक: इस ब्लॉक में वह कोड होता है जिसमें एक्सेप्शन हो सकता है।exceptब्लॉक: यदिtryब्लॉक में कोई एक्सेप्शन होता है, तो संबंधितexceptब्लॉक निष्पादित होता है। आप विशिष्ट एक्सेप्शन को पकड़ सकते हैं।elseब्लॉक (वैकल्पिक): यह ब्लॉक तब निष्पादित होता है जबtryब्लॉक में कोई एक्सेप्शन नहीं होता है।finallyब्लॉक (वैकल्पिक): यह ब्लॉक हमेशा निष्पादित होता है, चाहे कोई एक्सेप्शन हुआ हो या नहीं। इसका उपयोग आमतौर पर संसाधनों को साफ करने (releasing resources) के लिए किया जाता है।
उदाहरण कोड: “`python def divide_numbers(a, b): try: # वह कोड जिसमें एक्सेप्शन हो सकता है result = a / b except ZeroDivisionError: # जब शून्य से भाग दिया जाता है तो इसे संभालना print(“Error: Division by zero is not allowed.”) return None except TypeError: # जब गैर-संख्यात्मक मानों पर ऑपरेशन किया जाता है print(“Error: Both inputs must be numbers.”) return None else: # जब कोई एक्सेप्शन नहीं होता है print(“Division was successful.”) return result finally: # यह हमेशा निष्पादित होगा print(“Execution of the divide function finished.”) # सफल निष्पादन print(divide_numbers(10, 2)) print(“-” * 20) # ZeroDivisionError को ट्रिगर करना print(divide_numbers(10, 0)) print(“-” * 20) # TypeError को ट्रिगर करना print(divide_numbers(10, “a”)) “`
Q3. (a) Write Python code to create a database file and a table named “STUDENT’ with three columns “Enrollment No”, “Name” and “Programme”. Write suitable comments in your code. (b) What is method overloading and method overriding ? Explain how these concepts are implemented in Python.
Ans.
(a) पाइथन में डेटाबेस और टेबल बनाने के लिए कोड
यह पाइथन कोड sqlite3 मॉड्यूल का उपयोग करके एक SQLite डेटाबेस फ़ाइल बनाता है। SQLite एक सर्वर-रहित, स्व-निहित (self-contained) SQL डेटाबेस इंजन है जो पाइथन के साथ आता है। कोड एक डेटाबेस फ़ाइल university.db बनाएगा और उसमें STUDENT नामक एक टेबल बनाएगा।
“`python # sqlite3 मॉड्यूल को इम्पोर्ट करें, जो SQLite डेटाबेस के साथ काम करने के लिए आवश्यक है import sqlite3 try: # 1. डेटाबेस से कनेक्ट करें (या इसे बनाएं यदि यह मौजूद नहीं है) # ‘university.db’ नामक फ़ाइल से कनेक्ट करें। यदि यह फ़ाइल मौजूद नहीं है, तो यह बनाई जाएगी। conn = sqlite3.connect(‘university.db’) # 2. एक कर्सर ऑब्जेक्ट बनाएं # कर्सर का उपयोग SQL कमांड को निष्पादित करने के लिए किया जाता है। cursor = conn.cursor() print(“Database created and successfully connected.”) # 3. ‘STUDENT’ नामक एक टेबल बनाने के लिए SQL कमांड # ”’ का उपयोग करके एक बहु-पंक्ति स्ट्रिंग में SQL क्वेरी को परिभाषित करें। # IF NOT EXISTS यह सुनिश्चित करता है कि यदि टेबल पहले से मौजूद है तो कोई त्रुटि न हो। create_table_query = ”’ CREATE TABLE IF NOT EXISTS STUDENT ( “Enrollment No” TEXT PRIMARY KEY, “Name” TEXT NOT NULL, “Programme” TEXT NOT NULL ); ”’
# 4. SQL कमांड को निष्पादित करें # cursor.execute() मेथड का उपयोग करके टेबल बनाने की क्वेरी चलाएं। cursor.execute(create_table_query) print(“Table ‘STUDENT’ created successfully.”) # 5. परिवर्तनों को सहेजें (कमिट करें) # डेटाबेस में किए गए किसी भी परिवर्तन को स्थायी रूप से सहेजने के लिए conn.commit() आवश्यक है। conn.commit() except sqlite3.Error as error: # यदि कोई SQL त्रुटि होती है तो उसे पकड़ें और प्रिंट करें print(“Error while creating table”, error) finally: # 6. कनेक्शन बंद करें # यह सुनिश्चित करें कि कनेक्शन हमेशा बंद हो, चाहे कोई त्रुटि हुई हो या नहीं। if conn: conn.close() print(“The SQLite connection is closed.”) “` कोड का स्पष्टीकरण:
import sqlite3: SQLite के साथ इंटरैक्ट करने के लिए आवश्यक मॉड्यूल को इम्पोर्ट करता है।sqlite3.connect():university.dbनामक एक डेटाबेस फ़ाइल बनाता है (यदि यह मौजूद नहीं है) और एक कनेक्शन ऑब्जेक्ट लौटाता है।conn.cursor(): एक कर्सर ऑब्जेक्ट बनाता है, जो हमें SQL स्टेटमेंट निष्पादित करने की अनुमति देता है।cursor.execute(): दी गई SQL क्वेरी को चलाता है। यहाँ, यहSTUDENTनामक एक टेबल बनाता है।CREATE TABLEस्टेटमेंट: यह टेबल की संरचना को परिभाषित करता है, जिसमें तीन कॉलम हैं:"Enrollment No"(एक टेक्स्ट और प्राइमरी की),"Name"(एक टेक्स्ट और खाली नहीं हो सकता), और"Programme"(एक टेक्स्ट और खाली नहीं हो सकता)।conn.commit(): लेन-देन (transaction) को डेटाबेस में स्थायी रूप से सहेजता है।conn.close(): डेटाबेस से कनेक्शन बंद कर देता है।
(b) मेथड ओवरलोडिंग और मेथड ओवरराइडिंग
मेथड ओवरलोडिंग और मेथड ओवरराइडिंग दोनों पॉलीमॉर्फिज्म (Polymorphism) की अवधारणाएं हैं, जो ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग में महत्वपूर्ण हैं। मेथड ओवरलोडिंग (Method Overloading):
मेथड ओवरलोडिंग एक ही क्लास में एक ही नाम के कई मेथड बनाने की क्षमता है, लेकिन उनके पैरामीटर अलग-अलग होते हैं (या तो पैरामीटर की संख्या में या उनके प्रकार में)। कंपाइलर या इंटरप्रेटर कॉल के समय पास किए गए आर्गुमेंट्स के आधार पर यह तय करता है कि कौन सा मेथड कॉल करना है। पाइथन में कार्यान्वयन:
पाइथन पारंपरिक अर्थों में मेथड ओवरलोडिंग का समर्थन नहीं करता है । यदि आप एक ही नाम के साथ कई मेथड परिभाषित करते हैं, तो अंतिम परिभाषित मेथड पिछले सभी को ओवरराइट कर देगा। हालांकि, आप डिफ़ॉल्ट आर्गुमेंट्स या वैरिएबल-लेंथ आर्गुमेंट्स ( args , *kwargs ) का उपयोग करके समान व्यवहार प्राप्त कर सकते हैं। उदाहरण (डिफ़ॉल्ट आर्गुमेंट्स का उपयोग करके): “`python class Adder: def add(self, a, b, c=0): # c एक वैकल्पिक पैरामीटर है return a + b + c # ऑब्जेक्ट बनाना calculator = Adder() # दो आर्गुमेंट्स के साथ मेथड को कॉल करना print(f”Sum of two numbers: {calculator.add(5, 10)}”) # आउटपुट: 15 # तीन आर्गुमेंट्स के साथ उसी मेथड को कॉल करना print(f”Sum of three numbers: {calculator.add(5, 10, 15)}”) # आउटपुट: 30 “` मेथड ओवरराइडिंग (Method Overriding):
मेथड ओवरराइडिंग एक ऐसी सुविधा है जो एक सब-क्लास या चाइल्ड क्लास को एक ऐसा मेथड प्रदान करने की अनुमति देती है जो पहले से ही उसकी सुपर-क्लास या पैरेंट क्लास में प्रदान किया गया है। सब-क्लास में ओवरराइड किया गया मेथड सुपर-क्लास के मेथड को प्रतिस्थापित (replace) कर देता है। पाइथन में कार्यान्वयन:
यह इनहेरिटेंस का एक सीधा परिणाम है। यदि एक सब-क्लास एक मेथड को उसी नाम, सिग्नेचर और पैरामीटर के साथ परिभाषित करती है जैसा कि उसकी सुपर-क्लास में है, तो सब-क्लास का मेथड सुपर-क्लास के मेथड को ओवरराइड कर देगा। उदाहरण: “`python # पैरेंट क्लास class Animal: def speak(self): print(“Animal makes a sound”) # चाइल्ड क्लास जो Animal से विरासत में मिलती है class Dog(Animal): # speak() मेथड को ओवरराइड करना def speak(self): print(“Dog barks”) # एक और चाइल्ड क्लास class Cat(Animal): # speak() मेथड को ओवरराइड करना def speak(self): print(“Cat meows”) # ऑब्जेक्ट बनाना generic_animal = Animal() my_dog = Dog() my_cat = Cat() generic_animal.speak() # आउटपुट: Animal makes a sound my_dog.speak() # आउटपुट: Dog barks (ओवरराइड किया गया मेथड) my_cat.speak() # आउटपुट: Cat meows (ओवरराइड किया गया मेथड) “` इस उदाहरण में, Dog और Cat दोनों क्लास Animal क्लास के speak() मेथड को ओवरराइड करती हैं ताकि वे अपना विशिष्ट व्यवहार प्रदान कर सकें।
Q4. (a) Write a Python program which define Account class. Define appropriate constructor for this class. Also this class should have a method to display the account details. (b) Write a Python program using while loop to find the sum of first N numbers. (c) Explain operator overloading in Python with an example.
Ans.
(a) पाइथन में `Account` क्लास
यह प्रोग्राम एक Account क्लास को परिभाषित करता है। इसमें एक कंस्ट्रक्टर ( __init__ ) है जो खाता संख्या, धारक का नाम और शेष राशि के साथ एक नए खाते को इनिशियलाइज़ करता है। इसमें एक display_details मेथड भी है जो खाते के विवरण को प्रिंट करता है।
“`python class Account: “”” यह क्लास एक बैंक खाते का प्रतिनिधित्व करती है। “”” def __init__(self, account_number, holder_name, initial_balance=0.0): “”” Account क्लास के लिए कंस्ट्रक्टर। :param account_number: खाते की संख्या (string) :param holder_name: खाता धारक का नाम (string) :param initial_balance: प्रारंभिक शेष राशि (float) “”” self.account_number = account_number self.holder_name = holder_name self.balance = initial_balance print(f”Account for {self.holder_name} created successfully.”) def display_details(self): “”” खाते के विवरण को प्रदर्शित करने वाला मेथड। “”” print(“\n— Account Details —“) print(f”Account Number: {self.account_number}”) print(f”Account Holder: {self.holder_name}”) print(f”Current Balance: ${self.balance:.2f}”) print(“———————–“) # प्रोग्राम का उपयोग कैसे करें: # Account क्लास का एक ऑब्जेक्ट (इंस्टेंस) बनाएं acc1 = Account(“123456789”, “John Doe”, 5000.75) # खाते का विवरण प्रदर्शित करने के लिए मेथड को कॉल करें acc1.display_details() # एक और अकाउंट ऑब्जेक्ट बनाएं acc2 = Account(“987654321”, “Jane Smith”) # प्रारंभिक शेष राशि डिफ़ॉल्ट रूप से 0.0 होगी acc2.display_details() “` कोड का स्पष्टीकरण:
class Account:एक नई क्लास को परिभाषित करता है जिसका नामAccountहै।def __init__(...):यह कंस्ट्रक्टर मेथड है। जब भीAccountका कोई नया ऑब्जेक्ट बनाया जाता है तो यह स्वचालित रूप से कॉल होता है। यह ऑब्जेक्ट की विशेषताओं (attributes)account_number,holder_name, औरbalanceको सेट करता है।def display_details(self):यह एक इंस्टेंस मेथड है जो ऑब्जेक्ट की विशेषताओं का उपयोग करके खाते के विवरण को एक स्वरूपित (formatted) तरीके से प्रिंट करता है।selfपैरामीटर क्लास के वर्तमान इंस्टेंस को संदर्भित करता है और इसका उपयोग इंस्टेंस की विशेषताओं और तरीकों तक पहुंचने के लिए किया जाता है।
(b) पहले N नंबरों का योग खोजने के लिए `while` लूप
यह प्रोग्राम उपयोगकर्ता से एक संख्या (N) लेता है और फिर 1 से N तक की सभी संख्याओं का योग गणना करने के लिए while लूप का उपयोग करता है। “`python try: # उपयोगकर्ता से N का मान इनपुट करने के लिए कहें n = int(input(“Enter a positive number (N): “)) if n < 0: print(“Please enter a positive number.”) else: # योग और काउंटर को इनिशियलाइज़ करें total_sum = 0 counter = 1 # while लूप का उपयोग करके 1 से N तक की संख्याओं का योग करें while counter <= n: total_sum = total_sum + counter counter = counter + 1 # काउंटर को बढ़ाएं # परिणाम प्रिंट करें print(f”The sum of the first {n} numbers is: {total_sum}”) except ValueError: print(“Invalid input. Please enter an integer.”) “` लूप कैसे काम करता है:
- इनिशियलाइज़ेशन:
total_sumको 0 औरcounterको 1 पर सेट किया जाता है। - शर्त की जाँच: लूप तब तक चलता है जब तक
counterका मानnसे कम या उसके बराबर रहता है (while counter <= n:)। - बॉडी का निष्पादन: प्रत्येक पुनरावृत्ति (iteration) में,
counterका वर्तमान मानtotal_sumमें जोड़ा जाता है। - अपडेट:
counterको 1 से बढ़ाया जाता है (counter = counter + 1)। - समाप्ति: जब
counternसे बड़ा हो जाता है, तो लूप की शर्त झूठी हो जाती है और लूप समाप्त हो जाता है। अंत में, परिकलितtotal_sumप्रिंट होता है।
(c) पाइथन में ऑपरेटर ओवरलोडिंग
ऑपरेटर ओवरलोडिंग एक ऐसी प्रक्रिया है जो प्रोग्रामर को पायथन के अंतर्निहित ऑपरेटरों (जैसे + , – , * , < , > ) के व्यवहार को उपयोगकर्ता-परिभाषित क्लास के ऑब्जेक्ट्स के लिए फिर से परिभाषित करने की अनुमति देती है। डिफ़ॉल्ट रूप से, ये ऑपरेटर केवल पूर्णांक, फ्लोट और स्ट्रिंग जैसे अंतर्निहित डेटा प्रकारों के साथ काम करते हैं। ऑपरेटर ओवरलोडिंग के साथ, आप इन ऑपरेटरों को अपनी कस्टम क्लास के ऑब्जेक्ट्स के लिए एक विशेष अर्थ दे सकते हैं। पाइथन में, ऑपरेटर ओवरलोडिंग विशेष तरीकों को लागू करके किया जाता है, जिन्हें “मैजिक मेथड्स” या “डंडर मेथड्स” (double underscore methods) के रूप में जाना जाता है, जैसे __add__ , __sub__ , __mul__ आदि। उदाहरण: वेक्टर जोड़ने के लिए + ऑपरेटर को ओवरलोड करना
आइए एक Vector क्लास बनाएं जो 2D वेक्टर का प्रतिनिधित्व करती है और दो वेक्टर ऑब्जेक्ट्स को जोड़ने के लिए + ऑपरेटर को ओवरलोड करें। “`python class Vector: def __init__(self, x, y): self.x = x self.y = y def __str__(self): # ऑब्जेक्ट को प्रिंट करते समय एक पठनीय प्रतिनिधित्व प्रदान करता है return f”Vector({self.x}, {self.y})” def __add__(self, other): “”” ‘+’ ऑपरेटर को ओवरलोड करता है। यह मेथड तब कॉल होता है जब इस क्लास के ऑब्जेक्ट पर ‘+’ ऑपरेटर का उपयोग किया जाता है। “”” if isinstance(other, Vector): # दो वेक्टर ऑब्जेक्ट्स के x और y घटकों को जोड़ें new_x = self.x + other.x new_y = self.y + other.y return Vector(new_x, new_y) else: # यदि ऑपरेशन समर्थित नहीं है तो त्रुटि उत्पन्न करें raise TypeError(“Unsupported operand type for +: ‘Vector’ and ” + str(type(other))) # दो वेक्टर ऑब्जेक्ट बनाएं v1 = Vector(2, 4) v2 = Vector(3, 5) # ‘+’ ऑपरेटर का उपयोग करके दो वेक्टर जोड़ें। # यह आंतरिक रूप से v1.__add__(v2) को कॉल करेगा। v3 = v1 + v2 # परिणाम प्रिंट करें print(f”{v1} + {v2} = {v3}”) # आउटपुट: Vector(2, 4) + Vector(3, 5) = Vector(5, 9) # एक असमर्थित प्रकार के साथ जोड़ने का प्रयास try: result = v1 + 10 except TypeError as e: print(e) # आउटपुट: Unsupported operand type for +: ‘Vector’ and “` इस उदाहरण में, __add__ मेथड यह परिभाषित करता है कि जब दो Vector ऑब्जेक्ट्स पर + ऑपरेटर का उपयोग किया जाता है तो क्या होना चाहिए। यह एक नया Vector ऑब्जेक्ट लौटाता है जो दोनों के घटकों का योग है।
Q5. Briefly explain any three of the following using suitable example/diagram : (a) Kivy Architecture (b) String Slices (c) Tuples and List (d) Mobile application development platforms
Ans.
(a) किवी आर्किटेक्चर (Kivy Architecture)
किवी एक ओपन-सोर्स पाइथन लाइब्रेरी है जिसका उपयोग मल्टीटच समर्थन वाले एप्लिकेशन बनाने के लिए किया जाता है। किवी का आर्किटेक्चर मॉड्यूलर है और इसे परतों (layers) में डिज़ाइन किया गया है, जो इसे लचीला और विस्तारणीय (extensible) बनाता है। मुख्य घटक नीचे दिए गए हैं:
- कोर (Core): यह आर्किटेक्चर का आधार है। यह इनपुट प्रोवाइडर्स, विंडो प्रोवाइडर्स और अन्य निम्न-स्तरीय कार्यों को संभालता है। यह ऑपरेटिंग सिस्टम के साथ सीधे इंटरैक्ट करता है ताकि इनपुट (माउस, कीबोर्ड, टच) प्राप्त किया जा सके और एक विंडो बनाई जा सके।
- ग्राफिक्स (Graphics): यह परत OpenGL ES 2.0 पर आधारित है और रेंडरिंग के लिए जिम्मेदार है। यह विजेट्स को स्क्रीन पर खींचने (drawing) के लिए निर्देश प्रदान करती है। यह बनावट (textures), कैनवास और ग्राफिकल निर्देशों का प्रबंधन करती है।
- यूआईएक्स (UIX): इस परत में उपयोगकर्ता इंटरफ़ेस के सभी तत्व होते हैं, जैसे कि विजेट्स (बटन, लेबल, स्लाइडर्स) और लेआउट (ग्रिड, बॉक्स)। ये बिल्डिंग ब्लॉक्स हैं जिनका उपयोग आप अपने एप्लिकेशन का GUI बनाने के लिए करते हैं। किवी के विजेट्स कस्टम-ड्रॉ किए जाते हैं, जिसका अर्थ है कि वे सभी प्लेटफार्मों पर समान दिखते हैं।
- मॉड्यूल्स (Modules): ये ऐसे घटक हैं जिन्हें कोर सिस्टम में प्लग किया जा सकता है ताकि अतिरिक्त कार्यक्षमता प्रदान की जा सके, जैसे कि स्पेल चेकिंग या डीबगिंग।
- इनपुट (Input): यह परत विभिन्न स्रोतों से इनपुट घटनाओं (events) को संभालती है, जैसे कि टच, माउस, कीबोर्ड, और उन्हें विजेट ट्री के माध्यम से भेजती है।
किवी का आर्किटेक्चर गति और लचीलेपन के लिए डिज़ाइन किया गया है, जो डेवलपर्स को क्रॉस-प्लेटफॉर्म एप्लिकेशन बनाने की अनुमति देता है जो विभिन्न उपकरणों पर मूल रूप से चलते हैं।
(b) स्ट्रिंग स्लाइस (String Slices)
पाइथन में, स्ट्रिंग स्लाइसिंग एक स्ट्रिंग के कुछ हिस्सों या सबस्ट्रिंग को निकालने की एक सुविधा है। यह एक शक्तिशाली तकनीक है जो आपको एक स्ट्रिंग के वर्णों की एक श्रृंखला तक पहुंचने की अनुमति देती है। सिंटैक्स: स्लाइसिंग का सिंटैक्स [start:stop:step] है:
start: स्लाइस का प्रारंभिक सूचकांक (index)। यह वर्ण शामिल है। यदि छोड़ा जाता है, तो यह डिफ़ॉल्ट रूप से 0 होता है।stop: स्लाइस का अंतिम सूचकांक। यह वर्ण शामिल नहीं है। यदि छोड़ा जाता है, तो यह स्ट्रिंग के अंत तक जाता है।step: यह इंगित करता है कि कितने वर्णों को छोड़ना है। डिफ़ॉल्ट 1 है। एक ऋणात्मक मान स्ट्रिंग को उल्टा करता है।
उदाहरण: आइए एक स्ट्रिंग s = “Python Programming” पर विचार करें। “`python s = “Python Programming” # 7वें सूचकांक से अंत तक वर्ण प्राप्त करें # आउटपुट: ‘Programming’ print(s[7:]) # शुरुआत से 6वें सूचकांक (शामिल नहीं) तक वर्ण प्राप्त करें # आउटपुट: ‘Python’ print(s[:6]) # 1वें सूचकांक से 10वें सूचकांक तक वर्ण प्राप्त करें # आउटपुट: ‘ython Pro’ print(s[1:11]) # पूरी स्ट्रिंग, हर दूसरे वर्ण को छोड़कर # आउटपुट: ‘Pto rgamn’ print(s[::2]) # स्ट्रिंग को उलटने के लिए ऋणात्मक स्टेप का उपयोग करें # आउटपुट: ‘gnimmargorP nohtyP’ print(s[::-1]) # ऋणात्मक सूचकांकों का उपयोग # अंत से 8वां वर्ण प्राप्त करें # आउटपुट: ‘gramming’ print(s[-8:]) “` स्ट्रिंग स्लाइसिंग मूल स्ट्रिंग को नहीं बदलती है; यह हमेशा एक नई स्ट्रिंग लौटाती है।
(c) ट्यूपल और लिस्ट (Tuples and List)
लिस्ट (List) और ट्यूपल (Tuple) दोनों पाइथन में अंतर्निहित डेटा संरचनाएं हैं जो वस्तुओं के एक क्रमबद्ध संग्रह को संग्रहीत करने के लिए उपयोग की जाती हैं। हालांकि, उनके बीच मुख्य अंतर परिवर्तनशीलता (mutability) है।
विशेषता
लिस्ट (List)
ट्यूपल (Tuple)
परिवर्तनशीलता
म्यूटेबल (Mutable): एक लिस्ट बनाने के बाद उसके तत्वों को जोड़ा, हटाया या बदला जा सकता है।
इम्यूटेबल (Immutable): एक ट्यूपल बनाने के बाद उसके तत्वों को नहीं बदला जा सकता है।
सिंटैक्स
स्क्वायर ब्रैकेट [] का उपयोग करके परिभाषित किया जाता है। my_list = [1, ‘a’, 3.5]
पैरेंथेसिस () का उपयोग करके परिभाषित किया जाता है। my_tuple = (1, ‘a’, 3.5)
प्रदर्शन
ट्यूपल की तुलना में थोड़ी धीमी होती है क्योंकि वे डायनेमिक होती हैं।
लिस्ट की तुलना में थोड़ी तेज और मेमोरी कुशल होती है क्योंकि वे स्थिर होती हैं।
उपयोग के मामले
जब आपको वस्तुओं के संग्रह की आवश्यकता होती है जिसे संशोधित करने की आवश्यकता हो सकती है, जैसे किसी शॉपिंग कार्ट में आइटम।
जब आपके पास डेटा होता है जिसे आप बदलना नहीं चाहते हैं, जैसे सप्ताह के दिन या निर्देशांक (x, y)। ट्यूपल को डिक्शनरी की के रूप में भी इस्तेमाल किया जा सकता है।
मेथड्स
इसमें append() , remove() , pop() , sort() जैसे कई मेथड होते हैं।
इसमें केवल count() और index() जैसे कुछ ही मेथड होते हैं क्योंकि इसे बदला नहीं जा सकता।
उदाहरण: “`python # लिस्ट (Mutable) my_list = [10, 20, 30] my_list[1] = 25 # मान्य my_list.append(40) # मान्य print(f”List: {my_list}”) # आउटपुट: List: [10, 25, 30, 40] # ट्यूपल (Immutable) my_tuple = (10, 20, 30) # my_tuple[1] = 25 # यह एक TypeError देगा print(f”Tuple: {my_tuple}”) # आउटपुट: Tuple: (10, 20, 30) “`
(d) मोबाइल एप्लिकेशन डेवलपमेंट प्लेटफॉर्म (Mobile application development platforms)
मोबाइल एप्लिकेशन डेवलपमेंट प्लेटफॉर्म वे उपकरण और वातावरण हैं जिनका उपयोग स्मार्टफोन, टैबलेट और अन्य मोबाइल उपकरणों के लिए सॉफ्टवेयर बनाने के लिए किया जाता है। इन्हें मुख्य रूप से दो श्रेणियों में बांटा जा सकता है: नेटिव और क्रॉस-प्लेटफॉर्म।
- नेटिव ऐप डेवलपमेंट (Native App Development):
इस दृष्टिकोण में, ऐप को एक विशिष्ट ऑपरेटिंग सिस्टम (जैसे iOS या Android) के लिए उसकी मूल प्रोग्रामिंग भाषा और टूल का उपयोग करके बनाया जाता है।
- एंड्रॉइड (Android): Google द्वारा प्रदान किया गया। ऐप्स को जावा (Java) या कोट्लिन (Kotlin) का उपयोग करके एंड्रॉइड स्टूडियो (Android Studio) IDE में विकसित किया जाता है। नेटिव ऐप्स डिवाइस की हार्डवेयर सुविधाओं (जैसे कैमरा, GPS) तक सीधी पहुंच प्रदान करते हैं और सर्वश्रेष्ठ प्रदर्शन और उपयोगकर्ता अनुभव प्रदान करते हैं।
- आईओएस (iOS): Apple द्वारा प्रदान किया गया। ऐप्स को स्विफ्ट (Swift) या ऑब्जेक्टिव-सी (Objective-C) का उपयोग करके एक्सकोड (Xcode) IDE में विकसित किया जाता है, जो केवल macOS पर चलता है। नेटिव iOS ऐप्स अपने सहज प्रदर्शन और Apple के इकोसिस्टम के साथ सहज एकीकरण के लिए जाने जाते हैं।
- क्रॉस-प्लेटफॉर्म ऐप डेवलपमेंट (Cross-Platform App Development):
यह दृष्टिकोण डेवलपर्स को एक ही कोडबेस लिखने और उसे कई प्लेटफार्मों (iOS और Android दोनों) पर चलाने की अनुमति देता है, जिससे समय और लागत की बचत होती है।
- फ्लटर (Flutter): Google का UI टूलकिट। यह डार्ट (Dart) प्रोग्रामिंग भाषा का उपयोग करता है और अपने तेज़ प्रदर्शन और अभिव्यंजक UI के लिए जाना जाता है।
- रिएक्ट नेटिव (React Native): फेसबुक (मेटा) द्वारा बनाया गया। यह जावास्क्रिप्ट (JavaScript) और रिएक्ट (React) का उपयोग करता है, जो इसे वेब डेवलपर्स के बीच लोकप्रिय बनाता है।
- किवी (Kivy): एक ओपन-सोर्स पाइथन लाइब्रेरी। यह तेजी से प्रोटोटाइपिंग और उन अनुप्रयोगों के लिए उपयुक्त है जिन्हें व्यापक मल्टीटच समर्थन की आवश्यकता होती है।
- ज़ामरिन (Xamarin): माइक्रोसॉफ्ट के स्वामित्व वाला। यह C# और .NET फ्रेमवर्क का उपयोग करता है ताकि iOS, Android और Windows के लिए नेटिव UI के साथ ऐप्स बना सके।
सही प्लेटफॉर्म का चुनाव परियोजना की आवश्यकताओं, बजट, लक्षित दर्शकों और डेवलपर की विशेषज्ञता पर निर्भर करता है।
IGNOU BCS-094 Previous Year Solved Question Paper in English
Q1. (a) Write a Python program which take marks of a class test of a student and display “Good” if marks are greater than 30 and display “Need improvement” otherwise. (b) What is Object Oriental Programming ? Explain the concept of object and class with example. Write code to explain how a class is defined in Python. (c) What is the need of software testing ? Explain the difference between functional and non-functional testing. (d) What is syntax error ? Explain how syntax error may be avoided in Python with the help of an example. (e) What is data encapsulation ? Compare public, private and protected access specifiers used in Python.
Ans. (a) Python Program to Display Performance Based on Marks: This program takes marks as input from the user and then uses an if-else statement to check if the marks are greater than 30.
“`python # Ask the user to input the marks try: marks = int(input(“Enter the marks obtained in the class test: “))
# Check if the marks are greater than 30 if marks > 30: print(“Good”) else: print(“Need improvement”)
except ValueError: print(“Invalid input. Please enter a valid number for marks.”)
“` Explanation of the program:
- The
input()function is used to get input from the user, which is returned as a string. - The
int()function is used to convert the string into an integer. - The if condition
marks > 30checks whether the student’s marks are greater than 30. If it’s true, “Good” is printed. - If the if condition is false, the else block is executed, and “Need improvement” is printed.
- A try-except block is used to handle invalid input, such as entering text instead of a number.
(b) Object-Oriented Programming (OOP) Object-Oriented Programming (OOP) is a programming paradigm based on the concept of “objects”, which can contain data, in the form of fields (often known as attributes or properties), and code, in the form of procedures (often known as methods). The primary purpose of OOP is to model real-world entities in programming, making code more modular, reusable, and easier to maintain.
Concept of Class and Object:
- Class: A class is a blueprint or a template for creating objects. It defines the attributes (properties) and behaviors (methods) that all objects of a particular type will have. For example, a
Carclass might have attributes like color, model, and year, and behaviors like start, stop, and drive. - Object: An object is an instance of a class. It is a real-world entity that has a state and behavior defined by its class. For example, if
Caris a class, then a “Red Maruti Swift” and a “Blue Honda City” would be objects of that class.
Defining a Class in Python: In Python, a class is defined using the class keyword.
“`python # Defining a class named ‘Car’ class Car: # This is a constructor method def __init__(self, color, model): self.color = color # Attribute of the object self.model = model # Attribute of the object
# A method to display the object’s details def display_details(self): print(f”This is a {self.color} {self.model}.”)
# Creating an object of the ‘Car’ class my_car = Car(“Red”, “Maruti Swift”)
# Calling the method of the object my_car.display_details() # Output: This is a Red Maruti Swift. “` (c) Need for Software Testing and its Types Need for Software Testing: Software testing is the process of evaluating a software application to check whether it meets the specified requirements. It is needed for the following reasons:
- Ensuring Quality: It ensures that the software product is reliable, efficient, and user-friendly.
- Defect Detection: It helps in identifying and fixing bugs and errors early in the development cycle, reducing cost later on.
- Customer Satisfaction: A well-tested software performs better and meets user expectations, leading to higher customer satisfaction.
- Security: It helps in securing the application by finding vulnerabilities.
Difference between Functional and Non-functional Testing:
Basis |
Functional Testing |
Non-Functional Testing |
|---|---|---|
Objective |
Verifies what the system does. It tests the functionalities of the application. |
Verifies how the system works. It tests aspects like performance, usability, and reliability. |
Focus |
Validation of business requirements. | Evaluation of user experience and system performance. |
Examples |
Unit Testing, Integration Testing, Regression Testing, API Testing. | Performance Testing, Load Testing, Security Testing, Usability Testing. |
Type |
Can be performed as black-box testing. | Evaluates performance, load, and stress. |
(d) Syntax Error A syntax error is a violation of the grammar or rules of a programming language. When the Python interpreter tries to parse the code and finds a statement that does not conform to the language rules, it raises a syntax error. These errors are detected before the program starts to execute.
Example: “`python # Example of a syntax error x = 10 if x > 5 # The colon (:) is missing here print(“x is greater than 5”) “` In the code above, the colon (:) is missing at the end of the if statement, which is a violation of Python’s syntax. Running this will cause the interpreter to raise a SyntaxError: expected ':' .
How to Avoid Syntax Errors in Python:
- Use a Code Editor/IDE: Modern code editors like VS Code, PyCharm, etc., have syntax highlighting and linting features. They highlight syntax errors as you type.
- Read Code Carefully: Review your code before submitting or running it. Check for common mistakes like mismatched parentheses, missing colons, and incorrect indentation.
- Pay Attention to Indentation: In Python, indentation is part of the syntax. Incorrect indentation can cause an
IndentationError, which is a type of syntax error. - Code and Test in Small Chunks: Instead of writing the entire program at once, write small, manageable pieces of code and test each piece. This makes it easier to find and fix errors.
(e) Data Encapsulation Data encapsulation is a fundamental principle of Object-Oriented Programming. It refers to the bundling of data (attributes) and the methods that operate on that data into a single unit, i.e., a class. It restricts direct access to an object’s data from the outside world and allows access only through the methods of the class. This helps in implementing the concept of “data hiding”.
Comparison of Access Specifiers in Python: Python does not have strict public , private , or protected keywords like Java or C++. Instead, it uses naming conventions to enforce similar concepts.
- Public:
- Convention: No special prefix (e.g.,
self.name). - Access: Public members can be accessed from anywhere—inside the class, outside the class, and in subclasses. This is the default behavior in Python.
- Example:
my_object.name
- Convention: No special prefix (e.g.,
- Protected:
- Convention: Single underscore prefix (e.g.,
self._balance). - Access: It is a hint that the attribute is intended for internal use within the class and its subclasses only. However, Python does not technically enforce this; you can still access it from outside, but it is considered bad practice.
- Example:
my_object._balance(technically possible, but not recommended).
- Convention: Single underscore prefix (e.g.,
- Private:
- Convention: Double underscore prefix (e.g.,
self.__account_number). - Access: Private members can only be accessed from within their own class. Python enforces this using a technique called name mangling . It renames the attribute to
_ClassName__attributeName, making it difficult to access directly from outside. - Example: You cannot access
my_object.__account_numberdirectly. You would have to usemy_object._ClassName__account_numberto access it.
- Convention: Double underscore prefix (e.g.,
Q2. (a) What is unit test in Python ? Explain the concepts used in unit test framework. (b) What is exception ? Explain the difference between built-in exception and user defined exception. Also explain how exceptions are handled in Python the example code.
Ans. (a) Unit Test in Python A unit test is a software testing method where individual parts or “units” of a software application are tested in isolation. A “unit” is the smallest testable piece of code, usually a function, method, or class. The primary goal of unit testing is to verify that each unit of the code performs as expected.
In Python, there is a built-in framework called unittest which provides a standardized way to create and run unit tests.
Concepts Used in the Unit Test Framework: Python’s unittest framework is based on the following key concepts:
- Test Case: This is the basic unit of testing. It is created by subclassing
unittest.TestCase. Within this class, you define the individual test methods. Each test method’s name must start withtest_. - Test Fixture: A test fixture represents the actions that must be performed before and after running one or more tests. This includes setting up necessary resources for the test (like creating a database connection or temporary files) and cleaning them up after the test. In
unittest, this is achieved using thesetUp()andtearDown()methods.setUp()runs before each test method, andtearDown()runs after each test method. - Test Suite: A test suite is a collection of test cases, other test suites, or both. It is used to group tests that should be executed together. You can use test suites to bundle tests from different modules together.
- Test Runner: This is a component that orchestrates the execution of tests and provides the outcome to the user. It runs the tests, collects the results, and formats them into a report, detailing failures and errors.
- Assertions: Within a test case, you use assertion methods to check if a condition is true. If an assertion fails, the test is marked as failed. Examples include
assertEqual(),assertTrue(),assertIn(), andassertRaises().
(b) Exception and its Handling What is an Exception? An exception is an error that disrupts the normal execution flow of a program. It occurs when something unexpected or exceptional happens in a Python script, such as dividing by zero, trying to open a file that doesn’t exist, or using the wrong data type. When an exception occurs, Python creates an exception object and stops the program’s flow unless it is handled.
Difference between Built-in and User-defined Exceptions:
Basis |
Built-in Exception |
User-defined Exception |
|---|---|---|
Definition |
These are exceptions that are pre-defined in the Python interpreter. They cover standard errors. | These are custom exceptions created by the programmer for specific application requirements. |
Examples |
, , , , . |
An application might have custom exceptions likeor . |
Creation |
They are raised automatically by Python when a corresponding error occurs. |
They are created by inheriting from theclass or one of its subclasses. |
Usage |
Used to handle common programming errors. | Used to handle application-specific error conditions in a more explicit and readable way. |
Exception Handling in Python: Exceptions in Python are handled using the try...except block.
tryblock: Contains the code that might cause an exception.exceptblock: If an exception occurs in thetryblock, the correspondingexceptblock is executed. You can catch specific exceptions.elseblock (optional): This block is executed if no exception occurs in thetryblock.finallyblock (optional): This block is always executed, regardless of whether an exception occurred or not. It is typically used for releasing resources.
Example Code: “`python def divide_numbers(a, b): try: # The code that might raise an exception result = a / b except ZeroDivisionError: # Handling the case of division by zero print(“Error: Division by zero is not allowed.”) return None except TypeError: # Handling non-numeric values print(“Error: Both inputs must be numbers.”) return None else: # Executed if no exception occurs print(“Division was successful.”) return result finally: # This will always be executed print(“Execution of the divide function finished.”)
# Successful execution print(divide_numbers(10, 2))
print(“-” * 20)
# Triggering ZeroDivisionError print(divide_numbers(10, 0))
print(“-” * 20)
# Triggering TypeError print(divide_numbers(10, “a”)) “`
Q3. (a) Write Python code to create a database file and a table named “STUDENT’ with three columns “Enrollment No”, “Name” and “Programme”. Write suitable comments in your code. (b) What is method overloading and method overriding ? Explain how these concepts are implemented in Python.
Ans. (a) Code to Create a Database and Table in Python This Python code uses the sqlite3 module to create an SQLite database file. SQLite is a server-less, self-contained SQL database engine that comes with Python. The code will create a database file named university.db and a table named STUDENT within it.
“`python # Import the sqlite3 module, which is needed to work with SQLite databases import sqlite3
try: # 1. Connect to the database (or create it if it doesn’t exist) # Connect to a file named ‘university.db’. If this file does not exist, it will be created. conn = sqlite3.connect(‘university.db’)
# 2. Create a cursor object # The cursor is used to execute SQL commands. cursor = conn.cursor() print(“Database created and successfully connected.”)
# 3. SQL command to create a table named ‘STUDENT’ # Define the SQL query in a multi-line string using ”’. # IF NOT EXISTS ensures no error is raised if the table already exists. create_table_query = ”’ CREATE TABLE IF NOT EXISTS STUDENT ( “Enrollment No” TEXT PRIMARY KEY, “Name” TEXT NOT NULL, “Programme” TEXT NOT NULL ); ”’ # 4. Execute the SQL command # Run the table creation query using the cursor.execute() method. cursor.execute(create_table_query) print(“Table ‘STUDENT’ created successfully.”)
# 5. Save (commit) the changes # conn.commit() is necessary to save any changes made to the database permanently. conn.commit()
except sqlite3.Error as error: # Catch and print any SQL errors that occur print(“Error while creating table”, error)
finally: # 6. Close the connection # Ensure the connection is always closed, whether an error occurred or not. if conn: conn.close() print(“The SQLite connection is closed.”)
“` Explanation of the Code:
import sqlite3: Imports the necessary module to interact with SQLite.sqlite3.connect(): Creates a database file nameduniversity.db(if it doesn’t exist) and returns a connection object.conn.cursor(): Creates a cursor object, which allows us to execute SQL statements.cursor.execute(): Runs the given SQL query. Here, it creates a table namedSTUDENT.CREATE TABLEstatement: Defines the structure of the table with three columns:"Enrollment No"(TEXT and PRIMARY KEY),"Name"(TEXT and cannot be null), and"Programme"(TEXT and cannot be null).conn.commit(): Saves the transaction permanently to the database.conn.close(): Closes the connection to the database.
(b) Method Overloading and Method Overriding Method overloading and method overriding are both concepts of Polymorphism, which is crucial in object-oriented programming.
Method Overloading: Method overloading is the ability to have multiple methods with the same name within the same class, but with different parameters (either in the number of parameters or their types). The compiler or interpreter decides which method to call based on the arguments passed at the time of the call.
Implementation in Python: Python does not support method overloading in the traditional sense. If you define multiple methods with the same name, the last defined method will overwrite all the previous ones.
However, you can achieve similar behavior using default arguments or variable-length arguments ( args , *kwargs ).
Example (using default arguments): “`python class Adder: def add(self, a, b, c=0): # c is an optional parameter return a + b + c
# Create an object calculator = Adder()
# Call the method with two arguments print(f”Sum of two numbers: {calculator.add(5, 10)}”) # Output: 15
# Call the same method with three arguments print(f”Sum of three numbers: {calculator.add(5, 10, 15)}”) # Output: 30 “`
Method Overriding: Method overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by its super-class or parent class. The overridden method in the subclass replaces the method from the super-class.
Implementation in Python: This is a direct consequence of inheritance. If a subclass defines a method with the same name, signature, and parameters as a method in its super-class, the method in the subclass will override the one in the super-class.
Example: “`python # Parent class class Animal: def speak(self): print(“Animal makes a sound”)
# Child class inheriting from Animal class Dog(Animal): # Overriding the speak() method def speak(self): print(“Dog barks”)
# Another child class class Cat(Animal): # Overriding the speak() method def speak(self): print(“Cat meows”)
# Create objects generic_animal = Animal() my_dog = Dog() my_cat = Cat()
generic_animal.speak() # Output: Animal makes a sound my_dog.speak() # Output: Dog barks (overridden method) my_cat.speak() # Output: Cat meows (overridden method) “` In this example, both the Dog and Cat classes override the speak() method of the Animal class to provide their specific behavior.
Q4. (a) Write a Python program which define Account class. Define appropriate constructor for this class. Also this class should have a method to display the account details. (b) Write a Python program using while loop to find the sum of first N numbers. (c) Explain operator overloading in Python with an example.
Ans. (a) `Account` Class in Python This program defines an Account class. It has a constructor ( __init__ ) that initializes a new account with an account number, holder’s name, and balance. It also has a display_details method that prints the account’s details.
“`python class Account: “”” This class represents a bank account. “””
def __init__(self, account_number, holder_name, initial_balance=0.0): “”” Constructor for the Account class. :param account_number: The account number (string) :param holder_name: The name of the account holder (string) :param initial_balance: The initial balance (float) “”” self.account_number = account_number self.holder_name = holder_name self.balance = initial_balance print(f”Account for {self.holder_name} created successfully.”)
def display_details(self): “”” Method to display the details of the account. “”” print(“\n— Account Details —“) print(f”Account Number: {self.account_number}”) print(f”Account Holder: {self.holder_name}”) print(f”Current Balance: ${self.balance:.2f}”) print(“———————–“)
# How to use the program: # Create an object (instance) of the Account class acc1 = Account(“123456789”, “John Doe”, 5000.75)
# Call the method to display the account details acc1.display_details()
# Create another account object acc2 = Account(“987654321”, “Jane Smith”) # initial_balance will default to 0.0 acc2.display_details() “` Explanation of the Code:
class Account:Defines a new class namedAccount.def __init__(...):This is the constructor method. It is called automatically whenever a new object ofAccountis created. It sets the object’s attributes:account_number,holder_name, andbalance.def display_details(self):This is an instance method that prints the account details in a formatted way, using the object’s attributes.- The
selfparameter refers to the current instance of the class and is used to access the attributes and methods of the instance.
(b) `while` Loop to Find the Sum of First N Numbers This program takes a number (N) from the user and then uses a while loop to calculate the sum of all numbers from 1 to N.
“`python try: # Ask the user to input the value of N n = int(input(“Enter a positive number (N): “))
if n < 0: print(“Please enter a positive number.”) else: # Initialize the sum and a counter total_sum = 0 counter = 1
# Use a while loop to sum numbers from 1 to N while counter <= n: total_sum = total_sum + counter counter = counter + 1 # Increment the counter
# Print the result print(f”The sum of the first {n} numbers is: {total_sum}”)
except ValueError: print(“Invalid input. Please enter an integer.”)
“` How the loop works:
- Initialization:
total_sumis set to 0 andcounteris set to 1. - Condition Check: The loop continues as long as the value of
counteris less than or equal ton(while counter <= n:). - Body Execution: In each iteration, the current value of
counteris added tototal_sum. - Update: The
counteris incremented by 1 (counter = counter + 1). - Termination: When
counterbecomes greater thann, the loop condition becomes false, and the loop terminates. Finally, the calculatedtotal_sumis printed.
(c) Operator Overloading in Python Operator overloading is a process that allows programmers to redefine the behavior of Python’s built-in operators (like + , - , * , < , > ) for objects of a user-defined class. By default, these operators work with built-in data types like integers, floats, and strings. With operator overloading, you can give a special meaning to these operators for objects of your custom class.
In Python, operator overloading is done by implementing special methods, known as “magic methods” or “dunder methods” (double underscore methods), such as __add__ , __sub__ , __mul__ , etc.
Example: Overloading the + Operator for Vector Addition Let’s create a Vector class representing a 2D vector and overload the + operator to add two vector objects.
“`python class Vector: def __init__(self, x, y): self.x = x self.y = y
def __str__(self): # Provides a readable representation when printing the object return f”Vector({self.x}, {self.y})”
def __add__(self, other): “”” Overloads the ‘+’ operator. This method is called when the ‘+’ operator is used on an object of this class. “”” if isinstance(other, Vector): # Add the x and y components of the two vector objects new_x = self.x + other.x new_y = self.y + other.y return Vector(new_x, new_y) else: # Raise an error if the operation is not supported raise TypeError(“Unsupported operand type for +: ‘Vector’ and ” + str(type(other)))
# Create two vector objects v1 = Vector(2, 4) v2 = Vector(3, 5)
# Add the two vectors using the ‘+’ operator. # This will internally call v1.__add__(v2). v3 = v1 + v2
# Print the result print(f”{v1} + {v2} = {v3}”) # Output: Vector(2, 4) + Vector(3, 5) = Vector(5, 9)
# Attempt to add with an unsupported type try: result = v1 + 10 except TypeError as e: print(e) # Output: Unsupported operand type for +: ‘Vector’ and __add__ method defines what should happen when the + operator is used on two Vector objects. It returns a new Vector object which is the sum of the components of the two.
Q5. Briefly explain any three of the following using suitable example/diagram : (a) Kivy Architecture (b) String Slices (c) Tuples and List (d) Mobile application development platforms
Ans. (a) Kivy Architecture Kivy is an open-source Python library used for creating applications with multi-touch support. The architecture of Kivy is modular and designed in layers, making it flexible and extensible. The main components are outlined below:
- Core: This is the foundation of the architecture. It handles input providers, window providers, and other low-level tasks. It interacts directly with the operating system to receive input (mouse, keyboard, touch) and create a window.
- Graphics: This layer is based on OpenGL ES 2.0 and is responsible for rendering. It provides instructions for drawing widgets on the screen. It manages textures, canvases, and graphical instructions.
- UIX: This layer contains all the user interface elements, such as widgets (buttons, labels, sliders) and layouts (grid, box). These are the building blocks you use to create your application’s GUI. Kivy’s widgets are custom-drawn, which means they look the same across all platforms.
- Modules: These are components that can be plugged into the core system to provide additional functionality, such as spell checking or debugging.
- Input: This layer handles input events from various sources, such as touch, mouse, keyboard, and dispatches them through the widget tree.
Kivy’s architecture is designed for speed and flexibility, allowing developers to create cross-platform applications that run smoothly on different devices.
(b) String Slices In Python, string slicing is a feature for extracting parts or substrings of a string. It is a powerful technique that allows you to access a range of characters in a string.
Syntax: The syntax for slicing is [start:stop:step] :
start: The starting index of the slice. This character is included. If omitted, it defaults to 0.stop: The ending index of the slice. This character is not included. If omitted, it goes to the end of the string.step: Indicates how many characters to skip. The default is 1. A negative value reverses the string.
Example: Let’s consider a string s = "Python Programming" . “`python s = “Python Programming”
# Get characters from index 7 to the end # Output: ‘Programming’ print(s[7:])
# Get characters from the beginning up to (but not including) index 6 # Output: ‘Python’ print(s[:6])
# Get characters from index 1 to index 10 # Output: ‘ython Pro’ print(s[1:11])
# The whole string, skipping every other character # Output: ‘Pto rgamn’ print(s[::2])
# Use a negative step to reverse the string # Output: ‘gnimmargorP nohtyP’ print(s[::-1])
# Using negative indices # Get the last 8 characters # Output: ‘gramming’ print(s[-8:]) “` String slicing does not modify the original string; it always returns a new string.
(c) Tuples and List List and Tuple are both built-in data structures in Python used to store an ordered collection of items. However, the key difference between them is mutability .
Feature |
List |
Tuple |
|---|---|---|
Mutability |
Mutable: Its elements can be added, removed, or changed after the list is created. |
Immutable: Its elements cannot be changed once the tuple is created. |
Syntax |
Defined using square brackets. |
Defined using parentheses. |
Performance |
Slightly slower than tuples because they are dynamic. | Slightly faster and more memory-efficient than lists because they are static. |
Use Cases |
When you need a collection of items that may need to be modified, like items in a shopping cart. | When you have data that you do not want to change, like days of the week or coordinates (x, y). Tuples can also be used as dictionary keys. |
Methods |
Has many methods like, , , . |
Has only a few methods likeand since it cannot be changed. |
Example: “`python # List (Mutable) my_list = [10, 20, 30] my_list[1] = 25 # Valid my_list.append(40) # Valid print(f”List: {my_list}”) # Output: List: [10, 25, 30, 40]
# Tuple (Immutable) my_tuple = (10, 20, 30) # my_tuple[1] = 25 # This will raise a TypeError print(f”Tuple: {my_tuple}”) # Output: Tuple: (10, 20, 30) “`
(d) Mobile application development platforms Mobile application development platforms are the tools and environments used to create software for smartphones, tablets, and other mobile devices. They can be broadly divided into two categories: native and cross-platform.
- Native App Development:
In this approach, the app is built for a specific operating system (like iOS or Android) using its native programming language and tools.
- Android: Provided by Google. Apps are developed using Java or Kotlin in the Android Studio IDE. Native apps offer direct access to the device’s hardware features (like camera, GPS) and provide the best performance and user experience.
- iOS: Provided by Apple. Apps are developed using Swift or Objective-C in the Xcode IDE, which runs only on macOS. Native iOS apps are known for their smooth performance and seamless integration with Apple’s ecosystem.
- Cross-Platform App Development:
This approach allows developers to write a single codebase and run it on multiple platforms (both iOS and Android), saving time and cost.
- Flutter: Google’s UI toolkit. It uses the Dart programming language and is known for its fast performance and expressive UI.
- React Native: Created by Facebook (Meta). It uses JavaScript and React , making it popular among web developers.
- Kivy: An open-source Python library. It is suitable for rapid prototyping and applications that require extensive multi-touch support.
- Xamarin: Owned by Microsoft. It uses C# and the .NET framework to build apps with native UI for iOS, Android, and Windows.
The choice of the right platform depends on the project requirements, budget, target audience, and developer expertise.
Download IGNOU previous Year Question paper download PDFs for BCS-094 to improve your preparation. These ignou solved question paper IGNOU Previous Year Question paper solved PDF in Hindi and English help you understand the exam pattern and score better.
Thanks!
Leave a Reply