← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

  1. 120
  2. 100
  3. 40
  4. 6
  5. 75
  6. sends back a value
  7. to reuse code
  8. Hi, Meera!
  9. 100
  10. a parameter
  11. 30
  12. 190
  13. 100
  14. Well done, Zoya!
  15. 500
  16. 70
  17. 12
  18. a named block of code
  19. 380
  20. 50
  21. greet()
  22. 30
  23. Hi, Aman!
  24. 52
  25. 66
  26. 40
  27. 50
  28. 50
  29. Hello, Meera!
  30. 80
  31. 270
  32. 48
  33. 0
  34. 180
  35. 60
  36. Hello, Riya!
  37. 90
  38. 69
  39. 75
  40. 325

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