My JS Quiz: the questions

2011-05-23

I love js quizes. It has been too long since I've seen one pass by and I've saved up a few oddities while creating my parsers and Zeon.js. So at first I planned on creating some fancy form based thingie, but instead I'll just put the questions here and let you think of the answers for a bit. Maybe you can answer them on your own blog. I'll post the answers in a new blog post here in about a week. Edit: added link at the bottom..

I should warn you, these are specification related questions about stuff you usually don't think about or don't even have to know about, unless you like to work with the specification like I do :) I will use ES5 when posting the answers.

The questions are framed in such a way that they don't try to give away the answers for a previous question, although that's very difficult in some cases. In any case, don't assume a follow up question to give you the answer for a previous or following question ;) I can reveal you that I did pull some fancy reversed psychology trick on you people at least once ;) So be warned...

Try to answer them without actually testing it first. It's all the more fun that way. Then after you gave the answer, test it in your console :)

Assume a clean environment, NaN, undefined and such are what they ought to be. Answers such as "who knows" are never valid here ;)

This is not an actual submission form! You cannot submit the results! The inputs are merely there for your convience of looking back.




1: What's the scope lookup depth for the variables e,f,g and h when they are actually used?
So when they are accessed in the inner-most catch. For instance, x in var x; function(){ x; } has a scope lookup depth of two. The function scope and the global scope.

Code:
var f;
try{}
catch(e){
try{
var h;
} catch(g){
e,f;
g,h;
}
}





2: How do you get two succeeding forward slashes in a js source without creating a single line comment or having them be the body of another token?
So "//" is not the answer, neither is /\// and /* // */, etc. No space allowed between the slashes. Two answers possible (that I know of).

1
2



3: Is the semi after while in a do ... while required?
So is do .. while(..) x(); valid?

yes no



4: When parsing tokens, which token are you more likely to encounter: the regular expression token or the division token?
For instance, a token with a semi-colon only occurs at the end of a statement and within a for-header...

regex division



5: What should be the value of e after the following snippet?

Code:
var e = 5;
try {
var e = 6;
} catch(e) {
var e = 7;
}






6: For how many different type of things is the forward slash (/) used in js?
For instance, the + is used for addition and string concatenation.





7: Name them ;) (from 6)





8: Is l33t a valid regular expression flag?
So is var r = /.../l33t; valid js?

yes no



9: May a regular expression contain newlines?
The actual character, not the escape sequence \n

yes no



10: May strings contain newlines?
The actual character, not the escape sequence \n

yes no



11: Is the space between 5 and in required?
for (5 in x) ..

yes no



12: Is the space between return and a string required?
return "foo";

yes no



13: Is the space between a division (/) and a regular expression required?
5 / /.*/

yes no



14: Is the space between return and a number required?
return 10;



15: What regular expression matches the empty string?
So what should regex be to make regex.test("") be true.





16: Can a backslash (\) occur somewhere in a variable name in the source code?

yes no



17: What is the result of f after this statement?

Code:
new function f(){
f = 5;
};






18: Whats the result of x after running this snippet?

Code:
var x = 5;
function f(){
x = x;
var x;
}
f();






19: May the default clause of a switch be placed before the last case clause?

yes no



20.a: If yes, are the remaining clauses used, ignored or do they cause an error?

used ignored I said no!



20.b: If no, what happens if you do anyways?





21: How many arguments may an accessor getter have?
var x = {get some(a,b,c,...?){ console.log('property read'); }};





22: How many arguments may an accessor setter have?
var x = {set some(a,b,c,...?){ console.log('property write'); }};





23: Are the parentheses required when invoking constructors?
new Person();

yes no



24: Are the parenthesis required when invoking functions?
Person();

yes no



25: Are the parenthesis required when invoking functions which are also constructors?
Person();

yes no



26: What happens if you declare a variable twice in the same scope?

Code:
var x = 5;
var x = "foo";






27: Why does the empty array equal false?
[] == false





28: Must a number start with a digit?
A digit is 0 ~ 9

yes no



29: Why will 2.toString() throw an error?





30: How do you fix the problem at #29?
There are three ways of getting around it (that I know of)

1
2
3



31: What's the longest expression in js you can think of that's still valid and only makes use of unique keywords, single character variables and (single) spaces?
Only alphabetic characters and spaces allowed. For instance, if is a keyword, but not valid in an expression. Neither is function because it requires parens and brackets.





32: What's the name of the property defined in following object literal?
{.3e-10:42}





What's the result value of ... after the expressions that follow it?


Assume the variable already exists if it's not explicitly defined



33: x after x=3?x=4:x=5





34: x in var x = new Array(5).map(function(){ return 0; })





35: x in var x=5,3





36: x in x=1,2





37: c in var C = function(){return C;}; var c = new new C;





38: f in function F(){ return 5; } var f = new F;





39: obj in var obj; (obj={a:1}).b=2;
Thanks to @cowboy





40: x in (x) = 5;





41: x in (1,x) = 5;





42: x in (x,x) = 5;





43: b in var a = {f:function(){return 1;}}; var b = a.f();





44: b in var a = {f:function(){return 1;}}; var b = (a.f)();





45: b in var a = {f:function(){return 1;}}; var b = (1,a.f)();





46: b in var n = NaN; b = n === n;





47: b in var b = NaN === new Number(NaN);
Thanks to @jdalton





48: b in var b = isNaN(new Number(NaN));
Thanks to @jdalton





49: b in var b = (function(){return this}).call(1) instanceof Number;
Thanks to @tobeytailor



Which of the following "scripts" are valid?


Assume they would be included as a complete script, as given. Will an error be thrown by the parser?

50: function f(){ break; }
51: x: function f(){}
52: x: switch(y){ break x; }
53: x: x: debugger;
54: x: while (y) x: continue x;
55: x: var x;
56: x: var x = x; break x;
57: x: try { throw y; } catch(e) { break x; }
58: try { throw y; } catch(e) { x: } break x;
59: x: try { throw x; } catch(e) { break x; }
60: if (x) a: debugger; if (y) break a;
61: x:var x;break x;
62: a = b && c = d
63: a & b = c
64: a + b = c
65: a = b ? c = d : e = f
66: a -- b
67: a - - b
68: a- -b
69: x: break x;

Answers follow in about a week... can be found here with explanations. I hope you will learn at least something from this quiz, regardless of your current level :)

If you spot an error, are convinced I meant something else with a certain question or are simply confused by a question please let me know on twitter :)