← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

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

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