What is output by the following code?
Choose all that apply
1: public class Main { 2: public static void main(String[] args) { 3: int numMain = 4; 4: String fishType = "tuna"; 5: String anotherMain = numMain + 1; 6: System.out.println(anotherMain + " " + fishType); 7: System.out.println(numMain + " " + 1); 8: } //from w w w . j av a 2s.com 9:} A. 4 1 B. 41 C. 5 D. 5 tuna E. 5tuna F. 51tuna G. The code does not compile.
G.
Line 5 does not compile.
This question is checking to see if you are paying attention to the types.
numMain
is an int and 1 is an int.
Therefore, we use numeric addition and get 5.
The problem is that we can't store an int in a String variable.
Supposing line 5 said String anotherMain
= numMain
+ 1 + "";.
In that case, the answer would be options A and D.
The variable defined on line 5 would be the string "5", and both output statements would use concatenation.