← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

  1. 170
  2. Well done, Kabir!
  3. 15
  4. 75
  5. a named block of code
  6. 60
  7. 60
  8. sends back a value
  9. 70
  10. 64
  11. 90
  12. a parameter
  13. 40
  14. 150
  15. Hi, Riya!
  16. 80
  17. 117
  18. 60
  19. 80
  20. 280
  21. 210
  22. 60
  23. 81
  24. 20
  25. 30
  26. Hello, Riya!
  27. 80
  28. 30
  29. 50
  30. 100
  31. 200
  32. 135
  33. 90
  34. to reuse code
  35. 70
  36. Hello, Meera!
  37. Hi, Kabir!
  38. Hi, Aman!
  39. 480
  40. 24

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