The IGNOU MCS-201 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 MCS-201 Solved Question Paper in Hindi
- IGNOU MCS-201 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 MCS-201 Solved Question Paper PDF

This section provides IGNOU MCS-201 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 MCS-201 Previous Year Solved Question Paper in Hindi
Q1. (a) एल्गोरिथम से आपका क्या तात्पर्य है? एल्गोरिथम में मौजूद होने वाले महत्वपूर्ण विशेषताओं का उल्लेख करें। 5 (b) लॉजिकल एरर (Logical error) क्या है? यह रनटाइम एरर (Runtime error) से कैसे भिन्न है? C प्रोग्राम में प्रत्येक के उपयुक्त उदाहरण दीजिए। 5 (c) टाइपकास्ट ऑपरेटर (typecast operator) क्या है? टाइपकास्ट ऑपरेटर के सिंटैक्स पर चर्चा करें। निम्नलिखित कोड का आउटपुट दीजिए: 5 main() { int num = 5; printf (“num = %f “, (float) num/3); } (d) C में break और continue स्टेटमेंट की तुलना करें। प्रत्येक स्टेटमेंट के लिए उपयुक्त उदाहरण कोड दीजिए। 5 (e) C-पाइथन (C-Python) क्या है? पाइथन की C प्रोग्रामिंग भाषा से तुलना करें। 5 (f) पाइथन में टेंसरफ्लो (Tensor flow) और केरस (Keras) लाइब्रेरी पर संक्षिप्त चर्चा करें। पाइथन में दोनों लाइब्रेरी की उपयोगिता बताएं। 5 (g) पाइथन में लिस्ट (List) क्या है? पाइथन में लिस्ट टपल (tuple) से कैसे भिन्न है? पाइथन में उपयुक्त उदाहरण कोड के साथ दोनों का वर्णन करें। 5 (h) पाइथन में लैम्ब्डा फंक्शन (Lambda function) क्या है? पाइथन में लैम्ब्डा फंक्शन एक सामान्य फंक्शन से कैसे भिन्न है? प्रत्येक के लिए एक उदाहरण दीजिए। 5
Ans.
(a) एल्गोरिथम (Algorithm)
एक एल्गोरिथम किसी समस्या को हल करने या किसी कार्य को पूरा करने के लिए अच्छी तरह से परिभाषित, चरण-दर-चरण निर्देशों का एक समूह है। यह एक रेसिपी की तरह है जो बताती है कि एक विशिष्ट आउटपुट उत्पन्न करने के लिए एक विशिष्ट इनपुट के साथ क्या करना है। प्रोग्रामिंग में, एल्गोरिथम कोड लिखने से पहले समस्या के समाधान के तर्क को डिजाइन करने के लिए एक खाका के रूप में कार्य करता है।
एक वैध एल्गोरिथम में निम्नलिखित महत्वपूर्ण विशेषताएं होनी चाहिए:
- इनपुट (Input): एक एल्गोरिथम में शून्य या अधिक अच्छी तरह से परिभाषित इनपुट होने चाहिए। ये वे मान हैं जिन पर एल्गोरिथम काम करता है।
- आउटपुट (Output): एक एल्गोरिथम में एक या एक से अधिक अच्छी तरह से परिभाषित आउटपुट होने चाहिए, जो इनपुट के लिए वांछित परिणाम हैं।
- परिमितता (Finiteness): एक एल्गोरिथम को निर्देशों की एक सीमित संख्या के बाद समाप्त होना चाहिए। यह अनिश्चित काल तक नहीं चलना चाहिए।
- निश्चितता (Definiteness): एल्गोरिथम का प्रत्येक चरण स्पष्ट और असंदिग्ध होना चाहिए। प्रत्येक निर्देश का केवल एक ही अर्थ होना चाहिए।
- प्रभावशीलता (Effectiveness): प्रत्येक निर्देश इतना बुनियादी होना चाहिए कि उसे एक व्यक्ति द्वारा कागज और पेंसिल का उपयोग करके सैद्धांतिक रूप से किया जा सके। यह संभव और करने योग्य होना चाहिए।
(b) लॉजिकल एरर बनाम रनटाइम एरर (Logical Error vs. Runtime Error)
लॉजिकल एरर (तार्किक त्रुटि): एक लॉजिकल एरर प्रोग्राम के तर्क में एक दोष है। प्रोग्राम सफलतापूर्वक संकलित (compile) और चलता है, लेकिन यह गलत या अप्रत्याशित आउटपुट उत्पन्न करता है। इन त्रुटियों का पता लगाना सबसे कठिन होता है क्योंकि कंपाइलर या रनटाइम सिस्टम कोई त्रुटि संदेश नहीं देता है। प्रोग्रामर को कोड की समीक्षा करनी होती है और तर्क में दोष खोजना होता है।
उदाहरण: दो संख्याओं के औसत की गणना करना लेकिन जोड़ के बजाय गुणा करना। // लॉजिकल एरर का उदाहरण int a = 10, b = 20; int average = (a * b) / 2; // गलत तर्क: a+b होना चाहिए printf(“Average: %d”, average); // गलत आउटपुट: 100, जबकि 15 होना चाहिए
रनटाइम एरर (रनटाइम त्रुटि): एक रनटाइम एरर प्रोग्राम के निष्पादन (execution) के दौरान होती है। ये त्रुटियाँ तब होती हैं जब प्रोग्राम कंप्यूटर को एक अवैध ऑपरेशन करने के लिए कहता है, जैसे कि शून्य से भाग देना या अमान्य मेमोरी लोकेशन तक पहुँचना। जब रनटाइम एरर होती है, तो प्रोग्राम आमतौर पर असामान्य रूप से समाप्त हो जाता है या क्रैश हो जाता है।
उदाहरण: शून्य से किसी संख्या को विभाजित करने का प्रयास करना। // रनटाइम एरर का उदाहरण int x = 10; int y = 0; int z = x / y; // रनटाइम एरर: शून्य से भाग देना printf(“Result: %d”, z); // यह लाइन कभी निष्पादित नहीं होगी मुख्य अंतर यह है कि लॉजिकल एरर गलत परिणाम देती हैं जबकि प्रोग्राम चलता रहता है, जबकि रनटाइम एरर प्रोग्राम को बीच में ही रोक देती हैं।
(c) टाइपकास्ट ऑपरेटर (Typecast Operator)
C में टाइपकास्ट ऑपरेटर का उपयोग एक डेटा प्रकार के मान को दूसरे डेटा प्रकार में स्पष्ट रूप से परिवर्तित करने के लिए किया जाता है। इसे एक्सप्लिसिट टाइप कन्वर्जन के रूप में भी जाना जाता है। यह तब उपयोगी होता है जब आप यह सुनिश्चित करना चाहते हैं कि किसी ऑपरेशन में चर का इलाज एक अलग प्रकार के रूप में किया जाए, ताकि डेटा हानि या अप्रत्याशित परिणामों से बचा जा सके।
सिंटैक्स:
(target_type) expression; यहाँ, `target_type` वह डेटा प्रकार है जिसमें आप `expression` के मान को परिवर्तित करना चाहते हैं।
दिए गए कोड का आउटपुट:
main() { int num = 5; printf (“num = %f “, (float) num/3); } विश्लेषण:
- `int num = 5;` एक पूर्णांक चर `num` को 5 मान के साथ घोषित करता है।
- `printf` स्टेटमेंट में, एक्सप्रेशन `(float) num/3` का मूल्यांकन किया जाता है।
- `(float) num` टाइपकास्ट ऑपरेटर का उपयोग करता है। यह पूर्णांक `num` (जो 5 है) को अस्थायी रूप से फ्लोट मान `5.0` में परिवर्तित करता है।
- अब, एक्सप्रेशन `5.0 / 3` बन जाता है। चूँकि एक ऑपरेंड एक फ्लोट है, C फ्लोटिंग-पॉइंट डिवीजन करता है।
- `5.0 / 3` का परिणाम लगभग `1.666667` होता है।
- `%f` प्रारूप विनिर्देशक इस फ्लोट मान को प्रिंट करता है।
अपेक्षित आउटपुट है: num = 1.666667
(d) C में break और continue स्टेटमेंट
`break` और `continue` दोनों ही C में लूप नियंत्रण स्टेटमेंट हैं जो लूप के सामान्य प्रवाह को बदलते हैं।
break स्टेटमेंट: `break` स्टेटमेंट का उपयोग लूप (`for`, `while`, `do-while`) या `switch` स्टेटमेंट को तुरंत समाप्त करने के लिए किया जाता है। जब `break` का सामना होता है, तो नियंत्रण तुरंत लूप या स्विच का अनुसरण करने वाले स्टेटमेंट पर स्थानांतरित हो जाता है।
उदाहरण:
#include
continue स्टेटमेंट: `continue` स्टेटमेंट लूप के वर्तमान पुनरावृत्ति (iteration) को छोड़ देता है और लूप के अगले पुनरावृत्ति के साथ निष्पादन जारी रखता है। `break` के विपरीत, यह लूप को समाप्त नहीं करता है।
उदाहरण:
#include
(e) C-पाइथन और C के साथ तुलना
C-पाइथन (CPython): C-पाइथन पाइथन प्रोग्रामिंग भाषा का मानक, सबसे व्यापक रूप से उपयोग किया जाने वाला और संदर्भ कार्यान्वयन है। इसे C और पाइथन भाषाओं में लिखा गया है। यह “C-पाइथन” कहलाता है क्योंकि यह एक C प्रोग्राम है जो पाइथन कोड की व्याख्या (interpret) करता है। जब आप आधिकारिक python.org वेबसाइट से पाइथन डाउनलोड करते हैं, तो आपको C-पाइथन मिलता है। यह पाइथन कोड को मध्यवर्ती बाइटकोड में संकलित (compile) करता है, जिसे बाद में C-पाइथन वर्चुअल मशीन द्वारा निष्पादित (execute) किया जाता है।
पाइथन बनाम C प्रोग्रामिंग भाषा:
विशेषता
पाइथन
C
टाइपिंग (Typing)
डायनामिक रूप से टाइप किया हुआ (रनटाइम पर प्रकार की जाँच की जाती है)
स्थैतिक रूप से टाइप किया हुआ (संकलन-समय पर प्रकार की जाँच की जाती है)
निष्पादन (Execution)
व्याख्यायित (Interpreted) (बाइटकोड में संकलित और फिर व्याख्यायित)
संकलित (Compiled) (सीधे मशीन कोड में संकलित)
मेमोरी प्रबंधन
स्वचालित (Automatic) (गारबेज कलेक्टर के माध्यम से)
मैनुअल (Manual) (`malloc`, `free` के माध्यम से)
गति (Speed)
C की तुलना में धीमा
बहुत तेज़ क्योंकि यह हार्डवेयर के करीब है
जटिलता (Complexity)
सरल सिंटैक्स, सीखने में आसान, उच्च-स्तरीय भाषा
अधिक जटिल सिंटैक्स, पॉइंटर्स, मध्य-स्तरीय भाषा
अनुप्रयोग (Application)
वेब विकास, डेटा विज्ञान, AI/ML, स्क्रिप्टिंग
सिस्टम प्रोग्रामिंग, ऑपरेटिंग सिस्टम, एम्बेडेड सिस्टम
(f) टेंसरफ्लो (TensorFlow) और केरस (Keras) लाइब्रेरी
टेंसरफ्लो (TensorFlow): टेंसरफ्लो Google द्वारा विकसित एक शक्तिशाली, ओपन-सोर्स सॉफ्टवेयर लाइब्रेरी है। यह मुख्य रूप से मशीन लर्निंग और आर्टिफिशियल इंटेलिजेंस , विशेष रूप से बड़े पैमाने पर संख्यात्मक संगणना और डीप लर्निंग मॉडल के लिए उपयोग किया जाता है। यह डेटा फ्लो ग्राफ के रूप में संगणना का प्रतिनिधित्व करता है, जहाँ नोड्स गणितीय संचालन का प्रतिनिधित्व करते हैं, और ग्राफ के किनारे (एज) उनके बीच संचारित बहु-आयामी डेटा एरे (टेंसर) का प्रतिनिधित्व करते हैं।
उपयोगिता: टेंसरफ्लो अत्यधिक स्केलेबल है और इसे CPU, GPU और TPU सहित विभिन्न प्लेटफार्मों पर चलाया जा सकता है। यह उत्पादन-स्तर (production-grade) के मॉडल बनाने, जटिल तंत्रिका नेटवर्क (neural networks) को प्रशिक्षित करने और बड़े डेटासेट पर तैनात करने के लिए आदर्श है।
केरस (Keras): केरस एक उच्च-स्तरीय तंत्रिका नेटवर्क API है जो पाइथन में लिखा गया है। यह टेंसरफ्लो, Theano, या Microsoft CNTK के शीर्ष पर चलने में सक्षम है। केरस को उपयोगकर्ता-मित्रता, मॉड्यूलरिटी और विस्तारशीलता पर ध्यान केंद्रित करके तेजी से प्रयोग को सक्षम करने के लिए डिज़ाइन किया गया था।
उपयोगिता: केरस का मुख्य लक्ष्य डीप लर्निंग मॉडल के साथ काम करना यथासंभव सरल बनाना है। यह तेजी से प्रोटोटाइपिंग के लिए उत्कृष्ट है, जिससे उपयोगकर्ता न्यूनतम कोड के साथ मॉडल बना और प्रशिक्षित कर सकते हैं। यह सीखने और उपयोग करने में आसान है, जो इसे डीप लर्निंग के शुरुआती लोगों के लिए एक लोकप्रिय विकल्प बनाता है। TensorFlow 2.x के बाद से, Keras TensorFlow का आधिकारिक उच्च-स्तरीय API बन गया है।
(g) पाइथन में लिस्ट (List) और टपल (Tuple)
लिस्ट (List): पाइथन में एक लिस्ट विभिन्न प्रकार के आइटमों का एक क्रमबद्ध और परिवर्तनशील (mutable) संग्रह है। “परिवर्तनशील” का अर्थ है कि आप इसे बनाने के बाद इसके तत्वों को बदल सकते हैं, जोड़ सकते हैं या हटा सकते हैं। सूचियों को वर्ग कोष्ठक `[]` में संलग्न किया जाता है।
उदाहरण:
# एक लिस्ट बनाना my_list = [1, “hello”, 3.14] print(my_list) # आउटपुट: [1, ‘hello’, 3.14] # एक तत्व को संशोधित करना my_list[1] = “world” print(my_list) # आउटपुट: [1, ‘world’, 3.14] # एक तत्व जोड़ना my_list.append(True) print(my_list) # आउटपुट: [1, ‘world’, 3.14, True]
टपल (Tuple): एक टपल भी विभिन्न प्रकार के आइटमों का एक क्रमबद्ध संग्रह है, लेकिन यह अपरिवर्तनीय (immutable) है। “अपरिवर्तनीय” का अर्थ है कि एक बार टपल बन जाने के बाद, आप इसके तत्वों को बदल, जोड़ या हटा नहीं सकते। टपल्स को कोष्ठक `()` में संलग्न किया जाता है।
उदाहरण:
# एक टपल बनाना my_tuple = (1, “hello”, 3.14) print(my_tuple) # आउटपुट: (1, ‘hello’, 3.14) # एक तत्व को संशोधित करने का प्रयास (यह एक त्रुटि देगा) # my_tuple[1] = “world” # यह एक TypeError का कारण बनेगा
लिस्ट बनाम टपल: मुख्य अंतर म्यूटेबिलिटी (mutability) है। लिस्ट परिवर्तनशील हैं और टपल अपरिवर्तनीय हैं। इस अंतर के कारण, टपल लिस्ट की तुलना में थोड़ी तेज और अधिक मेमोरी-कुशल होती हैं। टपल्स का उपयोग तब किया जाता है जब आप यह सुनिश्चित करना चाहते हैं कि डेटा संग्रह बनाने के बाद नहीं बदलता है, जैसे निर्देशांक के एक सेट या डेटाबेस रिकॉर्ड के लिए। लिस्ट का उपयोग तब किया जाता है जब आपको एक ऐसे संग्रह की आवश्यकता होती है जिसे आप अपने प्रोग्राम के दौरान संशोधित करने की उम्मीद करते हैं।
(h) लैम्ब्डा फंक्शन (Lambda Function) बनाम सामान्य फंक्शन
लैम्ब्डा फंक्शन (Lambda Function): पाइथन में एक लैम्ब्डा फंक्शन एक छोटा, अनाम (anonymous) फंक्शन होता है जिसे `lambda` कीवर्ड का उपयोग करके परिभाषित किया जाता है। लैम्ब्डा फंक्शन में कई तर्क हो सकते हैं, लेकिन केवल एक ही अभिव्यक्ति हो सकती है। अभिव्यक्ति का मूल्यांकन किया जाता है और परिणाम लौटाया जाता है। इन्हें आमतौर पर उन स्थितियों में उपयोग किया जाता है जहाँ एक छोटे, एक-बार उपयोग होने वाले फंक्शन की आवश्यकता होती है।
उदाहरण:
# एक लैम्ब्डा फंक्शन जो दो संख्याओं को जोड़ता है add = lambda a, b: a + b print(add(5, 3)) # आउटपुट: 8
सामान्य फंक्शन (Regular Function): एक सामान्य फंक्शन को `def` कीवर्ड का उपयोग करके परिभाषित किया जाता है और इसका एक नाम होता है। इसमें कई कथन, अभिव्यक्तियाँ, और एक `return` स्टेटमेंट हो सकता है जो स्पष्ट रूप से एक मान लौटाता है (या यदि कोई `return` नहीं है तो `None` लौटाता है)। सामान्य फंक्शन अधिक जटिल तर्क, पुन: प्रयोज्य कोड ब्लॉक और दस्तावेज़ीकरण (docstrings के माध्यम से) के लिए डिज़ाइन किए गए हैं।
उदाहरण:
# एक सामान्य फंक्शन जो दो संख्याओं को जोड़ता है def add(a, b): return a + b print(add(5, 3)) # आउटपुट: 8
अंतर:
- नाम (Name): लैम्ब्डा फंक्शन अनाम होते हैं, जबकि सामान्य फंक्शन का नाम `def` का उपयोग करके दिया जाता है।
- सिंटैक्स (Syntax): लैम्ब्डा फंक्शन एक-पंक्ति वाली अभिव्यक्तियाँ हैं। सामान्य फंक्शन एक कोड का ब्लॉक होते हैं।
- जटिलता (Complexity): लैम्ब्डा फंक्शन केवल एक अभिव्यक्ति तक सीमित हैं। सामान्य फंक्शन में कई कथन और जटिल तर्क हो सकते हैं।
- उपयोग (Use Case): लैम्ब्डा का उपयोग अक्सर उन तर्कों के रूप में किया जाता है जहाँ फंक्शन ऑब्जेक्ट्स की अपेक्षा की जाती है (जैसे `map()`, `filter()`), जबकि सामान्य फंक्शन का उपयोग किसी भी पुन: प्रयोज्य या जटिल कार्य के लिए किया जाता है।
Q2. (a) उपयोगकर्ता द्वारा दर्ज किए गए पांच अंकों की पूर्णांक संख्या में अंकों की संख्या गिनने और सभी अंकों का योग ज्ञात करने के लिए C में एक प्रोग्राम लिखें। प्रोग्राम को उपयुक्त टिप्पणियों के साथ समर्थन दें। 10 (b) (i) C (फ़ाइल हैंडलिंग के लिए) में fread और fwrite कमांड पर एक संक्षिप्त नोट लिखें। एक प्रोग्राम खंड की मदद से सचित्र करें। 5 (ii) किसी संख्या की घात (x^n) की गणना करने के लिए एक पुनरावर्ती C प्रोग्राम लिखें, बिना इन-बिल्ट फ़ंक्शन का उपयोग किए। 5
Ans.
(a) C प्रोग्राम: अंकों की संख्या गिनना और योग ज्ञात करना
यह प्रोग्राम उपयोगकर्ता से पांच अंकों की एक पूर्णांक संख्या इनपुट के रूप में लेता है। फिर यह एक `while` लूप का उपयोग करके संख्या के प्रत्येक अंक को अलग करता है, अंकों की गिनती करता है, और उनका योग करता है।
#include
नमूना आउटपुट:
Enter a five-digit integer number: 12345 Original Number: 12345 Number of digits: 5 Sum of all digits: 15
(b) (i) C में `fread()` और `fwrite()`
`fread()` और `fwrite()` C में बाइनरी फ़ाइल संचालन के लिए उपयोग किए जाने वाले फ़ंक्शन हैं। वे टेक्स्ट फ़ाइलों के लिए `fscanf()` और `fprintf()` के समतुल्य हैं, लेकिन वे डेटा को बाइनरी प्रारूप में पढ़ते और लिखते हैं, जो आमतौर पर स्ट्रक्चर (structs) या एरे जैसे डेटा के ब्लॉक के लिए अधिक कुशल होता है।
`fwrite()`: यह फ़ंक्शन मेमोरी से फ़ाइल स्ट्रीम में डेटा के ब्लॉक लिखता है।
सिंटैक्स: `size_t fwrite(const void ptr, size_t size, size_t count, FILE stream);`
- `ptr`: लिखे जाने वाले डेटा के एरे के पहले तत्व का पॉइंटर।
- `size`: प्रत्येक तत्व का बाइट्स में आकार।
- `count`: लिखे जाने वाले तत्वों की संख्या।
- `stream`: `FILE` ऑब्जेक्ट का पॉइंटर जो आउटपुट स्ट्रीम को निर्दिष्ट करता है।
`fread()`: यह फ़ंक्शन फ़ाइल स्ट्रीम से मेमोरी में डेटा के ब्लॉक पढ़ता है।
सिंटैक्स: `size_t fread(void ptr, size_t size, size_t count, FILE stream);`
- `ptr`: पढ़े गए डेटा को स्टोर करने के लिए मेमोरी ब्लॉक का पॉइंटर।
- `size`: प्रत्येक तत्व का बाइट्स में आकार।
- `count`: पढ़े जाने वाले तत्वों की संख्या।
- `stream`: `FILE` ऑब्जेक्ट का पॉइंटर जो इनपुट स्ट्रीम को निर्दिष्ट करता है।
प्रोग्राम खंड का उदाहरण:
#include
// संरचना को फ़ाइल में लिखें printf(“Writing student record to file…\n”); fwrite(&s_write, sizeof(struct Student), 1, file_ptr); fclose(file_ptr); // बाइनरी रीड मोड में फ़ाइल खोलें file_ptr = fopen(“students.dat”, “rb”); if (file_ptr == NULL) { printf(“Error opening file for reading.\n”); return; } // फ़ाइल से संरचना पढ़ें printf(“Reading student record from file…\n”); fread(&s_read, sizeof(struct Student), 1, file_ptr); // पढ़े गए डेटा को प्रिंट करें printf(“Roll No: %d, Name: %s\n”, s_read.roll_no, s_read.name); fclose(file_ptr); }
(b) (ii) C में पुनरावर्ती (Recursive) पावर फंक्शन
यह प्रोग्राम `power(x, n)` की गणना के लिए एक पुनरावर्ती दृष्टिकोण का उपयोग करता है, जो x की घात n है। पुनरावर्तन `x^n = x * x^(n-1)` के गणितीय सिद्धांत पर आधारित है। बेस केस तब होता है जब घात `n` 0 हो, उस स्थिति में परिणाम 1 होता है। #include
पुनरावर्तन कैसे काम करता है (उदाहरण: `power(2, 3)`):
- `power(2, 3)` कॉल किया जाता है। यह `2 * power(2, 2)` लौटाता है।
- `power(2, 2)` कॉल किया जाता है। यह `2 * power(2, 1)` लौटाता है।
- `power(2, 1)` कॉल किया जाता है। यह `2 * power(2, 0)` लौटाता है।
- `power(2, 0)` कॉल किया जाता है। यह बेस केस से मेल खाता है और 1 लौटाता है।
- परिणाम वापस अनियंत्रित होते हैं: `2 1 = 2`, फिर `2 2 = 4`, फिर `2 * 4 = 8`। अंतिम परिणाम 8 है।
Q3. (a) (i) दो स्ट्रिंग्स इनपुट करने और उन्हें जोड़ने के लिए एक C प्रोग्राम लिखें। बिल्ट-इन फ़ंक्शन का उपयोग किए बिना परिणामी स्ट्रिंग की लंबाई की गणना करें। 5 (ii) C में यूनियन (union) क्या है? यह स्ट्रक्चर (structure) से कैसे भिन्न है? प्रत्येक के लिए उपयुक्त उदाहरण दीजिए। 5 (b) स्ट्रक्चर का उपयोग करते हुए, पांच छात्रों का डेटा स्वीकार करने के लिए एक C प्रोग्राम लिखें जिसमें उनका नामांकन संख्या, नाम, विषय, अंक शामिल हों। प्रत्येक छात्र पांच विषयों में अंक प्राप्त करता है। प्रत्येक छात्र के कुल अंकों की गणना करें और पता लगाएं कि किसने सबसे अधिक अंक प्राप्त किए हैं। 10
Ans.
(a) (i) C प्रोग्राम: स्ट्रिंग जोड़ना और लंबाई की गणना
यह प्रोग्राम `strcat()` या `strlen()` जैसे किसी भी अंतर्निहित स्ट्रिंग फ़ंक्शन का उपयोग किए बिना दो स्ट्रिंग्स को जोड़ता है और परिणामी स्ट्रिंग की लंबाई की गणना करता है।
#include
नमूना आउटपुट:
Enter the first string: Hello Enter the second string: World Concatenated String: HelloWorld Length of the resultant string: 10
(a) (ii) यूनियन (Union) बनाम स्ट्रक्चर (Structure)
C में, `structure` और `union` दोनों उपयोगकर्ता-परिभाषित डेटा प्रकार हैं जो विभिन्न प्रकार के सदस्यों को एक ही इकाई के तहत समूहित करते हैं। हालांकि, वे मेमोरी आवंटन में मौलिक रूप से भिन्न हैं।
स्ट्रक्चर (Structure): एक स्ट्रक्चर में, प्रत्येक सदस्य को अपना अलग मेमोरी स्थान आवंटित किया जाता है। स्ट्रक्चर का कुल आकार उसके सभी सदस्यों के आकार के योग (और संरेखण के लिए किसी भी पैडिंग) के बराबर होता है।
उदाहरण:
struct ExampleStruct { int i; // 4 बाइट्स (मान लें) char c; // 1 बाइट float f; // 4 बाइट्स }; // कुल आकार लगभग 4 + 1 + 4 = 9 बाइट्स + पैडिंग स्ट्रक्चर के सभी सदस्यों को एक ही समय में मान रख सकते हैं।
यूनियन (Union): एक यूनियन में, सभी सदस्य एक ही मेमोरी स्थान साझा करते हैं। यूनियन का कुल आकार उसके सबसे बड़े सदस्य के आकार के बराबर होता है। इसका मतलब है कि एक समय में केवल एक सदस्य ही एक सार्थक मान रख सकता है। यदि आप एक सदस्य को मान निर्दिष्ट करते हैं, तो यह अन्य सदस्यों के मानों को अधिलेखित कर सकता है।
उदाहरण:
union ExampleUnion { int i; // 4 बाइट्स (मान लें) char c; // 1 बाइट float f; // 4 बाइट्स }; // कुल आकार सबसे बड़े सदस्य के आकार के बराबर = 4 बाइट्स
मुख्य अंतर:
विशेषता
स्ट्रक्चर (Structure)
यूनियन (Union)
कीवर्ड
`struct`
`union`
मेमोरी
प्रत्येक सदस्य को अलग मेमोरी मिलती है।
सभी सदस्य एक ही मेमोरी स्थान साझा करते हैं।
आकार
सभी सदस्यों के आकार का योग (पैडिंग के साथ)।
सबसे बड़े सदस्य का आकार।
मान
एक साथ कई सदस्यों के मान संग्रहीत कर सकता है।
एक समय में केवल एक सदस्य का मान संग्रहीत कर सकता है।
उपयोग
संबंधित डेटा को एक साथ समूहित करने के लिए (जैसे छात्र रिकॉर्ड)।
मेमोरी बचाने के लिए जब एक ही चर को विभिन्न प्रकार के मान रखने की आवश्यकता होती है।
(b) C प्रोग्राम: छात्र डेटा और उच्चतम अंक
यह प्रोग्राम पांच छात्रों के लिए रिकॉर्ड प्रबंधित करने के लिए `struct` का उपयोग करता है। यह प्रत्येक छात्र के लिए नामांकन संख्या, नाम और पांच विषयों में अंक इनपुट के रूप में लेता है। फिर यह प्रत्येक छात्र के लिए कुल अंकों की गणना करता है और अंत में उस छात्र का पता लगाता है और प्रदर्शित करता है जिसने उच्चतम कुल अंक प्राप्त किए हैं। #include
printf(” Enter Enrollment Number: “); scanf(“%lld”, &students[i].enrollment_no); printf(” Enter Name: “); scanf(“%s”, students[i].name); students[i].total_marks = 0; // कुल अंक 0 से शुरू करें printf(” Enter marks for %d subjects:\n”, NUM_SUBJECTS); for (j = 0; j < NUM_SUBJECTS; j++) { printf(” Subject %d: “, j + 1); scanf(“%d”, &students[i].marks[j]); // कुल अंकों की गणना करें students[i].total_marks += students[i].marks[j]; } } // — उच्चतम अंक पाने वाले को खोजना — int highest_marks = -1; // एक बहुत छोटे मान से शुरू करें int top_student_index = -1; for (i = 0; i < NUM_STUDENTS; i++) { if (students[i].total_marks > highest_marks) { highest_marks = students[i].total_marks; top_student_index = i; } } // — परिणाम प्रदर्शित करना — printf(“\n— Student Details and Totals —\n”); for (i = 0; i < NUM_STUDENTS; i++) { printf(“Student: %s, Total Marks: %d\n”, students[i].name, students[i].total_marks); }
printf(“\n— Student with Highest Marks —\n”); if (top_student_index != -1) { printf(“Enrollment No: %lld\n”, students[top_student_index].enrollment_no); printf(“Name: %s\n”, students[top_student_index].name); printf(“Highest Total Marks: %d\n”, students[top_student_index].total_marks); } else { printf(“No student data found.\n”); } return 0; }
Q4. (a) निम्नलिखित करने के लिए एक पाइथन कोड और सहायक एल्गोरिथम लिखें: (i) एक फ़ाइल में प्रत्येक शब्द की आवृत्ति प्रदर्शित करें। 5 (ii) एक फ़ाइल से अंतिम n पंक्तियाँ प्रदर्शित करें, जहाँ n उपयोगकर्ता द्वारा दिया गया है। 5 (b) स्ट्रिंग्स वाली दो सूचियों को संयोजित करने के लिए एक पाइथन कोड लिखें। इस प्रोग्राम का उपयोग संयुक्त सूची से अधिकतम स्वरों वाली स्ट्रिंग को प्रिंट करने के लिए करें। 10
Ans.
(a) पाइथन फ़ाइल संचालन
(i) एक फ़ाइल में प्रत्येक शब्द की आवृत्ति प्रदर्शित करें
एल्गोरिथम:
- प्रारंभ: प्रोग्राम शुरू करें।
- फ़ाइल का नाम: एक फ़ाइल का नाम निर्दिष्ट करें जिसे पढ़ना है (उदा., ‘sample.txt’)।
- फ़ाइल पढ़ें: फ़ाइल को पढ़ें और उसकी पूरी सामग्री को एक स्ट्रिंग में संग्रहीत करें।
- डेटा साफ़ करें: स्ट्रिंग को लोअरकेस में बदलें ताकि केस-असंवेदनशील गिनती हो सके। विराम चिह्नों को यदि आवश्यक हो तो हटा दें।
- शब्दों में विभाजित करें: स्ट्रिंग को अलग-अलग शब्दों की सूची में विभाजित करें।
- आवृत्ति शब्दकोश: एक खाली शब्दकोश (dictionary) प्रारंभ करें जिसे `word_freq` कहा जाता है।
- शब्दों पर पुनरावृति करें: शब्दों की सूची के प्रत्येक शब्द के लिए:
- यदि शब्द `word_freq` शब्दकोश में पहले से मौजूद है, तो उसकी गिनती 1 से बढ़ाएँ।
- अन्यथा, शब्द को 1 की गिनती के साथ शब्दकोश में जोड़ें।
- परिणाम प्रदर्शित करें: शब्द आवृत्ति शब्दकोश को प्रिंट करें।
- समाप्त: प्रोग्राम समाप्त करें।
पाइथन कोड:
# sample.txt फ़ाइल बनाएं और उसमें कुछ टेक्स्ट डालें # उदाहरण: “Hello world this is a test. Hello again.” try: with open(‘sample.txt’, ‘w’) as f: f.write(“Hello world this is a test. Hello again.”) except IOError: print(“Could not create sample file.”) def display_word_frequency(filename): “””एक फ़ाइल में प्रत्येक शब्द की आवृत्ति को पढ़ता और प्रदर्शित करता है।””” try: with open(filename, ‘r’) as file: text = file.read().lower() # पढ़ें और लोअरकेस में बदलें except FileNotFoundError: print(f”Error: The file ‘{filename}’ was not found.”) return # सरल विभाजन के लिए विराम चिह्नों को स्पेस से बदलें text = text.replace(‘.’, ‘ ‘).replace(‘,’, ‘ ‘)
words = text.split() # शब्दों की सूची में विभाजित करें word_freq = {} # आवृत्तियों को संग्रहीत करने के लिए शब्दकोश for word in words: if word in word_freq: word_freq[word] += 1 else: word_freq[word] = 1
print(f”Word frequencies in ‘{filename}’:”) for word, count in word_freq.items(): print(f”‘{word}’: {count}”) # फ़ंक्शन को कॉल करें display_word_frequency(‘sample.txt’)
(ii) एक फ़ाइल से अंतिम n पंक्तियाँ प्रदर्शित करें
एल्गोरिथम:
- प्रारंभ: प्रोग्राम शुरू करें।
- उपयोगकर्ता से इनपुट: उपयोगकर्ता से `n` (प्रदर्शित की जाने वाली पंक्तियों की संख्या) का मान पूछें।
- फ़ाइल का नाम: एक फ़ाइल का नाम निर्दिष्ट करें जिसे पढ़ना है (उदा., ‘long_sample.txt’)।
- फ़ाइल पढ़ें: फ़ाइल खोलें और उसकी सभी पंक्तियों को एक सूची (list) में पढ़ें।
- अंतिम n पंक्तियाँ निकालें: सूची स्लाइसिंग का उपयोग करके पंक्तियों की सूची से अंतिम `n` तत्वों का चयन करें (`lines[-n:]`)।
- पंक्तियाँ प्रदर्शित करें: निकाली गई अंतिम `n` पंक्तियों के प्रत्येक पंक्ति पर पुनरावृति करें और उसे प्रिंट करें।
- समाप्त: प्रोग्राम समाप्त करें।
पाइथन कोड:
# long_sample.txt फ़ाइल बनाएं और उसमें कई पंक्तियाँ डालें try: with open(‘long_sample.txt’, ‘w’) as f: for i in range(1, 16): f.write(f”This is line number {i}.\\n”) except IOError: print(“Could not create sample file.”) def display_last_n_lines(filename): “””एक फ़ाइल की अंतिम n पंक्तियों को प्रदर्शित करता है, जहाँ n उपयोगकर्ता द्वारा दिया गया है।””” try: n = int(input(“Enter the number of last lines to display (n): “)) except ValueError: print(“Invalid input. Please enter an integer.”) return try: with open(filename, ‘r’) as file: lines = file.readlines() # सभी पंक्तियों को एक सूची में पढ़ें except FileNotFoundError: print(f”Error: The file ‘{filename}’ was not found.”) return
print(f”\\nLast {n} lines of ‘{filename}’:”) # सूची स्लाइसिंग का उपयोग करके अंतिम n पंक्तियाँ प्राप्त करें और उन्हें प्रिंट करें last_lines = lines[-n:] for line in last_lines: print(line.strip()) # अतिरिक्त नई लाइनों को हटाने के लिए .strip() का उपयोग करें # फ़ंक्शन को कॉल करें display_last_n_lines(‘long_sample.txt’)
(b) पाइथन कोड: सूचियाँ संयोजित करें और अधिकतम स्वरों वाली स्ट्रिंग खोजें
यह प्रोग्राम स्ट्रिंग्स वाली दो सूचियों को लेता है, उन्हें एक में संयोजित करता है, और फिर संयुक्त सूची के माध्यम से पुनरावृति करता है ताकि उस स्ट्रिंग को खोजा जा सके जिसमें सबसे अधिक स्वर (vowels) हों। def count_vowels(s): “””एक दी गई स्ट्रिंग में स्वरों की संख्या की गणना करता है।””” vowels = “aeiou” count = 0 # स्ट्रिंग के प्रत्येक अक्षर के लिए, जांचें कि क्या यह स्वर है for char in s.lower(): # केस-असंवेदनशील बनाने के लिए लोअरकेस में बदलें if char in vowels: count += 1 return count # स्ट्रिंग्स वाली दो सूचियाँ list1 = [“python”, “programming”, “is”, “fun”] list2 = [“data”, “science”, “and”, “machine”, “learning”, “education”] # ‘+’ ऑपरेटर का उपयोग करके दो सूचियों को संयोजित करें combined_list = list1 + list2 print(“Combined List:”, combined_list) # अधिकतम स्वरों वाली स्ट्रिंग को खोजने के लिए वेरिएबल्स प्रारंभ करें max_vowel_count = -1 string_with_max_vowels = “” # संयुक्त सूची के प्रत्येक स्ट्रिंग पर पुनरावृति करें for s in combined_list: # वर्तमान स्ट्रिंग के लिए स्वरों की संख्या की गणना करें current_vowel_count = count_vowels(s)
# यदि वर्तमान गणना अधिकतम से अधिक है तो अधिकतम को अपडेट करें if current_vowel_count > max_vowel_count: max_vowel_count = current_vowel_count string_with_max_vowels = s # परिणाम प्रिंट करें if string_with_max_vowels: print(f”\\nThe string with the maximum number of vowels is: ‘{string_with_max_vowels}'”) print(f”It has {max_vowel_count} vowels.”) else: print(“No strings found in the list.”)
आउटपुट:
Combined List: [‘python’, ‘programming’, ‘is’, ‘fun’, ‘data’, ‘science’, ‘and’, ‘machine’, ‘learning’, ‘education’] The string with the maximum number of vowels is: ‘education’ It has 5 vowels.
Q5. (a) पाइथन में क्लास (class) क्या है? एक क्लास ‘circle’ बनाएं जो एक वृत्त का प्रतिनिधित्व करती है जिसमें ‘area’ और ‘perimeter’ नामक विधियाँ हों? परिधि विधि परिधि लौटाती है और क्षेत्रफल विधि वृत्त का क्षेत्रफल लौटाती है। दी गई क्लास ‘Circle’ में विधियों के परिणाम प्रदर्शित करें। 10 (b) निम्नलिखित में से किन्हीं दो के बीच अंतर करें: 5+5 (i) सब-रूटीन (Sub-routine) बनाम को-रूटीन (Co-routine) (ii) मेथड ओवरलोडिंग (Method Overloading) बनाम मेथड ओवरराइडिंग (Method Overriding) (iii) मॉड्यूल (Module) बनाम स्क्रिप्ट (Script)
Ans.
(a) पाइथन क्लास ‘Circle’
पाइथन में क्लास (Class): पाइथन में एक क्लास ऑब्जेक्ट बनाने के लिए एक ब्लूप्रिंट है। यह एक उपयोगकर्ता-परिभाषित डेटा प्रकार है जो डेटा सदस्यों (विशेषताओं या वेरिएबल्स) और विधियों (सदस्य फ़ंक्शंस या व्यवहार) को एक साथ बांधता है। एक क्लास आपको एक नए प्रकार की वस्तु बनाने की अनुमति देती है, जिसमें नए प्रकार के गुण और संचालन हो सकते हैं। क्लास का एक उदाहरण (instance) एक ऑब्जेक्ट होता है, और आप एक ही क्लास से कई ऑब्जेक्ट बना सकते हैं। क्लास ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग (OOP) का एक मौलिक हिस्सा हैं।
‘Circle’ क्लास का कार्यान्वयन:
निम्नलिखित कोड एक `Circle` क्लास को परिभाषित करता है। `__init__` कंस्ट्रक्टर विधि त्रिज्या (radius) के साथ एक वृत्त ऑब्जेक्ट को प्रारंभ करती है। `area` और `perimeter` विधियाँ क्रमशः वृत्त के क्षेत्रफल और परिधि की गणना करती हैं और लौटाती हैं। अंत में, हम क्लास का एक उदाहरण बनाते हैं और परिणाम प्रदर्शित करने के लिए इसकी विधियों को कॉल करते हैं।
import math # पाई (pi) स्थिरांक का उपयोग करने के लिए class Circle: “””एक वृत्त का प्रतिनिधित्व करने वाली एक क्लास।””” def __init__(self, radius): “”” त्रिज्या के साथ एक Circle ऑब्जेक्ट को प्रारंभ करता है। :param radius: वृत्त की त्रिज्या (एक सकारात्मक संख्या होनी चाहिए)। “”” if radius <= 0: raise ValueError(“Radius must be a positive number.”) self.radius = radius def area(self): “”” वृत्त के क्षेत्रफल की गणना करता है और लौटाता है। सूत्र: π * r^2 “”” return math.pi (self.radius * 2) def perimeter(self): “”” वृत्त की परिधि (परिधि) की गणना करता है और लौटाता है। सूत्र: 2 π r “”” return 2 math.pi self.radius # — मुख्य प्रोग्राम — # 7 की त्रिज्या के साथ “Circle” क्लास का एक ऑब्जेक्ट बनाएं try: my_circle = Circle(7) # क्षेत्रफल की गणना करें और उसे प्रिंट करें circle_area = my_circle.area() print(f”The radius of the circle is: {my_circle.radius}”) print(f”The area of the circle is: {circle_area:.2f}”) # 2 दशमलव स्थानों तक स्वरूपित # परिधि की गणना करें और उसे प्रिंट करें circle_perimeter = my_circle.perimeter() print(f”The perimeter (circumference) of the circle is: {circle_perimeter:.2f}”) except ValueError as e: print(f”Error creating circle: {e}”)
आउटपुट:
The radius of the circle is: 7 The area of the circle is: 153.94 The perimeter (circumference) of the circle is: 43.98
(b) अंतर (Differentiations)
(i) सब-रूटीन (Sub-routine) बनाम को-रूटीन (Co-routine)
पहलू
सब-रूटीन (जैसे, एक सामान्य फ़ंक्शन)
को-रूटीन
प्रवाह नियंत्रण
कॉल करने वाले से शुरू होता है, पूरा होने तक चलता है, और कॉल करने वाले को नियंत्रण लौटाता है।
निष्पादन को रोक सकता है (yield), नियंत्रण लौटा सकता है, और बाद में उसी बिंदु से फिर से शुरू किया जा सकता है।
राज्य (State)
कॉल के बीच स्थानीय स्थिति को बनाए नहीं रखता है (जब तक कि वैश्विक/स्थैतिक वेरिएबल्स का उपयोग न किया जाए)।
रोकने और फिर से शुरू करने के बीच अपनी स्थानीय स्थिति (वेरिएबल्स) को बनाए रखता है।
निष्पादन मॉडल
कॉल-रिटर्न मॉडल। पूरी तरह से रन-टू-कम्प्लीशन।
सहकारी मल्टीटास्किंग। को-रूटीन स्वेच्छा से नियंत्रण छोड़ देते हैं।
पाइथन में उदाहरण
`def` के साथ परिभाषित कोई भी सामान्य फ़ंक्शन।
जेनरेटर (`yield` का उपयोग करके), और `async`/`await` सिंटैक्स के साथ परिभाषित फ़ंक्शन।
(ii) मेथड ओवरलोडिंग (Method Overloading) बनाम मेथड ओवरराइडिंग (Method Overriding)
पहलू
मेथड ओवरलोडिंग
मेथड ओवरराइडिंग
परिभाषा
एक ही क्लास में एक ही नाम के कई मेथड बनाना, लेकिन अलग-अलग पैरामीटर (संख्या, प्रकार, या क्रम) के साथ।
एक सबक्लास में एक मेथड के लिए एक विशिष्ट कार्यान्वयन प्रदान करना जो पहले से ही उसकी सुपरक्लास में परिभाषित है।
स्थान
एक ही क्लास के भीतर होता है।
दो क्लासों के बीच होता है जिनका वंशानुक्रम (inheritance) संबंध होता है (सुपरक्लास और सबक्लास)।
मेथड सिग्नेचर
मेथड सिग्नेचर (नाम और पैरामीटर) अलग-अलग होना चाहिए।
मेथड सिग्नेचर (नाम और पैरामीटर) सबक्लास और सुपरक्लास दोनों में समान होना चाहिए।
पाइथन समर्थन
पाइथन पारंपरिक अर्थों में मेथड ओवरलोडिंग का समर्थन नहीं करता है। इसे डिफ़ॉल्ट तर्कों या ` args`/` *kwargs` का उपयोग करके अनुकरण किया जा सकता है।
पाइथन मेथड ओवरराइडिंग का पूरी तरह से समर्थन करता है। यह बहुरूपता (polymorphism) का एक प्रमुख हिस्सा है।
(iii) मॉड्यूल (Module) बनाम स्क्रिप्ट (Script)
पहलू
मॉड्यूल (Module)
स्क्रिप्ट (Script)
उद्देश्य
अन्य प्रोग्रामों या स्क्रिप्टों में आयात (import) और उपयोग किए जाने के लिए। यह पुन: प्रयोज्य कार्यक्षमता (फ़ंक्शंस, क्लास) प्रदान करता है।
सीधे निष्पादित (execute) किए जाने के लिए। यह एक प्रोग्राम का प्रवेश बिंदु है जो एक विशिष्ट कार्य करता है।
उपयोग
`import module_name` का उपयोग करके इसे अन्य फ़ाइलों में लाया जाता है।
कमांड लाइन से चलाया जाता है (उदा., `python my_script.py`)।
संरचना
इसमें मुख्य रूप से परिभाषाएँ होती हैं: फ़ंक्शंस, क्लास, और वेरिएबल्स। इसमें सीधे निष्पादन योग्य कोड कम होता है।
इसमें कथनों का एक क्रम होता है जो शुरू से अंत तक निष्पादित होते हैं, अक्सर प्रोग्राम के मुख्य तर्क को चलाने के लिए फ़ंक्शंस या क्लास को कॉल करते हैं।
`__name__` वेरिएबल
जब आयात किया जाता है, तो इसका `__name__` वेरिएबल मॉड्यूल के फ़ाइल नाम पर सेट होता है (बिना `.py` के)।
जब सीधे निष्पादित किया जाता है, तो इसका `__name__` वेरिएबल स्ट्रिंग `”__main__”` पर सेट होता है।
ब्लॉक `if __name__ == “__main__”:` एक पाइथन फ़ाइल को दोहरी भूमिका निभाने की अनुमति देता है: यदि इसे सीधे चलाया जाता है तो यह एक स्क्रिप्ट के रूप में कार्य कर सकता है, और यदि इसे आयात किया जाता है तो यह एक मॉड्यूल के रूप में कार्य कर सकता है।
IGNOU MCS-201 Previous Year Solved Question Paper in English
Q1. (a) What do you mean by an algorithm ? Mention important features that must be present in the algorithm. 5 (b) What is a Logical error ? How is it different from Runtime error ? Give suitable examples of each in a C program. 5 (c) What is a typecast operator ? Discuss the syntax of typecast operator. Give output of the following code : 5 main() { int num = 5; printf (“num = %f “, (float) num/3); } (d) Compare break and continue statements in C. Give suitable example code for each statement. 5 (e) What is C-Python ? Compare Python with C programming language. 5 (f) Briefly discuss Tensor flow and Keras libraries in Python. Give the utility of both libraries in Python. 5 (g) What is a List in Python ? How does a list differ from tuple in Python ? Illustrate both with suitable example code in Python. 5 (h) What is a Lambda function in Python ? How does Lambda function differ from function in Python ? Give an example for each. 5
Ans. (a) Algorithm An algorithm is a well-defined, step-by-step set of instructions or a finite sequence of operations to solve a problem or accomplish a task. It’s like a recipe that specifies exactly what to do with a specific input to produce a specific output. In programming, an algorithm serves as a blueprint for designing the logic of a solution before writing the actual code. The important features that a valid algorithm must possess are:
- Input: An algorithm should have zero or more well-defined inputs. These are the values on which the algorithm operates.
- Output: An algorithm must have one or more well-defined outputs, which are the desired results for the inputs.
- Finiteness: An algorithm must terminate after a finite number of steps. It should not run indefinitely.
- Definiteness: Every step of the algorithm must be clear and unambiguous. Each instruction should have only one interpretation.
- Effectiveness: Every instruction must be basic enough that it can be carried out, in principle, by a person using only paper and pencil. It must be feasible and doable.
(b) Logical Error vs. Runtime Error
Logical Error:
A logical error is a flaw in the program’s logic. The program compiles and runs successfully, but it produces incorrect or unexpected output. These errors are often the hardest to detect because the compiler or runtime system gives no error message. The programmer has to review the code and find the flaw in the logic.
Example:
Calculating the average of two numbers but using multiplication instead of addition.
// Example of a Logical Error int a = 10, b = 20; int average = (a * b) / 2; // Flawed logic: should be a+b printf("Average: %d", average); // Outputs 100, which is wrong. Should be 15.
Runtime Error: A runtime error occurs during the execution of a program. These errors happen when the program asks the computer to perform an illegal operation, such as dividing by zero or accessing an invalid memory location. When a runtime error occurs, the program typically terminates abnormally or crashes. Example: Attempting to divide a number by zero.
// Example of a Runtime Error int x = 10; int y = 0; int z = x / y; // Runtime Error: Division by zero printf("Result: %d", z); // This line will never be executed
The key difference is that logical errors produce wrong results while the program keeps running, whereas runtime errors halt the program mid-execution. (c) Typecast Operator The typecast operator in C is used to explicitly convert the value of one data type into another. This is also known as explicit type conversion . It is useful when you want to ensure that a variable is treated as a different type in an operation, to avoid data loss or unexpected results. Syntax:
(target_type) expression;
Here, `target_type` is the data type you want to convert the value of the `expression` to. Output of the given code:
main() { int num = 5; printf ("num = %f ", (float) num/3); }
Analysis:
- `int num = 5;` declares an integer variable `num` with the value 5.
- In the `printf` statement, the expression `(float) num/3` is evaluated.
- `(float) num` uses the typecast operator. It temporarily converts the integer `num` (which is 5) to the float value `5.0`.
- Now, the expression becomes `5.0 / 3`. Since one operand is a float, C performs floating-point division.
- The result of `5.0 / 3` is approximately `1.666667`.
- The `%f` format specifier prints this float value.
The expected output is:
num = 1.666667
(d) break and continue Statements in C Both `break` and `continue` are loop control statements in C that alter the normal flow of a loop. break Statement: The `break` statement is used to terminate a loop (`for`, `while`, `do-while`) or a `switch` statement immediately. When `break` is encountered, control is transferred to the statement immediately following the loop or switch. Example:
#include <stdio.h> int main() { for (int i = 1; i <= 10; i++) { if (i == 5) { break; // Terminate the loop when i is 5 } printf("%d ", i); } // Output: 1 2 3 4 return 0; }
continue Statement: The `continue` statement skips the current iteration of a loop and continues execution with the next iteration of the loop. Unlike `break`, it does not terminate the loop. Example:
#include <stdio.h> int main() { for (int i = 1; i <= 10; i++) { if (i == 5) { continue; // Skip the iteration for i = 5 } printf("%d ", i); } // Output: 1 2 3 4 6 7 8 9 10 return 0; }
In summary, `break` “jumps out” of the loop, while `continue` “skips” the current iteration and “jumps to” the next one. (e) C-Python and Comparison with C C-Python (CPython): CPython is the standard, most widely-used, and reference implementation of the Python programming language. It is written in the C and Python languages. It is called “C-Python” because it is a C program that interprets Python code. When you download Python from the official python.org website, you are getting CPython. It compiles the Python code into intermediate bytecode, which is then executed by the CPython virtual machine. Python vs. C Programming Language:
| Feature | Python | C |
|---|---|---|
| Typing | Dynamically typed (types checked at runtime) | Statically typed (types checked at compile-time) |
| Execution | Interpreted (compiled to bytecode and then interpreted) | Compiled (compiled directly to machine code) |
| Memory Management | Automatic (via a garbage collector) | Manual (via `malloc`, `free`) |
| Speed | Slower compared to C | Very fast as it is closer to hardware |
| Complexity | Simple syntax, easy to learn, high-level language | More complex syntax, pointers, mid-level language |
| Application | Web development, data science, AI/ML, scripting | System programming, operating systems, embedded systems |
(f) TensorFlow and Keras Libraries TensorFlow: TensorFlow is a powerful, open-source software library developed by Google. It is primarily used for machine learning and artificial intelligence , especially large-scale numerical computation and deep learning models. It represents computation as dataflow graphs, where nodes represent mathematical operations, and the graph edges represent the multi-dimensional data arrays (tensors) communicated between them. Utility: TensorFlow is highly scalable and can run on various platforms, including CPUs, GPUs, and TPUs. It is ideal for building production-grade models, training complex neural networks, and deploying them on large datasets. Keras: Keras is a high-level neural networks API, written in Python. It is capable of running on top of TensorFlow, Theano, or Microsoft CNTK. Keras was designed to enable fast experimentation, focusing on user-friendliness, modularity, and extensibility. Utility: The primary goal of Keras is to make working with deep learning models as simple as possible. It is excellent for rapid prototyping , allowing users to build and train models with minimal code. It is easy to learn and use, making it a popular choice for beginners in deep learning. Since TensorFlow 2.x, Keras has become the official high-level API of TensorFlow. (g) List and Tuple in Python List: A list in Python is an ordered and mutable collection of items of different types. “Mutable” means that you can change, add, or remove its elements after it has been created. Lists are enclosed in square brackets `[]`. Example:
# Creating a list my_list = [1, "hello", 3.14] print(my_list) # Output: [1, 'hello', 3.14]# Modifying an element my_list[1] = "world" print(my_list) # Output: [1, 'world', 3.14]
# Appending an element my_list.append(True) print(my_list) # Output: [1, 'world', 3.14, True]
Tuple:
A tuple is also an ordered collection of items of different types, but it is
immutable
. “Immutable” means that once a tuple is created, you cannot change, add, or remove its elements. Tuples are enclosed in parentheses `()`.
Example:
# Creating a tuple my_tuple = (1, "hello", 3.14) print(my_tuple) # Output: (1, 'hello', 3.14)
# Attempting to modify an element (this will give an error) # my_tuple[1] = "world" # This will cause a TypeError
List vs. Tuple:
The key difference is
mutability
. Lists are mutable, and tuples are immutable. Because of this difference, tuples are slightly faster and more memory-efficient than lists. Tuples are used when you want to ensure the data collection does not change after creation, such as for a set of coordinates or a database record. Lists are used when you need a collection that you expect to modify during your program.
(h) Lambda Function vs. Regular Function
Lambda Function:
A lambda function in Python is a small,
anonymous
function defined using the `lambda` keyword. A lambda function can have any number of arguments but only one expression. The expression is evaluated, and the result is returned. They are typically used in situations where a small, one-time-use function is needed.
Example:
# A lambda function that adds two numbers add = lambda a, b: a + b print(add(5, 3)) # Output: 8
Regular Function: A regular function is defined using the `def` keyword and has a name. It can contain multiple statements, expressions, and a `return` statement that explicitly returns a value (or `None` if there is no `return`). Regular functions are designed for more complex logic, reusable code blocks, and documentation (via docstrings). Example:
# A regular function that adds two numbers def add(a, b): return a + b print(add(5, 3)) # Output: 8
Differences:
- Name: Lambda functions are anonymous, while regular functions are named using `def`.
- Syntax: Lambda functions are single-line expressions. Regular functions are a block of code.
- Complexity: Lambda functions are limited to only one expression. Regular functions can contain multiple statements and complex logic.
- Use Case: Lambdas are often used as arguments where function objects are expected (like `map()`, `filter()`), while regular functions are used for any reusable or complex task.
Q2. (a) Write a program in C to count the number of digits and find sum of all the digits in a five-digit integer number entered by the user. Support the program with suitable comments. 10 (b) (i) Write a short note on fread and fwrite commands in C (for file handling). Illustrate with the help of a program segment. 5 (ii) Write a recursive C program to calculate power of a number (x^n), without using an in-built function. 5
Ans. (a) C Program: Count Digits and Find Sum This program takes a five-digit integer number as input from the user. It then uses a `while` loop to isolate each digit of the number, counts the digits, and sums them up.
#include <stdio.h>int main() { // Variable declarations int number; // To store the number entered by the user int temp_number; // A copy of the original number for calculation int sum_of_digits = 0; // To store the sum of digits, initialized to 0 int digit_count = 0; // To count the number of digits, initialized to 0 int remainder; // To store the current digit (remainder)
// Prompt the user for input printf("Enter a five-digit integer number: "); scanf("%d", &number);
// Copy the original number to a temporary variable // so the original number is preserved after calculations temp_number = number;
// Handle the special case if the number is 0 if (temp_number == 0) { digit_count = 1; } else { // Loop until the number becomes 0 while (temp_number != 0) { // Get the last digit remainder = temp_number % 10;
// Add the digit to the sum sum_of_digits = sum_of_digits + remainder;
// Increment the digit counter digit_count++;
// Remove the last digit from the number temp_number = temp_number / 10; } }
// Print the results printf("\nOriginal Number: %d\n", number); printf("Number of digits: %d\n", digit_count); printf("Sum of all digits: %d\n", sum_of_digits);
return 0;}
Sample Output:
Enter a five-digit integer number: 12345Original Number: 12345 Number of digits: 5 Sum of all digits: 15
(b) (i) `fread()` and `fwrite()` in C
`fread()` and `fwrite()` are functions used for binary file operations in C. They are the counterparts to `fscanf()` and `fprintf()` for text files, but they read and write data in a binary format, which is generally more efficient for blocks of data like structures or arrays.
`fwrite()`:
This function writes blocks of data from memory to the file stream.
Syntax:
`size_t fwrite(const void
ptr, size_t size, size_t count, FILE
stream);`
- `ptr`: Pointer to the first element of an array of data to be written.
- `size`: Size in bytes of each element.
- `count`: Number of elements to be written.
- `stream`: Pointer to a `FILE` object that specifies an output stream.
`fread()`:
This function reads blocks of data from the file stream into memory.
Syntax:
`size_t fread(void
ptr, size_t size, size_t count, FILE
stream);`
- `ptr`: Pointer to a block of memory to store the data being read.
- `size`: Size in bytes of each element.
- `count`: Number of elements to be read.
- `stream`: Pointer to a `FILE` object that specifies an input stream.
Example Program Segment:
#include <stdio.h>// Define a student structurestruct Student { int roll_no; char name[50];};
void file_io_example() { FILE *file_ptr; struct Student s_write = {101, "Alice"}; struct Student s_read;
// Open file in binary write mode file_ptr = fopen("students.dat", "wb"); if (file_ptr == NULL) { printf("Error opening file for writing.\n"); return; } // Write the structure to the file printf("Writing student record to file...\n"); fwrite(&s_write, sizeof(struct Student), 1, file_ptr); fclose(file_ptr);
// Open file in binary read mode file_ptr = fopen("students.dat", "rb"); if (file_ptr == NULL) { printf("Error opening file for reading.\n"); return; }
// Read the structure from the file printf("Reading student record from file...\n"); fread(&s_read, sizeof(struct Student), 1, file_ptr);
// Print the read data printf("Roll No: %d, Name: %s\n", s_read.roll_no, s_read.name); fclose(file_ptr);}
(b) (ii) Recursive Power Function in C
This program uses a recursive approach to calculate `power(x, n)`, which is x raised to the power of n. The recursion is based on the mathematical principle that `x^n = x * x^(n-1)`. The base case is when the exponent `n` is 0, in which case the result is 1.
#include <stdio.h>// Recursive function to calculate power of x to the nlong long power(int base, int exponent) { // Base case: if exponent is 0, result is 1 if (exponent == 0) { return 1; } // Recursive step: multiply base by power(base, exponent-1) else if (exponent > 0) { return (long long)base * power(base, exponent - 1); } // For negative exponents (optional, question specifies integer power) // Here we are only handling non-negative exponents for simplicity. else { // Return an error or special value for negative exponents return -1; // as an error indicator }}
int main() { int base, exponent; long long result;
printf("Enter the base number (x): "); scanf("%d", &base);
printf("Enter the exponent (n, non-negative): "); scanf("%d", &exponent);
if (exponent < 0) { printf("This program supports only non-negative exponents.\n"); } else { result = power(base, exponent); printf("%d raised to the power of %d is %lld\n", base, exponent, result); }
return 0;}
How recursion works (Example: `power(2, 3)`):
- `power(2, 3)` is called. It returns `2 * power(2, 2)`.
- `power(2, 2)` is called. It returns `2 * power(2, 1)`.
- `power(2, 1)` is called. It returns `2 * power(2, 0)`.
- `power(2, 0)` is called. It matches the base case and returns 1.
- The results are unwound: `2 1 = 2`, then `2 2 = 4`, then `2 * 4 = 8`. The final result is 8.
Q3. (a) (i) Write a C program to input two strings and concatenate them. Calculate the length of the resultant string without using built-in function. 5 (ii) What is union in C ? How does it differ from structure ? Give suitable example for each. 5 (b) Using structures, write a C program to accept data of five students containing their enrollment number, name, subjects, marks. Each student scores marks in five subjects. Calculate total marks of each student and find out who has scored the highest marks among all. 10
Ans. (a) (i) C Program: String Concatenation and Length Calculation This program concatenates two strings and calculates the length of the resulting string without using any of the built-in string functions like `strcat()` or `strlen()`.
#include <stdio.h>int main() { char str1[50], str2[50], result[100]; int i = 0, j = 0; int len = 0;
// Input the first string printf("Enter the first string: "); scanf("%s", str1);
// Input the second string printf("Enter the second string: "); scanf("%s", str2);
// --- String Concatenation --- // Copy the first string into the result string while (str1[i] != '\0') { result[i] = str1[i]; i++; }
// Append the second string to the end of the result string while (str2[j] != '\0') { result[i] = str2[j]; i++; j++; } // Terminate the resultant string with a null character result[i] = '\0';
printf("\nConcatenated String: %s\n", result);
// --- Length Calculation --- // Calculate the length of the resultant string i = 0; // reset counter while (result[i] != '\0') { len++; i++; }
printf("Length of the resultant string: %d\n", len);
return 0;}
Sample Output:
Enter the first string: Hello Enter the second string: WorldConcatenated String: HelloWorld Length of the resultant string: 10
(a) (ii) Union vs. Structure
In C, both `structure` and `union` are user-defined data types that group together members of different types under a single entity. However, they differ fundamentally in their memory allocation.
Structure:
In a structure, each member is allocated its own separate memory space. The total size of the structure is the sum of the sizes of all its members (plus any padding for alignment).
Example:
struct ExampleStruct { int i; // 4 bytes (assume) char c; // 1 byte float f; // 4 bytes }; // Total size is roughly 4 + 1 + 4 = 9 bytes + padding
All members of a structure can hold values at the same time. Union: In a union, all members share the same memory location. The total size of the union is the size of its largest member. This means only one member can hold a meaningful value at any given time. If you assign a value to one member, it may overwrite the values of other members. Example:
union ExampleUnion { int i; // 4 bytes (assume) char c; // 1 byte float f; // 4 bytes }; // Total size is size of largest member = 4 bytes
Key Differences:
| Feature | Structure | Union |
|---|---|---|
| Keyword | `struct` | `union` |
| Memory | Each member gets separate memory. | All members share the same memory location. |
| Size | Sum of the sizes of all members (with padding). | Size of the largest member. |
| Values | Can store values of multiple members simultaneously. | Can store a value for only one member at a time. |
| Usage | For grouping related data together (e.g., a student record). | To save memory when a single variable needs to hold different types of values. |
(b) C Program: Student Data and Highest Marks This program uses a `struct` to manage records for five students. It takes enrollment number, name, and marks in five subjects as input for each student. It then calculates the total marks for each student and finally finds and displays the student who scored the highest total marks.
#include <stdio.h>#include <string.h>#define NUM_STUDENTS 5#define NUM_SUBJECTS 5
// Define a structure for student datastruct Student { long long enrollment_no; char name[100]; int marks[NUM_SUBJECTS]; int total_marks;};
int main() { // Create an array of structures for 5 students struct Student students[NUM_STUDENTS]; int i, j;
// --- Data Input --- // Loop to enter data for each student for (i = 0; i < NUM_STUDENTS; i++) { printf("\nEnter details for Student %d:\n", i + 1); printf(" Enter Enrollment Number: "); scanf("%lld", &students[i].enrollment_no);
printf(" Enter Name: "); scanf("%s", students[i].name);
students[i].total_marks = 0; // Initialize total marks printf(" Enter marks for %d subjects:\n", NUM_SUBJECTS); for (j = 0; j < NUM_SUBJECTS; j++) { printf(" Subject %d: ", j + 1); scanf("%d", &students[i].marks[j]); // Calculate total marks students[i].total_marks += students[i].marks[j]; } }
// --- Find Highest Scorer --- int highest_marks = -1; // Initialize with a very low value int top_student_index = -1;
for (i = 0; i < NUM_STUDENTS; i++) { if (students[i].total_marks > highest_marks) { highest_marks = students[i].total_marks; top_student_index = i; } }
// --- Display Results --- printf("\n--- Student Details and Totals ---\n"); for (i = 0; i < NUM_STUDENTS; i++) { printf("Student: %s, Total Marks: %d\n", students[i].name, students[i].total_marks); } printf("\n--- Student with Highest Marks ---\n"); if (top_student_index != -1) { printf("Enrollment No: %lld\n", students[top_student_index].enrollment_no); printf("Name: %s\n", students[top_student_index].name); printf("Highest Total Marks: %d\n", students[top_student_index].total_marks); } else { printf("No student data found.\n"); }
return 0;}
Q4. (a) Write a Python code and supporting algorithm to perform the following : (i) Display frequency of each word in a file. 5 (ii) Display last n lines from a file, where n is given by the user. 5 (b) Write a Python code to combine two lists containing strings. Use this program to print the string with maximum vowels from the combined list. 10
Ans. (a) Python File Operations (i) Display frequency of each word in a file Algorithm:
- Start: Begin the program.
- Filename: Specify a filename to read (e.g., ‘sample.txt’).
- Read File: Read the file and store its entire content into a string.
- Clean Data: Convert the string to lowercase for case-insensitive counting. Remove punctuation marks if necessary.
- Split into Words: Split the string into a list of individual words.
- Frequency Dictionary: Initialize an empty dictionary, say `word_freq`.
- Iterate over Words: For each word in the list of words:
- If the word already exists as a key in the `word_freq` dictionary, increment its count by 1.
- Otherwise, add the word to the dictionary with a count of 1.
- Display Result: Print the word frequency dictionary.
- End: Terminate the program.
Python Code:
# Create a sample.txt file with some text# Example: "Hello world this is a test. Hello again."try: with open('sample.txt', 'w') as f: f.write("Hello world this is a test. Hello again.")except IOError: print("Could not create sample file.")def display_word_frequency(filename): """Reads and displays the frequency of each word in a file.""" try: with open(filename, 'r') as file: text = file.read().lower() # Read and convert to lowercase except FileNotFoundError: print(f"Error: The file '{filename}' was not found.") return
# Replace punctuation with space for simple splitting text = text.replace('.', ' ').replace(',', ' ') words = text.split() # Split into a list of words word_freq = {} # Dictionary to store frequencies
for word in words: if word in word_freq: word_freq[word] += 1 else: word_freq[word] = 1 print(f"Word frequencies in '{filename}':") for word, count in word_freq.items(): print(f"'{word}': {count}")
# Call the functiondisplay_word_frequency('sample.txt')
(ii) Display last n lines from a file
Algorithm:
- Start: Begin the program.
- User Input: Prompt the user for the value of `n` (the number of lines to display).
- Filename: Specify a filename to read (e.g., ‘long_sample.txt’).
- Read File: Open the file and read all of its lines into a list.
- Extract Last n Lines: Using list slicing, select the last `n` elements from the list of lines (`lines[-n:]`).
- Display Lines: Iterate over each line in the extracted last `n` lines and print it.
- End: Terminate the program.
Python Code:
# Create a long_sample.txt file with multiple linestry: with open('long_sample.txt', 'w') as f: for i in range(1, 16): f.write(f"This is line number {i}.\\n")except IOError: print("Could not create sample file.")def display_last_n_lines(filename): """Displays the last n lines of a file, where n is given by the user.""" try: n = int(input("Enter the number of last lines to display (n): ")) except ValueError: print("Invalid input. Please enter an integer.") return
try: with open(filename, 'r') as file: lines = file.readlines() # Read all lines into a list except FileNotFoundError: print(f"Error: The file '{filename}' was not found.") return print(f"\\nLast {n} lines of '{filename}':") # Get the last n lines using list slicing and print them last_lines = lines[-n:] for line in last_lines: print(line.strip()) # Use .strip() to remove extra newlines
# Call the functiondisplay_last_n_lines('long_sample.txt')
(b) Python Code: Combine Lists and Find String with Maximum Vowels
This program takes two lists of strings, combines them into one, and then iterates through the combined list to find the string that contains the most vowels.
def count_vowels(s): """Counts the number of vowels in a given string.""" vowels = "aeiou" count = 0 # For each character in the string, check if it is a vowel for char in s.lower(): # Convert to lower for case-insensitivity if char in vowels: count += 1 return count# Two lists containing stringslist1 = ["python", "programming", "is", "fun"]list2 = ["data", "science", "and", "machine", "learning", "education"]
# Combine the two lists using the '+' operatorcombined_list = list1 + list2print("Combined List:", combined_list)
# Initialize variables to find the string with max vowelsmax_vowel_count = -1string_with_max_vowels = ""
# Iterate over each string in the combined listfor s in combined_list: # Calculate vowel count for the current string current_vowel_count = count_vowels(s) # Update the max if the current count is greater if current_vowel_count > max_vowel_count: max_vowel_count = current_vowel_count string_with_max_vowels = s
# Print the resultif string_with_max_vowels: print(f"\\nThe string with the maximum number of vowels is: '{string_with_max_vowels}'") print(f"It has {max_vowel_count} vowels.")else: print("No strings found in the list.")
Output:
Combined List: ['python', 'programming', 'is', 'fun', 'data', 'science', 'and', 'machine', 'learning', 'education']The string with the maximum number of vowels is: 'education' It has 5 vowels.
Q5. (a) What is a class in Python ? Create a class circle that represents a circle containing methods namely “area” and “perimeter” ? The perimeter method returns perimeter and area method returns area of the circle. Display results for methods in the given class “Circle”. 10 (b) Differentiate between any two of the following : 5+5 (i) Sub-routine vs. Co-routine (ii) Method Overloading vs. Method Overriding (iii) Module vs. Script
Ans. (a) Python Class ‘Circle’ Class in Python: A class in Python is a blueprint for creating objects. It is a user-defined data type that binds data members (attributes or variables) and methods (member functions or behaviors) together. A class allows you to create a new type of object, which can have new kinds of properties and operations. An instance of a class is an object, and you can create multiple objects from the same class. Classes are a fundamental part of Object-Oriented Programming (OOP). Implementation of ‘Circle’ Class: The following code defines a `Circle` class. The `__init__` constructor method initializes a circle object with a radius. The `area` and `perimeter` methods calculate and return the area and perimeter of the circle, respectively. Finally, we create an instance of the class and call its methods to display the results.
import math # To use the pi constantclass Circle: """A class that represents a circle."""
def __init__(self, radius): """ Initializes a Circle object with a radius. :param radius: The radius of the circle (must be a positive number). """ if radius <= 0: raise ValueError("Radius must be a positive number.") self.radius = radius
def area(self): """ Calculates and returns the area of the circle. Formula: π * r^2 """ return math.pi (self.radius * 2)
def perimeter(self): """ Calculates and returns the perimeter (circumference) of the circle. Formula: 2 π r """ return 2 math.pi self.radius
# --- Main Program ---
# Create an object of the "Circle" class with a radius of 7try: my_circle = Circle(7)
# Calculate and print the area circle_area = my_circle.area() print(f"The radius of the circle is: {my_circle.radius}") print(f"The area of the circle is: {circle_area:.2f}") # Formatted to 2 decimal places
# Calculate and print the perimeter circle_perimeter = my_circle.perimeter() print(f"The perimeter (circumference) of the circle is: {circle_perimeter:.2f}")
except ValueError as e: print(f"Error creating circle: {e}")
Output:
The radius of the circle is: 7 The area of the circle is: 153.94 The perimeter (circumference) of the circle is: 43.98
(b) Differentiations (i) Sub-routine vs. Co-routine
| Aspect | Sub-routine (e.g., a normal function) | Co-routine |
|---|---|---|
| Control Flow | Starts from the caller, runs to completion, and returns control to the caller. | Can pause its execution (yield), return control, and can be resumed later from the same point. |
| State | Does not retain local state between calls (unless using global/static variables). | Retains its local state (variables) between pauses and resumptions. |
| Execution Model | Call-return model. Fully run-to-completion. | Cooperative multitasking. Coroutines voluntarily give up control. |
| Example in Python | Any normal function defined with `def`. | Generators (using `yield`), and functions defined with `async`/`await` syntax. |
(ii) Method Overloading vs. Method Overriding
| Aspect | Method Overloading | Method Overriding |
|---|---|---|
| Definition | Creating multiple methods in the same class with the same name but different parameters (number, type, or order). | Providing a specific implementation in a subclass for a method that is already defined in its superclass. |
| Location | Occurs within the same class. | Occurs between two classes that have an inheritance relationship (superclass and subclass). |
| Method Signature | Method signatures (name and parameters) must be different. | Method signature (name and parameters) must be the same in both subclass and superclass. |
| Python Support | Python does not support method overloading in the traditional sense. It can be simulated using default arguments or ` args`/` *kwargs`. | Python fully supports method overriding. It is a key part of polymorphism. |
(iii) Module vs. Script
| Aspect | Module | Script |
|---|---|---|
| Purpose | Intended to be imported and used in other programs or scripts. It provides reusable functionality (functions, classes). | Intended to be executed directly. It is the entry point of a program that performs a specific task. |
| Usage | Brought into other files using `import module_name`. | Run from the command line (e.g., `python my_script.py`). |
| Structure | Contains mainly definitions: functions, classes, and variables. It has less direct executable code. | Contains a sequence of statements that are executed from top to bottom, often calling functions or classes to drive the main logic of the program. |
| `__name__` variable | When imported, its `__name__` variable is set to the module’s filename (without `.py`). | When executed directly, its `__name__` variable is set to the string `”__main__”`. |
The block `if __name__ == “__main__”:` allows a Python file to play a dual role: it can act as a script if run directly, and as a module if imported.
Download IGNOU previous Year Question paper download PDFs for MCS-201 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