Sei sulla pagina 1di 5

What will be the output of the following code:

for (var i = 0; i < 5; i++) {


setTimeout(function() { console.log(i); }, i * 1000 );
}

The code sample shown will not display the values 0, 1, 2, 3, and 4 as might be expected; rather, it will
display 5, 5, 5, 5, and 5.

The following recursive code will cause a stack overflow if the array list is too large. How can you fix this
and still retain the recursive pattern?
var list = readHugeList();

var nextListItem = function() {


var item = list.pop();

if (item) {
// process the list item...
nextListItem();
}
};
The potential stack overflow can be avoided by modifying the nextListItem function as follows:
var list = readHugeList();

var nextListItem = function() {


var item = list.pop();

if (item) {
// process the list item...
setTimeout( nextListItem, 0);
}
};
The stack overflow is eliminated because the event loop handles the recursion, not the call stack.
When nextListItem runs, if item is not null, the timeout function ( nextListItem ) is pushed to the
event queue and the function exits, thereby leaving the call stack clear. When the event queue runs its
timed-out event, the next item is processed and a timer is set to again invoke nextListItem .
Accordingly, the method is processed from start to finish without a direct recursive call, so the call stack
remains clear, regardless of the number of iterations.

What will the code below output to the console and why ?
console.log(1 + "2" + "2");
console.log(1 + +"2" + "2");
console.log(1 + -"1" + "2");
console.log(+"1" + "1" + "2");
console.log( "A" - "B" + "2");
console.log( "A" - "B" + 2);
The above code will output the following to the console:
"122"
"32"
"02"
"112"
"NaN2"
NaN

What will the code below output to the console and why?
var arr1 = "john".split('');
var arr2 = arr1.reverse();
var arr3 = "jones".split('');
arr2.push(arr3);
console.log("array 1: length=" + arr1.length + " last=" + arr1.slice(-1));
console.log("array 2: length=" + arr2.length + " last=" + arr2.slice(-1));

he logged output will be:


"array 1: length=5 last=j,o,n,e,s"
"array 2: length=5 last=j,o,n,e,s"
arr1 and arr2 are the same after the above code is executed for the following reasons:
What is the output out of the following code? Explain your answer.
var a={},
b={key:'b'},
c={key:'c'};

a[b]=123;
a[c]=456;

console.log(a[b]);
The output of this code will be 456 ( not 123 ).
The reason for this is as follows: When setting an object property, JavaScript will implicitly stringify the
parameter value. In this case, since b and c are both objects, they will both be converted
to "[object Object]" . As a result, a[b] and a[c] are both equivalent to a["[object
Object]"] and can be used interchangeably. Therefore, setting or referencing a[c] is precisely the
same as setting or referencing a[b] .

Consider the code snippet below. What will the console output be and why?
(function(x) {
return (function(y) {
console.log(x);
})(2)
})(1);

he output will be 1 , even though the value of x is never set in the inner function. Heres why:
As explained in our JavaScript Hiring Guide, a closure is a function, along with all variables or functions
that were in-scope at the time that the closure was created. In JavaScript, a closure is implemented as an
inner function; i.e., a function defined within the body of another function. An important feature of
closures is that an inner function still has access to the outer functions variables.
Therefore, in this example, since x is not defined in the inner function, the scope of the outer function is
searched for a defined variable x , which is found to have a value of 1 .

PYTHON

What will be the output of the code below? Explain your answer.

def extendList(val, list=[]):


list.append(val)
return list

list1 = extendList(10)
list2 = extendList(123,[])
list3 = extendList('a')

print "list1 = %s" % list1


print "list2 = %s" % list2
print "list3 = %s" % list3

list1 = [10, 'a']


list2 = [123]
list3 = [10, 'a']

What will be the output of the code below? Explain your answer.

class Parent(object):
x = 1

class Child1(Parent):
pass

class Child2(Parent):
pass

print Parent.x, Child1.x, Child2.x


Child1.x = 2
print Parent.x, Child1.x, Child2.x
Parent.x = 3
print Parent.x, Child1.x, Child2.x

The output of the above code will be:

1 1 1
1 2 1
3 2 3

Consider the following code snippet:

1. list = [ [ ] ] * 5
2. list # output?
3. list[0].append(10)
4. list # output?
5. list[1].append(20)
6. list # output?
7. list.append(30)
8. list # output?

What will be the ouput of lines 2, 4, 6, and 8? Explain your answer.


The output will be as follows:
[[], [], [], [], []]
[[10], [10], [10], [10], [10]]
[[10, 20], [10, 20], [10, 20], [10, 20], [10, 20]]
[[10, 20], [10, 20], [10, 20], [10, 20], [10, 20], 30]

What will be the output of the code below?

list = ['a', 'b', 'c', 'd', 'e']


print list[10:]

What will be the output of the code below in Python 2? Explain your answer.

def div1(x,y):
print "%s/%s = %s" % (x, y, x/y)

def div2(x,y):
print "%s//%s = %s" % (x, y, x//y)

div1(5,2)
div1(5.,2)
div2(5,2)
div2(5.,2.)
In Python 2 , the output of the above code will be:
5/2 = 2
5.0/2 = 2.5
5//2 = 2
5.0//2.0 = 2.0

Also, how would the answer differ in Python 3 (assuming, of course, that the above print statements
were converted to Python 3 syntax)?

Given the following subclass of dictionary:

class DefaultDict(dict):
def __missing__(self, key):
return []
Will the code below work? Why or why not?
d = DefaultDict()
d['florp'] = 127
Yes, it will work. With this implementation of the DefaultDict class, whenever a key is missing, the
instance of the dictionary will automatically be instantiated with a list.

Potrebbero piacerti anche