← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

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

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