ICSE Class 10 Computer Applications
Question 58 of 76
Revising Basic Java Concepts — Question 62
Back to all questions 62
Question Question 39
Find the error:
class Test {
public static void main (String args[]) {
int x = 10;
if(x == 10) {
int y = 20;
System.out.println("x and y: "+ x +" "+ y);
x = y * 2;
}
y = 100;
System.out.println("x is"+ x);
}
}The variable y is declared inside 'if' block. Its scope is limited to if block and it cannot be used outside the block.
The correct code snippet is as follows:
class Test {
public static void main (String args[]) {
int x = 10;
int y;
if(x == 10) {
y = 20;
System.out.println("x and y: "+ x +" "+ y);
x = y * 2;
}
y = 100;
System.out.println("x is"+ x);
}
}