620 likes | 756 Views
http://telerikacademy.com. JavaScript Test Preparation. Doncho Minkov. Telerik Software Academy. academy.telerik.com. Technical Trainer. Data Types. Question. What is the value of the variable date after the assignment ? undefined 2 May 2013 null 16 February 2012
E N D
http://telerikacademy.com JavaScript Test Preparation Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer
Question • What is the value of the variable date after the assignment? • undefined • 2 May 2013 • null • 16 February 2012 • the code will throw an exception • 2 April 2013 • 2 December 2013 var date = new Date(2012, 16, 2);
Answer • Months in Javascript are assigned from index 0 • If we order all months, their "indices" will be: • 0-> January • 1-> February • 11-> December) • We pass as a second argument in the Date object constructor a value greater than 11 • JavaScript adds a whole year (2013) • The remaining value 4 (= 16 months – 12 months) is interpreted as May
Question • What will be the result of the following code? • null • 1 • NaN • Undefined • The code will throw an exception • 9007199254740992 var numberOne = 1; var nullValue = null; var result = numberOne + nullValue; document.writeln(result);
Question • Which expression should be used if we want to find the position at which the word "Telerik" appears in? • text.match("Telerik") • text.substring("Telerik") • text.find("Telerik") • text.indexOf("Telerik") • text.getPosition("Telerik") • text["Telerik"] var text = "We all love Telerik Academy";
Question • What will be the result after executing the following code? • Number • Null • Object • NaN • Undefined • The code will throw an exception var value = null; console.log(typeof value);
Question • What will be the output of the following source code? (ignore the time and time zone information) • undefined | 4 | undefined • January 1 2012 | 4 | telerik1 • February 1 20121 | 4 | telerik1 • Undefined | 4 | telerik1 • January 1 2012 | 4 | telerik • January 1 20121 | 4 | telerik1 var container = [new Date(2012, 1, 1), 3, "telerik"]; for (i = 0; i < container.length; i++){ container[i] = container[i] + 1; document.write(container[i] + " | "); }
Answer • Since in JS value cannot be added to a date, it is interpreted as string • So when we add the value "1" to the date, "1" is appended to the string "February 1 2012" • The result of 3 + 1 is 4 • When working with string, the operators "+" means concatenation • JS interprets the value 1 as a string "1" and appends it to "telerik", so it becomes "telerik1".
Question • What is the output after executing the code: • 1 2 3 4 5 6 7 8 9 10 10 • 10 • 1 2 3 4 5 6 7 8 9 10 11 • The code will throw an exception for misusing the "i" variable • 10 10 10 10 10 10 10 10 10 10 10 i = 10; for (var i = 1; i <= 10; i++){ document.write(i + " "); } document.write(i);
Question • What will be the result of this code? var a = 1, var b = 1; console.log( a + b ); console.log( a + b++ ); console.log( a + b ); console.log( a + (++b) ); console.log( a + b ); • 2, 3, 3, 4, 4 • 2, 3, 3, 3, 4 • 2, 2, 3, 3, 4 • 11, 11, 12, 13, 13 • 2, 2, 3, 4, 4 • nothing will be printed • depends of the JS version • syntax error (the code is incorrect)
Question • What will be the result of this code? • 4 3 • 4.0 3.0 • 4 3.6666666666666665 • 4.0 3.6666666666666665 • 4 3.67 • syntax error (the code is incorrect) console.log(12 / 3); // result: 4 console.log(11 / 3); // result: 3
Question • What will be the result of this code? • 0 0 0 • 0.0 0.0 0.0 • -Infinity, NaN, Infinity • -Infinity, Infinity, Infinity • runtime error: DivideByZeroException • syntax error (the code is incorrect) console.log(-1.5 / 0.0); console.log(0.00 / 0.0); console.log(1.50 / 0.0);
Question • What will be the result of this code? var a = null; console.log(!a); console.log(a || true); console.log(!a && false); • false, false, false • true, true, true • true, true, false • null, true, null • true, true, null • null, null, null • false, false, true • 0, false, true • syntax error (the code is incorrect)
Question • What will be the result of this code? var a = 1, b = true; console.log(a != b); console.log(a === b); console.log(!(a == a)); console.log(a > b); console.log(a != b++); • false, false, true, false, false • false, false, false, true, false • false, false, false, false, false • false, false, false, false, true • syntax error (the code is incorrect)
Question • What will be the result of this code? var one = "2", two = "1", three = 3; console.log(one + two); console.log(one + three); console.log(three + 1); • 21, 23, 4 • 3, 4, 4 • 12, 13, 31 • 21, 23, 31 • 3, 5, 4 • syntax error (the code is incorrect)
Question • What will be the result of this code? var a = 6, b = 4; console.log(a > b ? "a>b" : "b>=a"); console.log(typeof(2)); console.log((a+b)/2); console.log(Number("33.00")); • a>b, number, 5, 33 • a>b, int, 5, 33 • a>b, number, 5.0, 33 • a>b, number, 5.0, 33.0 • a>b, integer, 5, 33 • syntax error (the code is incorrect)
Question • What will be the result of this code? var r = 1 / (true + 1); var perimeter = 2 * Math.PI * r; console.log(Math.round(perimeter*100)/100); • NaN • Infinity • 314 • 628 • 3.14 • 6.28 • syntax error (the code is incorrect)
Question • What will be the result of this code? • true • false • 0 • 1 • nothing will be printed • syntax error (the code is incorrect) var result = (5 <= 6) && (7 > 3) || (1 >= 2); if result console.log(!result);
Question • What will be the result of this code? • true, true • true, false • false, true • false, false • false • syntax error (the code is incorrect) var a = true, b = false; console.log( !((a && b) + ', ' + (!a || !b)) );
Question • What will be the result of this code? • true • false • 0 • 1 • syntax error (the code is incorrect) var a=50, b="51"; console.log(a<b - 1);
Question • What will be the result of this code? • 50 • 51 • true • false • syntax error (the code is incorrect) var a=50, b=51; if (a>=b) console.log(a) else console.log(b);
Question • What will be the result of this code? • a < b == c • nothing will be printed • syntax error (the code is incorrect) var a=1, b=2, c=1; if ((a < b) == c) { console.log("a < b == c"); }
Question • What will be the result of this code? • X • Y • Z • err • syntax error (the code is incorrect) var ch = 'y' - 1; if (ch == 'Y' || ch == 'y') console.log("Y"); else if (ch == 'X' || ch == 'x') console.log("X"); else if (ch == 'Z' || ch == 'z') console.log("E"); else console.log("err");
Question • What will be the result of this code? int day = "Monday"; switch (day) { case 1: console.log("Monday"); break; case 2: console.log("Tuesday"); break; case 3: console.log("Wednesday"); break; default: console.log("Error!"); break; } • Monday • Tuesday • Wednesday • Error! • syntax error (the code is incorrect)
Question • What will be the result of this code? day = 2; switch (day) { case 1: console.log("Monday"); break; case 2: console.log("Tuesday"); break; case 3: console.log("Wednesday"); break; default: console.log("Error!"); break; } • Monday • Tuesday • Wednesday • Error! • syntax error (the code is incorrect)
Question • What will be the result of this code? day = "2"; switch (day) { case 1: console.log("Monday"); break; case 2: console.log("Tuesday"); break; case 3: console.log("Wednesday"); break; default: console.log("Error!"); break; } • Monday • Tuesday • Wednesday • Error! • syntax error (the code is incorrect)
Question • What will be the result of this code? var day = "Wed"; switch (day) { case "Mon": console.log("Monday"); break; case "Tue": console.log("Tuesday"); break; case "Wed": console.log("Wednesday"); break; default: console.log("Error!"); break; } • Monday • Tuesday • Wednesday • Error! • syntax error (the code is incorrect)
Question • What will be the result of this code? var day = 1; switch (day) { case 1: console.log("Mon"); case 2: console.log("Tue"); default: console.log("Err"); } • MonTueErr • Mon • Tue • Err • syntax error (the code is incorrect)
Question • Which of the following is the correct way to declare an array in JavaScript? var arr = new Array("0":1,"1":2,"1":3) var arr = new [1,2,3] var arr = new ([1,2,3]); var arr = new Array(1,2,3) var arr = new Array;
Answer • Initialing an array in JavaScript can be done in three ways: • Using new Array(elements): • Using new Array(initialLength): • Using array literals: var arr = new Array(1,2,3,4,5); var arr = new Array(10); var arr = [1,2,3,4,5];
Question • What will the following script result in? • An exception will be thrown • The second line won’t be executed • A pop-up box with text "undefined" will appear • A pop-up box with text "2" will appear • A pop-up box with text "0" will appear var arr = new Array(2); alert(arr[0]);
Answer • The code initializes an array of 2 elements • No element has a set value • So all elements are "undefined" • alert(undefined) will produce a pop-up box with the text "undefined" • The code is valid, so no exceptions will be thrown
Question • What will the following script result in? • An exception will be thrown • A pop-up box with text "undefined" will appear • A pop-up box with text "7" will appear • A pop-up box with text "6" will appear var arr = [1, 2, 7] arr[3] = 6; alert(arr[3]);
Answer • The code initializes an array with the elements 1, 2 and 7 • Writing to index 3 causes expansion of the array • Element at index 3 is set to 6 by the script • We "alert" the element at index 3 • A pop-up box with the text "6" appears
Question • What will the following script make the arrarray look like? • won’t change • won’t change and an exception will be thrown • will be: • will be: var arr = [1, 2, 7]; var i = 0; for(i=6; i>0; i--);{ arr[i] = i} arr arr [0, 2, 7] arr arr [1, 1, 2, 3, 4, 5, 6]
Answer • There’s a semi-colon after the loop • The only thing the loop changes is • After the loop, is 0 (zero) • will set to i i 0 {arr[i] = i} arr[0]
Question • What will the following script result in? • A pop-up with the text "false" will appear • A pop-up with the text "true" will appear • A pop-up with the text "undefined" will appear • The code will stop execution when it reaches the third line • An exception will be thrown var first = [1, 2, 7, 2, 5]; var second = [1, 7, 5]; var res = (first == second.join(",2,")) alert(res);
Answer • The join operation return a string from array • By inserting ",2," between every two sequential elements • In this script, the resulting string is "1,2,7,2,5" • Comparing array to a string does a .toString() on the array • .toString() on the array results in "1,2,7,2,5" • "1,2,7,2,5" == "1,2,7,2,5" returns true second first
Question • What will the following script print on the console? • undefined undefinedundefined 1 2 • 0 0 0 1 2 • 1 1 0 0 0 • 1 2 undefined undefinedundefined • 1 2 var arr = new Array(3); arr.push(1); arr.push(2); for(var i = 0; i < arr.length; i++) console.log(arr[i]);
Answer • The array is initialized with 3 undefined elements • The push operation places elements at the end of the array • The last two elements become 1 and 2 • The array length is 5 • The final form of the array is: [undefined, undefined, undefined, 1, 2]
Question • What will running the following script result in? • An endless loop • The array will be • The array will be • The array will be • The array will be • The array will be var arr = new Array(1, 2, 3, 4, 5); for(var i = 0; i < arr.length; i++){ arr.unshift(arr[i]); arr.splice(arr[i+1], 1); } console.log(arr); 1,2,3,4,5 5,4,3,2,1,2,3,4,5 1,1,1,1,1,2,2,2,3,3,3,3,4,4,5 5,4,3,2,1 1
Answer • The array is initialized with 1, 2, 3, 4, 5 • The script takes the current element • Inserts copy in the beginning • All previous elements move 1 index forward • Meaning former "current element" is now at i+1 • Splices 1 position at i+1 • Meaning delete the former "current element" • Repeats this 5 times (array doesn’t change size) • Finally, array is reversed (warning: very slow)
Question • What will running the following script result in? • An exception will be thrown • The code will stop execution after the second line • A pop-up with the text "2,22,7" will appear • A pop-up with the text "-1,2,7,22" will appear • A pop-up with the text "undefined" will appear • A pop-up with the text "-1,2,22,7" will appear var arr = new Array(7, -1, 2, 22); arr.splice(arr.indexOf( arr.lastIndexOf(arr.length)), 1); arr.sort(); alert(arr);
Answer • The array is initialized with 7, -1, 2, 22 • arr.length is 4 • lastIndexOf(4) will return -1 (nothing found) • arr.indexOf(-1) will return 1 (index of -1) • arr.splice(1, 1) will remove the element -1 • arr.sort() will sort the array • Using the string representations of elements • The resulting array contains 2,22,7 • This will be shown in the pop-up