Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Quiz Note: It is recommended that you save your response as you complete each qu

ID: 3881757 • Letter: Q

Question

Quiz

Note: It is recommended that you save your response as you complete each question.

Question 1 (1 point)

In javaScript the sort() function of the Array sorts values as ______

Question 1 options:

Save

Question 2 (1 point)

In the following code sort is a method.


var a = ["3", "2", "1"];
document.getElementById("demo").innerHTML = a.sort();

Question 2 options:

Save

Question 3 (1 point)

What is the output of the following code?


var fruits = ["Banana", "Orange"];
fruits.splice(1, 1);
document.getElementById("demo").innerHTML = fruits;

Question 3 options:

Save

Question 4 (1 point)

If points is an array, the following call to the sort function is passed a function


points.sort(function(a, b){return a - b});


The passed function is called a _____ function

Question 4 options:

Save

Question 5 (1 point)

What will be the output of the following code?


var fruits = ["Banana", "Orange"];
fruits.unshift("Apple");
document.getElementById("demo").innerHTML = fruits;

Question 5 options:

Save

Question 6 (1 point)

In the following code sort is a property.


var a = ["3", "2", "1"];
document.getElementById("demo").innerHTML = a.sort();

Question 6 options:

Save

Question 7 (1 point)

What will be the output of the following code?


var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo2").innerHTML = fruits.shift();

Question 7 options:

Save

Question 8 (1 point)

A variable naming convention where the first letter is lower case and all subsequent new words have the first letter as upper case is called lower ______ case.

Question 8 options:

Save

Question 9 (1 point)

A javascript _______ is a block of Javascript code that can be called - which means executed by a "call" to it.

Question 9 options:

Save

Question 10 (1 point)

what is the time complexity of the following function?


function fun(n, a) {
var i = 0, j = 0;
for (i = 0; i < n; ++i) {
j = 0;
while (j < n && a[i] < a[j]) {
j++;
}
}
}

Question 10 options:

1)

2)

3)

4)

Save

Save All ResponsesGo to Submit Quiz

Explanation / Answer

Q1    

By default, the sort() function sorts values as strings.

This works well for strings ("Apple" comes before "Banana").

However, if numbers are sorted as strings, "25" is bigger than "100", because "2" is bigger than "1".

Because of this, the sort() method will produce an incorrect result when sorting numbers.

Q2

This statement tells the browser to write a.sort() i.e ["3","2","1"] inside an HTML element with id="demo" as JS sorts array as strings.Hence it is TRUE as sort is a method.

Q3

The slice() method returns the selected elements in an array, as a new array object.

The slice() method selects the elements starting at the given start argument, and ends at, but does not include, the given end argument.

Here the answer is an empty array.

Q4

Such a function can be called anonymous function since it does not have to have a name.

for example:

Q5

The unshift() method adds new items to the beginning of an array, and returns the new length.

HTML element with id="demo" displays the modified array fruits=["Apple","Banana","Orange"]

Q6

Since sort() is a method not a property .It is FALSE.

Q7

The shift() method removes the first item of an array, and returns that item.

So the answer would be demo element would show ["Orange","Apple","Mango"].

Q8

For variables that are not constants or constructors, multi-word variables and functions SHOULD be lowerCamelCased.

The first letter of each variable or function SHOULD be lowercase, and the first letter of subsequent words SHOULD be capitalized. There SHOULD NOT be underscores between the words.

Q9

A JavaScript function is a block of JavaScript code, that can be executed when "called" for.

For example, a function can be called when an event occurs, like when the user clicks a button.

Q10

I think the answer is O(n^2) considering the worst case i.e each time the inner loop of j stops at i because when i==j a[i] is not less than a[j] so the time complexity in the worst case would be n*(n-1)/2 i.e O(n^2).