JDB Interview Questions and Answers for experienced

100 JDB Interview Questions and Answers
  1. What is JDB and what are its primary uses?

    • Answer: JDB (Java Debugger) is a command-line debugger for Java programs. Its primary uses include setting breakpoints, stepping through code line by line, inspecting variables, evaluating expressions, and analyzing the call stack during program execution. It's invaluable for identifying and resolving bugs in Java applications.
  2. How do you start a debugging session using JDB?

    • Answer: You typically start a debugging session by launching the JVM with debugging enabled using the `-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000` option (or similar, adjusting port and suspend settings as needed). Then, attach JDB to the running JVM using the command `jdb -attach 8000`.
  3. Explain the difference between `step`, `next`, and `cont` commands in JDB.

    • Answer: `step` executes the next line of code, stepping *into* method calls. `next` executes the next line of code, stepping *over* method calls (treating them as a single step). `cont` continues program execution until the next breakpoint or the program terminates.
  4. How do you set a breakpoint in JDB?

    • Answer: You set a breakpoint using the `stop at` command followed by the class name, line number, or method name. For example: `stop at MyClass:10` sets a breakpoint at line 10 of MyClass.
  5. How do you inspect the value of a variable in JDB?

    • Answer: You use the `print` command followed by the variable name. For example: `print myVariable` will print the value of `myVariable`.
  6. What is the `locals` command in JDB used for?

    • Answer: The `locals` command displays the values of all local variables in the current stack frame.
  7. How do you evaluate an expression in JDB?

    • Answer: You can evaluate an expression using the `eval` command. For example: `eval myVariable + 5` will evaluate the expression and print the result.
  8. Explain the use of the `where` command in JDB.

    • Answer: The `where` command displays the current call stack, showing the sequence of method calls that led to the current point of execution.
  9. How do you use JDB to debug a multi-threaded application?

    • Answer: JDB allows you to select threads using the `thread` command and set breakpoints and step through code within specific threads. You can use `threads` to list all threads and their states.
  10. What are some limitations of JDB?

    • Answer: JDB is a command-line debugger, which can be less user-friendly than graphical debuggers. It lacks advanced features found in IDE debuggers like visual variable inspection and source code navigation.
  11. How to use the watch command in JDB?

    • Answer: The `watch` command allows you to specify an expression that will be automatically printed whenever its value changes during execution. This is useful for tracking variable changes over time.
  12. What is the significance of the `suspend` parameter in the JDWP options?

    • Answer: The `suspend` parameter in the `-agentlib:jdwp` option determines whether the JVM should pause execution when the debugger is attached. `suspend=y` pauses, `suspend=n` allows the application to continue running immediately.
  13. How can you use JDB to debug a remote application?

    • Answer: You would need to specify the remote host and port using the `-attach` command with the proper address information. The remote JVM must be configured to accept debug connections.
  14. How do you clear a breakpoint in JDB?

    • Answer: You can clear a breakpoint using the `clear` command followed by the breakpoint location (class and line number or method). You can also clear all breakpoints using `clear`.
  15. Describe the use of the `classes` command in JDB.

    • Answer: The `classes` command lists the classes loaded by the JVM. This is useful for identifying classes involved in a problem.
  16. What is the purpose of the `pop` and `up` commands in JDB?

    • Answer: `pop` pops the current stack frame, moving the execution context to the calling method. `up` moves the execution context up the stack to a specific frame.
  17. How do you handle exceptions during debugging with JDB?

    • Answer: JDB allows you to set exception breakpoints using the `catch` command to automatically pause execution when specific exceptions are thrown.
  18. Explain the concept of "stepping into" versus "stepping over" in the context of JDB.

    • Answer: "Stepping into" (using `step`) executes the next line of code, entering called methods. "Stepping over" (using `next`) executes the next line of code, treating method calls as a single step, without entering them.
  19. How can you use JDB to debug code that uses native methods?

    • Answer: Debugging native methods directly with JDB is limited. You primarily debug the Java code that calls the native methods and may need other tools to debug the native code itself.

Thank you for reading our blog post on 'JDB Interview Questions and Answers for experienced'.We hope you found it informative and useful.Stay tuned for more insightful content!