Advertisement
pavelperc

kotlin-small-tasks

Apr 2nd, 2019
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. --------- Tasks for if ---------
  2.  
  3.  
  4.  
  5. //1. just general
  6.  
  7. one girl has a mother, a father, a sister and a brother.
  8.  
  9. each person has sex and two parents.
  10.  
  11. how many ifs should we use to find out relationship between the girl and her random relative?
  12.  
  13. answer: 3
  14. example:
  15. if (me.F = he.F)
  16. if (he.male) "brother"
  17. else "sister"
  18. else if (me.F = he) "father"
  19. else "mother"
  20.  
  21. (may be expanded to other relatives, like granny, aunt and so on... It becomes much more interesting then)
  22.  
  23.  
  24. //2. Theory about braces
  25. Choose all that apply.
  26.  
  27. when we want to return the if result as an expression, we must omit curly braces. (-)
  28.  
  29. when we want to use if without else branch we must include curly braces. (-)
  30.  
  31. we must use curly braces if we have several statements inside if or else branch. (+)
  32.  
  33. when we use if not as an expression we must include curly braces. (-)
  34.  
  35. we can't omit round brackets around boolean condition if we use if as an expression (+)
  36.  
  37.  
  38. //3. More theory:
  39. Choose all that apply.
  40.  
  41. the if-else-if branch can omit else branch. (+)
  42.  
  43. we can use if-else-if-else branch as an expression (+)
  44.  
  45. we can not nest non expressional style ifs inside expressional style one (-)
  46.  
  47. all expression style if branches must return the same type. (-)
  48.  
  49. expression style if must have else branch (+)
  50.  
  51. //4. some code exapmle
  52.  
  53. What will be the result of this code
  54.  
  55. println(if (if (false) true else false) if (true) { true } else false)
  56.  
  57. true(+)
  58. false(-)
  59. It won't compile because of the first if(+)
  60. It won't compile because of the second if(-)
  61. It won't compile because of the third if(-)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement