← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

  1. 30
  2. to reuse code
  3. 72
  4. 100
  5. 22
  6. 80
  7. 210
  8. 70
  9. 480
  10. Hello, Riya!
  11. sends back a value
  12. 50
  13. Hello, Zoya!
  14. 60
  15. 90
  16. 60
  17. 40
  18. 30
  19. 100
  20. 80
  21. 51
  22. 16
  23. 180
  24. Hello, Arjun!
  25. 160
  26. 150
  27. a parameter
  28. a named block of code
  29. Hello, Meera!
  30. greet()
  31. 60
  32. 15
  33. 20
  34. 170
  35. 90
  36. 80
  37. 80
  38. 6
  39. 30
  40. 325

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