+ 2

Either the answer is wrong or I have been learning Java incorrectly.

static int i; static{i=0;} public static void main(String[] args) { System.out.print(i); i=1; } static{i=2;} Options are: No output 1 0 2 What I think the answer is: 2 What Sololearn's answer is: No output.

8th Feb 2018, 8:12 PM
Samay Sharma
9 Answers
+ 7
Your code doesn't run without the class in the playground. You can put junk in there and no compiler errors occur. That is why no output is seen. The playground failed to find a class and therefore main so it doesn't use your code. Or the error generated killed the process so once more no output. The question should be reported as invalid.
8th Feb 2018, 11:09 PM
John Wells
John Wells - avatar
+ 6
Based on what I've seen, it must be reported by multiple people to get the attention needed to be removed. Everything SoloLearn does is based on community votes or reports. There is some number of different people reports needed for the question to be removed.
8th Feb 2018, 11:43 PM
John Wells
John Wells - avatar
+ 3
On my end, I get 2. Do you have a link to your code? I don't mind taking a peek at it. WHAT I TESTED: public class Program { static int i; static{i=0;} public static void main(String[] args) { System.out.print(i); i=1; } static{i=2;} }
8th Feb 2018, 8:41 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 3
Ah okay, I see what you were doing then. Unfortunately, everything in Java is part of a class, even the main loop, so it's necessary. lol
8th Feb 2018, 8:53 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 2
Ooooh!!! Haha :D I get what's going on now. Yes, you're right in what you assumed the answer was. The online compiler is unfortunately limited, but for good reasoning; just a side effect of how it deals with security.
8th Feb 2018, 9:19 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 1
Exactly the answer should be 2. If you use eclipse, NetBeans, even cmd, you will always get 2. Because you cannot run any code without a class. Compiler needs a class. I am adding the code link which doesn't have a class defined, only the code. And Sololearn output's it as No output. https://code.sololearn.com/c4drJ2fJYUpe I saw this code in one of the challenges.
8th Feb 2018, 8:51 PM
Samay Sharma
+ 1
It's not me. I answered 2 in the challenge and it was wrong. I got furious, I went to eclipse wrote the whole code and I got 2. I WAS CORRECT. So to understand why Sololearn allowed the wrong answer in the challenge, I wrote the same code in the code playground and removed the class definition and it gave the result as No output. MY MIND WAS BLOWN. I just wrote a code in Java without a class. Obviously it's a bug from their side, and I hope they remove it before other people answer it "incorrectly" because of the bug.
8th Feb 2018, 8:59 PM
Samay Sharma
+ 1
I did twice in the last week but this is third time I am seeing this question. Idk how much time they need to update the questions.
8th Feb 2018, 11:24 PM
Samay Sharma
+ 1
Overall, I believe their main focus is getting the new content out to everyone, and a question on the quiz is lower priority, but they'll get to it eventually. From what I hear, they're in the process of adding a lot of new courses and adding a lot of advanced content for the current courses. I haven't seen it yet, but from what I'm told, there are some really great updates on the way.
9th Feb 2018, 2:19 PM
Fata1 Err0r
Fata1 Err0r - avatar