Featured Post

Thursday, May 4, 2017

Upwork 2017 Javascript test questions

Q Consider the following JavaScript function to change the color of the text box named txtName:
Function color(col) {
document.forms[0].txtName.style.background = col
}
Which of the following will change the color of the text box to green, as long as user is pressing a key?

<input type="text" onkeypress="color('green')" onkeyup="color('white')" name="txtName"/>

Q Consider three variables:
var someText = 'JavaScript1.2';
var pattern = /(\w+)(\d)\.(\d)/i;
var outCome = pattern.exec(someText);
What does pattern.ignoreCase contain?
True

Q What kind of function is function* in JavaScript? (Choose all that apply)
It is a function which can return value several times

Q When toggling a variable. Its cycle must be 0 and 1. When the variable’s active is 0, the next value should be 1, and when the variable’s active value is 1, the next value should be 0.
Considering the scenario above, which of the following is correct?
 Q=1-q

Q Which of the following code snippets shows the correct way to detect an array that does not contain a specific value.
var aVar =["Banana","Orange","Apple","Mango"];
var bVar = aVar.indexOf("Orange");
If(bVar == -1)
{
// requirement
}
Fruits variable does not exist so considering it aVar

Q Which of the following will change the color of paragraph’s text to blue when a user hovers over it, and reset it back to black when the user hovers out?
<p onmouseover=”style.color=’blue'” onmouseout=»style.color=’black'»> The text of the paragraph..</p>

Q Which of the following choices will turn a string into a JavaScript function call (case with objects) of the following code snippet?
<script>
window.foo = {
bar: {
baz: function() {
alert(‘Hello!’);
}
}
};
</script>

window['foo']['bar']['baz']();

Q What will be logged in the console?
let a ={['foo_' + (() => 1)()]:2};
console.log(a);

{foo_1: 2}

Q Consider the following code snippet:
<form name="frmOne">
<select name="selList" size="1" onChange="change()">
<option value="http://www.hotmail.com">tHotmail</option>
<option value="http://www.yahoo.com">tHotmail</option>
</select>
</form>
Considering that when option button is selected , the appropriate website should be opened immediately, what should the change() function look like?

location = document.frmOne.selList.options[document.frmOne.selList.selectedIndex].value;

Q Which of the following will check whether the variable vRast exists or not?

if(typeof vRast =="undefined")

Q Which of the following is the correct way to stop setInterval() from calling a function in Javascript?

setInterval() returns an internal ID, which can pass to clearInterval() to stop it from calling its designated function.

Q what will be written in the alert box after executing the following code?
var a=5;
var b =1;
if(!!"false")
{
a =a+5;
b=3
}
if(!!0)
{
a =a+5;
b = b+2;
}
console.log(a+b);

13

Q What kind of array representation of the pixels does Canvas ImageData return when you call Imagedata.data?

Uint32Array

Q what do JS classes support?

None, JS has prototype chain instead of classes

Q What is the output of following code?
if(typeof(Storage) !== "undefined") {
localStorage.age =5;
sessionStorage.age = 5;
alert(localStorage.age  + sessionStorage.age);
}
else
{
alert("Sorry, your browser does not support web storage");
}

55

Q What is true about strict mode? (Choose all that apply)

It applies to individual functions
It apples to entire script

Q  Which of the following code snippets changes an image on the page?

Var img = document.getElementById(“imageid”);
Img.src = “newimage.gif”;

Q what is the output of the following code?
Var foo = 123e5;
Var bar = 10;
Var foobar = foo + bar;

12300010

A form contains two fields named id1 and id2.  How can you copy the value of the id2 field to id1?

a. document.forms[0].id1.value=document.forms[0].id2.value
b. document.forms[0].id2.value=document.forms[0].id1.value
c. document.id1.value=document.id2.value
d. document.id2.value=document.id1.value

Q Which of the following is not a valid string method?
a. link()
b. italics()
c. str()
d. sup()

Q Which of the following code samples creates an alert box with "false" in it.

var n =3.2;
alert(n % 1 === 0);

Q what is Promise?

Async call

Q which event handlers can be used to create hover effect on HTML element?

OnMouseover and onMouseOut

Q what does ~ mean in bitwise operation javascript

NOT

Q What will be returned if we call f(3)?
const f = (x,y = true) => x * y;

3

Q What will be the value of s.size after executing this code?

let s = new Set();
s.add('aa').add('bb').add('cc').add('aa').add('bb');

3

Q What is true about an Array?

This code can be used to create an array let a = []; a[10.5] = "Hey";
This code can be used to create an array let a = new Array("Hey",10.5);

Q what are valid states of the created Promise?
Following are the valid states in promise:
  • Pending – not yet resolved
  • Succeeded / Fulfilled – pending operation succeeded
  • Failed / Rejected – pending operation failed
Answer is:
Pending, Fulfilled, Rejected

Q Which function will return 10 when called like this : f(10)

const f = (...f) => f.reduce(f =>f);
const dJ = dJ => dJ; 

Q Which of the following are the valid JavaScript code to obtain the number of milliseconds since the epoch? (Check all that apply)

var timestamp = new Date().getTime();
var timestamp = Number(new Date());
 var timestamp = new Date().valueOf();


Q what will be the output of the following code?
var x = [typeof x, typeof y][1];
console.log(typeof typeof x);

string

Q What is JavaScript?

It’s the programming language used by only web developers

Q  Which of the following determines whether cookies are enabled in browser or not ?

(navigator.cookieEnabled)? true: false;

Q Which are the correct ways to redirect to a URL in JavaScript?

window.location = "http://www.foobar.com";
window.location.assign("http://www.foobar.com");

Q how can you remove spaces, tabs and line terminator characters around the string? String is defined like this let string = ‘  hey  ’;

string = string.trim();

Q How can you get type of the variable

Using type of operator

Q Given a p element with some text content, which of the following code snippets sets the background color of the text to yellow, its color to red and its font size to 20px?

var p = document.getElementById("demo1");
p.setAttribute("style","background-color:yellow;color:red;font-size:20px");

Q How many yield keywords can be used in function* functions?

As many as needed

Q what does let mean in JavaScript?

It is used to create local block scoped variables

Q which statement about Promises is not correct?

Promise may never resolve value

It’s possible to test which promise resolves first with method from Promise object (chaining promise)

All Promises will eventually resolve or reject

Q promise is created. When will it resolve?
Immediately
In the feature
Never
UnKnown

Q  The following are sample codes on how to merge properties of two JavaScript objects dynamically:


var obj1 = {food: 'pizza', car: 'ford'};
var obj2 = {animal :'dog'};

Function MergeRecursive(obj1,obj2)
{
var obj3 = {};
for(var attrname in obj1){ obj3[attrname] = obj1[attrname]}
for(var attrname in obj2){ obj3[attrname] = obj2[attrname]}
}

Q What does a “for…in” loop do?

It iterates over the enumerable properties of an object, in arbitrary order.

Note: The for...in statement iterates over the enumerable properties of an object, in original insertion order. For each distinct property, statements can be executed.
Here in options each answer is given with arbitrary order.

Q how often will message “hey” be logged in console?

setInterval(() => {console.log('hey'),1});

Every 1 milliseconds

Q Which statement is correct about null and undefined?

Undefined indicates that a variable is not defined in the scope. Null can be assigned to a variable as a representation of no value.

Q Which function determine whether the beginning of the param1 instance matches the param2?

function abc(para1,para2)
{
 return para1.indexOf(para2) == 0;

}  

No comments:

Post a Comment