पहले 10 प्रश्नों के उत्तर और हल यहीं खुले हैं; बाक़ी 20 के उत्तर और हल FliQi Question Bank app में।
भाषा
प्रश्न 1उत्तर + हल
नीचे दिए गए प्रोग्राम का आउटपुट बताइए।
#include <stdio.h>
#define EVEN 0
#define ODD 1
int main()
{
int i = 3;
switch (i & 1)
{
case EVEN: printf("Even");
break;
case ODD: printf("Odd");
break;
default: printf("Default");
}
return 0;
}
Predict the output of the below program.
#include <stdio.h>
#define EVEN 0
#define ODD 1
int main()
{
int i = 3;
switch (i & 1)
{
case EVEN: printf("Even");
break;
case ODD: printf("Odd");
break;
default: printf("Default");
}
return 0;
}
AEven
BOdd
CDefault
DCompile-time error
सही उत्तर / Answer: (B)
हल / Solution
i का मान 3 है और यहाँ बिटवार AND (bitwise AND) लगाया गया है। 3 का द्विआधारी रूप 11 है और 1 का 01; दोनों का AND 01 यानी 1 निकलता है। #define से ODD का अर्थ 1 है, इसलिए switch उसी लेबल पर पहुँचता है, Odd छापता है और break से बाहर आ जाता है। यह तरकीब सम-विषम जाँचने का तेज़ तरीक़ा है — आख़िरी बिट 1 हुआ तो संख्या विषम। इसलिए Even या Default नहीं आता।
The value of i is 3, and a bitwise AND is applied here. In binary 3 is 11 and 1 is 01, so their AND gives 01, that is 1. The #define makes ODD stand for 1, so the switch reaches that label, prints Odd and leaves through break. This trick is a fast way of testing whether a number is odd.
प्रश्न 2उत्तर + हल
नीचे दिए गए प्रोग्राम का आउटपुट क्या है?
#include <stdio.h>
int main()
{
int i = 0;
switch (i)
{
case '0': printf("FliQi");
break;
case '1': printf("India");
break;
default: printf("FliQi India");
}
return 0;
}
What is the output of the below program?
#include <stdio.h>
int main()
{
int i = 0;
switch (i)
{
case '0': printf("FliQi");
break;
case '1': printf("India");
break;
default: printf("FliQi India");
}
return 0;
}
AFliQi
BIndia
CFliQi India
DCompile-time error
सही उत्तर / Answer: (C)
हल / Solution
लेबल में लिखे अक्षर कैरेक्टर स्थिरांक (character constant) हैं, और C उन्हें उनके ASCII मान से लेता है — यानी 48 और 49। switch में i का मान शून्य है, जो न 48 से मिलता है और न 49 से। कोई case न मिलने पर नियंत्रण default में जाता है, इसलिए FliQi India छपता है। अकेला FliQi पाने के लिए लेबल case 0 होता या i में वह अक्षर रखा जाता। कोड वैध है, सो एरर नहीं आता।
The characters written in the labels are character constants, and C takes them by their ASCII values, that is 48 and 49. In the switch the value of i is zero, which matches neither 48 nor 49. When no case label matches, control goes to the default label, so FliQi India is printed. To get FliQi alone the label would have to be case 0.
प्रश्न 3उत्तर + हल
निम्नलिखित C प्रोग्राम का आउटपुट क्या होगा?
#include <stdio.h>
int main(){
int a = 0;
switch(a)
{
case 0:
printf("Delhi");
break;
case 3<5:
printf("Jaipur");
break;
case 5<3:
printf("Gurugram");
}
}
What will be the output of the following C program?
#include <stdio.h>
int main(){
int a = 0;
switch(a)
{
case 0:
printf("Delhi");
break;
case 3<5:
printf("Jaipur");
break;
case 5<3:
printf("Gurugram");
}
}
ADelhi
BDelhi Gurugram
CJaipur
DCompile Time Error
सही उत्तर / Answer: (D)
हल / Solution
case के आगे स्थिरांक अभिव्यक्ति (constant expression) चल जाती है, इसलिए तुलना वाले लेबल लिखना अपने आप में ग़लत नहीं — उनका मान संकलन के समय ही 1 और शून्य निकल आता है। असली चूक यह है कि तीसरे लेबल से बना शून्य पहले वाले case 0 से टकरा जाता है, और एक ही switch में दो case की क़ीमत एक नहीं हो सकती। डुप्लिकेट case मिलते ही संकलक (compiler) रुक जाता है, सो उत्तर कम्पाइल टाइम एरर है।
A case label may use any constant expression, so labels written with a comparison are not wrong in themselves; their values are worked out at compile time as 1 and zero. The real fault is that the zero produced by the third label clashes with the earlier case 0, and two case labels in the same switch can never carry the same value. On finding a duplicate case the compiler stops, so the answer is a compile time error.
प्रश्न 4उत्तर + हल
निम्नलिखित C प्रोग्राम का आउटपुट क्या होगा?
#include <stdio.h>
int main(){
int a = 65;
switch(a)
{
case 'A':
printf("FliQi");
break;
case 65:
printf("FliQi India");
break;
default:
printf("FliQi Education");
}
}
What will be the output of following C Program?
#include <stdio.h>
int main(){
int a = 65;
switch(a)
{
case 'A':
printf("FliQi");
break;
case 65:
printf("FliQi India");
break;
default:
printf("FliQi Education");
}
}
AFliQi
BFliQi India
CFliQi Education
DCompile Time Error
सही उत्तर / Answer: (D)
हल / Solution
ASCII में 'A' का मान 65 है। इसलिए case 'A' और case 65 दोनों एक ही मान 65 बताते हैं। एक switch में दो case का मान एक जैसा नहीं हो सकता। इसलिए compiler duplicate case value error देता है और प्रोग्राम चलता नहीं। अगर इनमें से एक case हटा दिया जाए, तो a = 65 होने पर बचा हुआ case चलता। यहाँ FliQi, FliQi India या FliQi Education कुछ नहीं छपता।
In ASCII, 'A' has the value 65. So case 'A' and case 65 both stand for the same value, 65. Two cases in one switch cannot have the same value. So the compiler gives a duplicate case value error, and the program does not run. If one of these cases were removed, the remaining one would run for a = 65. Here none of FliQi, FliQi India or FliQi Education is printed.
प्रश्न 5उत्तर + हल
निम्नलिखित C प्रोग्राम का आउटपुट क्या होगा?
#include <stdio.h>
int main(){
char ch[] = "FliQi";
switch(ch)
{
case "FliQi":
printf("FliQi");
default:
printf("FliQi Education");
}
}
What will be the output of following C Program?
#include <stdio.h>
int main(){
char ch[] = "FliQi";
switch(ch)
{
case "FliQi":
printf("FliQi");
default:
printf("FliQi Education");
}
}
AFliQi
BFliQi Education
CFliQiFliQi Education
DCompile Time Error
सही उत्तर / Answer: (D)
हल / Solution
ch एक char array है, यानी एक string। switch सिर्फ़ integer मानों पर चलता है, और case label भी integer constant होना चाहिए। यहाँ switch(ch) और case "FliQi" दोनों string से जुड़े हैं, इसलिए compiler error देता है। प्रोग्राम compile ही नहीं होता, इसलिए FliQi या FliQi Education कुछ भी नहीं छपता। string की तुलना के लिए strcmp() के साथ if-else लगाना पड़ता है।
ch is a char array, which means a string. A switch works only on integer values, and a case label must also be an integer constant. Here both switch(ch) and case "FliQi" involve strings, so the compiler gives an error. The program never compiles, so neither FliQi nor FliQi Education is printed. To compare strings, you need strcmp() with if-else instead of a switch.
प्रश्न 6उत्तर + हल
निम्नलिखित C प्रोग्राम का आउटपुट क्या होगा?
#include <stdio.h>
int main(){
switch(printf("FliQi"))
{
case 20:
printf("FliQi 1");
break;
case 60:
printf("FliQi 2");
break;
default:
printf("INDIA");
break;
}
}
What will be the output of following C Program?
#include <stdio.h>
int main(){
switch(printf("FliQi"))
{
case 20:
printf("FliQi 1");
break;
case 60:
printf("FliQi 2");
break;
default:
printf("INDIA");
break;
}
}
AFliQi 1
BINDIA
CFliQi INDIA
DCompile Time Error
सही उत्तर / Answer: (C)
हल / Solution
printf() जितने अक्षर छापता है, उतनी संख्या लौटाता है। FliQi में 5 अक्षर हैं। इसलिए switch(printf("FliQi")) पहले FliQi छापता है और फिर switch(5) बनता है। 5 न case 20 से मेल खाता है, न case 60 से। इसलिए default चलता है और INDIA छपता है। पूरा आउटपुट FliQiINDIA है, जिसे विकल्प में FliQi INDIA लिखा है। सिर्फ़ INDIA ग़लत है, क्योंकि FliQi पहले छप चुका है।
printf() returns the number of characters it prints. FliQi has 5 characters. So switch(printf("FliQi")) first prints FliQi and then becomes switch(5). 5 matches neither case 20 nor case 60. So default runs and INDIA is printed. The full output is FliQiINDIA, shown in the option as FliQi INDIA. INDIA alone is wrong, because FliQi has already been printed. There is no error.
प्रश्न 7उत्तर + हल
निम्नलिखित C प्रोग्राम का आउटपुट क्या होगा?
#include <stdio.h>
int main(){
int a=10, b=20;
switch(a+b+30)
{
case 20:
printf("FliQi");
break;
case 60:
printf("BHARAT");
break;
default:
printf("INDIA");
break;
}
}
What will be the output of following C Program?
#include <stdio.h>
int main(){
int a=10, b=20;
switch(a+b+30)
{
case 20:
printf("FliQi");
break;
case 60:
printf("BHARAT");
break;
default:
printf("INDIA");
break;
}
}
AFliQI
BBHARAT
CINDIA
DCompile Time Error
सही उत्तर / Answer: (B)
हल / Solution
switch में expression चल सकता है, बशर्ते नतीजा integer हो। यहाँ a + b + 30 = 10 + 20 + 30 = 60 बनता है। इसलिए switch(60) चलता है। case 20 मेल नहीं खाता। case 60 मेल खाता है और BHARAT छपता है, फिर break से switch ख़त्म। default का INDIA नहीं छपता। expression वैध है, इसलिए compile error नहीं आता। FliQi तब छपता जब नतीजा 20 होता।
A switch can use an expression, as long as the result is an integer. Here a + b + 30 = 10 + 20 + 30 = 60. So the code runs switch(60). Case 20 does not match. Case 60 matches, BHARAT is printed, and the break ends the switch. The INDIA of default is not printed. The expression is valid, so there is no compile error. FliQi would appear only if the result were 20.
प्रश्न 8उत्तर + हल
निम्नलिखित C प्रोग्राम का आउटपुट क्या होगा?
#include <stdio.h>
int main(){
switch('A')
{
case 'A':
printf("FliQi");
break;
default:
printf("INDIA");
break;
}
}
What will be the output of following C Program?
#include <stdio.h>
int main(){
switch('A')
{
case 'A':
printf("FliQi");
break;
default:
printf("INDIA");
break;
}
}
AFliQi
BINDIA
CCompiler Error
DPrint Nothing
सही उत्तर / Answer: (A)
हल / Solution
switch('A') में character 'A' दिया गया है, जो अंदर से integer 65 है। character से switch करना पूरी तरह वैध है। case 'A' मेल खाता है, इसलिए FliQi छपता है। उसके बाद break है, इसलिए control switch से बाहर आ जाता है और default का INDIA नहीं छपता। कोई error नहीं है, और कुछ न छपने की भी कोई वजह नहीं है। इसलिए आउटपुट FliQi है।
switch('A') is given the character 'A', which is the integer 65 inside. A switch on a character is fully valid. Case 'A' matches, so FliQi is printed. A break follows, so control leaves the switch and the INDIA of default is not printed. There is no error, and there is no reason for nothing to be printed. So the output is FliQi.
प्रश्न 9उत्तर + हल
निम्नलिखित C प्रोग्राम का आउटपुट क्या होगा?
#include <stdio.h>
int main(){
switch("FliQi")
{
case "FliQi":
printf("FliQi");
default:
printf("INDIA");
break;
}
}
What will be the output of following C Program?
#include <stdio.h>
int main(){
switch("FliQi")
{
case "FliQi":
printf("FliQi");
default:
printf("INDIA");
break;
}
}
AFliQi
BFliQi INDIA
CINDIA
DCompile Time Error
सही उत्तर / Answer: (D)
हल / Solution
switch("FliQi") में एक string दी गई है, और case में भी string लिखी है। C में switch सिर्फ़ integer मानों पर चलता है, string पर नहीं। string असल में characters की array होती है, जिसका पता pointer के रूप में जाता है। इसलिए compiler error देता है। प्रोग्राम चलता नहीं, तो FliQi या INDIA नहीं छपता। string की तुलना के लिए strcmp() और if-else लगाते हैं।
switch("FliQi") is given a string, and the case label is also a string. In C, a switch works only on integer values, not on strings. A string is really an array of characters, passed as a pointer to its first character. So the compiler gives an error. The program does not run, so neither FliQi nor INDIA is printed. To compare strings, use strcmp() with if-else.
प्रश्न 10उत्तर + हल
निम्नलिखित C प्रोग्राम का आउटपुट क्या होगा?
#include <stdio.h>
int main(){
switch(5.3)
{
case 5:
printf("FliQi");
case 5.3:
printf("FliQi India");
break;
}
}
What will be the output of following C Program?
#include <stdio.h>
int main(){
switch(5.3)
{
case 5:
printf("FliQi");
case 5.3:
printf("FliQi India");
break;
}
}
AFliQi FliQi India
BFliQi India
CFliQi
DCompile Time Error
सही उत्तर / Answer: (D)
हल / Solution
switch(5.3) में 5.3 एक double मान है। switch का expression integer प्रकार का ही होना चाहिए। साथ ही case 5.3 भी दशमलव है, जबकि case label integer constant होना चाहिए। इन दोनों वजहों से compiler error देता है। प्रोग्राम compile नहीं होता, इसलिए FliQi या FliQi India कुछ भी नहीं छपता। दशमलव मान पर फ़ैसला करना हो तो if-else लगाना पड़ता है।
In switch(5.3), the value 5.3 is a double. The switch expression must be of an integer type. Also, case 5.3 is a decimal value, while a case label must be an integer constant. For both reasons the compiler gives an error. The program does not compile, so neither FliQi nor FliQi India is printed. To decide on decimal values, you must use if-else instead.
आगे के 20 प्रश्नों के उत्तर और हल app मेंFliQi Question Bank — अभ्यास, हल और आपकी प्रगति
Aहमें स्विच के अंदर सबसे आखिर में डिफॉल्ट स्टेटमेंट लिखना होगा।We must write the default statement at the last inside the switch.
Bएक स्विच में डिफॉल्ट स्टेटमेंट का उपयोग केवल एक बार किया जा सकता है।The default statement in a switch can be used only once.
Cहम किसी भी स्टेटमेंट को केस या डिफॉल्ट के बाहर लिखते हैं लेकिन कोई कंपाइल टाइम एरर नहीं होगा।we write any statement outside case or default but no compile time error will occur.
Dडिफ़ॉल्ट केस को एक्सीक्यूट किया जाएगा यदि और केवल अगर कोई केस मैच नहीं खाता है।Default case will be executed if and only if there is no case matched.
Aहम केस लेबल के रूप में केवल int और char कॉन्सटैंटऔर वेरिएबल ले सकते हैं।We can take only int and char constants and variables as case label.
Bडुप्लीकेट केस लेबल की अनुमति नहीं है अन्यथा हमें कंपाइल टाइम एरर मिलेगा।Duplicate case labels are not allowed otherwise we will get compile time error.
Cएक स्विच में डिफॉल्ट स्टेटमेंट का उपयोग केवल एक बार किया जा सकता है।The default statement in a switch can be used only once.
Dडिफ़ॉल्ट केस को एक्सीक्यूट किया जाएगा यदि और केवल अगर कोई केस मैच नहीं खाता है।Default case will be executed if and only if there is no case matched.
Aस्विच आर्गुमेंट के लिए अलाउड आर्गुमेंट प्रकार char, Short, int, long int हैं।The allowed argument types for the switch arguments are char, short, int, long int.
Bहम खाली स्विच लिख सकते हैं जिसमें कोई केस और डिफॉल्ट उपलब्ध नहीं है।We can write empty switch in which there is no case and default available.
Cप्रत्येक केस लेबल कॉन्सटैंट या कॉन्सटैंट एक्सप्रेशन होना चाहिए।Every case label should be constant or constant expression.
Dस्विच आर्गुमेंट एक एक्सप्रेशन नहीं हो सकता।switch arguments cannot be an expression.