← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

  1. 30
  2. greet()
  3. sends back a value
  4. to reuse code
  5. Hi, Aman!
  6. 14
  7. Hi, Riya!
  8. 40
  9. a named block of code
  10. 425
  11. 70
  12. 500
  13. Well done, Zoya!
  14. 60
  15. 57
  16. 50
  17. Hello, Meera!
  18. Well done, Aman!
  19. 200
  20. 220
  21. 20
  22. 70
  23. a parameter
  24. 56
  25. 180
  26. 60
  27. 22
  28. Hi, Arjun!
  29. Well done, Meera!
  30. 58
  31. 400
  32. 48
  33. 24
  34. 100
  35. 46
  36. 28
  37. 12
  38. 100
  39. 9
  40. 30

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