JS quiz Flashcards

1
Q
Which  fn  will  be  called  from  line  10?
  1| if  (true)  {
  2|     function  fn()  {
  3|         return  10;
  4|     }
  5| }  else        {
  6|     function  fn()  {
  7|         return  20;
  8|     }
  9| }
10| fn();
A

answer is 20, because function is defined

twice during parsing, so one on line 6 one is used.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
Which  fn  will  be  called  from  line  11?
  1| var  fn;
  2| if  (false)  {
  3|     fn  =  function()  {
  4|         return  30;
  5|     };
  6| }  else  {
  7|     fn  =  function()  {
  8|         return  40;
  9|     };
10| }
11| fn();
A

answer is 40, because fn is assigned on

line 7, during execution.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
What  is  the  output  of  this  program?
  1| if  (false)  {
  2|     var  fn  =  function()  {
  3|         return  70;
  4|     };
  5| }  else  {
  6|     var  fn  =  function()  {
  7|         return  80;
  8|     };
  9| }
10| fn();
A

answer is 80, because fn is defined on line 6,

during execution of the true part.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
What  will  fpn  return?
  1| function  fpn(){
  2|     return 
  3|         {  2  +  3  };
  4| }
  5| fpn(2+3);
A

answer is undefined, because fpn returns undefined on line 2, a semicolon isn’t needed to end the statement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the output of this program, and what does it do in general?
(function (n,a,b) { return n>0 ?
arguments.callee(n-1,b,a+b) : a; })(10,0,1);

A

answer is 55

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
What  is  the  output  of  this  program?
  1| var  fn22  =  1;
  2| function  gn22()  {
  3|     if  (!fn22)  {
  4|         var  fn22  =  2;
  5|     }
  6|     console.log(fn22);
  7| }
  8| gn22();
A

answer is 2, because fn22 on line 1 is a global,

while fn22 on line 3 is different and local to gn22.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
What  is  the  output  of  this  program?
  1| var  a23  =  1;
  2| function  b24()  {
  3|     a23  =  10;
  4|     return;
  5|     function  a23()  {
  6|         return  11;
  7|     }
  8| }
  9| b24();
10| console.log(a23);
A

answer is 1, because inner function a23 on line 5
was hoisted and changed to 10 on line 3;
global a23=1 on line 1 is unaffected

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
What  is  the  output  of  this  program?
  1| var  a44  =  1;
  2| console.log(a44);
  3| if  (true)  {
  4|     var  a44  =  2;
  5|     console.log(a44);
  6| }
  7| console.log(a44);
A

answer is 1 2 1, because only one global var a44,

inner block doesn’t define a local variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
What  is  the  output  of  this  program?
  1| function  f28()  {
  2|     var  x56  =  94;
  3|     if  (false)  {
  4|         (function  ()  {
  5|             var  x56  =  83;
  6|             console.log(x56);
  7|         }());
  8|         console.log(x56);
  9|     }
10|     console.log(x56);
11| }
12| f28();
A
answer is 94, because anonymous  function  creates  a
new  local  var  x56=83, global  x56  remains  94.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
What  is  the  output  of  this  program?
  1| function  main()  {
  2|     try  {  f77();  }  catch(e)  {  console.log(e);  };
  3|     try  {  b78();  }  catch(e)  {  console.log(e);  };
  4|     var  f77  =  function  ()  {
  5|         console.log("f77");
  6|     }
  7|     function  b78()  {
  8|         console.log("b78");
  9|     }
10| }
11| main();
A

answer is Error, b78, because both declarations on line 4 and line 7 are hoisted to top,
but var f77 is only assigned on line 4, during execution.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
What  is  the  output  of  this  program?
  1| f90();
  2| function  f90()  {
  3|     console.log('f90');
  4| };
A

answer is f90, because function f90 hoisted to top

and available on line 1.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
What  is  the  output  of  this  program?
  1| f91();
  2| var  f91  =  function  ()  {
  3|     console.log('f91');
  4| };
A

answer is Error, because var f91 is hoisted, but

var f91 is used before being assigned.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
What  is  the  output  of  this  program?
  1|  var  x  =  0;
  2|  function  hn()  {
  3|          x++;
  4|          console.log(x);
  5|          return  function  hn()  {
  6|              x+=10;
  7|              console.log(x);
  8|          }
  9|  } 
10|  hn(hn())(hn())
A

answer is 1 2 3 13, because only one global variable x,
there are two hn functions in different scope.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
What  is  the  output  of  this  program?
Do  fn  gn  and  kn  update  the  same  num?
(What  is  a  closure?)
  1|  function  fn()  {
  2|      var  num  =  1;
  3|      var  gn  =  function()  {
  4|          console.log(num);
  5|          num+=10;
  6|      }
  7|      var  kn  =  function()  {
  8|          console.log(num);
  9|          num+=100;
10|      }
11|      num  +=  3000;
12|      return  [gn,kn];
13|  }
14|  gn  =  fn()[0];
15|  kn  =  fn()[1];
16|  gn(gn())*kn(kn())
A

answer is 3001 3011 3001 3101, because local var num is in a closure,
separate copies for gn and kn.

If you remove the var on line 2,
you get: 3001 3011 3021 3121.
If you move line 2 to line 1,
you get 6001 6011 6021 6121.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly