• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

casting question from Java cert mock exam

 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I came across this question on Java cert Mock exam
from the link https://www.java8certificationquestions.com/ question number 18 of free java 8 mock test.



A. compilation fails due to error at line 5
B. compilation fails due to multiple errors.

I thought it was B as line 5 doesnt compile and i is also not declared in line 6. But the answer specified is A. any thoughts.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya Subraamanian wrote:I thought it was B as line 5 doesnt compile and i is also not declared in line 6. But the answer specified is A. any thoughts.

You are 100% correct! The code snippet as-is has two compiler errors:
1/ on line5 you try to cast incompatible types (int and String) which is (of course) not allowed.
2/ on line6 you try to print a variable i which is not defined at all and again that's not allowed.

Probably line6 contains a typo and has to be and then the provided correct answer is actually correct So I would contact them and report this errata item (and you can of course refer to this topic).

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic