पहले 10 प्रश्नों के उत्तर और हल यहीं खुले हैं; बाक़ी 13 के उत्तर और हल FliQi Question Bank app में।
भाषा
प्रश्न 1उत्तर + हल
निम्नलिखित Java code snippet पर विचार कीजिए:
int i = 0;
while (i < 5) {
if (i == 3) {
break;
}
System.out.print(i + " ");
i++;
}
इस code का output क्या होगा?
Consider the following Java code snippet:
int i = 0;
while (i < 5) {
if (i == 3) {
break;
}
System.out.print(i + " ");
i++;
}
What will be the output of the code?
A0 1 2 3 4
B0 1 2 3
C0 1 2
D0 1 2 3 4 5
सही उत्तर / Answer: (C)
हल / Solution
i की शुरुआत 0 से होती है। i = 0, 1 और 2 पर condition i == 3 false है, इसलिए ये तीनों छपते हैं और हर बार i एक बढ़ता है। जब i = 3 होता है, तो print से पहले ही break चलता है। loop वहीं ख़त्म हो जाता है। इसलिए 3 और 4 कभी नहीं छपते। 5 तो वैसे भी i < 5 की वजह से नहीं आ सकता। इसलिए output 0 1 2 है।
i starts at 0. For i = 0, 1 and 2, the condition i == 3 is false, so these three values are printed and i grows by one each time. When i becomes 3, break runs before the print. The loop ends right there. So 3 and 4 are never printed. The value 5 could never appear anyway, because of the condition i < 5. So the output is 0 1 2.
प्रश्न 2उत्तर + हल
निम्नलिखित Java code snippet पर विचार कीजिए:
int sum = 0;
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
sum += i;
}
System.out.println(sum);
इस code का output क्या होगा?
Consider the following Java code snippet:
int sum = 0;
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
sum += i;
}
System.out.println(sum);
What will be the output of the code?
A9
B12
C11
D15
सही उत्तर / Answer: (B)
हल / Solution
sum की शुरुआत 0 से होती है और loop i = 1 से 5 तक चलता है। i = 3 पर continue चलता है, इसलिए उस बार sum += i छूट जाता है और 3 नहीं जुड़ता। बाक़ी संख्याएँ जुड़ती हैं: 1 + 2 + 4 + 5 = 12। अगर सारी संख्याएँ जुड़तीं तो 15 आता। 9 या 11 तब आते जब कोई और संख्या छोड़ी जाती। इसलिए output 12 है।
sum starts at 0 and the loop runs for i from 1 to 5. When i is 3, continue runs, so sum += i is skipped for that pass and 3 is not added. The other values are added: 1 + 2 + 4 + 5 = 12. If every number were added, the total would be 15. The values 9 or 11 would need some other number to be skipped. So the output is 12.
प्रश्न 3उत्तर + हल
निम्नलिखित Java code snippet पर विचार कीजिए:
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break;
}
System.out.print(i + " ");
}
इस code का output क्या होगा?
Consider the following Java code snippet:
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break;
}
System.out.print(i + " ");
}
What will be the output of the code?
A1 2 3 4 5
B1 2
C1 2 3
D1
सही उत्तर / Answer: (B)
हल / Solution
loop i = 1 से शुरू होता है। i = 1 और i = 2 पर condition i == 3 false है, इसलिए ये दोनों छपते हैं। i = 3 होते ही break चलता है और loop तुरंत पूरी तरह ख़त्म हो जाता है। break print से पहले है, इसलिए 3 नहीं छपता। loop 4 और 5 तक पहुँचता ही नहीं। अकेला 1 भी ग़लत है, क्योंकि 2 पर loop नहीं रुकता। इसलिए output 1 2 है।
The loop starts at i = 1. For i = 1 and i = 2, the condition i == 3 is false, so both values are printed. As soon as i becomes 3, break runs and the loop ends completely. The break comes before the print, so 3 is not printed. The loop never reaches 4 or 5. Printing only 1 is also wrong, because nothing stops the loop at 2. So the output is 1 2.
प्रश्न 4उत्तर + हल
निम्नलिखित Java code snippet पर विचार कीजिए:
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.print(i + " ");
}
इस code का output क्या होगा?
Consider the following Java code snippet:
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.print(i + " ");
}
What will be the output of the code?
A1 2 3 4 5
B1 2 3
C1 2 4 5
D1 2 4
सही उत्तर / Answer: (C)
हल / Solution
loop i = 1 से 5 तक चलता है। जब i = 3 होता है, तो continue चलता है। इससे उस बार का print छूट जाता है और loop सीधे i = 4 पर पहुँचता है। loop ख़त्म नहीं होता। इसलिए 1, 2, 4 और 5 छपते हैं। सारे पाँच अंक तब छपते जब continue न होता। 1 2 3 में continue को break समझने की भूल है, और 1 2 4 में आख़िरी 5 छूट गया है।
The loop runs for i from 1 to 5. When i is 3, continue runs. This skips the print for that pass only, and the loop moves straight to i = 4. The loop does not end. So 1, 2, 4 and 5 are printed. All five numbers would print only without continue. The option 1 2 3 confuses continue with break, and 1 2 4 wrongly drops the last value 5.
प्रश्न 5उत्तर + हल
निम्नलिखित Java code snippet पर विचार कीजिए:
int x = 5;
String result = "";
switch (x) {
case 1:
case 2:
case 3:
result = "Small";
break;
case 4:
case 5:
case 6:
result = "Medium";
break;
default:
result = "Large";
}
System.out.println(result);
इस code का output क्या होगा?
Consider the following Java code snippet:
int x = 5;
String result = "";
switch (x) {
case 1:
case 2:
case 3:
result = "Small";
break;
case 4:
case 5:
case 6:
result = "Medium";
break;
default:
result = "Large";
}
System.out.println(result);
What will be the output of the code?
ASmall
BMedium
CLarge
DCompilation Error
सही उत्तर / Answer: (B)
हल / Solution
x की value 5 है। case 4, 5 और 6 एक साथ समूह में लिखे हैं, इसलिए तीनों का code एक ही है। 5 मेल खाता है, इसलिए result = "Medium" होता है और break से switch से बाहर निकल जाते हैं। Small सिर्फ़ 1, 2 और 3 के लिए है। Large तब आता जब कोई case न मिलता। code पूरी तरह वैध है, इसलिए compile error भी नहीं आती। output Medium है।
The value of x is 5. Cases 4, 5 and 6 are grouped together, so they share the same code. Since 5 matches, result is set to "Medium" and break exits the switch. Small is only for 1, 2 and 3. Large would appear only if no case matched. The code is fully valid, so there is no compile error either. The output is Medium.
प्रश्न 6उत्तर + हल
निम्नलिखित Java code snippet पर विचार कीजिए:
char grade = 'B';
String remark;
switch (grade) {
case 'A':
case 'B':
remark = "Good";
break;
case 'C':
remark = "Average";
break;
default:
remark = "Poor";
}
System.out.println(remark);
इस code का output क्या होगा?
Consider the following Java code snippet:
char grade = 'B';
String remark;
switch (grade) {
case 'A':
case 'B':
remark = "Good";
break;
case 'C':
remark = "Average";
break;
default:
remark = "Poor";
}
System.out.println(remark);
What will be the output of the code?
AGood
BAverage
CPoor
DCompilation Error
सही उत्तर / Answer: (A)
हल / Solution
grade की value 'B' है। switch में case 'A' के नीचे कोई statement नहीं है, वह सीधे case 'B' से जुड़ा है। 'B' मेल खाता है, इसलिए remark = "Good" होता है और break से switch ख़त्म हो जाता है। remark बिना शुरुआती value के घोषित है, पर default समेत हर रास्ते में उसे value मिलती है। इसलिए compile error नहीं आती और Good छपता है।
The value of grade is 'B'. In the switch, case 'A' has no statements of its own and is grouped with case 'B'. Since 'B' matches, remark is set to "Good" and break ends the switch. remark is declared without a starting value, but every path, including default, assigns it. So there is no compile error, and the program prints Good.
प्रश्न 7उत्तर + हल
यदि किसी case block में break statement न लिखा जाए, तो क्या होता है?
What happens if the break statement is omitted in a case block?
Aprogram compilation error देता है।The program throws a compilation error.
Bexecution अगले case block में चलता रहता है।It continues to execute the next case block.
Cprogram runtime exception देता है।The program throws a runtime exception.
Dswitch statement चलना बंद कर देता है।The switch statement stops executing.
सही उत्तर / Answer: (B)
हल / Solution
switch में break न हो तो मेल खाने वाले case के बाद अगले case के statements भी चल जाते हैं। इसे fall-through कहते हैं। यह तब तक चलता है जब तक कोई break न मिले या switch ख़त्म न हो जाए। Java इसे पूरी तरह वैध मानता है, इसलिए न compile error आती है, न runtime exception। switch रुकता नहीं, बल्कि आगे बढ़ता रहता है। इसलिए विकल्प 2 सही है।
If a case has no break, the statements of the next case also run after the matching case. This is called fall-through. It continues until a break is found or the switch ends. Java treats this as fully valid, so there is no compile error and no runtime exception. The switch does not stop; it keeps moving forward into later cases. So option 2 is correct.
प्रश्न 8उत्तर + हल
Java में switch statement किस काम की सुविधा देता है?
What does the switch statement in Java allow?
Aकिसी collection पर iterate करनाIterating over a collection
Bcustom data types परिभाषित करनाDefining custom data types
Cकई code blocks में से किसी एक को चुनकर चलानाSelecting one of many code blocks to execute
switch एक selection statement है। यह एक expression की value देखता है और उसी से मेल खाने वाले case का code चलाता है। यानी कई संभव blocks में से एक चुना जाता है। किसी collection पर घूमना for-each जैसे loop का काम है। नया data type class, enum या interface से बनता है। गणनाएँ operators करते हैं, switch नहीं। इसलिए विकल्प 3 सही है।
The switch statement is a selection statement. It looks at the value of one expression and runs the code of the matching case. In other words, one block is chosen out of many possible blocks. Walking through a collection is the job of a loop such as for-each. New data types are made with a class, enum or interface. Calculations are done by operators, not by switch.
प्रश्न 9उत्तर + हल
निम्नलिखित code snippet का output क्या होगा?
int x = 10, y = 20;
if (x > y)
System.out.println("x is greater than y");
else if (x < y)
System.out.println("x is less than y");
else
System.out.println("x is equal to y");
What is the output of the following code snippet?
int x = 10, y = 20;
if (x > y)
System.out.println("x is greater than y");
else if (x < y)
System.out.println("x is less than y");
else
System.out.println("x is equal to y");
Ax is greater than y
Bx is less than y
Cx is equal to y
Dकोई output नहीं, क्योंकि syntax error हैNo output, as there is a syntax error
सही उत्तर / Answer: (B)
हल / Solution
यहाँ x = 10 और y = 20 है। पहली condition x > y यानी 10 > 20 false है, इसलिए पहला संदेश नहीं छपता। फिर else if में x < y यानी 10 < 20 true है। इसलिए "x is less than y" छपता है और आख़िरी else छोड़ दिया जाता है। braces न होने से कोई दिक़्क़त नहीं, क्योंकि हर शाखा में एक ही statement है। इसलिए syntax error वाला विकल्प ग़लत है।
Here x = 10 and y = 20. The first condition x > y, that is 10 > 20, is false, so the first message is not printed. Next, the else if checks x < y, that is 10 < 20, which is true. So "x is less than y" is printed and the last else is skipped. Missing braces cause no problem, because each branch has one statement. So the syntax error option is wrong.
प्रश्न 10उत्तर + हल
निम्नलिखित code snippet का output क्या होगा?
int num = 0;
if (num > 0)
System.out.println("Positive");
else if (num < 0)
System.out.println("Negative");
else
System.out.println("Zero");
What is the output of the following code snippet?
int num = 0;
if (num > 0)
System.out.println("Positive");
else if (num < 0)
System.out.println("Negative");
else
System.out.println("Zero");
APositive
BNegative
CZero
Dकोई output नहीं, क्योंकि syntax error हैNo output, as there is a syntax error
सही उत्तर / Answer: (C)
हल / Solution
num की value 0 है। पहली condition num > 0 false है, क्योंकि 0 धनात्मक नहीं है। दूसरी condition num < 0 भी false है, क्योंकि 0 ऋणात्मक भी नहीं है। जब दोनों false हों, तो अंत वाला else चलता है और "Zero" छपता है। यहाँ braces नहीं लगे हैं, पर हर शाखा में एक ही statement है, इसलिए syntax सही है। error वाला विकल्प ग़लत है।
The value of num is 0. The first condition num > 0 is false, because 0 is not positive. The second condition num < 0 is also false, because 0 is not negative. When both are false, the final else runs and prints "Zero". There are no braces here, but each branch has only one statement, so the syntax is valid. The error option is therefore wrong.
आगे के 13 प्रश्नों के उत्तर और हल app मेंFliQi Question Bank — अभ्यास, हल और आपकी प्रगति
switch statement में "default" case क्या दर्शाता है?
What does the "default" case represent in a "switch" statement?
Aयह किसी variable की default value दर्शाता है।It represents the default value of a variable.
Bयह switch statement का आख़िरी case दर्शाता है।It represents the last case in the switch statement.
Cयह वह case है जो तब चलता है जब बाक़ी कोई भी case expression से मेल न खाए।It represents a case that is executed when none of the other cases match the expression.
Dयह एक reserved keyword है जिसका कोई ख़ास काम नहीं है।It represents a reserved keyword and has no specific functionality.