← Functions lesson New set →

App Dev Worksheet: Functions

App development lesson 20: Functions · Set 72 · 40 questions
TalentJR

Developer's tip (Write once, use often): function name(parameter) { …; return result; } — call it with name(value). Work out each call separately, then finish the line. Inside a function, if / return can give different results for different inputs.

NameDateTime takenScore ___ / 40
  1. 1.🧰 What number does this code show?
    function total(price, qty) {
      return price * qty;
    }
    show(total(25, 6));
  2. 2.🧰 A museum app works out the entry fee. What number does this code show?
    function fee(age) {
      if (age < 5) {
        return 0;
      }
      return 30;
    }
    show(fee(1) + fee(11));
  3. 3.🧰 What number does this code show?
    function double(n) {
      return n * 2;
    }
    show(double(31));
  4. 4.🧰 What does this code show?
    function greet(name) {
      return "Hello, " + name + "!";
    }
    show(greet("Zoya"));
    (a) Zoya (b) greet (c) Hello, Zoya! (d) Hello, name!
  5. 5.🧰 A museum app works out the entry fee. What number does this code show?
    function fee(age) {
      if (age < 5) {
        return 0;
      }
      return 50;
    }
    show(fee(2) + fee(11));
  6. 6.🧰 What number does this code show?
    function points(stars) {
      return stars * 10 + 5;
    }
    show(points(3) + points(3));
  7. 7.🧰 What is a function? (a) a list of colours (b) a type of phone (c) a named block of code (d) a screen size
  8. 8.🧰 In function tip(bill), what is bill? (a) an event (b) a parameter (c) a screen (d) a loop
  9. 9.🧰 How do you run (call) a function called greet? (a) greet() (b) run greet (c) #greet (d) greet[]
  10. 10.🧰 What number does this code show?
    function total(price, qty) {
      return price * qty;
    }
    show(total(10, 5));
  11. 11.🧰 A museum app works out the entry fee. What number does this code show?
    function fee(age) {
      if (age < 5) {
        return 0;
      }
      return 20;
    }
    show(fee(11) + fee(5));
  12. 12.🧰 What number does this code show?
    function points(stars) {
      return stars * 10 + 5;
    }
    show(points(3) + points(1));
  13. 13.🧰 What number does this code show?
    function total(price, qty) {
      return price * qty;
    }
    show(total(15, 4));
  14. 14.🧰 What number does this code show?
    function double(n) {
      return n * 2;
    }
    show(double(37));
  15. 15.🧰 What number does this code show?
    function points(stars) {
      return stars * 10 + 5;
    }
    show(points(4) + points(0));
  16. 16.🧰 What number does this code show?
    function points(stars) {
      return stars * 10 + 5;
    }
    show(points(5) + points(4));
  17. 17.🧰 What number does this code show?
    function points(stars) {
      return stars * 10 + 5;
    }
    show(points(1) + points(4));
  18. 18.🧰 A museum app works out the entry fee. What number does this code show?
    function fee(age) {
      if (age < 5) {
        return 0;
      }
      return 50;
    }
    show(fee(1) + fee(1));
  19. 19.🧰 What number does this code show?
    function total(price, qty) {
      return price * qty;
    }
    show(total(40, 3));
  20. 20.🧰 What number does this code show?
    function triple(n) {
      return n * 3;
    }
    show(triple(39));
  21. 21.🧰 What number does this code show?
    function points(stars) {
      return stars * 10 + 5;
    }
    show(points(1) + points(1));
  22. 22.🧰 What does return do in a function? (a) sends back a value (b) closes the screen (c) deletes the app (d) repeats the code
  23. 23.🧰 A museum app works out the entry fee. What number does this code show?
    function fee(age) {
      if (age < 5) {
        return 0;
      }
      return 20;
    }
    show(fee(12) + fee(10));
  24. 24.🧰 What number does this code show?
    function double(n) {
      return n * 2;
    }
    show(double(10));
  25. 25.🧰 What number does this code show?
    function points(stars) {
      return stars * 10 + 5;
    }
    show(points(2) + points(1));
  26. 26.🧰 What number does this code show?
    function points(stars) {
      return stars * 10 + 5;
    }
    show(points(3) + points(0));
  27. 27.🧰 What number does this code show?
    function double(n) {
      return n * 2;
    }
    show(double(12));
  28. 28.🧰 A museum app works out the entry fee. What number does this code show?
    function fee(age) {
      if (age < 5) {
        return 0;
      }
      return 50;
    }
    show(fee(9) + fee(12));
  29. 29.🧰 What does this code show?
    function greet(name) {
      return "Hi, " + name + "!";
    }
    show(greet("Riya"));
    (a) greet (b) Riya (c) Hi, name! (d) Hi, Riya!
  30. 30.🧰 What number does this code show?
    function total(price, qty) {
      return price * qty;
    }
    show(total(35, 3));
  31. 31.🧰 What number does this code show?
    function triple(n) {
      return n * 3;
    }
    show(triple(19));
  32. 32.🧰 Why write a function? (a) to slow the app (b) to make code longer (c) to reuse code (d) to hide errors
  33. 33.🧰 What number does this code show?
    function total(price, qty) {
      return price * qty;
    }
    show(total(20, 3));
  34. 34.🧰 What number does this code show?
    function total(price, qty) {
      return price * qty;
    }
    show(total(100, 2));
  35. 35.🧰 What number does this code show?
    function points(stars) {
      return stars * 10 + 5;
    }
    show(points(5) + points(0));
  36. 36.🧰 What number does this code show?
    function triple(n) {
      return n * 3;
    }
    show(triple(25));
  37. 37.🧰 What number does this code show?
    function points(stars) {
      return stars * 10 + 5;
    }
    show(points(4) + points(5));
  38. 38.🧰 What number does this code show?
    function total(price, qty) {
      return price * qty;
    }
    show(total(15, 2));
  39. 39.🧰 What number does this code show?
    function double(n) {
      return n * 2;
    }
    show(double(3));
  40. 40.🧰 A museum app works out the entry fee. What number does this code show?
    function fee(age) {
      if (age < 5) {
        return 0;
      }
      return 60;
    }
    show(fee(1) + fee(9));

Answer key

  1. 150
  2. 30
  3. 62
  4. Hello, Zoya!
  5. 50
  6. 70
  7. a named block of code
  8. a parameter
  9. greet()
  10. 50
  11. 40
  12. 50
  13. 60
  14. 74
  15. 50
  16. 100
  17. 60
  18. 0
  19. 120
  20. 117
  21. 30
  22. sends back a value
  23. 40
  24. 20
  25. 40
  26. 40
  27. 24
  28. 100
  29. Hi, Riya!
  30. 105
  31. 57
  32. to reuse code
  33. 60
  34. 200
  35. 60
  36. 75
  37. 100
  38. 30
  39. 6
  40. 60

Free app development lessons, practice and worksheets at talentjr.in/app-development-for-students