← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

  1. Welcome, Arjun!
  2. a named block of code
  3. 90
  4. 76
  5. greet()
  6. sends back a value
  7. 105
  8. 90
  9. 50
  10. 72
  11. Hello, Meera!
  12. 570
  13. 80
  14. 30
  15. 0
  16. 120
  17. to reuse code
  18. 60
  19. 60
  20. 62
  21. 510
  22. 20
  23. 180
  24. 50
  25. 60
  26. Hello, Kabir!
  27. Hi, Zoya!
  28. 111
  29. 0
  30. 0
  31. 40
  32. 69
  33. 36
  34. 57
  35. 24
  36. 80
  37. 100
  38. Hi, Riya!
  39. 110
  40. 150

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