Computer Applications
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);
}
}
Java Conditional Stmts
8 Likes
Answer
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);
}
}
Answered By
3 Likes
Related Questions
Find the error
for(count = 0, count < 5, count = count + 1) System.out.println("This is count:" + count); System.out.println("Done!");
Write a program that inputs a character and prints if the typed character is in uppercase or lowercase.
Find the error :
x = 3; y = 4; z = math.power(x*x, y/2);
Write a program that inputs a number and tests if the given number is a multiple of both 3 and 5.