← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

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

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