← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

  1. 100
  2. 325
  3. 111
  4. a parameter
  5. Well done, Zoya!
  6. 100
  7. Hi, Kabir!
  8. to reuse code
  9. 40
  10. 300
  11. 400
  12. 30
  13. 22
  14. 40
  15. 50
  16. 60
  17. 27
  18. 0
  19. 52
  20. a named block of code
  21. 120
  22. 300
  23. 20
  24. 220
  25. sends back a value
  26. 340
  27. 110
  28. Well done, Aman!
  29. greet()
  30. 90
  31. 63
  32. 150
  33. 90
  34. 70
  35. 120
  36. 40
  37. 60
  38. 60
  39. 56
  40. 20

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