Here am going to write a question as a puzzle, it is also very similar to the those questions you find in the SCJP (Sun Certified Java Programmer) examination
public class LittleFuzzy {
public int method(int x) {
int val = x;
try {
return val;
} finally {
val = x + x;
}
}
public static void main(String[] args) {
LittleFuzzy littleFuzzy = new LittleFuzzy();
System.out.println(littleFuzzy.method(10));
}
}
1Review the program clearly and think what would be the output.
If I change this program a little bit then is output will change or not ?
public class LittleFuzzy {
private int val;
public int method(int x) {
val = x;
try {
return val;
} finally {
val = x + x;
}
}
public static void main(String[] args) {
LittleFuzzy littleFuzzy = new LittleFuzzy();
System.out.println(littleFuzzy.method(10));
}
}
The output will not change it will be the same. Because when you use the return key word it return the value at that time and it ignores the rest of the part of a method. Therefore the output will be 10 in both the cases.
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: End of Year Puzzle
Post your Comment