← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

  1. 50
  2. 81
  3. 74
  4. sends back a value
  5. 135
  6. 0
  7. a parameter
  8. Hello, Kabir!
  9. 80
  10. to reuse code
  11. a named block of code
  12. 120
  13. 100
  14. 64
  15. 105
  16. 56
  17. 475
  18. 30
  19. 40
  20. Hello, Riya!
  21. 50
  22. 175
  23. 70
  24. 16
  25. 54
  26. 110
  27. Hello, Meera!
  28. Hi, Meera!
  29. 22
  30. 120
  31. 160
  32. 32
  33. 60
  34. 24
  35. 60
  36. 46
  37. greet()
  38. 44
  39. 130
  40. 8

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