पहले 10 प्रश्नों के उत्तर और हल यहीं खुले हैं; बाक़ी 15 के उत्तर और हल FliQi Question Bank app में।
भाषा
प्रश्न 1उत्तर + हल
इस प्रोग्राम का आउटपुट क्या होगा? #include<stdio.h> #include<stdlib.h> int main() { int *j = (int*)malloc(4 * sizeof(int)); *j = 15; free(j); printf("%d", *j); return 0; }
What is the output of this program? #include<stdio.h> #include<stdlib.h> int main() { int *j = (int*)malloc(4 * sizeof(int)); *j = 15; free(j); printf("%d", *j); return 0; }
Cकोई कचरा मान (Some Garbage value)Some Garbage value
Dकुछ नहीं छपेगा (Nothing prints)Nothing prints
सही उत्तर / Answer: (C)
हल / Solution
free(j) के बाद j लटकता पॉइंटर (dangling pointer) बन जाता है, क्योंकि वह खंड अब प्रोग्राम का नहीं रहा। उसे *j से पढ़ना अपरिभाषित व्यवहार (undefined behaviour) है, इसलिए वहाँ पड़ा मान अनिश्चित यानी कचरा होता है। कोड ठीक संकलित होता है, सो कंपाइलेशन एरर नहीं आती। printf चलता भी है, इसलिए कुछ न छपने वाला विकल्प भी ग़लत है। ठीक शून्य छपने की कोई गारंटी नहीं, वह एक ख़ास दावा भर है।
After free(j) the pointer j becomes a dangling pointer, because that block no longer belongs to the program. Reading it through *j is undefined behaviour, so whatever sits there is an indeterminate, garbage value. The code itself compiles cleanly, so a compilation error is ruled out, and printf really does run, which rules out Nothing prints. Printing exactly zero is a specific claim that nothing in the language guarantees.
प्रश्न 2उत्तर + हल
malloc() का उपयोग करके डायनेमिक रूप से मेमोरी आवंटित करते समय इनपुट के रूप में लिए जाने वाले आर्गुमेंट्स की संख्या _______ है
The number of arguments taken as input while allocating memory dynamically using malloc() is _______
A3
B2
C1
D0
सही उत्तर / Answer: (C)
हल / Solution
malloc का prototype void *malloc(size_t size); है, इसलिए malloc() ठीक एक argument लेता है — कुल कितने byte चाहिए। 10 int के array के लिए हम malloc(10 * sizeof(int)) लिखते हैं, जो फिर भी एक ही argument है। दो argument लेने वाला function calloc() है — element की संख्या और हर element का आकार। कोई standard allocation function तीन या शून्य argument नहीं लेता। इसलिए विकल्प 1, 2 और 4 ग़लत हैं।
The prototype is void *malloc(size_t size);, so malloc() takes exactly one argument: the total number of bytes to reserve. For an array of 10 ints we write malloc(10 * sizeof(int)), which is still a single argument. calloc() is the function that takes two arguments, the number of elements and the size of each. No standard allocation function takes three or zero arguments, so options 1, 2 and 4 are wrong.
प्रश्न 3उत्तर + हल
हम malloc से पहले (int *) क्यों लिखते हैं?
int *ip = (int *)malloc(sizeof(int));
Why do we write (int *) before malloc?
int *ip = (int *)malloc(sizeof(int));
Aयह malloc फ़ंक्शन को अपेक्षित डेटा-प्रकार के बारे में सूचित करना है।It is to inform malloc function about the data-type expected
Bयह टाइप-कास्टिंग के लिए है।It is for the type-casting
Cयह सिंटैक्स शुद्धता के लिए है।It is for the syntax correctness
Dउल्लिखित में से कोई नहीं।None of the mentioned
सही उत्तर / Answer: (B)
हल / Solution
malloc() सामान्य pointer void * लौटाता है। आगे लिखा (int *) type cast है, जो void * को int * में बदलता है ताकि वह ip से मेल खाए। C में यह cast ज़रूरी नहीं, C++ में ज़रूरी है। malloc() को सिर्फ़ byte की संख्या मिलती है, इसलिए cast उसे data type के बारे में कुछ नहीं बताता। cast के बिना भी कथन सही है, इसलिए यह syntax के लिए भी नहीं है।
malloc() returns void *, a generic pointer. Writing (int *) before it is a type cast that converts this void * into int * so that it matches ip. In C the cast is optional because void * converts automatically, but C++ requires it. malloc() only receives the number of bytes, so the cast does not tell it anything about the data type. The statement is also valid without the cast, so it is not needed for syntax correctness.
प्रश्न 4उत्तर + हल
malloc() का उपयोग करके मेमोरी एलोकेशन कहाँ किया जाता है?
Memory allocation using malloc() is done in?
Aस्टैक एरियाStack area
Bहीप एरियाHeap area
Cस्टैक और हीप एरिया दोनोंBoth Stack & Heap area
Dस्टैटिक एरियाStatic area
सही उत्तर / Answer: (B)
हल / Solution
malloc() heap से मेमोरी लेता है। heap वह हिस्सा है जिसे प्रोग्राम रन टाइम पर dynamic allocation के लिए बरतता है। वहाँ का block free() बुलाए जाने तक रहता है। stack में सिर्फ़ local variable और function call की जानकारी रहती है। static area में global और static variable रहते हैं। malloc() इनसे मेमोरी नहीं लेता, इसलिए विकल्प 1, 3 और 4 ग़लत हैं और heap area सही है।
malloc() takes memory from the heap, the region that the program manages at run time for dynamic allocation. The block stays there until free() is called, even after the function that called malloc() has returned. The stack holds only local variables and call frames, and the static area holds global and static variables. malloc() does not take memory from them, so options 1, 3 and 4 are wrong and heap area is correct.
प्रश्न 5उत्तर + हल
निम्न में से कौनसा स्टैटिक मेमोरी अलोकेशन का एक उदाहरण है?
Which of the following is an example of static memory allocation?
Aऐरे (Array)Array
Bक्यू (Queue)Queue
Cस्टैक (Stack)Stack
Dलिंक्ड लिस्ट (Linked list)Linked list
सही उत्तर / Answer: (A)
हल / Solution
int a[10]; जैसे साधारण array का आकार तय होता है और compiler को प्रोग्राम चलने से पहले ही पता होता है। यही static memory allocation है। linked list एक-एक node जोड़कर बढ़ती है, और हर node आम तौर पर रन टाइम पर malloc() से बनता है। इसलिए वह dynamic allocation का उदाहरण है। stack और queue ऐसे ढाँचे हैं जो दोनों तरीक़ों से बन सकते हैं, इसलिए वे static के उदाहरण नहीं माने जाते।
A normal array such as int a[10]; has a fixed size that the compiler knows before the program runs, so its memory is decided in advance. That is static memory allocation. A linked list grows node by node, and each node is usually created at run time with malloc(), so it is the standard example of dynamic allocation. Stack and queue are abstract structures that can be built either way, so they are not the standard example.
प्रश्न 6उत्तर + हल
realloc() फ़ंक्शन का कार्य क्या है?
What is functionality of realloc() function?
Afree() फ़ंक्शन द्वारा हटाई गई मेमोरी को फिर से आवंटित करता है।Reallocates memory deleted by free() function.
Bइसका उपयोग पहले आवंटित मेमोरी स्पेस के आकार को संशोधित करने के लिए किया जाता है।It is used to modify the size of the previously allocated memory space.
Cmalloc() या calloc() द्वारा आवंटित मेमोरी का स्थान बदलें।Change the location of memory allocated by malloc() or calloc().
Dइनमें से कोई नहींNone of these
सही उत्तर / Answer: (B)
हल / Solution
realloc(ptr, new_size) उस block का आकार बदलता है जो पहले malloc(), calloc() या realloc() से लिया गया था। पुराना data छोटे आकार तक सुरक्षित रहता है। ज़रूरत हो तो यह block को नई जगह ले जाकर नया पता लौटा सकता है, पर यह सिर्फ़ साथ में होने वाली बात है, इसका असली काम नहीं। इसलिए विकल्प 3 ग़लत है। free() से लौटाई जा चुकी मेमोरी यह वापस नहीं लाता, इसलिए विकल्प 1 भी ग़लत है। इसलिए विकल्प 2 सही है।
realloc(ptr, new_size) changes the size of a block that was earlier obtained from malloc(), calloc() or realloc(). It keeps the old contents up to the smaller of the two sizes. If needed, it may move the block and return a new address, but moving is only a side effect, not its purpose, so option 3 is wrong. It cannot bring back memory already released by free(), so option 1 is wrong. Hence option 2 is correct.
प्रश्न 7उत्तर + हल
इस C कोड का आउटपुट क्या होगा? #include<stdio.h> #include<stdlib.h> void main() { char *p = calloc(100, 1); p = "welcome"; printf("%s\n", p); }
What is the output of this C code? #include<stdio.h> #include<stdlib.h> void main() { char *p = calloc(100, 1); p = "welcome"; printf("%s\n", p); }
calloc() से सौ बाइट मिलते हैं और उनका पता p में आता है। अगली पंक्ति p में स्ट्रिंग नियतांक (string literal) का पता भर देती है, इसलिए पुराना पता खो जाता है। यह मेमोरी रिसाव (memory leak) है, त्रुटि नहीं; कोड संकलित होकर चलता है। printf अब उसी नियतांक को छापता है, सो आउटपुट welcome आता है। एरर वाला विकल्प इसलिए ग़लत है। कचरा मान या सेगमेंटेशन फॉल्ट तब आता, जब p मुक्त या अवैध पते पर जा चुका होता।
calloc() returns a block of hundred bytes and its address is kept in p. The very next line overwrites that pointer with the address of a string literal, so the allocated block is simply leaked. A leak is not an error, and the code compiles and runs, so printf prints that literal and the output is welcome. This is why the Error option fails. Garbage or a segmentation fault would need p to point at freed or invalid memory.
प्रश्न 8उत्तर + हल
निम्नलिखित में से कौन सा कथन C में malloc() फ़ंक्शन का सही प्रोटोटाइप है?
Which of the following statement is correct prototype of the malloc() function in C ?
AChar* malloc(char);
Bint* malloc(int);
Cvoid* malloc(size_t);
Dunsigned int* malloc(unsigned int);
सही उत्तर / Answer: (C)
हल / Solution
में malloc का standard prototype void *malloc(size_t size); है। इसका argument size_t प्रकार का है, जो byte में आकार बताने वाला unsigned प्रकार है। return type void * है, यानी सामान्य pointer, जिसे किसी भी object pointer में रखा जा सकता है। विकल्प 1 char लेता है और char * लौटाता है। विकल्प 2 में int है और विकल्प 4 में unsigned int। इनमें से कोई असली घोषणा से मेल नहीं खाता, इसलिए सिर्फ़ विकल्प 3 सही है।
The standard prototype, declared in , is void *malloc(size_t size);. The argument has type size_t, an unsigned type used for sizes in bytes. The return type is void *, a generic pointer that can be stored in any object pointer. Option 1 takes a char and returns char *. Option 2 uses int, and option 4 uses unsigned int. None of these match the real declaration, so only option 3 is correct.
प्रश्न 9उत्तर + हल
निम्नलिखित में से कौनसा फ़ंक्शन मेमोरी के कई ब्लॉक आवंटित करता है, प्रत्येक ब्लॉक समान आकार का?
Which of the following functions allocates multiple blocks of memory, each block of the same size?
Afree()
Bcalloc()
Cmalloc()
Drealloc()
सही उत्तर / Answer: (B)
हल / Solution
calloc(n, size) n block देता है, हर block 'size' byte का। यह array के लिए ठीक बैठता है, और यह सारी मेमोरी को शून्य से भर देता है। malloc(size) सिर्फ़ कुल byte की एक संख्या लेता है और एक block लौटाता है, इसलिए कई बराबर block वाली बात उसके argument में नहीं है। realloc() पहले से बने block का आकार बदलता है, और free() मेमोरी लौटाता है। इसलिए कई समान आकार के block देने वाला function calloc() है।
calloc(n, size) allocates n blocks, each of 'size' bytes, which suits arrays, and it fills them with zero. malloc(size) takes a single size in bytes and returns one block, so the idea of many equal blocks is not part of its arguments. realloc() changes the size of an existing block, and free() releases memory instead of allocating it. So calloc() is the function that allocates multiple blocks of the same size.
प्रश्न 10उत्तर + हल
फंक्शन free(p) में, p एक है
In function free(p), p is a
Aint
Bmalloc() द्वारा लौटाया गया पॉइंटरPointer returned by malloc()
Ccalloc() द्वारा लौटाया गया पॉइंटरPointer returned by calloc()
Dmalloc() और calloc() द्वारा लौटाया गया पॉइंटरPointer returned by malloc() & calloc()
सही उत्तर / Answer: (D)
हल / Solution
free(p) heap से लिया गया block लौटाता है। इसलिए p ऐसा pointer होना चाहिए जो malloc(), calloc() या realloc() से मिला हो, या फिर NULL हो। malloc() और calloc() दोनों ऐसे pointer देते हैं, इसलिए विकल्प 4 दोनों हालात को ढकता है। विकल्प 2 और 3 सिर्फ़ एक-एक स्रोत बताते हैं, इसलिए अधूरे हैं। p एक pointer है, int नहीं, इसलिए विकल्प 1 ग़लत है। free() को कोई और पता देना undefined behaviour है।
free(p) releases a block that was obtained from the heap. So p must be a pointer that came from malloc(), calloc() or realloc(), or be NULL. Both malloc() and calloc() give such pointers, so option 4 covers both cases. Options 2 and 3 each name only one source, so each is incomplete. p is a pointer, not an int, so option 1 is wrong. Passing any other address to free() gives undefined behaviour.
आगे के 15 प्रश्नों के उत्तर और हल app मेंFliQi Question Bank — अभ्यास, हल और आपकी प्रगति
निम्नलिखित प्रोग्राम पर विचार करें, i, j और k को मेमोरी में कहाँ स्टोर किया जाता है?
#include<stdio.h>
int i;
int main()
{
int j;
int *k = (int *) malloc (sizeof(int));
}
Consider the following program, where are i, j and k stored in memory?
#include<stdio.h>
int i;
int main()
{
int j;
int *k = (int *) malloc (sizeof(int));
}
Ai, j और *k को स्टैक सेगमेंट में स्टोर किया जाता हैi, j and *k are stored in stack segment
Bi को डेटा सेगमेंट के BSS भाग में स्टोर किया जाता है, j को स्टैक सेगमेंट में स्टोर किया जाता है। *k हीप में स्टोर होता है।i is stored in BSS part of data segment, j is stored in stack segment. *k is stored on heap.
Cj को डेटा सेगमेंट के BSS हिस्से में स्टोर किया जाता है, i को स्टैक सेगमेंट में स्टोर किया जाता है। *k हीप में स्टोर होता है।j is stored in BSS part of data segment, i is stored in stack segment. *k is stored on heap.
Di और j को स्टैक सेगमेंट में स्टोर किया जाता है। *k हीप में स्टोर होता है।i and j are stored in stack segment. *k is stored on heap.
डायनेमिक मेमोरी आवंटन के संबंध में गलत कथन का चयन करें।
Choose the statement which is incorrect with respect to dynamic memory allocation.
Aअनप्रेडिक्टेबल मेमोरी आवश्यकताओं के लिए उपयोग किया जाता हैUsed for unpredictable memory requirements
Bमेमोरी को मेमोरी के कम स्ट्रक्चर्ड एरिया में अलोकेटेड किया जाता है, जिसे हीप कहा जाता हैMemory is allocated in a less structured area of memory, known as heap
Cप्रोग्राम का एक्सिक्यूशन स्टैटिक मेमोरी एलोकेशन की तुलना में तेज़ हैExecution of the program is faster than that of static memory allocation
Dप्रोग्राम की आवश्यकता के आधार पर प्रोग्राम के रन टाइम के दौरान एलोकेशन मेमोरी को बदला जा सकता हैAllocated memory can be changed during the run time of the program based on the requirement of the program
निम्नलिखित C कोड का आउटपुट क्या होगा? #include <stdio.h> #include <stdlib.h> int *f(); int main(){ int *p = f(); printf("%d\n", *p); } int *f(){ int *j = (int*)malloc(sizeof(int)); *j = 10; return j; }
What will be the output of the following C code? #include <stdio.h> #include <stdlib.h> int *f(); int main(){ int *p = f(); printf("%d\n", *p); } int *f(){ int *j = (int*)malloc(sizeof(int)); *j = 10; return j; }
A10
Bकम्पाइल टाइम एररCompile time error
Cसेगमेंटेशन फॉल्ट या रनटाइम क्रैश, क्योंकि लोकल वेरिएबल का पॉइंटर लौटाया गया हैSegmentation fault/runtime crash since pointer to local variable is returned