Exam 1z0-808 All QuestionsBrowse all questions from this exam
Question 4

Given the code fragment:

What is the result?

    Correct Answer: C

    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.

Discussion
theCloudCTOOption: C

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.

rasiferOption: C

Sorry, answer is C....tested..

GyzmouOption: C

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); }

testOzOption: C

the answer is C. it is about casting but it gives alert at the compile time

SaftschnitzelOption: C

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.

SSJ5Option: C

Correct Answer is C. It fails to compile at line n2

Dibya17Option: C

Correct answer is C, as we cannot cast from long to string

SezamOption: C

Answer is C

NabilBenAskerOption: C

Compilation fail cannot cast from Long to String. we have to use String.valueOf(s2+s3) or change the variable s4 to Long

Mete5554Option: B

output = java: incompatible types: long cannot be converted to java.lang.String

iSnoverOption: C

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.

bakhdakOption: C

Answer is C . It is compile error, due to String is not Primitive .

Average_JoeOption: E

Answer is E

LUISGAROption: B

If it does not compiles it cannot throw an Exception

Varsha_vanshiOption: C

Answer is C

hackGhOption: C

c compile error

chainthechanOption: E

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)

Gyzmou

read message, compilation error not runtime error. cast exeption u can choose when it is runtime error. Now it is answer C