पहले 10 प्रश्नों के उत्तर और हल यहीं खुले हैं; बाक़ी 20 के उत्तर और हल FliQi Question Bank app में।
भाषा
प्रश्न 1उत्तर + हल
निम्न C कोड का आउटपुट क्या होगा?
#include <stdio.h>
union p
{
int x;
char y;
}k = {.y = 97};
int main()
{
printf("%d\n", k.y);
}
What will be the output of the following C code?
#include <stdio.h>
union p
{
int x;
char y;
}k = {.y = 97};
int main()
{
printf("%d\n", k.y);
}
Aकम्पाइल टाइम एररCompile time error
B97
Ca
Dस्टैण्डर्ड पर निर्भर करता हैDepends on the standard
सही उत्तर / Answer: (B)
हल / Solution
यहाँ नामित आरंभीकरण (designated initializer) का रूप है, जो C99 से वैध है। कोष्ठक में .y लिखने पर यूनियन का सदस्य y चुना जाता है और उसमें सत्तानबे रखा जाता है। y का प्रकार char है, पर printf में %d लगाया गया है, सो मान पूर्णांक की तरह छपता है और 97 दिखता है। अगर वहाँ %c होता तो अक्षर a छपता। इसलिए कम्पाइल टाइम एरर या मानक पर निर्भर वाले विकल्प ग़लत हैं।
This is a designated initializer, a form that has been valid since C99. Writing .y inside the braces selects the member y of the union and stores ninety seven in it. The type of y is char, but printf is given %d, so the value is printed as an integer and 97 appears. Had the specifier been %c, the letter a would have been printed instead. So neither a compile time error nor any dependence on the standard arises.
प्रश्न 2उत्तर + हल
निम्न C कोड का आउटपुट क्या होगा?
#include <stdio.h>
union p
{
int x;
char y;
}k = {1, 97};
int main()
{
printf("%d %d", k.x,k.y);
}
What will be the output of the following C code?
#include <stdio.h>
union p
{
int x;
char y;
}k = {1, 97};
int main()
{
printf("%d %d", k.x,k.y);
}
A1 1
B1 97
C97 97
D97 1
सही उत्तर / Answer: (A)
हल / Solution
यूनियन (union) के सारे सदस्य एक ही मेमोरी साझा करते हैं, इसलिए उसमें एक बार में एक ही मान रह सकता है। यहाँ दो मान दिए गए हैं, जिनमें दूसरा अतिरिक्त है; compiler पहला ही लेता है, सो k.x का मान एक बनता है। फिर k.y उसी जगह का पहला byte पढ़ता है, और little-endian मशीन पर वह भी एक निकलता है। इसलिए 1 1 छपता है। मान 97 कहीं भंडारित ही नहीं हुआ।
All members of a union share the same memory, so it can hold only one value at a time. Two values are supplied here, of which the second is one too many; the compiler keeps the first, so k.x becomes one. Then k.y reads the first byte of that same storage, and on a little endian machine that byte is also one. The output is therefore 1 1, since the value 97 is never stored at all.
प्रश्न 3उत्तर + हल
निम्न C कोड का आउटपुट little-endian मशीन पर क्या होगा? #include <stdio.h> union p { int x; char y; }; int main() { union p p; p.y = 60; p.x = 12; printf("%d ", p.y); printf("%d", p.x); }
What will be the output of the following C code on a little-endian machine? #include <stdio.h> union p { int x; char y; }; int main() { union p p; p.y = 60; p.x = 12; printf("%d ", p.y); printf("%d", p.x); }
Aकम्पाइल टाइम एररCompile time error
B60 12
C60 60
D12 12
सही उत्तर / Answer: (D)
हल / Solution
union में x और y एक ही मेमोरी साझा करते हैं। पहले y में साठ रखा, पर अगली पंक्ति में x को बारह करते ही वही बाइट दोबारा लिखे गए, सो y का पुराना मान मिट गया। little-endian मशीन पर int का सबसे नीचा बाइट पहले पड़ता है, और बारह उसी एक बाइट में समा जाता है — इसलिए y पढ़ने पर भी बारह मिलता है। साठ वाले विकल्प तब बनते जब दोनों के अपने खाने होते।
In this union x and y share the same memory. The value sixty goes into y first, but the very next line writes twelve into x and overwrites those same bytes, so the old value of y is gone. On a little-endian machine the lowest byte of the int comes first, and twelve fits inside that single byte, so reading y also gives twelve. The other answers would need separate storage for each member, as a structure gives.
प्रश्न 4उत्तर + हल
निम्न C कोड का आउटपुट क्या होगा? #include <stdio.h> union { int x; char y; }p; int main() { p.x = 10; printf("%d\n", sizeof(p)); }
What will be the output of the following C code? #include <stdio.h> union { int x; char y; }p; int main() { p.x = 10; printf("%d\n", sizeof(p)); }
Aकम्पाइल टाइम एररCompile time error
Bsizeof(int) + sizeof(char)
Cकंपाइलर पर निर्भर करता हैDepends on the compiler
Dsizeof(int)
सही उत्तर / Answer: (D)
हल / Solution
union के सारे सदस्य एक ही मेमोरी खाना साझा करते हैं, सो उसका आकार सबसे बड़े सदस्य के बराबर होता है। यहाँ int और char में बड़ा int है, इसलिए sizeof(p) हर compiler पर sizeof(int) ही रहता है। वह संख्या मशीन से बदल सकती है, पर विकल्प संख्या नहीं बता रहे, सूत्र बता रहे हैं — इसीलिए «कंपाइलर पर निर्भर» ग़लत है। दोनों का जोड़ structure का नियम है, और कोड सही है सो कम्पाइल एरर भी नहीं आता।
Every member of a union shares the same block of memory, so the union is as large as its biggest member. Between an int and a char the int is bigger, so sizeof(p) equals sizeof(int) on every compiler. The printed number may differ from machine to machine, but the options give formulas rather than numbers, so saying it depends on the compiler is wrong. Adding the two sizes is a structure rule, and the code itself compiles cleanly.
प्रश्न 5उत्तर + हल
निम्न C कोड का आउटपुट क्या होगा?
#include <stdio.h>
union stu
{
int ival;
float fval;
};
void main()
{
union stu r;
r.ival = 5;
printf("%d", r.ival);
}
What will be the output of the following C code?
#include <stdio.h>
union stu
{
int ival;
float fval;
};
void main()
{
union stu r;
r.ival = 5;
printf("%d", r.ival);
}
A9
Bकम्पाइल टाइम एररCompile time error
C16
D5
सही उत्तर / Answer: (D)
हल / Solution
यूनियन stu का वेरिएबल r बनाया गया। r.ival = 5 से ival में 5 रखा गया। उसके बाद उसी सदस्य r.ival को %d से छापा गया। जो सदस्य लिखा गया वही पढ़ा गया, इसलिए मान पक्का 5 है। fval को छुआ ही नहीं गया, इसलिए साझा मेमोरी से कोई गड़बड़ नहीं होती। कोड सही है, इसलिए compile error नहीं है। 9 और 16 का कोई आधार नहीं है।
A variable r of type union stu is created. r.ival = 5 stores 5 in ival. Then the same member r.ival is printed with %d. The member that was written is the one that is read, so the value is surely 5. fval is never touched, so the shared memory causes no problem here. The code is valid, so there is no compile time error. The values 9 and 16 have no basis.
प्रश्न 6उत्तर + हल
निम्न C कोड में वेरिएबल u किस प्रकार का डेटा रख सकता है?
#include <stdio.h>
union u_tag
{
int ival;
float fval;
char *sval;
} u;
What type of data is held by the variable u in the following C code?
#include <stdio.h>
union u_tag
{
int ival;
float fval;
char *sval;
} u;
Aतीनों types में से सबसे बड़े को रख सकने लायक़ बड़ा होगाWill be large enough to hold the largest of the three types;
Bतीनों types में से सबसे छोटे को रख सकने लायक़ बड़ा होगाWill be large enough to hold the smallest of the three types;
Cतीनों types को एक साथ रख सकने लायक़ बड़ा होगाWill be large enough to hold the all of the three types;
Dउल्लिखित में से कोई नहींNone of the mentioned
सही उत्तर / Answer: (A)
हल / Solution
यूनियन के तीनों सदस्य ival, fval और sval एक ही मेमोरी साझा करते हैं। इसलिए compiler यूनियन u को इतनी जगह देता है कि तीनों में से सबसे बड़ा type उसमें समा जाए। एक समय पर उसमें इन तीनों में से कोई एक ही मान रहता है। सबसे छोटे जितनी जगह में बड़ा type नहीं समाएगा। तीनों को एक साथ रखने की जगह स्ट्रक्चर देता है, यूनियन नहीं। इसलिए पहला विकल्प सही है।
The three members ival, fval and sval share the same memory. So the compiler gives the union u enough space to hold the largest of the three types. At any moment it holds only one of these values. Space for only the smallest type would not fit the larger ones. Room for all three together is what a structure provides, not a union. So the first option is correct, as K&R also states.
प्रश्न 7उत्तर + हल
निम्नलिखित C कोड में REF LINE के बाद यूनियन का कौन-सा सदस्य सक्रिय होगा? #include <stdio.h> union temp { int a; float b; char c; }; union temp s = {1, 2.5, 'A'}; //REF LINE
Which member of the union will be active after REF LINE in the following C code? #include <stdio.h> union temp { int a; float b; char c; }; union temp s = {1, 2.5, 'A'}; //REF LINE
Aa
Bb
Cc
Dतीनों सदस्य एक साथAll three members at the same time
सही उत्तर / Answer: (A)
हल / Solution
union में एक समय पर एक ही सदस्य ज़िंदा रहता है। कोष्ठक वाला initializer union के सिर्फ़ पहले सदस्य को मान देता है — यहाँ a को एक मिला, और 2.5 तथा 'A' अतिरिक्त initializer हैं जिन्हें compiler छोड़ देता है। इसलिए REF LINE के बाद सक्रिय सदस्य a है। b या c में मान रखने के लिए s.b या s.c पर अलग assignment करना पड़ता। तीनों सदस्य एक साथ सक्रिय होना union में संभव ही नहीं।
Only one member of a union is alive at a time, because they all share the same memory. A braced initializer gives a value to the first member only, so here a receives one, while 2.5 and 'A' are excess initializers that the compiler drops. The active member after REF LINE is therefore a. Putting a value into b or c would need a separate assignment to s.b or s.c. Having all three active together is impossible in a union.
प्रश्न 8उत्तर + हल
इस प्रोग्राम का आउटपुट क्या है?
#include <stdio.h>
int main()
{
union demo {
int x;
int y;
};
union demo a = 100;
printf("%d %d",a.x,a.y);
}
What is the output of this program?
#include <stdio.h>
int main()
{
union demo {
int x;
int y;
};
union demo a = 100;
printf("%d %d",a.x,a.y);
}
A100 0
B100 100
C0 0
Dकम्पाइलेशन एररCompilation Error
सही उत्तर / Answer: (D)
हल / Solution
यूनियन वेरिएबल को initialize करने के लिए braces वाली सूची चाहिए, जैसे union demo a = {100}; या फिर उसी प्रकार का कोई दूसरा यूनियन। यहाँ union demo a = 100; में सीधे एक int दिया गया है। int और union demo अलग प्रकार हैं, इसलिए compiler यहाँ error देता है (invalid initializer)। प्रोग्राम compile ही नहीं होता, तो कोई आउटपुट नहीं छपता। अगर {100} लिखा होता तो x और y एक ही मेमोरी साझा करने से 100 100 छपता।
A union variable must be initialized with a brace-enclosed list, such as union demo a = {100};, or with another union of the same type. Here union demo a = 100; gives a plain int. An int and union demo are different types, so the compiler reports an error (invalid initializer). The program does not compile, so nothing is printed. With {100}, x and y would share the same memory and print 100 100.
प्रश्न 9उत्तर + हल
यूनियन के सदस्य ________ के रूप में एक्सेस किए जाते हैं।
Members of a union are accessed as ________.
Aयूनियन-वेरिएबल.मेंबरunion-variable.member
Bयूनियन-पॉइंटर->मेंबरunion-pointer->member
Ca और b दोनोंBoth a & b
Dउल्लिखित में से कोई नहींNone of the mentioned
सही उत्तर / Answer: (C)
हल / Solution
यूनियन के सदस्य तक दो तरह से पहुँचा जाता है। अगर हमारे पास यूनियन का वेरिएबल है तो dot ऑपरेटर लगता है, जैसे u.member। अगर यूनियन का पॉइंटर है तो arrow ऑपरेटर लगता है, जैसे p->member। p->member असल में (*p).member का छोटा रूप है। दोनों तरीक़े सही और आम हैं, इसलिए उत्तर a और b दोनों है। "कोई नहीं" ग़लत है क्योंकि दोनों तरीक़े मान्य हैं।
A union member can be reached in two ways. With a union variable, we use the dot operator, as in u.member. With a pointer to a union, we use the arrow operator, as in p->member. In fact, p->member is just a short form of (*p).member. Both ways are correct and common, so the answer is both a and b. "None of the mentioned" is wrong because both forms are valid.
प्रश्न 10उत्तर + हल
यूनियन का वर्णन करने के लिए सबसे उपयुक्त वाक्य कौन-सा है?
Most appropriate sentence to describe union is:
Aयूनियन स्ट्रक्चर जैसे होते हैंUnions are like structures
Bयूनियन में अलग-अलग प्रकार के सदस्य होते हैं, जो मेमोरी में एक ही स्थान साझा करते हैंUnion contain members of different types which share the same storage area in memory
Cप्रोग्राम में यूनियन का उपयोग कम होता हैUnion less Frequently used in the program
Dयूनियन का उपयोग set operations के लिए होता हैUnion are used for set operations
सही उत्तर / Answer: (B)
हल / Solution
यूनियन की असली पहचान यह है कि उसके अलग-अलग प्रकार के सदस्य मेमोरी का एक ही हिस्सा साझा करते हैं। यही बात उसे स्ट्रक्चर से अलग करती है। "स्ट्रक्चर जैसे होते हैं" सिर्फ़ लिखने के ढंग की समानता बताता है, यूनियन का काम नहीं। कम उपयोग होना कोई परिभाषा नहीं है। set operations से C के यूनियन का कोई संबंध नहीं, वह गणित वाले union से अलग है। इसलिए दूसरा वाक्य सबसे उपयुक्त है।
The defining feature of a union is that its members, which may be of different types, share the same area of memory. That is exactly what separates it from a structure. "Unions are like structures" only points to similar syntax, not to how a union works. Being used less often is not a definition. A C union has nothing to do with set operations; it is not the union of sets in maths. So option 2 fits best.
आगे के 20 प्रश्नों के उत्तर और हल app मेंFliQi Question Bank — अभ्यास, हल और आपकी प्रगति
निम्नलिखित घोषणा का आकार क्या होगा? (मान लीजिए double का आकार 8, int का आकार 4 और char का आकार 1 है) union uTemp { double a; int b[10]; char c; } u;
What would be the size of the following declaration? (Assuming size of double = 8, size of int = 4, size of char = 1) union uTemp { double a; int b[10]; char c; } u;
ऊपर दिए प्रोग्राम का आउटपुट बताइए। मान लीजिए int का आकार 4 बाइट और char का आकार 1 बाइट है। यह भी मान लीजिए कि कोई alignment ज़रूरी नहीं है।
union test
{
int x;
char arr[4];
int y;
};
int main()
{
union test t;
t.x = 0;
t.arr[1] = 'G';
printf("%s", t.arr);
return 0;
}
Predict the output of above program. Assume that the size of an integer is 4 bytes and size of character is 1 byte. Also assume that there is no alignment needed.
Aकुछ भी प्रिंट नहीं होताNothing is printed
Bएक garbage अक्षर और उसके बाद 'G'Garbage character followed by 'G'
Cएक garbage अक्षर, फिर 'G', फिर और garbage अक्षरGarbage character followed by 'G', followed by more garbage characters
मान लीजिए short, float और long प्रकार के ऑब्जेक्ट क्रमशः 2 बाइट, 4 बाइट और 8 बाइट लेते हैं। alignment को अनदेखा करते हुए वेरिएबल t के लिए कितनी मेमोरी चाहिए? (GATE CS 2000)
Consider the following C declaration:
struct {
short s[5];
union {
float y;
long z;
}u;
} t;
Assume that objects of the type short, float and long occupy 2 bytes, 4 bytes and 8 bytes, respectively. The memory requirement for variable t, ignoring alignment considerations, is (GATE CS 2000)
struct emp का sizeof क्या है (16 बिट मशीन पर, और मान लीजिए कि padding नहीं लगती यानी byte packing है)? struct emp { int x; union emp1 { char p; }r1; }r2;
What is sizeof struct emp (according to a 16 bit machine, assuming byte packing with no padding)? struct emp { int x; union emp1 { char p; }r1; }r2;
निम्नलिखित C कोड (64 बिट मशीन पर) का आउटपुट क्या है? union Sti { int nu; char m; }; int main() { union Sti s; printf("%d", sizeof(s)); return 0; }
What is the output of the following C code (on a 64 bit machine)? union Sti { int nu; char m; }; int main() { union Sti s; printf("%d", sizeof(s)); return 0; }