tg-me.com/topJavaQuizExplain/21
Create:
Last Update:
Last Update:
What does the following code output?
1: public class Salmon {
2: int count;
3: public void Salmon() {
4: count = 4;
5: }
6: public static void main(String[] args) {
7: Salmon s = new Salmon();
8: System.out.println(s.count);
9: } }
✅ A. 0
❌ B. 4
❌ C. Compilation fails on line 3.
❌ D. Compilation fails on line 4.
❌ E. Compilation fails on line 7.
❌ F. Compilation fails on line 8.
Explanation:
While the code on line 3 does compile, it is not a constructor because it has a return type. It is a method that happens to have the same name as the class. When the code runs, the default constructor is called and count has the default value (0) for an int.
BY Explanations “Top Java Quiz Questions”
Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283
Share with your friend now:
tg-me.com/topJavaQuizExplain/21