← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

  1. 108
  2. 210
  3. to reuse code
  4. Hi, Aman!
  5. 62
  6. 60
  7. 30
  8. Welcome, Riya!
  9. 50
  10. 250
  11. 300
  12. a parameter
  13. 87
  14. 170
  15. a named block of code
  16. 20
  17. 69
  18. 105
  19. 150
  20. 80
  21. greet()
  22. Well done, Arjun!
  23. 30
  24. 40
  25. 120
  26. 280
  27. 39
  28. 20
  29. 40
  30. 21
  31. 84
  32. 90
  33. 60
  34. 150
  35. 15
  36. sends back a value
  37. 50
  38. 180
  39. 60
  40. 225

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