← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

  1. 135
  2. to reuse code
  3. 210
  4. a named block of code
  5. 60
  6. 0
  7. 30
  8. 70
  9. greet()
  10. 90
  11. 390
  12. 76
  13. a parameter
  14. Hello, Arjun!
  15. Hi, Zoya!
  16. 140
  17. 120
  18. 34
  19. 28
  20. 30
  21. 12
  22. 20
  23. sends back a value
  24. 225
  25. 105
  26. 20
  27. 18
  28. Hi, Aman!
  29. 0
  30. 66
  31. 200
  32. 45
  33. 40
  34. 70
  35. 120
  36. 70
  37. 60
  38. 60
  39. 90
  40. 300

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