Top Banner
JavaScript Test Preparation Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer h ttp://telerikacad emy.com
61

Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

Dec 29, 2015

Download

Documents

Pearl Robbins
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

JavaScript Test Preparation

Doncho Minkov

Telerik Software Academyacademy.telerik.com

Technical Trainer

http://telerikacademy.com

Page 2: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

Data Types

Page 3: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

3

Question What is the value of the variable

date after the assignment?

a) undefined

b) 2 May 2013

c) null

d) 16 February 2012

e) the code will throw an exception

f) 2 April 2013

g) 2 December 2013

var date = new Date(2012, 16, 2);

Page 4: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

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

Page 5: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

What will be the result of the following code?

a) null

b) 1

c) NaN

d) Undefined

e) The code will throw an exception

f) 9007199254740992

var numberOne = 1;var nullValue = null;var result = numberOne + nullValue;document.writeln(result);

5

Question

Page 6: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

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

Page 7: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

What will be the result after executing the following code?

a) Number

b) Null

c) Object

d) NaN

e) Undefined

f) The code will throw an exception

var value = null;console.log(typeof value);

7

Question

Page 8: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

Loops

8

Page 9: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

What will be the output of the following source code? (ignore the time and time zone information)

a) undefined | 4 | undefined

b) January 1 2012 | 4 | telerik1

c) February 1 20121 | 4 | telerik1

d) Undefined | 4 | telerik1

e) January 1 2012 | 4 | telerik

f) 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

Page 10: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

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

Page 11: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

What is the output after executing the code:

a) 1 2 3 4 5 6 7 8 9 10 10

b) 10

c) 1 2 3 4 5 6 7 8 9 10 11

d) The code will throw an exception for misusing the "i" variable

e) 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

Page 12: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

Operators (without Bitwise)

Page 13: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

a)2, 3, 3, 4, 4

b)2, 3, 3, 3, 4

c) 2, 2, 3, 3, 4

d)11, 11, 12, 13, 13

e)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 );

Page 14: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

14

Question What will be the result of this

code?

a) 4 3

b) 4.0 3.0

c) 4 3.6666666666666665

d) 4.0 3.6666666666666665

e) 4 3.67

f) syntax error (the code is incorrect)

console.log(12 / 3); // result: 4console.log(11 / 3); // result: 3

Page 15: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

15

Question What will be the result of this

code?

a) 0 0 0

b) 0.0 0.0 0.0

c) -Infinity, NaN, Infinity

d) -Infinity, Infinity, Infinity

e) runtime error: DivideByZeroException

f) syntax error (the code is incorrect)

console.log(-1.5 / 0.0); console.log(0.00 / 0.0);console.log(1.50 / 0.0);

Page 16: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

a) false, false, false

b) true, true, true

c) true, true, false

d) null, true, null

e) true, true, null

f) null, null, null

g) false, false, true

h) 0, false, true

i) 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);

Page 17: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

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++);

Page 18: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

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);

Page 19: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

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"));

Page 20: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

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);

Page 21: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

Conditional Statements

Page 22: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

22

Question What will be the result of this

code?

a)true

b)false

c) 0

d)1

e)nothing will be printed

f) syntax error (the code is incorrect)

var result = (5 <= 6) && (7 > 3) || (1 >= 2);if result console.log(!result);

Page 23: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

23

Question What will be the result of this

code?

a)true, true

b)true, false

c) false, true

d)false, false

e)false

f) syntax error (the code is incorrect)

var a = true, b = false;console.log( !((a && b) + ', ' + (!a || !b)) );

Page 24: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

24

Question What will be the result of this

code?

a)true

b)false

c) 0

d)1

e)syntax error (the code is incorrect)

var a=50, b="51";console.log(a<b - 1);

Page 25: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

25

Question What will be the result of this

code?

a)50

b)51

c) true

d)false

e)syntax error (the code is incorrect)

var a=50, b=51;if (a>=b) console.log(a) else console.log(b);

Page 26: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

26

Question What will be the result of this

code?

a)a < b == c

b)nothing will be printed

c) syntax error (the code is incorrect)

var a=1, b=2, c=1;if ((a < b) == c){ console.log("a < b == c");}

Page 27: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

27

Question What will be the result of this

code?

a)X

b)Y

c) Z

d)err

e)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");

Page 28: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

a) Monday

b) Tuesday

c) 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;

}

Page 29: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

a) Monday

b) Tuesday

c) 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;

}

Page 30: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

a) Monday

b) Tuesday

c) 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;

}

Page 31: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

a) Monday

b) Tuesday

c) 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;

}

Page 32: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

a) MonTueErr

b) Mon

c) Tue

d) Err

e) 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");}

Page 33: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

Arrays

Page 34: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

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;

Page 35: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

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];

Page 36: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

36

Question

What will the following script

result in?

a) An exception will be thrown

b) The second line won’t be executed

c) A pop-up box with text "undefined" will appear

d) A pop-up box with text "2" will appear

e) A pop-up box with text "0" will appear

var arr = new Array(2);alert(arr[0]);

Page 37: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

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

Page 38: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

38

Question

What will the following script

result in?

a) An exception will be thrown

b) A pop-up box with text "undefined" will appear

c) A pop-up box with text "7" will appear

d) A pop-up box with text "6" will appear

var arr = [1, 2, 7]arr[3] = 6;alert(arr[3]);

Page 39: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

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

Page 40: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

40

Question

What will the following script

make the arr array look like?

a) won’t change

b) won’t change and an exception will be thrown

c) 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]

Page 41: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

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

Page 42: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

42

Question

What will the following script result in?

a) A pop-up with the text "false" will appear

b) A pop-up with the text "true" will appear

c) A pop-up with the text "undefined" will appear

d) The code will stop execution when it reaches the third line

e) 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);

Page 43: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

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

Page 44: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

44

Question

What will the following script print on the console?

a) undefined undefined undefined 1 2

b) 0 0 0 1 2

c) 1 1 0 0 0

d) 1 2 undefined undefined undefined

e) 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]);

Page 45: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

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]

Page 46: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

46

Question What will running the following

script result in?

a) An endless loop

b) The array will be

c) The array will be

d) The array will be

e) The array will be

f) 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

Page 47: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

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)

Page 48: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

48

Question

What will running the following script result in?

a) An exception will be thrown

b) The code will stop execution after the second line

c) A pop-up with the text "2,22,7" will appear

d) A pop-up with the text "-1,2,7,22" will appear

e) A pop-up with the text "undefined" will appear

f) 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);

Page 49: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

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

Page 50: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

DOM Manipulation

Page 51: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

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");

Page 52: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

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

Page 53: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

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

Page 54: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

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

Page 55: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

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"));

Page 56: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

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>

Page 57: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

JavaScript OOP

Page 58: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

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{}

Page 59: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

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";}

Page 60: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

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

Page 61: Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer .

форум програмиране, форум уеб дизайнкурсове и уроци по програмиране, уеб дизайн – безплатно

програмиране за деца – безплатни курсове и уроцибезплатен 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, PhoneGap

free C# book, безплатна книга C#, книга Java, книга C#Дончо Минков - сайт за програмиранеНиколай Костов - блог за програмиранеC# курс, програмиране, безплатно

?

? ? ??

?? ?

?

?

?

??

?

?

? ?

Questions?

?

JavaScript Sample Test

http://academy.telerik.com 6

1