Javascript Quiz on variables, function, hoisting, scope, closure, and currying.
You can try the examples in 'chrome inspector' or 'node js'.
Number | Question | Answer | Your answer |
1 | 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(); |
1 10 2 Error 3 20 |
|
2 | 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(); |
1 40 2 30 3 Error |
|
3 | 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(); |
1 80 2 Error 3 70 |
|
4 | What will fpn return? 1| function fpn(){ 2| return 3| { 2 + 3 }; 4| } 5| fpn(2+3); |
1 object 2 2+3 3 anonymous object 4 5 5 undefined |
|
5 | What is the output of this program? 1| console.log((function (x) { \ 2| var x=1+x; return x; })(1)) |
1 undefined 2 1 3 2 4 stack overflow |
|
6 | What is the output of this program, and what does it do in general? 1| (function (n,a,b) { return n>0 ? \ 2| arguments.callee(n-1,b,a+b) : a; }) \ 3| (10,0,1); |
1 stack overflow 2 undefined 3 55 4 10 |
|
7 | 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(); |
1 2 2 1 |
|
8 | 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); |
1 Error 2 10 3 1 |
|
9 | 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); |
1 1 2 1 2 Error 3 1 1 1 4 1 2 2 5 2 2 2 |
|
10 | What is the output of this program? 1| function f25() { 2| var x55 = 91; 3| if (x55) { 4| (function () { 5| var x55 = 82; 6| console.log(x55); 7| }()); 8| console.log(x55); 9| } 10| console.log(x55); 11| } 12| f25(); |
1 82 82 82 2 82 91 91 3 82 82 91 |
|
Number | Question | Answer | Your answer |
11 | 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(); |
1 94 2 83 |
|
12 | 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(); |
1 Error, b78 2 Error, Error 3 f77, Error 4 f77, b78 |
|
13 | What is the output of this program? 1| f90(); 2| function f90() { 3| console.log('f90'); 4| }; |
1 f90 2 Error. |
|
14 | What is the output of this program? 1| f91(); 2| var f91 = function () { 3| console.log('f91'); 4| }; |
1 f91 2 Error |
|
15 | 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()) |
1 1 2 12 22 2 Error 3 1 2 3 13 4 1 11 12 22 |
|
16 | CWhat 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()) |
1 3001 3011 3021 3121 2 6001 6011 6021 6121 3 Error 4 3001 3011 3001 3101 |