← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

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

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