Java Script Exam 2

1. What keyword creates a block-scoped variable that can be reassigned?

let score = 10;

2. Which statement correctly creates a function named sum?

function sum(a, b) { return a + b; }

3. What will this log?

console.log(typeof null);

4. Which method adds a value to the end of an array?

let a = [1,2]; a.push(3);

5. What does === test for?

6. Which of these creates an object with a property name?

let obj = { name: 'Sam' };

7. What will [1,2,3].map(x => x * 2) return?

[1,2,3].map(x => x * 2);

8. Which is true about const?

9. What is hoisting in JavaScript?

10. Which method parses a JSON string into an object?

JSON.parse('{"a":1}');

11. What will this expression return: [...'abc'] ?

[...'abc']

12. How do you schedule a function to run after 2 seconds?

setTimeout(fn, 2000);

13. Which event fires when the DOM has been loaded (but before images load)?

14. What does Array.prototype.filter return?

15. Which of these accesses the element with id ‘app’?

document.getElementById('app')

16. What is a Promise?

17. How do you catch errors from a Promise?

promise.then(...).catch(err => ...)

18. What does async/await provide?

19. Which built-in method returns the characters in a string as uppercase?

'hi'.toUpperCase()

20. How do you remove the first element from an array?

let a = [1,2,3]; a.shift();

21. Which operator spreads an array into separate arguments?

Math.max(...[1,2,3])

22. What will 0 == '0' evaluate to?

23. Which method trims whitespace from both ends of a string?

'  hi  '.trim()

24. What will typeof [] return?

25. Which regex flag makes matching case-insensitive?

/abc/i

26. Which built-in object provides methods for rounding and generating random numbers?

27. What is the output of typeof NaN?

28. Which DOM API finds elements by class name?

document.getElementsByClassName('btn')

29. What does event.preventDefault() do?

30. Which of these creates a shallow copy of an object?

let copy = Object.assign({}, obj);

31. What does document.createElement('div') do?

32. Which method returns the index of the first match in an array?

33. Which will parse integer from string?

34. Which of the following is a truthy value?

35. What does Object.keys(obj) return?

36. Which will stop propagation of an event so it doesn’t bubble up?

37. Which creates a new object that delegates to a specified prototype?

38. Which will convert a value to a number, returning NaN on failure?

39. What will 0 === -0 evaluate to?

40. Which statement throws an exception?

41. Which returns milliseconds since 1970-01-01 UTC?

42. What does call() do on functions?

43. Which removes a property from an object?

delete obj.prop;

44. Which is true about arrow functions?

45. What is event delegation?

46. Which array method reduces values to a single value?

47. What does void 0 evaluate to?

48. Which error type indicates invalid JSON or code syntax?

49. Which returns true if at least one element passes a predicate?

arr.some(x => x > 5)

50. Which method adds an element as the last child of a node?

parent.appendChild(child)

JavaScript Exam 1

1. What keyword creates a changeable variable?




2. What is the boolean value of the string “0”?

Boolean("0")



3. What is the length of this array?

let arr = ["x", "y", "z"];



4. What does a function return if it has no return statement?




5. Can JavaScript be included in any valid HTML file?




6. What will this display?

let x = 4; let y = 6; let z = x + "" + y; alert(z);



7. Will this behave the same in all browsers?

"hello".substring(-1);



8. What does parseInt return here?

parseInt(52.9);



9. Can browser JavaScript directly read/write arbitrary local files?




10. What will this loop alert?

let total=0; for(let i=1;i<=8;i++){ if(i%3===0) continue; if(i>5) break; total+=i } alert(total);



12. Do all JavaScript engines behave identically for every feature?




13. What is the result of “super”+”man”?




14. What does s.replace(/l/g,”#”) do to “balloon”?




15. What does s.replace(“l”,”#”) do to “balloon”?




16. Which returns the integer part of a number?

Math.floor(3.9)



17. What will this alert?

let a=4, b=2; function calc(x){ b = a + x; return ++b; } alert(calc(3));



18. How to append 5 to array nums?




19. Which event fires when the mouse enters an element?




20. typeof Infinity returns?




21. Which keyword declares a constant?




22. What does Number(“123”) return?




23. What does console.log(“5” – 2) output?




24. What is the result of [] + [] ?




25. typeof [] returns?




26. Which method removes the last item of an array?




27. What value does let x; create?




28. What does “hello”.toUpperCase() return?




29. Which operator checks both value and type?




30. What does “cat”.includes(“a”) return?




31. What does typeof null return?




32. What does !true evaluate to?




33. Which literal creates an array?




34. What does push() return?




35. Which loop runs the body at least once?




36. Which event triggers on a click?




37. What is the result of NaN === NaN ?




38. Which is a primitive type?




39. typeof function(){} returns?




40. What does JSON.stringify({a:1}) do?




41. Which method adds elements to the start of an array?




42. What does “10” + 1 produce?




43. How to make a shallow copy of array a?




44. Which checks array membership?




45. typeof (function(){}) returns?




46. Which method converts a string to lowercase?




47. What does 0 == “0” evaluate to?




48. Which declares a block-scoped variable?




49. Which method splits a string into an array?




50. Which array method returns true if every element passes the test?