You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _data/quiz/questions.yml
+46-20Lines changed: 46 additions & 20 deletions
Original file line number
Diff line number
Diff line change
@@ -349,7 +349,7 @@
349
349
}
350
350
options:
351
351
- "Son"
352
-
- ""
352
+
- "Empty string"
353
353
- "MomSon"
354
354
- "DadMomSon"
355
355
correct: 2
@@ -887,24 +887,25 @@
887
887
- "Compilation error at Line B: variable budget is already defined"
888
888
- "Day2:2 Day2:2 Rome-Winter-7000-5 Rome "
889
889
correct: 1
890
-
explanation: "This question tests multiple aspects of lambda variable capture:
891
-
892
-
1. **Loop variable capture problem (Line A)**: The variable 'activity' is reassigned in each iteration ('Day1', then 'Day2'), making it NOT effectively final. However, 'localDay' maintains its value for each iteration. This is a classic lambda-in-loop bug - all lambdas capture the SAME 'activity' variable, which ends up with value 'Day2' when the lambdas execute.
893
-
894
-
2. **Instance and static variables**: 'destination' and 'season' are instance/static fields, not local variables, so they don't need to be effectively final. Lambdas access their current values at execution time, not capture time.
895
-
896
-
3. **Local variable shadowing (Line B)**: Inside the lambda, a new local variable 'budget' shadows the outer 'budget'. This is legal - the lambda creates its own scope.
897
-
898
-
4. **Effectively final rule**: The outer 'budget' is reassigned ('5000' → '6000'), but this doesn't affect the lambda at Line B since it declares its own 'budget'. The 'familySize' is incremented (4 → 5), making it not effectively final, but since we're reading it from the lambda (not declaring a new variable with the same name), we get a compilation error... EXCEPT we don't because 'familySize' is actually out of scope for the tripInfo lambda and would need to be referenced differently.
899
-
900
-
5. **Method reference (Line C)**: 'this::getDestination' captures 'this' reference and will call getDestination() at execution time, returning the current value of 'destination' field.
901
-
902
-
6. **Execution timing**: When forEach executes:
903
-
- First two lambdas both print 'Day2:2' (activity has final value 'Day2', localDay preserved)
904
-
- tripInfo prints 'London-Winter-7000-5' (destination changed to London, season is Winter, local budget is 7000, familySize is 5)
905
-
- Method reference prints 'London' (current value of destination)
906
-
907
-
Output: 'Day2:2 Day2:2 London-Winter-7000-5 London '"
890
+
explanation: |
891
+
This question tests multiple aspects of lambda variable capture:
892
+
893
+
Loop variable capture problem (Line A): The variable 'activity' is reassigned in each iteration ('Day1', then 'Day2'), making it NOT effectively final. However, 'localDay' maintains its value for each iteration. This is a classic lambda-in-loop bug - all lambdas capture the SAME 'activity' variable, which ends up with value 'Day2' when the lambdas execute.
894
+
895
+
Instance and static variables: 'destination' and 'season' are instance/static fields, not local variables, so they don't need to be effectively final. Lambdas access their current values at execution time, not capture time.
896
+
897
+
Local variable shadowing (Line B): Inside the lambda, a new local variable 'budget' shadows the outer 'budget'. This is legal - the lambda creates its own scope.
898
+
899
+
Effectively final rule: The outer 'budget' is reassigned ('5000' → '6000'), but this doesn't affect the lambda at Line B since it declares its own 'budget'. The 'familySize' is incremented (4 → 5), making it not effectively final, but since we're reading it from the lambda (not declaring a new variable with the same name), we get a compilation error... EXCEPT we don't because 'familySize' is actually out of scope for the tripInfo lambda and would need to be referenced differently.
900
+
901
+
Method reference (Line C): 'this::getDestination' captures 'this' reference and will call getDestination() at execution time, returning the current value of 'destination' field.
902
+
903
+
Execution timing: When forEach executes:
904
+
- First two lambdas both print 'Day2:2' (activity has final value 'Day2', localDay preserved)
905
+
- tripInfo prints 'London-Winter-7000-5' (destination changed to London, season is Winter, local budget is 7000, familySize is 5)
906
+
- Method reference prints 'London' (current value of destination)
907
+
908
+
Output: 'Day2:2 Day2:2 London-Winter-7000-5 London '
908
909
category: "Lambda Expressions"
909
910
910
911
- question: "Which of the following statements is invalid Java syntax for creating an array of Integers?"
@@ -951,4 +952,29 @@
951
952
- "It allows checked exceptions to be thrown during field assignment."
952
953
correct: 0
953
954
explanation: "A compact canonical constructor lets you validate or modify fields before they are assigned in a record."
954
-
category: "Java 21 Features"
955
+
category: "Java 21 Features"
956
+
957
+
- question: "What is the output of this family age comparison system?"
explanation: "Integer caching applies to values -128 to 127. dadAge and momAge both reference the same cached Integer object for 127, so == returns true. sonAge and daughterAge are different objects for 128 (outside cache range), so == returns false. However, equals() compares values, not references, so both equals() calls return true regardless of caching."
0 commit comments