← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

  1. 70
  2. a parameter
  3. Hi, Riya!
  4. a named block of code
  5. Hi, Zoya!
  6. 500
  7. 240
  8. 105
  9. 74
  10. 20
  11. Welcome, Riya!
  12. greet()
  13. 72
  14. 30
  15. 40
  16. sends back a value
  17. 60
  18. 225
  19. Well done, Aman!
  20. 350
  21. 30
  22. Hello, Kabir!
  23. to reuse code
  24. 120
  25. 140
  26. 40
  27. Hello, Zoya!
  28. 60
  29. 0
  30. 240
  31. Well done, Zoya!
  32. 57
  33. 69
  34. 20
  35. 200
  36. 80
  37. 45
  38. 12
  39. 21
  40. 60

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