Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817
Moderator: admin
-
teodorj
- Posts: 28
- Joined: Tue Jun 19, 2018 10:27 am
-
Contact:
Post
by teodorj »
Given the code...
Code: Select all
Stream<Integer> sin = Stream.of(1, 2, 3 );
Consumer<Integer> c1 = System.out::print;
Consumer<Integer> c2 = x->{ System.out.println(" * "+number+" = "+x*number); };
INSERT CODE HERE
This will actually not compile because unknown
number variable
The given variable must be changed to x like below :
Code: Select all
public static void main(String[] args) throws IOException {
Stream<Integer> sin = Stream.of(1, 2, 3 );
Consumer<Integer> c1 = System.out::print;
Consumer<Integer> c2 = x->{ System.out.println(" * "+x+" = "+x*x); };
sin.forEach(c1.andThen(c2));
}
-
admin
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
-
Contact:
Post
by admin »
Tried it just now. Works fine!
-
teodorj
- Posts: 28
- Joined: Tue Jun 19, 2018 10:27 am
-
Contact:
Post
by teodorj »
From where did Consumer body reference the "number" variable?
-
admin
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
-
Contact:
Post
by admin »
It is the method parameter. You have to try the code exactly as given in the question:
public static void generateMultiplicationTable(int number){
Stream<Integer> sin = Stream.of(1, 2, 3 );
Consumer<Integer> c1 = System.out::print;
Consumer<Integer> c2 = x->{ System.out.println(" * "+number+" = "+x*number); };
INSERT CODE HERE
}
public static void main(String[] args) throws Exception{
generateMultiplicationTable(2);
}
-
teodorj
- Posts: 28
- Joined: Tue Jun 19, 2018 10:27 am
-
Contact:
Post
by teodorj »
I see. I wrap the code in main method and missed the variable
Thanks for the confirmation

Users browsing this forum: No registered users and 3 guests