← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

  1. 80
  2. 180
  3. 30
  4. to reuse code
  5. 90
  6. 20
  7. Well done, Riya!
  8. Well done, Meera!
  9. Hello, Aman!
  10. sends back a value
  11. Hi, Meera!
  12. 60
  13. 500
  14. 30
  15. 105
  16. 117
  17. Welcome, Riya!
  18. 20
  19. greet()
  20. 240
  21. 90
  22. Well done, Arjun!
  23. Hi, Riya!
  24. 175
  25. a parameter
  26. 42
  27. a named block of code
  28. 90
  29. 70
  30. 66
  31. 60
  32. 60
  33. 70
  34. 42
  35. 40
  36. 12
  37. 22
  38. 60
  39. 32
  40. 45

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