← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

  1. 57
  2. a named block of code
  3. 60
  4. Welcome, Meera!
  5. 0
  6. 60
  7. 58
  8. 20
  9. 100
  10. 120
  11. 100
  12. 60
  13. 30
  14. greet()
  15. 30
  16. 69
  17. to reuse code
  18. 40
  19. 18
  20. 18
  21. sends back a value
  22. 40
  23. 225
  24. Hello, Kabir!
  25. 165
  26. 60
  27. 30
  28. 100
  29. 75
  30. 0
  31. 24
  32. Hi, Meera!
  33. 30
  34. 50
  35. 60
  36. 240
  37. Hello, Meera!
  38. 70
  39. 110
  40. a parameter

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