← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

  1. to reuse code
  2. Hi, Aman!
  3. a parameter
  4. 210
  5. 70
  6. 40
  7. 0
  8. 52
  9. 140
  10. 30
  11. 44
  12. 90
  13. 150
  14. 30
  15. 64
  16. greet()
  17. 0
  18. 69
  19. Well done, Arjun!
  20. 8
  21. 60
  22. 40
  23. 90
  24. 80
  25. sends back a value
  26. 100
  27. 50
  28. 40
  29. 80
  30. 110
  31. Hi, Zoya!
  32. 16
  33. a named block of code
  34. 200
  35. 32
  36. 22
  37. 20
  38. 66
  39. 76
  40. Hello, Meera!

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