Prashnikaप्रश्निका
Code
About
‹ Back to the paper

Question 1

1 mark

Computer ScienceISC Computer Science - Competency Focused Practice Questions (CISCE, August 2024)MCQStatements, Scope

Choose the correct option.

For the given code snippet: int i; for( i = 5; i>=1; i--) System.out.println( )…

For the given code snippet:
int i;
for( i = 5; i>=1; i--)
    System.out.println( );
System.out.println(i);
  • (a)Zero will be displayed as variable i has local scope with respect to the loop.
  • (b)Zero will be displayed as variable i has global scope with respect to the loop.
  • (c)Error – Undeclared variable i.
  • (d)Output will be 1.

Answer

Answer

Correct option: b

Answer: (b) Zero will be displayed as variable i has global scope with respect to the loop. The variable i is declared outside the for statement (int i; then for(i=5;...)), so its scope is the enclosing method, not just the loop body. After the loop finishes, i has been decremented to 0, and since i remains in scope, System.out.println(i) prints 0.