← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

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

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