← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

  1. 40
  2. to reuse code
  3. 70
  4. 76
  5. 100
  6. sends back a value
  7. 20
  8. 12
  9. 100
  10. 90
  11. 60
  12. a named block of code
  13. Welcome, Arjun!
  14. 160
  15. 200
  16. 40
  17. 105
  18. greet()
  19. 60
  20. 60
  21. Hello, Zoya!
  22. Welcome, Meera!
  23. 40
  24. 100
  25. 60
  26. 20
  27. 120
  28. Welcome, Aman!
  29. 40
  30. 52
  31. 21
  32. 20
  33. 8
  34. 54
  35. 80
  36. 30
  37. Hello, Aman!
  38. 45
  39. 60
  40. a parameter

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