← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

  1. 24
  2. 50
  3. a named block of code
  4. 46
  5. 260
  6. 100
  7. Hi, Zoya!
  8. 120
  9. 54
  10. 50
  11. Welcome, Aman!
  12. to reuse code
  13. 120
  14. 20
  15. sends back a value
  16. a parameter
  17. 9
  18. 60
  19. Hello, Meera!
  20. 30
  21. 16
  22. greet()
  23. Hi, Kabir!
  24. 30
  25. 20
  26. 20
  27. 285
  28. 60
  29. 40
  30. 30
  31. 30
  32. 280
  33. 425
  34. 15
  35. 180
  36. 60
  37. 60
  38. 102
  39. 140
  40. 100

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