← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

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

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