← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

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

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