← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

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

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