javascript test preparation
DESCRIPTION
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 - PowerPoint PPT PresentationTRANSCRIPT
JavaScript Test Preparation
Doncho Minkov
Telerik Software Academyacademy.telerik.com
Technical Trainer
http://telerikacademy.com
Data Types
3
Question What is the value of the variable
date after the assignment?
a) undefinedb) 2 May 2013c) nulld) 16 February 2012e) the code will throw an
exceptionf) 2 April 2013g) 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
4
What will be the result of the following code?
a) nullb) 1c) NaNd) Undefinede) The code will throw an exceptionf) 9007199254740992
var numberOne = 1;var nullValue = null;var result = numberOne + nullValue;document.writeln(result);
5
Question
Which expression should be used if we want to find the position at which the word "Telerik" appears in?
a) text.match("Telerik")b) text.substring("Telerik")c) text.find("Telerik")d) text.indexOf("Telerik")e) text.getPosition("Telerik")f) text["Telerik"]
var text = "We all love Telerik Academy";
6
Question
What will be the result after executing the following code?
a) Numberb) Nullc) Objectd) NaNe) Undefinedf) The code will throw an exception
var value = null;console.log(typeof value);
7
Question
Loops
8
What will be the output of the following source code? (ignore the time and time zone information)
a) undefined | 4 | undefinedb) January 1 2012 | 4 | telerik1c) February 1 20121 | 4 | telerik1d) Undefined | 4 | telerik1e) January 1 2012 | 4 | telerikf) 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] + " | ");}
9
Question
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". 10
What is the output after executing the code:
a) 1 2 3 4 5 6 7 8 9 10 10b) 10c) 1 2 3 4 5 6 7 8 9 10 11d) The code will throw an exception
for misusing the "i" variablee) 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);
11
Question
Operators (without Bitwise)
a)2, 3, 3, 4, 4b)2, 3, 3, 3, 4c) 2, 2, 3, 3, 4d)11, 11, 12, 13, 13e)2, 2, 3, 4, 4
f) nothing will be printed
g)depends of the JS version
h)syntax error (the code is incorrect) 13
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 );
14
Question What will be the result of this
code?
a) 4 3b) 4.0 3.0c) 4 3.6666666666666665d) 4.0 3.6666666666666665e) 4 3.67f) syntax error (the code is
incorrect)
console.log(12 / 3); // result: 4console.log(11 / 3); // result: 3
15
Question What will be the result of this
code?
a) 0 0 0b) 0.0 0.0 0.0c) -Infinity, NaN, Infinityd) -Infinity, Infinity, Infinitye) runtime error:
DivideByZeroExceptionf) syntax error (the code is
incorrect)
console.log(-1.5 / 0.0); console.log(0.00 / 0.0);console.log(1.50 / 0.0);
a) false, false, falseb) true, true, truec) true, true, falsed) null, true, nulle) true, true, null
f) null, null, nullg) false, false, trueh) 0, false, truei) syntax error (the
code is incorrect)16
Question What will be the result of this
code?var a = null;console.log(!a);console.log(a || true);console.log(!a && false);
a) false, false, true, false, falseb) false, false, false, true, falsec) false, false, false, false, falsed) false, false, false, false, truee) syntax error (the code is incorrect)
17
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++);
a) 21, 23, 4b) 3, 4, 4c) 12, 13, 31d) 21, 23, 31e) 3, 5, 4f) syntax error (the code is incorrect)
18
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);
a) a>b, number, 5, 33b) a>b, int, 5, 33c) a>b, number, 5.0, 33d) a>b, number, 5.0, 33.0e) a>b, integer, 5, 33f) syntax error (the code is incorrect)
19
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) NaNb) Infinityc) 314d) 628e) 3.14f) 6.28g) syntax error (the code is incorrect)
20
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);
Conditional Statements
22
Question What will be the result of this
code?
a)trueb)falsec) 0d)1e)nothing will be printedf) syntax error (the code is
incorrect)
var result = (5 <= 6) && (7 > 3) || (1 >= 2);if result console.log(!result);
23
Question What will be the result of this
code?
a)true, trueb)true, falsec) false, trued)false, falsee)falsef) syntax error (the code is
incorrect)
var a = true, b = false;console.log( !((a && b) + ', ' + (!a || !b)) );
24
Question What will be the result of this
code?
a)trueb)falsec) 0d)1e)syntax error (the code is
incorrect)
var a=50, b="51";console.log(a<b - 1);
25
Question What will be the result of this
code?
a)50b)51c) trued)falsee)syntax error (the code is
incorrect)
var a=50, b=51;if (a>=b) console.log(a) else console.log(b);
26
Question What will be the result of this
code?
a)a < b == cb)nothing will be printedc) syntax error (the code is
incorrect)
var a=1, b=2, c=1;if ((a < b) == c){ console.log("a < b == c");}
27
Question What will be the result of this
code?
a)Xb)Yc) Zd)erre)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");
a) Mondayb) Tuesdayc) Wednesday
d) Error!e) syntax error
(the code is incorrect) 28
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;
}
a) Mondayb) Tuesdayc) Wednesday
d) Error!e) syntax error
(the code is incorrect) 29
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;
}
a) Mondayb) Tuesdayc) Wednesday
d) Error!e) syntax error
(the code is incorrect) 30
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;
}
a) Mondayb) Tuesdayc) Wednesday
d) Error!e) syntax error
(the code is incorrect) 31
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;
}
a) MonTueErr
b) Monc) Tue
d) Erre) syntax error
(the code is incorrect)
32
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");}
Arrays
34
Question Which of the following is the
correct way to declare an array in JavaScript?a) b) c) d) e)
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;
35
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];
36
Question What will the following script
result in? a) An exception will be thrownb) The second line won’t be executedc) A pop-up box with text "undefined"
will appeard) A pop-up box with text "2" will
appeare) A pop-up box with text "0" will
appear
var arr = new Array(2);alert(arr[0]);
37
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
38
Question What will the following script
result in? a) An exception will be thrownb) A pop-up box with text "undefined"
will appearc) A pop-up box with text "7" will
appeard) A pop-up box with text "6" will
appear
var arr = [1, 2, 7]arr[3] = 6;alert(arr[3]);
39
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
40
Question What will the following script
make the arr array look like? a) won’t changeb) won’t change and an exception
will be thrownc) will be:d) will be:
var arr = [1, 2, 7];var i = 0;for(i=6; i>0; i--);{arr[i] = i}
arrarrarrarr
[0, 2, 7][1, 1, 2, 3, 4, 5, 6]
41
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{arr[i] = i}
i
arr[0]
0
i
42
Question What will the following script result
in?
a) A pop-up with the text "false" will
appearb) A pop-up with the text "true" will
appearc) A pop-up with the text "undefined"
will appeard) The code will stop execution when it
reaches the third linee) 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);
43
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
44
Question What will the following script print
on the console?
a) undefined undefined undefined 1 2b) 0 0 0 1 2c) 1 1 0 0 0d) 1 2 undefined undefined undefinede) 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]);
45
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]
46
Question What will running the following
script result in?
a) An endless loopb) The array will bec) The array will be d) The array will be e) The array will bef) 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);
5,4,3,2,1,2,3,4,51,1,1,1,1,2,2,2,3,3,3,3,4,4,55,4,3,2,11
1,2,3,4,5
47
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)
48
Question What will running the following
script result in?
a) An exception will be thrownb) The code will stop execution after the
second linec) A pop-up with the text "2,22,7" will
appeard) A pop-up with the text "-1,2,7,22" will
appeare) A pop-up with the text "undefined"
will appearf) 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);
49
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
DOM Manipulation
51
Question What will running the following
script return?
a)Nothing, the script is invalid and won’t execute
b)Nothing and an exception will be thrown
c) null, because "#" is used in querySelector
d)The element with id="header" (if any)
e)The element with id="#header" (if any)
document.getElementById("#header");
52
Answer getElementById will return the element with the id matching the string parameter Regardless of the contents of the
parameter i.e. symbols like '#' are just symbols,
not commands If there is no element with that id,
return null
53
Question To which element will node point to
after the following script (if all referenced elements exist) ?
a)Nothing, the script is invalid and won’t execute
b)nodec) node.parentNoded)node.firstChild.previousSiblinge)node.firstChild.lastChildf) node.parentNode.previousSiblin
g
node = node.firstChild.nextSibling.lastChild.parentNode.previousSibling.parentNode
54
Answer We get to node.firstChild.nextSibling.lastChild Then we use .parentNode and thus
get back to node.firstChild.nextSibling
Then we use .previousSibling and get back to node.firstChild
Then we use .parentNode again and get back to node
So node = node
Question Which syntax could be used to create
the HTML code with JavaScript?a) b)
c) d) e) f) g)
<h1>Heading</h1>
document.createElement("h1").appendChild("Heading");var h = document.createElement("h1");var t = document.createTextNode("Heading");h.appendChild(t);var t = document.createTextNode("Heading");t = document.createTextNode("Heading");document.createElement(h1).appendChild("Heading");var t = document.createTextNode("Heading");t.createElement("h1");
document.createElement("h1"). firstChild(createTextNode("Heading"));
document.createElement(h1). nodeValue(createTextNode("Heading"));
Question Which of the following can be used to
select the body node if you have following html code?
a) body = document.getElementsByTagName(body);
b) var body=document.getElementsByTagName("body");
c) var body=document.getElementById("p").parentNode
d) var body = document.getElementsById("body");
e) bodyElement = document.getElementByTagName[0];
f) body=document.getElementsByClassName(p)[0]. parentNode
<html><body id="body"> <p id="p" class="p"> </p></body></html>
JavaScript OOP
Question Which of the following is the
correct way to create a class in JavaScript?a)class Person{}b)function Person(){}c) class Person(){}d)var Person as Class{}
Question What will happen when the following
code executes?
a) Selects all div elements with class "comment" and sets their background to red
b) Selects all HTML elements with class "comment" and sets their background to green
c) Selects all JS variables and makes them HTML elements
d) Selects all HTML elements with class "comment" and sets their background to red
e) Selects JavaScript elements of class "comment and sets their background to red
divs = document.getElementsByClassName("comment");for(var i in divs){ divs[i].style.background="#f00";}
Question Which is TRUE for JavaScript?
a) JavaScript is extension to Java and is created for easier software development
b) JavaScript is a programming language, used in embed systems development
c) JavaScript is a programing language for web, desktop and mobile development
d) JavaScript is a programing language is platform dependent
форум програмиране, форум уеб дизайнкурсове и уроци по програмиране, уеб дизайн – безплатно
програмиране за деца – безплатни курсове и уроцибезплатен SEO курс - оптимизация за търсачки
уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop
уроци по програмиране и уеб дизайн за ученициASP.NET MVC курс – HTML, SQL, C#, .NET, ASP.NET MVC
безплатен курс "Разработка на софтуер в cloud среда"
BG Coder - онлайн състезателна система - online judge
курсове и уроци по програмиране, книги – безплатно от Наков
безплатен курс "Качествен програмен код"
алго академия – състезателно програмиране, състезанияASP.NET курс - уеб програмиране, бази данни, C#, .NET, ASP.NET
курсове и уроци по програмиране – Телерик академия
курс мобилни приложения с iPhone, Android, WP7, PhoneGapfree C# book, безплатна книга C#, книга Java, книга C# Дончо Минков - сайт за програмиране
Николай Костов - блог за програмиранеC# курс, програмиране, безплатно
?? ? ?
??? ?
?
? ?
??
?
?
? ?
Questions?
?
JavaScript Sample Test
http://academy.telerik.com 6
1