← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

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

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