Given the code fragment:
What is the result?
Given the code fragment:
What is the result?
Compilation fails at line n2 due to an invalid cast. The code attempts to cast a long to a String, which is not permissible in Java. In detail, objects of primitives type like `long` cannot be directly cast to a non-primitive type like `String`. A suitable approach would be to use `String.valueOf()` method or concatenation with an empty string to convert the result into a String.
C. This is a casting issue but the code fails to compile so the answer is c. The ClassCastException is a runtime exception where you're trying to cast an object to a different object type, but here the code fails to compile because you cannot cast a long to a string.
Sorry, answer is C....tested..
C: $javac HelloWorld.java HelloWorld.java:7: error: incompatible types: long cannot be converted to String String s4 = (String) (s3*s2); ^ 1 error public static void main(String []args){ Short s1 = 200; Integer s2 = 400; Long s3 = (long) s1 + s2; String s4 = (String) (s3*s2); System.out.println("sum is "+s4); }
the answer is C. it is about casting but it gives alert at the compile time
E is incorrect, C is correct. The compiler recognizes that there is a class cast issue before the application is even run. This is important to remember for the exam as well, you have to be able to differentiate between compilation and RTE failures.
Correct Answer is C. It fails to compile at line n2
Correct answer is C, as we cannot cast from long to string
Answer is C
Compilation fail cannot cast from Long to String. we have to use String.valueOf(s2+s3) or change the variable s4 to Long
output = java: incompatible types: long cannot be converted to java.lang.String
The answer is the letter C, string is not primitive type to cast, so it's not a Class Cast exception but compilation failure on line n2.
Answer is C . It is compile error, due to String is not Primitive .
Answer is E
If it does not compiles it cannot throw an Exception
Answer is C
c compile error
Typed it wrong in IDE. Though the answer is still E. Exception in thread "main" java.lang.Error: Unresolved compilation problem: Cannot cast from long to String at Question4.main(Question4.java:6)
read message, compilation error not runtime error. cast exeption u can choose when it is runtime error. Now it is answer C