ApexSensei

Drills Apex basics

Find the compile error

Apex basics · Beginner · 10 min · Runs in your Salesforce Developer org · Free

The starter method does not compile. Fix three errors so `total(price, qty)` returns a formatted line-item total such as `Total: 30`. A semicolon is missing, the return type is wrong and double quotes have snuck in. Keep the method name and parameters.

The challenge

Fix every compile error in total(Integer price, Integer qty) so it returns a String such as Total: 30.

Examples:

  • total(10, 3) → Total: 30
  • total(0, 100) → Total: 0
  • total(5, 5) → Total: 25

Requirements:

  1. total(10, 3) should be Total: 30.
  2. total(0, 100) should be Total: 0.
  3. total(5, 5) should be Total: 25.
  4. total(1, 1) should be Total: 1.
  5. total(100, 100) should be Total: 10000.

Starter code

Integer total(Integer price, Integer qty) {
    Integer sum = price * qty
    return "Total: " + sum
}

Related drills

Try this drill free

You write the Apex yourself. ApexSensei runs it and tells you what passed and what did not.

‹ break and continue · Group records in a Map<String, List<Contact>> ›