zuai-logo

What does the following code output?

java
for (int i = 0; i < 5; i++) {
    System.out.print(i + " ");
}

0 1 2 3 4

Flip to see [answer/question]
Flip to see [answer/question]

All Flashcards

What does the following code output?

java
for (int i = 0; i < 5; i++) {
    System.out.print(i + " ");
}

0 1 2 3 4

What does the following code output?

java
for (int i = 1; i <= 5; i++) {
    for (int j = 1; j <= i; j++) {
        System.out.print("*");
    }
    System.out.println();
}

**




How many times does the inner loop run?

java
for (int i = 1; i <= 5; i++) {
    for (int j = 1; j <= 3; j++) {
        System.out.print(j + " ");
    }
    System.out.println();
}

15

What does the following code output?

java
int i = 1;
while (i <= 5) {
    int j = 1;
    while (j <= i) {
        System.out.print(i + " ");
        j++;
    }
    System.out.println();
    i++;
}

1 2 2 3 3 3 4 4 4 4 5 5 5 5 5

What does the following code output?

java
int i = 1;
while (i <= 5) {
    int j = 5;
    while (j >= i) {
        System.out.print("*");
        j--;
    }
    System.out.println();
    i++;
}



** *

What is the output of the following code?

java
int x = 5;
int y = 10;
if (x < y) {
  System.out.println("x is less than y");
} else {
  System.out.println("x is not less than y");
}

x is less than y

What is the output of the following code?

java
int sum = 0;
for (int i = 1; i <= 3; i++) {
  sum += i;
}
System.out.println(sum);

6

What is the output of the following code?

java
int i = 0;
while (i < 4) {
  System.out.print(i + " ");
  i++;
}

0 1 2 3

Identify the error in the following code:

java
for (int i = 0; i < 10; i--)
System.out.println(i);

The loop will run infinitely because i is decremented instead of incremented, so the condition i < 10 will always be true.

What is the output of the following code?

java
int x = 10;
int y = 5;
System.out.println(x % y);

0

What are the steps for tracing a for loop?

  1. Initialize the loop counter. 2. Check the loop condition. 3. Execute the loop body. 4. Update the loop counter. 5. Repeat steps 2-4 until the condition is false.

What are the steps for tracing a while loop?

  1. Check the loop condition. 2. If true, execute the loop body. 3. Update variables within the loop. 4. Repeat steps 1-3 until the condition is false.

What are the steps to create a tracing table?

  1. Identify variables to track. 2. Create columns for iteration number, variables, and output. 3. Execute code line by line, updating the table.

What are the steps for tracing nested loops?

  1. Start with the outer loop. 2. For each iteration of the outer loop, execute the inner loop completely. 3. Track both outer and inner loop variables.

What are the steps for identifying an infinite loop?

  1. Check the loop condition. 2. Determine if the variables within the loop are updated in a way that will eventually make the condition false. 3. If not, it's likely an infinite loop.

What are the steps to debug code using tracing?

  1. Trace the code to identify where the program's state deviates from the expected state. 2. Examine the variable values at that point. 3. Correct the code to align with the intended logic.

What are the steps to predict output?

  1. Initialize variables. 2. Execute each line of code, updating variables as necessary. 3. Record any output statements.

What are the steps to solve code tracing problems?

  1. Read the code carefully. 2. Create a tracing table. 3. Execute the code line by line. 4. Record variable values and output. 5. Determine the final output or state.

What are the steps to manage time during a code tracing exam?

  1. Quickly assess the difficulty of the problem. 2. If stuck, move on and return later. 3. Allocate time based on complexity. 4. Double-check your work.

What are the steps to avoid off-by-one errors in loops?

  1. Carefully consider the loop condition. 2. Pay attention to whether the loop should include the boundary values. 3. Test with edge cases.

What are the differences between for and while loops?

For loop: Used for known number of iterations, initialization/condition/update in one line. While loop: Used for unknown number of iterations, condition checked before each iteration, update manual.

What are the differences between single and nested loops?

Single loop: Iterates a block of code once for each value. Nested loop: An inner loop runs completely for each iteration of the outer loop.

What are the differences between print and println?

print: Prints output to the console without a newline. println: Prints output to the console with a newline.