Practice 1Z0-829 Controlling Program Flow questions with full explanations on every answer.
Start practicing
Controlling Program Flow — choose a session length
Free · No account required
Click any question to see the full explanation and answer options, or start a focused practice session above.
A developer writes code to iterate over a list of strings and print each element. The code uses an enhanced for loop. Which statement is true about the enhanced for loop?
2Given the code snippet: int x = 10; if (x > 5) { System.out.print("A"); } else if (x > 7) { System.out.print("B"); } else { System.out.print("C"); } What is the output?
3A method contains a try-with-resources statement that uses two resources: a FileInputStream and a BufferedInputStream. The FileInputStream constructor throws a FileNotFoundException. Which statement about resource closing is true?
4Which TWO statements are true about the switch statement in Java?
5Which THREE statements are true about loops in Java?
6What is the output?
7What is the output?
8What is the output?
9You are developing a high-frequency trading application that processes a stream of market data ticks. Each tick is represented by a Tick object with fields: long timestamp, String symbol, double price, int volume. Ticks arrive in real-time and must be processed in order. A bug is reported: the application occasionally processes a tick out of order, causing incorrect trade decisions. The processing logic uses a while loop to read from a blocking queue and process each tick. The code is: BlockingQueue<Tick> queue = new LinkedBlockingQueue<>(); while (true) { Tick tick = queue.take(); process(tick); } After investigation, you find that the queue is fed by multiple producer threads that sometimes reorder ticks due to network delays. Which course of action best ensures ticks are processed in the correct chronological order without sacrificing throughput?
10Which loop construct guarantees that the body executes at least once?
11What is the output of the program?
12Which TWO statements about the switch statement in Java are correct?
13A Java application processes a list of orders. Each order has a status: NEW, PROCESSING, SHIPPED, or DELIVERED. The code must print a message based on the status: - If NEW: "Order received" - If PROCESSING: "Order in progress" - If SHIPPED: "Order shipped" - If DELIVERED: "Order delivered" - For any other status: "Unknown status" The developer writes the following code using a switch expression: String message = switch (status) { case NEW -> "Order received"; case PROCESSING -> "Order in progress"; case SHIPPED -> "Order shipped"; case DELIVERED -> "Order delivered"; default -> "Unknown status"; }; System.out.println(message); What is the correct course of action to ensure the code compiles and runs correctly?
14Which TWO statements about the switch statement in Java are true?
15What is the result?
16A Java developer is writing a batch processing application that reads records from a database and processes them. The processing must continue even if some records cause exceptions (e.g., data conversion errors). However, the application must log each failed record and its error, then continue with the next record. The developer uses a for loop to iterate over a list of records. Inside the loop, a try-catch block wraps the processing logic. After implementing, the developer notices that when an exception occurs, the loop terminates prematurely instead of continuing. The code structure is: List<Record> records = fetchRecords(); for (Record rec : records) { try { process(rec); } catch (Exception e) { log.error("Failed to process: " + rec.getId(), e); } } What is the most likely reason for the premature termination?
17Order the steps to properly handle resources using try-with-resources in Java.
18Order the steps to handle checked exceptions in a method that throws IOException.
19Match each Java module directive to its description.
20Match each I/O stream class to its description.
21A developer writes a method that processes a grade and returns a message using a switch expression. The code is: ```java public static String getMessage(int grade) { return switch (grade) { case 90, 80 -> "Excellent"; case 70, 60 -> "Good"; case 50 -> "Pass"; default -> "Fail"; }; } ``` Which statement is correct about this code?
22Given the following code snippet: ```java outer: for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (i == 1 && j == 1) { break outer; } System.out.print(i + "-" + j + " "); } } ``` What is the output?
23Consider the following code: ```java boolean a = true, b = false, c = false; if (a && b || c) { System.out.println("True"); } else { System.out.println("False"); } ``` What is the output?
24A method uses an enhanced for loop to iterate over a list of strings and prints each string. The code is: ```java List<String> list = List.of("A", "B", "C"); for (String s : list) { if (s.equals("B")) { break; } System.out.print(s); } ``` What is the result?
25Given the following switch statement: ```java int x = 2; switch (x) { default: System.out.print("default "); case 1: System.out.print("1 "); case 2: System.out.print("2 "); case 3: System.out.print("3 "); break; case 4: System.out.print("4 "); } ``` What is the output?
26What is the output of the following code? ```java int i = 5; while (i > 0) { System.out.print(i + " "); i--; } ```
27Consider the following do-while loop: ```java int x = 10; do { x--; } while (x < 10); System.out.println(x); ``` What is printed?
28Given the following code: ```java outer: for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (i == 1) { continue outer; } System.out.print(i + " " + j + " "); } } ``` What is the output?
29Which of the following correctly uses a switch expression with multiple constants per case? ```java int day = 2; String result = switch (day) { case 1, 2, 3 -> "Weekday"; case 6, 7 -> "Weekend"; default -> "Invalid"; }; ``` What is the value of result?
30Which TWO correctly describe the behavior of the following code? ```java int x = 10; switch (x) { case 10: System.out.print("ten "); default: System.out.print("default "); case 20: System.out.print("twenty "); } ```
31Which THREE statements are true about the enhanced for loop in Java?
32Which TWO of the following are valid forms of the switch statement/expression in Java?
33What is the output when this code is executed?
34Which change fixes the exception?
35What is the likely cause of this compilation error?
36Given: for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { if(i==1 && j==1) break; } } What is the value of i after the outer loop completes?
37Given: Object obj = "Hello"; String result = switch(obj) { case String s -> "String of length " + s.length(); case Integer i -> "Integer"; default -> "Unknown"; }; What is result?
38Given: outer: for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { if(j==1) continue outer; } } How many times does the innermost loop body execute?
39Given: int x=0; do { x++; } while(x<5); How many times does the loop body execute?
40Given: int a=5, b=10; String result = (a > b) ? "greater" : (a < b) ? "less" : "equal"; What is result?
41Given: Object obj = null; String s = switch(obj) { case null -> "null"; case String str -> str; default -> "other"; }; What is the result?
42Given: int i=0; outer: while(i<3) { for(int j=0; j<3; j++) { if(j==1) break outer; } i++; } What is the value of i after the outer loop?
43Given: for(int i=0; i<10; i++) { if(i%2==0) continue; System.out.print(i+" "); } What is the output?
44Given: int x=1; switch(x) { case 1: System.out.print("A"); case 2: System.out.print("B"); break; default: System.out.print("C"); } What is printed?
45Which two statements about the break statement are true?
46Which three of the following are valid case values in a traditional switch statement (non-pattern)?
47Which two statements about the do-while loop are true?
48What is the output?
49What is the output?
50What is the output?
51A developer is designing a loop to iterate through an array of integers and stop processing when the value -1 is encountered. Which loop construct should be used?
52Which statement is correct about the following switch expression? String result = switch(day) { case MONDAY, TUESDAY -> "weekday"; case WEDNESDAY -> "midweek"; default -> "other"; };
53A developer has a method that contains a try-catch-finally block inside a while loop. The try block throws a checked exception that is caught by the catch block. The catch block throws a new runtime exception. What is the behavior?
54Which two of the following statements are true about the continue statement in a loop?
55Which two of the following are valid ways to exit a loop in Java?
56Which three of the following statements about the switch expression in Java 17 are correct?
57Refer to the exhibit. The exhibit shows a stack trace from a Java application. Which line in the code caused the NullPointerException?
58Refer to the exhibit. The exhibit shows a code snippet. What is the output when the variable day is set to Day.WEDNESDAY?
59In a large enterprise application, a concurrent caching system is implemented using a ConcurrentHashMap that is accessed by multiple threads concurrently. The cache performs atomic operations on individual keys, but some operations require updates on multiple keys. To ensure consistency, the code acquires intrinsic locks on the keys using synchronized blocks. Over time, the system has been experiencing intermittent deadlocks. During post-mortem analysis, it was found that thread A holds a lock on key X and is waiting for key Y, while thread B holds a lock on key Y and is waiting for key X. The development team needs to redesign the locking strategy to eliminate these deadlocks while maintaining high throughput and minimizing code changes. They consider the following proposals: replacing ConcurrentHashMap with Collections.synchronizedMap, using a single ReentrantLock for all cache operations, always acquiring locks on keys in a consistent global order, or using a Lock with tryLock and a timeout and releasing all locks if timeout expires. Based on best practices in concurrent programming and considering the requirements to avoid deadlocks and maintain performance, which approach should they choose?
60What is the output of the following code snippet? int x = 5; if(x > 0) { System.out.print('A'); } else if(x > 2) { System.out.print('B'); } else { System.out.print('C'); }
61Given nested loops with labels, which statement correctly breaks out of the outer loop?
62A resource is declared in a try-with-resources statement. The try block throws an exception. The close method throws a different exception. What exception is thrown by the try-with-resources statement?
63How many times does the following loop execute? int i=0; while(i<5) { System.out.println(i); i++; }
64What is the output of the following code? int i=5; do { System.out.print(i); i--; } while(i>0);
65Given the following switch expression with colon syntax, what is the result when code equals 2? int val = switch(code) { case 1: yield 10; case 2: yield 20; default: yield 30; };
66A developer is implementing a method that processes a collection of objects. The objects are instances of various classes that implement a common interface. The developer wants to use a switch expression to perform different actions based on the runtime type of each object. Which approach is correct?
67A developer wrote a method that uses a for-each loop to iterate over a list of strings and remove elements that match a certain condition. However, the method throws a ConcurrentModificationException at runtime. What is the most likely cause?
68Which TWO statements about the break and continue statements in Java are correct?
69A junior developer writes a method that uses a switch statement to handle different types of user input. The input is an integer representing an operation code. The developer uses a traditional switch statement with break statements. However, when operation code 2 is entered, the program also executes the code for operation 3. What is the most likely cause?
70A developer is implementing a batch processing application that reads records from a list and processes them. The method uses a for loop with an index variable. Inside the loop, if a record is null, the developer wants to skip that iteration and continue with the next index. The developer writes: for (int i = 0; i < records.size(); i++) { if (records.get(i) == null) continue; process(records.get(i)); updateCounter(); } However, the counter is not updated correctly. The developer expects the counter to reflect the number of processed (non-null) records. What is the problem?
71A financial trading system uses a Java application to process market data. The core algorithm uses nested loops to compare price arrays. The developer uses a labeled continue statement to skip certain combinations. After a code review, the team suspects the algorithm has a bug that causes incorrect results. The developer writes a unit test and discovers that the labeled continue sometimes skips more iterations than intended. The code is: outer: for (int i = 0; i < prices1.length; i++) { for (int j = 0; j < prices2.length; j++) { if (prices1[i] < prices2[j]) continue outer; // process combination } } The developer intended that if any price in prices1 is less than a price in prices2, the entire row (i) should be skipped. However, the algorithm skips rows even when the condition is not met for all j. What is the most likely cause?
72A developer writes a method that uses a for loop to iterate over a list of strings and remove elements that match a specific pattern using the list's remove(int index) method. The developer uses an index variable that increments normally. However, after running the method, some elements that should have been removed are still present, and some elements are skipped. The list initially contains [A, B, C, D, E] and the developer expects to remove B and D. After the loop, the list is [A, C, E] as expected? Actually, the developer observes that after the loop, the list contains [A, C, D, E] (D was not removed). What is the most likely cause?
73A developer needs to iterate over a List<String> and remove elements that are null. Which approach guarantees correct behavior without throwing a ConcurrentModificationException?
74Given an enum Direction { NORTH, SOUTH, EAST, WEST } and a variable d of type Direction, which code snippet correctly uses a switch expression to map each direction to an abbreviation (N, S, E, W) without using a default branch?
75Which two statements about the enhanced for-each loop in Java are true? (Choose two.)
76Refer to the exhibit. What is the output when the program is executed?
77A financial application processes a daily batch of 10 million transactions. Each transaction is an object with fields: id, amount, and status (an enum: PENDING, APPROVED, REJECTED). The requirement is to find the first APPROVED transaction with amount greater than 1000. The current implementation uses a while loop with a nested if-else structure that checks each transaction sequentially. The loop also logs each transaction status, which involves a moderately expensive file write operation. Performance analysis shows the method is a bottleneck, often taking over 12 seconds. The development team is considering refactoring. Which course of action will most effectively reduce execution time while maintaining the requirement?
The Controlling Program Flow domain covers the key concepts tested in this area of the 1Z0-829 exam blueprint published by Oracle. Courseiva provides free domain-focused practice, mock exams, missed-question review, and readiness tracking across all 1Z0-829 domains — no account required.
The Courseiva 1Z0-829 question bank contains 77 questions in the Controlling Program Flow domain. Click any question to see the full explanation and answer breakdown.
Start with a 10-question focused session to identify your baseline accuracy in this domain. Read every explanation — even for questions you answer correctly — to understand the reasoning. Once you score consistently above 80%, move to a 20–30 question session to confirm depth before moving to the next domain.
Yes — the session launcher on this page draws questions exclusively from the Controlling Program Flow domain. Choose 10, 20, 30, or 50 questions for a focused session, or click individual questions to review them one by one.
Save your results, see per-domain analytics, and get readiness scores — free, for every certification.
Sign Up FreeFree forever · Every certification included