← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

  1. 100
  2. a named block of code
  3. 20
  4. 20
  5. Well done, Zoya!
  6. Hello, Kabir!
  7. Hi, Aman!
  8. 70
  9. 60
  10. greet()
  11. Well done, Meera!
  12. a parameter
  13. 80
  14. 36
  15. 9
  16. 6
  17. 10
  18. 38
  19. 360
  20. 72
  21. 50
  22. Well done, Kabir!
  23. Welcome, Riya!
  24. 90
  25. 70
  26. 100
  27. 60
  28. 70
  29. to reuse code
  30. 60
  31. Hi, Kabir!
  32. 21
  33. Hello, Riya!
  34. 240
  35. Well done, Aman!
  36. Hi, Riya!
  37. 60
  38. 12
  39. sends back a value
  40. 80

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