Breakpoints and stepping

Driving a debugger: plain and conditional breakpoints, step over, step into and step out, resume, the variables and call stack views, watch expressions.

  • 25 cards
  • 5 topics
  • Free

The outline, topic by topic

Each topic is weighted the way the exam weighs it.

  1. Concepts
  2. Questions
  3. Sort and link
  4. Scenarios
  5. Understand

A few cards from the deck

  • Breakpoint: what is it?

    Show the answer

    A mark set on a line: when execution reaches it, the debugger suspends it just before running that line.

    With the program suspended, you can read the variables and the call stack, then move forward line by line. The marked line has not run yet.

  • The debugger is suspended at line 9. On which line does it stop after a step into?

    ```java
    public class Commande {
    static int prix(int q) {
    int base = q * 5;
    return base;
    }

    public static void main(String[] args) {
    int q = 3;
    int total = prix(q);
    total = total + 1;
    System.out.println(total);
    }
    }
    `

    • 3
    • 10
    • 2
    • 4
    Show the answer

    3

    Step into goes down into prix and stops at its first statement, line 3 (line 2 is only the signature). Step over would have finished line 9 entirely and moved to line 10.

  • A program started in … mode stops at the breakpoints; the … command runs it until the next breakpoint.

    Show the answer

    A program started in debug mode stops at the breakpoints; the resume command runs it until the next breakpoint.

    Started normally, the program ignores breakpoints. Resume does not move line by line: it hands control back to the program until the next stop.

  • Conditional breakpoint: what is it?

    Show the answer

    A mark on a line, with a boolean expression attached: execution only stops when the expression is true.

    On a method called for every customer, the condition id == 42 suspends the program only for the customer that causes trouble, instead of stopping at every call.

  • A 10,000-round loop eventually makes stock negative. How do you find the faulty round without skipping 10,000 stops by hand?

    • Set a conditional breakpoint with the condition stock < 0
    • Add a watch on stock and wait for it to stop
    • Add a print of stock in the loop and read the 10,000 lines
    • Set a plain breakpoint at the end of the loop and resume up to the last-but-one round
    Show the answer

    Set a conditional breakpoint with the condition stock < 0

    Only a conditional breakpoint suspends the program on the first round where the condition becomes true, with the whole stack and all the variables visible. A watch shows a value without ever interrupting.