1Z0-829 Controlling Program Flow • Complete Question Bank
Complete 1Z0-829 Controlling Program Flow question bank — all 0 questions with answers and detailed explanations.
Given 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?
You 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?
Refer to the exhibit.
public class LoopTest {
public static void main(String[] args) {
outer:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i + j > 3) {
break outer;
}
System.out.print(i + j + " ");
}
}
}
}A 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?
A 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?
Drag steps to the numbered slots on the right, or tap a step then tap a slot.
Drag steps to the numbered slots on the right, or tap a step then tap a slot.
Drag a concept onto its matching description — or click a concept then click the description.
Makes a package accessible to other modules
Specifies a dependency on another module
Allows reflective access to a package at runtime
Declares a service implementation
Specifies a service interface consumed by the module
Drag a concept onto its matching description — or click a concept then click the description.
Reads text from a character-input stream, buffering characters
Reads raw bytes from a file
Writes primitive types and Java objects to an OutputStream
Prints formatted representations of objects to a text-output stream
Reads primitive Java data types from an underlying input stream
A 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?
Given 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?
Consider 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?
A 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?
Given 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?
What is the output of the following code?
```java
int i = 5;
while (i > 0) {System.out.print(i + " "); i--;
}
```
Consider the following do-while loop:
```java
int x = 10;
do { x--;
} while (x < 10);
System.out.println(x); ```
What is printed?
Given 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?
Which 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?
Which 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 ");
}
```
Refer to the exhibit.
```java
public class SwitchTest {
public static void main(String[] args) {
int value = 2;
switch (value) {
case 1:
System.out.print("One ");
case 2:
System.out.print("Two ");
case 3:
System.out.print("Three ");
break;
default:
System.out.print("Default ");
}
}
}
```Refer to the exhibit.
```
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
at LoopExample.main(LoopExample.java:5)
```
Source code of LoopExample.java:
```java
1: public class LoopExample {
2: public static void main(String[] args) {
3: int[] arr = {1,2,3};
4: for (int i = 0; i <= arr.length; i++) {
5: System.out.println(arr[i]);
6: }
7: }
8: }
```Refer to the exhibit.
```
Error: incompatible types: bad type in switch expression
return switch(day) {
^
required: int
found: String
```Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.length()" because "s" is null
at com.example.Main.main(Main.java:8)enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY }
int numLetters = switch (day) {
case MONDAY, FRIDAY, SUNDAY -> 6;
case TUESDAY -> 7;
default -> { int len = day.name().length(); yield len; }
};
System.out.println(numLetters);Refer to the exhibit.
```java
public class LoopExample {
public static void main(String[] args) {
outer:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i == 1 && j == 2) {
break outer;
}
System.out.print(i + " " + j + " ");
}
}
}
}
```