Skip to content

Commit 59f1d63

Browse files
author
Anass Rach
committed
Update questions.yml!
1 parent e1f3bc6 commit 59f1d63

1 file changed

Lines changed: 46 additions & 20 deletions

File tree

_data/quiz/questions.yml

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@
349349
}
350350
options:
351351
- "Son"
352-
- ""
352+
- "Empty string"
353353
- "MomSon"
354354
- "DadMomSon"
355355
correct: 2
@@ -887,24 +887,25 @@
887887
- "Compilation error at Line B: variable budget is already defined"
888888
- "Day2:2 Day2:2 Rome-Winter-7000-5 Rome "
889889
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 '
908909
category: "Lambda Expressions"
909910

910911
- question: "Which of the following statements is invalid Java syntax for creating an array of Integers?"
@@ -951,4 +952,29 @@
951952
- "It allows checked exceptions to be thrown during field assignment."
952953
correct: 0
953954
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?"
958+
code: |
959+
class FamilyAges {
960+
public static void main(String[] args) {
961+
Integer dadAge = 127;
962+
Integer momAge = 127;
963+
Integer sonAge = 128;
964+
Integer daughterAge = 128;
965+
966+
System.out.print((dadAge == momAge) + " ");
967+
System.out.print((sonAge == daughterAge) + " ");
968+
System.out.print(dadAge.equals(momAge) + " ");
969+
System.out.print(sonAge.equals(daughterAge) + " ");
970+
}
971+
}
972+
options:
973+
- "true false true true "
974+
- "false false true true "
975+
- "true true true true "
976+
- "false true false true "
977+
- "true false false false "
978+
correct: 0
979+
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."
980+
category: "Wrapper Classes"

0 commit comments

Comments
 (0)