FliQi Education App Download
प्रश्न-बैंक · Programmer Java

Flow control — प्रश्न और उत्तर

Programmer Java · 23 जाँचे हुए (verified) प्रश्न

  • 23 जाँचे हुए प्रश्न
  • हिंदी + English

पहले 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?
  1. A0 1 2 3 4
  2. B0 1 2 3
  3. C0 1 2
  4. 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?
  1. A9
  2. B12
  3. C11
  4. 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?
  1. A1 2 3 4 5
  2. B1 2
  3. C1 2 3
  4. 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?
  1. A1 2 3 4 5
  2. B1 2 3
  3. C1 2 4 5
  4. 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?
  1. ASmall
  2. BMedium
  3. CLarge
  4. 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?
  1. AGood
  2. BAverage
  3. CPoor
  4. 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?
  1. Aprogram compilation error देता है।The program throws a compilation error.
  2. Bexecution अगले case block में चलता रहता है।It continues to execute the next case block.
  3. Cprogram runtime exception देता है।The program throws a runtime exception.
  4. 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?
  1. Aकिसी collection पर iterate करनाIterating over a collection
  2. Bcustom data types परिभाषित करनाDefining custom data types
  3. Cकई code blocks में से किसी एक को चुनकर चलानाSelecting one of many code blocks to execute
  4. Dअंकगणितीय गणनाएँ करनाPerforming arithmetic calculations

सही उत्तर / Answer: (C)

हल / Solution
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");
  1. Ax is greater than y
  2. Bx is less than y
  3. Cx is equal to y
  4. 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");
  1. APositive
  2. BNegative
  3. CZero
  4. 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 — अभ्यास, हल और आपकी प्रगति
App खोलिए
प्रश्न 11उत्तर app में
निम्नलिखित code snippet चलाने के बाद "result" की value क्या होगी?

int x = 20, y = 10;

int result;

if (x > y) {

    result = x - y;

} else {

    result = y - x;

}
What is the value of "result" after executing the following code snippet?

int x = 20, y = 10;

int result;

if (x > y) {

    result = x - y;

} else {

    result = y - x;

}
  1. A10
  2. B20
  3. C30
  4. D-10
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 12उत्तर app में
निम्नलिखित code snippet का output क्या होगा?

int a = 5, b = 10;

if (a > b) {

    System.out.println("a is greater than b");

} else if (a < b) {

    System.out.println("a is less than b");

} else {

    System.out.println("a is equal to b");

}
What is the output of the following code snippet?

int a = 5, b = 10;

if (a > b) {

    System.out.println("a is greater than b");

} else if (a < b) {

    System.out.println("a is less than b");

} else {

    System.out.println("a is equal to b");

}
  1. Aa is greater than b
  2. Ba is less than b
  3. Ca is equal to b
  4. Dकोई output नहीं, क्योंकि syntax error हैNo output, as there is a syntax error
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 13उत्तर app में
जब "if-else if-else" ladder की कोई भी condition true न हो, तब कौन-सा statement चलता है?
Which statement is executed when none of the conditions in an "if-else if-else" ladder is true?
  1. Aपहला "if" blockThe first "if" block
  2. Bआख़िरी "else" blockThe last "else" block
  3. Cprogram समाप्त हो जाता हैThe program terminates
  4. Dएक error फेंकी जाती हैAn error is thrown
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 14उत्तर app में
Java में "if" statement का उद्देश्य क्या है?
What is the purpose of the "if" statement in Java?
  1. Acondition true होने पर code का एक block चलानाTo execute a block of code if a condition is true
  2. Bcode का एक block बार-बार चलानाTo execute a block of code repeatedly
  3. Cकोई method परिभाषित करनाTo define a method
  4. Dexceptions को संभालनाTo handle exceptions
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 15उत्तर app में
Programming में loop का मुख्य उद्देश्य क्या है?
What is the primary purpose of a loop in programming?
  1. Aएक ही operation को बार-बार करनाTo perform a single operation repeatedly
  2. Bcode को क्रम से (sequentially) चलानाTo execute code sequentially
  3. Cprogram के अलग-अलग हिस्सों में branch करनाTo branch to different parts of the program
  4. Dprogram का execution समाप्त करनाTo terminate the program execution
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 16उत्तर app में
निम्नलिखित code snippet का output क्या होगा?

