Skip to content

Commit d0d74ed

Browse files
author
Anass Rach
committed
Adding operator question!
1 parent 66cb158 commit d0d74ed

1 file changed

Lines changed: 33 additions & 1 deletion

File tree

_data/quiz/questions.yml

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,4 +626,36 @@
626626
- "Runtime exception"
627627
correct: 3
628628
explanation: "This program demonstrates a ReentrantLock bug that causes a deadlock. When the first thread calls dinnerLock.lock() at f1, it acquires the lock. Then at f2, dinnerLock.tryLock() succeeds because ReentrantLock allows the same thread to acquire the lock multiple times (reentrant behavior). So 'Eating dinner!' prints once. However, the thread acquired the lock twice (once at f1, once at f2) but only calls unlock() once. This means the lock is never fully released - it still has a hold count of 1. When the first thread exits, the lock remains held, causing the second thread to wait indefinitely at its dinnerLock.lock() call. The program hangs and never prints 'Family dinner complete' because the ExecutorService cannot shut down with a blocked thread."
629-
category: "Concurrency"
629+
category: "Concurrency"
630+
631+
question: "What is the output of this family reunion code?"
632+
code: |
633+
public class FamilyReunion {
634+
public static void main(String[] args) {
635+
int grandparents = 2;
636+
int parents = 4;
637+
int children = 0;
638+
639+
for (int cousin = 0; cousin < grandparents; cousin++) {
640+
children += ++parents;
641+
System.out.print(children + " ");
642+
}
643+
644+
while (children-- > 8) {
645+
int uncles = children;
646+
children -= uncles++;
647+
System.out.print(children + " ");
648+
}
649+
650+
System.out.print("Total: " + (grandparents + parents + children));
651+
}
652+
}
653+
options:
654+
- "5 11 0 Total: 7"
655+
- "4 10 0 Total: 6"
656+
- "5 11 5 Total: 12"
657+
- "5 10 0 Total: 8"
658+
- "4 11 0 Total: 7"
659+
correct: 1
660+
explanation: "For loop runs twice: First iteration: ++parents makes parents=5, children=0+5=5, prints '5'. Second iteration: ++parents makes parents=6, children=5+6=11, prints '11'. While loop: children-- > 8 checks 11>8 (true), then decrements children to 10. Inside loop: uncles=10, children=10-10=0, uncles++ doesn't affect children, prints '0'. Next check: 0>8 is false, loop exits. Final: grandparents=2, parents=6, children=-1 (from the failed while condition). Total: 2+6+(-1)=7."
661+
category: "Operators and Control Flow"

0 commit comments

Comments
 (0)