← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

  1. 33
  2. Hi, Riya!
  3. 10
  4. to reuse code
  5. 66
  6. sends back a value
  7. a named block of code
  8. Welcome, Aman!
  9. Hello, Meera!
  10. 63
  11. 90
  12. a parameter
  13. 275
  14. 120
  15. 60
  16. 70
  17. Welcome, Kabir!
  18. 60
  19. 475
  20. 45
  21. 72
  22. 80
  23. greet()
  24. 250
  25. 30
  26. 51
  27. 60
  28. 46
  29. 68
  30. 110
  31. 120
  32. 0
  33. 81
  34. Welcome, Riya!
  35. 80
  36. 30
  37. 6
  38. Hi, Zoya!
  39. 50
  40. Well done, Riya!

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