All sample questions / JavaScript
What does console.log(5 << 2) print? JavaScript Numbers, Strings and Regular Expressions
Block 7, Numbers, Strings and Regular Expressions, question 10 of the JavaScript sample
console.log(5 << 2)
What does this print?
Show the answer and why each option is right or wrong
Answer, C. 20
- A. 7
- Confuses the shift operator with addition, adding the shift amount to the original number.
- B. 10
- Assumes each left shift multiplies by 2 regardless of the shift count, applying the doubling only once instead of the two times requested.
- C. 20
- Correct. Shifting left by 2 multiplies the value by four, since each position shifted doubles it.
- D. 25
- Assumes each left shift multiplies by 5, confusing the shift count with the value being shifted.
Shifting an integer left by n bit positions is equivalent to multiplying it by two to the power of n. Shifting 5 left by 2 multiplies it by four, giving 20.
The answer above is what the Node interpreter printed when the snippet was run, not what someone expected it to do.
This question comes from a bank of 600 JavaScript interview questions, all built the same way, sold as one PDF with the full answer key.
Get the 600 JavaScript questions, 9 euros
Or play the free JavaScript sample, twenty questions in the browser, also as a free PDF.
Previous question Next question
Did this save you a lookup? Buy me a coffee on Ko-fi.