← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

  1. a named block of code
  2. Welcome, Aman!
  3. 100
  4. 60
  5. Hi, Meera!
  6. 12
  7. 114
  8. to reuse code
  9. 180
  10. 80
  11. 6
  12. 99
  13. 320
  14. sends back a value
  15. 60
  16. greet()
  17. 120
  18. 20
  19. 120
  20. Well done, Kabir!
  21. 40
  22. 50
  23. 60
  24. 140
  25. Hello, Riya!
  26. 0
  27. 54
  28. 100
  29. 40
  30. a parameter
  31. 500
  32. Well done, Meera!
  33. Well done, Riya!
  34. 80
  35. 40
  36. 74
  37. 16
  38. 270
  39. 68
  40. 27

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