पहले 10 प्रश्नों के उत्तर और हल यहीं खुले हैं; बाक़ी 20 के उत्तर और हल FliQi Question Bank app में।
भाषा
प्रश्न 1उत्तर + हल
इस C कोड का आउटपुट क्या है?
#include<stdio.h>
int x = 5;
void main(){
int x = 3;
printf("%d", x);
{
int x = 4;
}
printf("%d", x);
}
What is the output of this C code?
#include<stdio.h>
int x = 5;
void main(){
int x = 3;
printf("%d", x);
{
int x = 4;
}
printf("%d", x);
}
A3 3
B3 4
C3 5
Dरन टाइम एररRun time error
सही उत्तर / Answer: (A)
हल / Solution
बाहर x का मान पाँच है, पर main के भीतर नया x बनता है जिसका मान तीन है और जो बाहर वाले को ढक देता है। पहला printf इसी भीतरी चर को देखता है, सो तीन छपता है। आगे के कोष्ठक-खंड में एक और x बनता है जिसका मान चार है, पर वह उसी खंड तक सीमित है और कोष्ठक बंद होते ही मिट जाता है। दूसरा printf फिर main वाले x को पढ़ता है, इसलिए दोबारा तीन आता है।
Outside, x has the value five, but inside main a new x with value three hides it. The first printf sees this inner variable, so it prints three. The block in braces that follows creates yet another x with value four, yet that variable lives only inside that block and dies as soon as the brace closes. The second printf therefore reads main's own x once more and prints three again.
प्रश्न 2उत्तर + हल
इस C कोड का आउटपुट क्या है?
#include<stdio.h>
int x;
void main(){
printf("%d", x);
}
What is the output of this C code?
#include<stdio.h>
int x;
void main(){
printf("%d", x);
}
Aगार्बेजGarbage
Bरन टाइम एररRun time error
C0
Dकुछ नहीं छपेगाPrint Nothing
सही उत्तर / Answer: (C)
हल / Solution
x हर फ़ंक्शन के बाहर घोषित है, यानी वह वैश्विक (global) चर है। मानक कहता है कि ऐसे चर, जिन्हें शुरू में कोई मान न दिया जाए, अपने-आप शून्य से भर दिए जाते हैं। वे प्रोग्राम चलने से पहले ही स्थिर भंडारण में बन जाते हैं। इसलिए printf शून्य छापता है। गार्बेज तब आता जब x किसी फ़ंक्शन के भीतर का चर होता। रन टाइम एरर की कोई वजह नहीं, और छपना भी होगा ही।
Here x is declared outside every function, so it is a global variable. The standard says that such variables, when no initial value is given, are set to zero automatically. They are created in static storage before the program even starts running. So printf prints zero. Garbage would appear only if x were a local variable inside some function. There is no reason at all for a run time error, and something certainly does get printed.
प्रश्न 3उत्तर + हल
इस C कोड का आउटपुट क्या है?
#include<stdio.h>
void main(){
m();
printf("%d", x);
}
int x;
void m(){
x = 4;
}
What is the output of this C code?
#include<stdio.h>
void main(){
m();
printf("%d", x);
}
int x;
void m(){
x = 4;
}
A4
Bकम्पाइल टाइम एररCompile time error
C0
DअपरिभाषितUndefined
सही उत्तर / Answer: (B)
हल / Solution
C में किसी नाम का इस्तेमाल करने से पहले उसकी घोषणा दिख जानी चाहिए। यहाँ x की घोषणा main के नीचे लिखी है, पर printf उसे main के भीतर पढ़ने की कोशिश करता है। उस जगह compiler को x का कोई पता नहीं, सो वह «x undeclared» कहकर रुक जाता है। m() की कॉल भी अपनी परिभाषा से ऊपर है, जिस पर चेतावनी मिलती है। प्रोग्राम बनता ही नहीं, इसलिए चार या शून्य छपने का सवाल नहीं।
In C a name must be declared before it is used. Here x is declared below main, but printf tries to read it inside main. At that point the compiler knows nothing about x, so it stops with an undeclared identifier error. The call to m() also sits above its own definition, which draws a warning of its own. Since the program never compiles, neither four nor zero can be printed.
प्रश्न 4उत्तर + हल
नीचे दिए गए कोड का आउटपुट क्या है?
#include<stdio.h>
enum m{JAN, FEB, MAR};
enum m foo();
int main(){
enum m i = foo();
printf("%d\n", i);
}
int foo(){
return JAN;
}
What is the output of the code given below?
#include<stdio.h>
enum m{JAN, FEB, MAR};
enum m foo();
int main(){
enum m i = foo();
printf("%d\n", i);
}
int foo(){
return JAN;
}
Aकम्पाइल टाइम एररCompile time error
B0
CJAN
D1
सही उत्तर / Answer: (A)
हल / Solution
पहले घोषणा की गई है enum m foo(); यानी foo का प्रतिफल प्रकार enum m है। नीचे उसी फ़ंक्शन की परिभाषा int लौटाने वाली लिखी गई है। C यहाँ enum प्रकार और int को एक ही नहीं मानता, इसलिए दोनों घोषणाएँ टकराती हैं और compiler «conflicting types» की त्रुटि देता है। JAN का मान शून्य होता, पर प्रोग्राम बनता ही नहीं, सो शून्य या JAN छपने का सवाल नहीं उठता। उत्तर कम्पाइल टाइम एरर है।
The program first declares enum m foo(); which promises that foo returns the enum type. Lower down the same function is defined as returning int. C does not treat an enum type and int as the same type here, so the two declarations conflict and the compiler reports a conflicting types error. The value of JAN would have been zero, but since the program never compiles, neither zero nor JAN can be printed.
प्रश्न 5उत्तर + हल
क्या C में फ़ंक्शन ऐनुमरेशन कांस्टेंट लौटा सकते हैं?
Can functions return enumeration constants in c?
Aसत्यtrue
Bअसत्यfalse
Cकम्पाइलर पर निर्भरdepends on the compiler
Dमानक पर निर्भरdepends on the standard
सही उत्तर / Answer: (A)
हल / Solution
C में ऐनुमरेशन कांस्टेंट पूर्णांक मान ही होते हैं, सो फ़ंक्शन उन्हें बिना किसी अड़चन लौटा सकता है। जैसे enum day {MON, TUE}; के बाद ऐसा फ़ंक्शन बनाया जा सकता है जो MON लौटाए। घोषित प्रतिफल प्रकार enum हो या सादा int, दोनों चलते हैं, क्योंकि enum किसी पूर्णांक प्रकार के अनुरूप होता है। यह बरताव मानक में तय है, इसलिए कम्पाइलर या मानक पर निर्भर वाले विकल्प ग़लत हैं। सो उत्तर सत्य है।
Enumeration constants in C are nothing but integer values, so a function can return them without any trouble. For example after enum day {MON, TUE}; one can write a function that simply returns MON. The declared return type may be the enum type or plain int, since an enum is compatible with some integer type. This behaviour is fixed by the standard, so the answers that depend on the compiler or on the standard are both wrong.
प्रश्न 6उत्तर + हल
इस C कोड का आउटपुट क्या है?
#include<stdio.h>
double foo();
int main(){
foo();
return 0;
}
foo(){
printf("2 ");
return 2;
}
What is the output of this C code?
#include<stdio.h>
double foo();
int main(){
foo();
return 0;
}
foo(){
printf("2 ");
return 2;
}
A2
Bकम्पाइल टाइम एररCompile time error
Cकम्पाइलर पर निर्भरDepends on the compiler
Dमानक पर निर्भरDepends on the standard
सही उत्तर / Answer: (B)
हल / Solution
ऊपर घोषणा है double foo(); यानी foo को double लौटाने वाला कहा गया है। नीचे परिभाषा में प्रतिफल का प्रकार लिखा ही नहीं, सो वह int लौटाने वाला फ़ंक्शन बनता है। एक ही नाम के दो प्रकार आपस में टकराते हैं, इसलिए compiler «conflicting types» कहकर रुक जाता है। यह टकराव हर मानक में त्रुटि है, सो कम्पाइलर या मानक पर निर्भर वाले विकल्प ग़लत हैं। दो भी नहीं छपेगा, क्योंकि प्रोग्राम बनता ही नहीं।
The declaration at the top says double foo(); so foo is promised to return a double. The definition below gives no return type at all, which makes it return int instead. The two declarations of the same name therefore conflict, and the compiler stops with a conflicting types message. This clash is an error under every standard, so the choices that depend on the compiler or on the standard are wrong, and two is never printed.
प्रश्न 7उत्तर + हल
नीचे दिए गए कोड का आउटपुट क्या है?
#include<stdio.h>
void m(int k){
printf("hi");
}
void m(double k){
printf("hello");
}
void main(){
m(3);
}
The output of the code below is
#include<stdio.h>
void m(int k){
printf("hi");
}
void m(double k){
printf("hello");
}
void main(){
m(3);
}
Ahi
Bhello
Cकम्पाइल टाइम एररCompile time error
Dकुछ नहींNothing
सही उत्तर / Answer: (C)
हल / Solution
C में एक ही फ़ंक्शन का नाम दोबारा इस्तेमाल नहीं हो सकता, क्योंकि यहाँ फ़ंक्शन ओवरलोडिंग (function overloading) होती ही नहीं। कोड में m नाम दो बार परिभाषित है, एक बार int लेकर और एक बार double लेकर, सो compiler इसे पुनर्परिभाषा मानकर त्रुटि देता है। यही कोड C++ में चल जाता, क्योंकि वहाँ प्राचल के प्रकार से फ़ंक्शन छाँटे जाते हैं। प्रोग्राम बनता ही नहीं, इसलिए hi या hello छपने का सवाल नहीं।
A function name cannot be reused in C, because the language has no function overloading at all. Here m is defined twice, once taking an int and once taking a double, so the compiler treats the second one as a redefinition and reports an error. The very same code would work in C++, where functions are picked by the types of their parameters. Since the program never compiles, neither hi nor hello can ever be printed.
प्रश्न 8उत्तर + हल
नीचे दिए कोड का आउटपुट क्या होगा?
#include<stdio.h>
int *m();
void main(){
int *k = m();
printf("%d", k);
}
int *m(){
static int a[2] = {5, 8};
return a;
}
Find the output of this C code.
#include<stdio.h>
int *m();
void main(){
int *k = m();
printf("%d", k);
}
int *m(){
static int a[2] = {5, 8};
return a;
}
A5
B8
CपताAddress
Dगार्बेजGarbage
सही उत्तर / Answer: (C)
हल / Solution
m() का प्रतिफल प्रकार int* है, यानी वह पता लौटाता है। भीतर a स्थायी (static) ऐरे है, सो फ़ंक्शन ख़त्म होने पर भी वह ज़िंदा रहता है। ऐरे का नाम लौटाने पर उसके पहले element का पता मिलता है, और k उसी पते को रखता है। इसलिए छपने पर पता दिखेगा, पाँच या आठ नहीं। गार्बेज भी नहीं आता, क्योंकि static भंडारण मिटता ही नहीं। ध्यान रखिए, पते के लिए सही विनिर्देशक %p है।
The return type of m() is int*, so the function hands back an address. Inside it, a is a static array, which means it stays alive even after the function ends. Returning the name of an array gives the address of its first element, and k stores that address. Printing k therefore shows an address, not five and not eight, and not garbage either, because static storage is never destroyed. Note that the proper specifier for an address is %p.
प्रश्न 9उत्तर + हल
आउटपुट ज्ञात करें? #include<stdio.h>
int *m();
void main(){
int *k = m();
printf("%d", k[0]);
}
int *m(){
static int a[2] = {5, 8};
return a;
}
Find the output. #include<stdio.h>
int *m();
void main(){
int *k = m();
printf("%d", k[0]);
}
int *m(){
static int a[2] = {5, 8};
return a;
}
A5
B8
Cकुछ नहींNothing
Dगार्बेजGarbage
सही उत्तर / Answer: (A)
हल / Solution
m() के अंदर array a static है। static variable function ख़त्म होने के बाद भी memory में बना रहता है, इसलिए उसका address लौटाना सुरक्षित है। m() array a के पहले element का address लौटाता है, जो pointer k में आ जाता है। k[0] यानी a[0] की value 5 है, इसलिए 5 छपता है। 8 तो a[1] है। अगर array static न होता, तो local array का address लौटाने से garbage मिल सकता था।
Inside m(), the array a is static. A static variable stays in memory even after the function ends, so returning its address is safe. m() returns the address of the first element of a, which is stored in the pointer k. k[0] means a[0], whose value is 5, so 5 is printed. 8 is a[1]. If the array were not static, returning a local array's address could give garbage.
प्रश्न 10उत्तर + हल
निम्नलिखित फ़ंक्शन द्वारा लौटाया गया डेटा प्रकार क्या होगा? #include<stdio.h>
int func(){
return (double)(char)5.0;
}
What will be the data type returned for the following function? #include<stdio.h>
int func(){
return (double)(char)5.0;
}
Achar
Bint
Cdouble
Dreturn में कई बार type-casting करना अवैध हैmultiple type-casting in return is illegal
सही उत्तर / Answer: (B)
हल / Solution
function जो value लौटाता है, उसका type हमेशा function के return type से तय होता है। यहाँ 5.0 पहले char में बदला, फिर double में। पर return होते समय वह int में बदल जाता है, क्योंकि func को int घोषित किया गया है। एक के बाद एक कई cast लगाना पूरी तरह वैध है, इसलिए चौथा विकल्प ग़लत है। लौटाया गया type int है, char या double नहीं।
The type of the value a function returns is always set by the function's declared return type. Here 5.0 is first cast to char and then to double, but at the return it is converted to int, because func is declared to return int. Applying several casts one after another is perfectly legal in C, so option four is wrong. The returned type is int, not char or double.
आगे के 20 प्रश्नों के उत्तर और हल app मेंFliQi Question Bank — अभ्यास, हल और आपकी प्रगति