javascript test preparation n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
JavaScript Test Preparation PowerPoint Presentation
Download Presentation
JavaScript Test Preparation

Loading in 2 Seconds...

play fullscreen
1 / 61

JavaScript Test Preparation - PowerPoint PPT Presentation


  • 96 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'JavaScript Test Preparation' - menora


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
javascript test preparation

http://telerikacademy.com

JavaScript Test Preparation

Doncho Minkov

Telerik Software Academy

academy.telerik.com

Technical Trainer

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

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

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

question4
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] + " | ");

}

answer1
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".
question5
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);

question6
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)
question7
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

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

question9
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)
question10
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)
question11
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)
question12
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)
question13
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)
question14
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);

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

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

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

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

}

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

question20
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)
question21
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)
question22
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)
question23
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)
question24
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)
question25
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;

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

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

answer3
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
question27
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]);

answer4
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
question28
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]

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

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

answer6
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

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

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

question31
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

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

answer9
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
question33
Question
  • What will running the following script return?
    • Nothing, the script is invalid and won’t execute
    • Nothing and an exception will be thrown
    • null, because "#" is used in querySelector
    • The element with id="header" (if any)
    • The element with id="#header" (if any)

document.getElementById("#header");

answer10
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
question34
Question
  • To which element will node point to after the following script (if all referenced elements exist) ?
    • Nothing, the script is invalid and won’t execute
    • node
    • node.parentNode
    • node.firstChild.previousSibling
    • node.firstChild.lastChild
    • node.parentNode.previousSibling

node = node.firstChild.nextSibling.lastChild.parentNode.previousSibling.parentNode

answer11
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 tonode.firstChild
    • Then we use .parentNode again and get back to node
  • So node = node
question35
Question
  • Which syntax could be used to create the HTML code with JavaScript?

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

nodeValue(createTextNode("Heading"));

document.createElement("h1").

firstChild(createTextNode("Heading"));

question36
Question
  • Which of the following can be used to select the body node if you have following html code?
    • body = document.getElementsByTagName(body);
    • var body=document.getElementsByTagName("body");
    • var body=document.getElementById("p").parentNode
    • var body = document.getElementsById("body");
    • bodyElement = document.getElementByTagName[0];
    • body=document.getElementsByClassName(p)[0].parentNode

<html>

<body id="body">

<p id="p" class="p">

</p>

</body>

</html>

question37
Question
  • Which of the following is the correct way to create a class in JavaScript?
    • class Person{}
    • function Person(){}
    • class Person(){}
    • var Person as Class{}
question38
Question
  • What will happen when the following code executes?
    • Selects all div elements with class "comment" and sets their background to red
    • Selects all HTML elements with class "comment" and sets their background to green
    • Selects all JS variables and makes them HTML elements
    • Selects all HTML elements with class "comment" and sets their background to red
    • 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";}

question39
Question
  • Which is TRUE for JavaScript?
    • JavaScript is extension to Java and is created for easier software development
    • JavaScript is a programming language, used in embed systems development
    • JavaScript is a programing language for web, desktop and mobile development
    • JavaScript is a programing language is platform dependent
javascript sample test
JavaScript Sample Test

http://academy.telerik.com