• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

What is the correct answer to this question?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I am preparing for the Java11 1Z0-817 exam.
I am wondering if I can encounter question like this one:

List list = List.of("Many", "thanks", "for","the","help");
list.stream().forEach(s -> s.toLowerCase()); // Compilation fails here

The IDE says that this code doesn't compile as List is declared as a List<Object> (no type specified)

If I find code like that, can I answer that the Compilation fails or I have to check the other answers?
Have you already seen code like this on the real exam?

Thanks.
 
Marshal
Posts: 70654
288
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please tell us all the answers suggested. If the code doesn't compile, then that is the correct answer. Answers like, “prints nothing,” or, “throws an exception,” are therefore implicitly wrong. But in order to pass that sort of question in the real exam, you will have to work out that it won't compile without assistance from your IDE. I think you will pobably find the List#of() documentation helpful.
 
Adriano Cosimati
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the welcome and response.

Later on, in the example code, the Stream elements are printed,
so the suggested response is: code prints "Many thanks for the help".

But it's a practice test, so I think that the test is wrong;

List should be List<String> to make the code successfully compile.












 
Campbell Ritchie
Marshal
Posts: 70654
288
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Adriano Cosimati wrote:. . . I think that the test is wrong;

List should be List<String> . . .

You are right:

jshell
List list = List.of("Many", "thanks", "for","the","help");
list.stream().forEach(s -> s.toLowerCase()); // Compilation fails here
|  Welcome to JShell -- Version 14.0.1
|  For an introduction type: /help intro

jshell> List list = List.of("Many", "thanks", "for","the","help");
list ==> [Many, thanks, for, the, help]

jshell> list.stream().forEach(s -> s.toLowerCase()); // Compilation fails here
|  Warning:
|  unchecked call to forEach(java.util.function.Consumer<? super T>) as a member of the raw type java.util.stream.Stream
|  list.stream().forEach(s -> s.toLowerCase()); // Compilation fails here
|  ^-----------------------------------------^
|  Error:
|  cannot find symbol
|    symbol:   method toLowerCase()
|  list.stream().forEach(s -> s.toLowerCase()); // Compilation fails here
|                             ^-----------^

Please verify that you have copied the question exactly; that code will not print, “Many thanks for the help”.
 
author & internet detective
Posts: 40239
819
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What book or mock exam is that from? And what chapter # or exam #?



needs to be



or



 
Jeanne Boyarsky
author & internet detective
Posts: 40239
819
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interestingly, it is the same root cause as this question - when using methods/operators on a type, a generic (or var) needs to be used in the decoration.
 
Adriano Cosimati
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:Interestingly, it is the same root cause as this question - when using methods/operators on a type, a generic (or var) needs to be used in the decoration.



in fact, it's the same mock test  
 
Jeanne Boyarsky
author & internet detective
Posts: 40239
819
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Adriano Cosimati wrote:

Jeanne Boyarsky wrote:Interestingly, it is the same root cause as this question - when using methods/operators on a type, a generic (or var) needs to be used in the decoration.



in fact, it's the same mock test  


Hey, we were consistent!
 
Put a gun against his head, pulled my trigger, now he's dead, that tiny ad sure bled
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic