← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

  1. 30
  2. 50
  3. 50
  4. sends back a value
  5. 60
  6. 60
  7. greet()
  8. Hi, Kabir!
  9. Hi, Riya!
  10. 54
  11. to reuse code
  12. 60
  13. a named block of code
  14. Hello, Kabir!
  15. 50
  16. 475
  17. 480
  18. 22
  19. 150
  20. 80
  21. 16
  22. 30
  23. Well done, Kabir!
  24. 70
  25. 66
  26. 20
  27. 80
  28. 20
  29. 40
  30. 150
  31. 280
  32. 20
  33. 250
  34. 30
  35. 80
  36. 40
  37. 60
  38. 105
  39. 20
  40. 30

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