← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

  1. 120
  2. a named block of code
  3. Hi, Kabir!
  4. 20
  5. 60
  6. 66
  7. 340
  8. 22
  9. 255
  10. 42
  11. 450
  12. 63
  13. greet()
  14. a parameter
  15. Hi, Riya!
  16. 50
  17. 140
  18. 220
  19. 60
  20. Hello, Zoya!
  21. Welcome, Aman!
  22. 54
  23. 275
  24. Well done, Aman!
  25. 90
  26. 30
  27. Well done, Meera!
  28. 70
  29. 48
  30. 36
  31. 27
  32. 270
  33. Well done, Arjun!
  34. Well done, Riya!
  35. to reuse code
  36. 32
  37. sends back a value
  38. 120
  39. Welcome, Kabir!
  40. 16

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