← Functions lesson New set →

App Dev Worksheet: Functions

App development lesson 20: Functions · Set 759 · 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.🧰 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(2));
  2. 2.🧰 What number does this code show?
    function total(price, qty) {
      return price * qty;
    }
    show(total(95, 2));
  3. 3.🧰 What number does this code show?
    function points(stars) {
      return stars * 10 + 5;
    }
    show(points(5) + points(1));
  4. 4.🧰 What is a function? (a) a list of colours (b) a named block of code (c) a type of phone (d) a screen size
  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 60;
    }
    show(fee(2) + fee(12));
  6. 6.🧰 What number does this code show?
    function total(price, qty) {
      return price * qty;
    }
    show(total(70, 2));
  7. 7.🧰 What number does this code show?
    function double(n) {
      return n * 2;
    }
    show(double(16));
  8. 8.🧰 What number does this code show?
    function double(n) {
      return n * 2;
    }
    show(double(3));
  9. 9.🧰 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(1) + fee(12));
  10. 10.🧰 What number does this code show?
    function triple(n) {
      return n * 3;
    }
    show(triple(12));
  11. 11.🧰 What does this code show?
    function greet(name) {
      return "Hello, " + name + "!";
    }
    show(greet("Riya"));
    (a) Hello, Riya! (b) Riya (c) greet (d) Hello, name!
  12. 12.🧰 What number does this code show?
    function total(price, qty) {
      return price * qty;
    }
    show(total(10, 5));
  13. 13.🧰 What number does this code show?
    function total(price, qty) {
      return price * qty;
    }
    show(total(40, 3));
  14. 14.🧰 What number does this code show?
    function points(stars) {
      return stars * 10 + 5;
    }
    show(points(1) + points(1));
  15. 15.🧰 What does this code show?
    function greet(name) {
      return "Hello, " + name + "!";
    }
    show(greet("Zoya"));
    (a) Hello, name! (b) Hello, Zoya! (c) greet (d) Zoya
  16. 16.🧰 In function tip(bill), what is bill? (a) an event (b) a parameter (c) a screen (d) a loop
  17. 17.🧰 What number does this code show?
    function total(price, qty) {
      return price * qty;
    }
    show(total(95, 4));
  18. 18.🧰 What number does this code show?
    function double(n) {
      return n * 2;
    }
    show(double(19));
  19. 19.🧰 What number does this code show?
    function total(price, qty) {
      return price * qty;
    }
    show(total(35, 3));
  20. 20.🧰 What number does this code show?
    function points(stars) {
      return stars * 10 + 5;
    }
    show(points(3) + points(1));
  21. 21.🧰 Why write a function? (a) to hide errors (b) to make code longer (c) to slow the app (d) to reuse code
  22. 22.🧰 What number does this code show?
    function double(n) {
      return n * 2;
    }
    show(double(4));
  23. 23.🧰 What number does this code show?
    function points(stars) {
      return stars * 10 + 5;
    }
    show(points(4) + points(3));
  24. 24.🧰 What number does this code show?
    function points(stars) {
      return stars * 10 + 5;
    }
    show(points(2) + points(2));
  25. 25.🧰 What number does this code show?
    function total(price, qty) {
      return price * qty;
    }
    show(total(60, 2));
  26. 26.🧰 What does return do in a function? (a) sends back a value (b) repeats the code (c) deletes the app (d) closes the screen
  27. 27.🧰 What number does this code show?
    function points(stars) {
      return stars * 10 + 5;
    }
    show(points(4) + points(1));
  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 20;
    }
    show(fee(2) + fee(11));
  29. 29.🧰 What number does this code show?
    function total(price, qty) {
      return price * qty;
    }
    show(total(60, 4));
  30. 30.🧰 What number does this code show?
    function double(n) {
      return n * 2;
    }
    show(double(27));
  31. 31.🧰 What does this code show?
    function greet(name) {
      return "Well done, " + name + "!";
    }
    show(greet("Arjun"));
    (a) Well done, name! (b) Well done, Arjun! (c) Arjun (d) greet
  32. 32.🧰 What number does this code show?
    function double(n) {
      return n * 2;
    }
    show(double(20));
  33. 33.🧰 What number does this code show?
    function triple(n) {
      return n * 3;
    }
    show(triple(35));
  34. 34.🧰 What number does this code show?
    function double(n) {
      return n * 2;
    }
    show(double(32));
  35. 35.🧰 What number does this code show?
    function total(price, qty) {
      return price * qty;
    }
    show(total(85, 6));
  36. 36.🧰 What number does this code show?
    function points(stars) {
      return stars * 10 + 5;
    }
    show(points(2) + points(5));
  37. 37.🧰 What does this code show?
    function greet(name) {
      return "Hello, " + name + "!";
    }
    show(greet("Meera"));
    (a) greet (b) Hello, Meera! (c) Meera (d) Hello, name!
  38. 38.🧰 What number does this code show?
    function triple(n) {
      return n * 3;
    }
    show(triple(36));
  39. 39.🧰 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(2) + fee(10));
  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 30;
    }
    show(fee(12) + fee(7));

Answer key

  1. 0
  2. 190
  3. 70
  4. a named block of code
  5. 60
  6. 140
  7. 32
  8. 6
  9. 20
  10. 36
  11. Hello, Riya!
  12. 50
  13. 120
  14. 30
  15. Hello, Zoya!
  16. a parameter
  17. 380
  18. 38
  19. 105
  20. 50
  21. to reuse code
  22. 8
  23. 80
  24. 50
  25. 120
  26. sends back a value
  27. 60
  28. 20
  29. 240
  30. 54
  31. Well done, Arjun!
  32. 40
  33. 105
  34. 64
  35. 510
  36. 80
  37. Hello, Meera!
  38. 108
  39. 60
  40. 60

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