Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 37 ยท Projects and Practice

SonarQube Practice: Exercises and Interview Questions

Test your SonarQube knowledge with worked calculation exercises, scenario questions, troubleshooting drills and common interview questions on hotspots, coverage, CI and administration.

This closing lesson turns the chapter into practice. You will calculate ratings and coverage by hand, diagnose broken pipelines from symptoms, and answer the kind of questions that come up in interviews. Try each exercise before opening the answer. SonarQube is not runnable on this site, so the TryIts are small calculators that check your arithmetic.

Part 1: calculation exercises

Exercise 1: coverage. A module has 200 executable lines, 150 covered, 20 conditions (40 outcomes), 30 of them taken. What is line coverage, and what is overall coverage?

Exercise 2: debt ratio. A project has 25,000 lines of code, the default 30 minutes per line, and 6,000 minutes of technical debt. What rating does it get?

Exercise 3: hotspots. A pull request introduces 5 hotspots. Two are Safe, one is Acknowledged, one is Fixed, one To Review. What is Security Hotspots Reviewed, and does the default gate pass?

Use the block to check your answers.

JS Check Exercises 1 to 3

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Answers: 75% and 75%; the ratio is 0.8%, rating A; 80%, so the gate fails until the last hotspot is reviewed.

Part 2: scenario questions

Scenario A. After a green build, the SonarQube project shows coverage 0%. Name three things to check. Answer: the tests ran before the scan; the report path property is right for the language (for example sonar.javascript.lcov.reportPaths); the paths inside the report match the source paths the scanner sees (common in Docker); the report was actually written (look in the build log).

Scenario B. A pull request fails the gate for "Security Hotspots Reviewed". The author says the code is safe. What now? Answer: A person with the Administer Security Hotspots permission opens the Security Hotspots tab of the PR, reads the risk, sets Safe with a comment explaining why, and the gate turns green after the status refreshes. The author alone cannot do it without the permission.

Scenario C. The Jenkins stage waitForQualityGate hangs until timeout, but the analysis succeeded. Answer: the SonarQube webhook to https://jenkins.example.com/sonarqube-webhook/ is missing or unreachable; configure it in Administration > Configuration > Webhooks and check the server can reach Jenkins.

Scenario D. Code written last year has hundreds of issues, and the manager wants zero before the gate is enabled. Answer: explain Clean as You Code: gate the new code now, fix vulnerabilities and hotspots in old code, clean other debt when files are touched (workflow project).

Scenario E. A scanner in CI prints Not authorized. Answer: the token is missing, expired, of the wrong type or lacks Execute Analysis on that project; the variable may be empty on a fork PR.

Part 3: debugging a pipeline

The block below holds a table of symptoms and probable causes. Write your own answer first and then call diagnose to compare. It is a small knowledge base, not a real log analyser.

JS Stand-in: symptom to cause lookup

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Part 4: interview questions

  1. What is the difference between a bug, a vulnerability, a code smell and a security hotspot? A bug is likely wrong behaviour; a vulnerability is an exploitable weakness; a code smell hurts maintainability; a hotspot is security-sensitive code that needs a manual review.
  2. What is a quality gate? A set of conditions on measures (usually on new code) that gives OK or ERROR for a project, branch or pull request.
  3. What does Clean as You Code mean? Apply quality conditions to new and changed code, not to the whole legacy baseline.
  4. How does SonarQube get coverage? It imports reports from your own coverage tool; it does not run tests.
  5. Why is a shallow clone a problem? Blame and new code detection need history.
  6. Which tokens would you use in CI? A project analysis token stored in the CI secret store.
  7. What is cognitive complexity? A measure of how hard code is to understand, adding penalties for nesting; default rule threshold 15.
  8. How would you back up and upgrade SonarQube? Back up the database (and conf and plugins), stop, deploy the new version, run /setup to migrate, follow the LTA path.
  9. Which edition do you need for pull request analysis? Developer Edition or above.
  10. How do hotspots affect ratings? They do not affect the Security rating; they are tracked as a reviewed percentage that the gate checks.

Part 5: build your own mini-checker

Design task: write a function that accepts a PR's measures and a gate and returns the list of failing conditions. Compare with this solution only after trying.

JS Solution sketch: failing conditions

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A project has 1,000 executable lines with 800 covered and no conditions. What is its coverage?

    Without conditions overall coverage equals line coverage: 800 / 1,000 = 80%.
  2. 2. Which problem causes waitForQualityGate in Jenkins to hang although the analysis succeeded?

    Jenkins relies on the webhook for the result.
  3. 3. Which finding type does not change the Security rating?

    Ratings come from vulnerabilities; hotspots count only as a reviewed percentage.
  4. 4. What is the best way to start on a legacy codebase with 6,000 issues?

    That is the Clean as You Code approach.
  5. 5. Which token type is preferred for CI?

    A project analysis token limits damage if it leaks.

Summary

  • You can compute coverage, debt ratio, ratings and hotspot percentages by hand.
  • You can diagnose common pipeline failures from symptoms.
  • You can explain the core concepts in interview language: gate, new code, hotspots, tokens, LTA upgrades.
  • Keep practicing on a real project: set up a server locally, scan a sample repository and break things on purpose.

Next: SonarQube and Code Quality