Explanation: In first statment it will consider each element as a string because one element in string and no special format as in second statment. In second statement, it will give priority to expression which is in () hence first it will calculate 3+4 which is 7 and then appended to string.
If either (or both) of the operands of the + operator is a string, the other is automatically cast to a string. String concatenation and addition have the same precedence. Since they are left-associative, the operators are evaluated left-to-right. The parentheses in the second statement ensures that the second + operator performs addition instead of string concatenation.
Question : Given the code fragment: System.out.printIn("Result: " + 2 + 3 + 5); System.out.printIn("Result: " + 2 + 3 * 5); What is the result?
First line: System.out.println("Result: " + 2 + 3 + 5); String concatenation is produced.
Second line: System.out.println("Result: " + 2 + 3 * 5); 3*5 is calculated to 15 and is appended to string 2. Result 215. The output is: Result: 235 Result: 215
Note #1: To produce an arithmetic result, the following code would have to be used: System.out.println("Result: " + (2 + 3 + 5)); System.out.println("Result: " + (2 + 1 * 5)); run: Result: 10 Result: 7
Note #2: If the code was as follows: System.out.println("Result: " + 2 + 3 + 5"); System.out.println("Result: " + 2 + 1 * 5"); The compilation would fail. There is an unclosed string literal, 5", on each line.
Question : Given:
What is the result?
1. Base DerivedA 2. Base DerivedB 3. DerivedB DerivedB 4. DerivedB DerivedA 5. A classcast Except ion is thrown at runtime.
Correct Answer 3
Explanation: As both b1 and b4 point to same real instance of Class DerivedB . Hence, at runtime it will call method defined in DerivedB class. Casting with super class, does not change actual underline instance.
Which statement is true? 1. Both p and s are accessible by obj. 2. only s is accessible by obj. 3. Both r and s are accessible by obj. 4. p, r, and s are accessible by obj.