int x = 10; 

while (x > 0){ 

System.out.print(x + " "); 

x -= 2; 

}
What is the output of the following code snippet?

int x = 10; 

while (x > 0){ 

System.out.print(x + " "); 

x -= 2; 

}
  1. A10 8 6 4 2
  2. B10 8 6 4 2 0
  3. C10 8 6 4 2 -2
  4. Dयह code अनंत loop (infinite loop) बनाता है।This code causes an infinite loop.
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 17उत्तर app में
switch statement में "default" case क्या दर्शाता है?
What does the "default" case represent in a "switch" statement?
  1. Aयह किसी variable की default value दर्शाता है।It represents the default value of a variable.
  2. Bयह switch statement का आख़िरी case दर्शाता है।It represents the last case in the switch statement.
  3. Cयह वह case है जो तब चलता है जब बाक़ी कोई भी case expression से मेल न खाए।It represents a case that is executed when none of the other cases match the expression.
  4. Dयह एक reserved keyword है जिसका कोई ख़ास काम नहीं है।It represents a reserved keyword and has no specific functionality.
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 18उत्तर app में
यदि switch statement में कोई भी case value से मेल न खाए और "default" case भी न हो, तो क्या होता है?
What happens if no case matches the value in a "switch" statement and there's no "default" case?
  1. Aएक error फेंकी जाती है।An error is thrown.
  2. Bprogram बंद हो जाता है।The program exits.
  3. Ccontrol switch के बाद वाले statement पर चला जाता है।Control transfers to the next statement after the switch.
  4. Dprogram अनिश्चित काल तक अटक जाता है।The program hangs indefinitely.
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 20उत्तर app में
Java में "do-while" loop किस बात की गारंटी देता है?
What does the "do-while" loop guarantee in Java?
  1. Aloop body कम से कम एक बार चलती है।It executes the loop body at least once.
  2. Bloop body शून्य या अधिक बार चलती है।It executes the loop body zero or more times.
  3. Cloop body ठीक एक बार चलती है।It executes the loop body exactly once.
  4. Dloop body तब तक चलती है जब तक कोई शर्त पूरी न हो जाए।It executes the loop body until a condition is met.
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 21उत्तर app में
Java में "continue" statement का उद्देश्य क्या है?
What is the purpose of the "continue" statement in Java?
  1. Aloop को समाप्त करनाTo terminate the loop
  2. Bmethod से बाहर निकलनाTo exit the method
  3. Cloop का बचा हिस्सा छोड़कर अगले iteration पर जानाTo skip the rest of the loop and move to the next iteration
  4. Dकोई value लौटानाTo return a value
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
प्रश्न 22उत्तर app में
switch statement में "break" keyword का क्या काम है?
In a "switch" statement, what is the purpose of the "break" keyword?
  1. Aloop से बाहर निकलनाTo exit the loop
  2. Bअगले case पर जानाTo continue to the next case
  3. Cकोई value लौटानाTo return a value
  4. Dswitch statement से बाहर निकलनाTo exit the switch statement
उत्तर और हल FliQi Question Bank app में देखें (Answer & solution in the app)
Flow control के सभी 23 प्रश्न — उत्तर, हल और अभ्यास

FliQi Question Bank app में topic-वार अभ्यास, कठिनाई-स्तर और आपकी प्रगति।

  • पहला test मुफ़्त
  • हिंदी + English
  • जाँचे हुए उत्तर और हल

यह topic किन परीक्षाओं में आता है

Programmer Java के दूसरे topics

सभी topics