Tuesday, June 28, 2011

javascript tricky questions, their answers & explanation

Questions below are just tricky questions. I try to answer them simply for fun. 

1.
(function () { 
    return typeof arguments; 
})(); 

A. "object"
B. "array"
C. "arguments"
D. "undefined"

Answer: B
Reason: Javascript function is actually object so they have properties. arguments is a property of javascript function. It consists of an array of all the arguments passed to a function. Although arguments property is an array, typeof arguments  will simply tell 'object'. Although this information is not that useful, it is correct, because javascript array is also object.

2.
var f = function g() {
        return 23;
    };
typeof g();

A. "number"
B. "undefined"
C. "function"
D. Error

Answer: D
Reason: This is a named function expression. 'g' is the function name. The important part is this name can be used by the code inside the function(g()) to refer to the function itself, but the code outside the function cannot see it at all.

3.
(function (x) {
    delete x;
    return x;
})(1);

A. 1
B. null
C. undefined
D. Error

Answer: A
Reason: note that delete can only delete object properties. You can't use it to delete variables, no matter the variables are functions or objects.

4.
(function f(f) {
    return typeof f();
})(function () {
    return 1;
});

A. "number"
B. "undefined"
C. "function"
D. Error

Answer: A
Reason: Not hard. It is just the code not easy to read. 

5.
var foo = {
    bar: function () {
        return this.baz;
    },
    baz: 1
};
(function () {
    return typeof arguments[0]();
})(foo.bar);

A. "undefined"
B. "object"
C. "number"
D. "function"

Answer:A
Reason: Don't be fooled by picking C. This is a typicl callback scope issue: arguments[0]() is a callback just like you try to call foo.bar(); However, inside foo.bar, there is a keyword 'this'. We must remember that the callback method uses 'this' to refer to the object it belongs to. So, where is 'this' pointing to? We must note the way we do callback here: arguments[0](). 'this' is actully referring to the object 'arguments'! If we modify the code a little bit:
(function () {
    arguments.baz = 3;
    return typeof arguments[0]();
})(foo.bar);

Well this time we will get 'number'. I made a post about javascript callback function scope: http://hengrui-li.blogspot.com/2011/05/javascript-callback-function-scope.html

6.
var f = (function f() {
    return "1";
}, function g() {
    return 2;
})();
typeof f;

A. "string"
B. "number"
C. "function"
D. "undefined"

Answer: B
Reason: immediate function. f === 2.

7.
var x = 1;
if (function f() {}) {
    x += typeof f;
}
x;

A. 1
B. "1function"
C. "1undefined"
D. NaN

Answer:C
Reason:This is exactly like:
var x = 1;
if (g=function f(){}) {
    x += typeof f;
}
x;

8. var x = [typeof x, typeof y][1];
typeof typeof x;

A. "number"
B. "string"
C. "undefined"
D. "object"

Answer: B
Reason: Actually, you don't really have to figure out what x really is. No matter what it is, typeof x must be a string indicating what x type is. It is either 'undefined' ,'string', 'object' or 'number', typeof typeof x must be string then.

9.
(function (foo) {
    return typeof foo.bar;
})({
    foo: {
        bar: 1
    }
});

A "undefined"
B "object" 
C "number"
D Error

Answer: A
Reason: another wholy tricky question. in return typeof foo.bar, foo is actally the object {foo:{bar:1}}.

10.
(function f() {
    function f() {
        return 1;
    }
    return f();

    function f() {
        return 2;
    }
})();

A.1 
B.2 
C.Error (e.g. "Too much recursion") 
D.undefined

Answer:B
Reason:function hoisting.

function f() {
    return f;
}
new f() instanceof f;

A.true 
B.false

Answer: B
Reason: simply remember in javascript, everything is a singleton. More about javascript singleton: http://hengrui-li.blogspot.com/2011/05/javascript-design-patterns-singleton.html

2 comments:

Anonymous said...

tricky, but these questions are really not intended for a js interview. why? even if one knows the answers does not tell anything his/her knowledge of architecutre

Susan said...

The following are lists of people by Net worth list: Lists of richest people in the world. Contents. 1 By nationality; 2 By region; 3 Miscellaneous; 4 Forbes lists.