← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

  1. 12
  2. a named block of code
  3. 30
  4. 0
  5. Well done, Aman!
  6. 110
  7. 30
  8. sends back a value
  9. 80
  10. 40
  11. 60
  12. 80
  13. Hi, Kabir!
  14. 255
  15. 75
  16. 225
  17. 100
  18. to reuse code
  19. 70
  20. 30
  21. Hi, Aman!
  22. Welcome, Arjun!
  23. 450
  24. 24
  25. 50
  26. Well done, Kabir!
  27. Hi, Riya!
  28. 30
  29. 21
  30. 120
  31. 80
  32. 60
  33. 150
  34. a parameter
  35. 50
  36. 180
  37. 52
  38. 80
  39. 72
  40. greet()

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