Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2K views

Introduction To Javascript Answers

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views

Introduction To Javascript Answers

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

JavaScript Certification Practice Questions

1. What is the purpose of the var keyword in JavaScript?

A. To create a constant
B. To declare a variable
C. To define a function
D. To create a block-scoped variable

Answer: B
Explanation: The var keyword is used to declare a variable, but it has function scope, not
block scope (use let or const for block scope).

2. Which of the following is true about let in JavaScript?

A. It declares a variable with function scope


B. It creates a global variable
C. It has block-level scope
D. It is not used for declaring variables

Answer: C
Explanation: let is used to declare variables with block-level scope, meaning they are limited
to the block in which they are defined (e.g., inside a loop or an if statement).

3. What does the const keyword do?

A. Declares a variable that can be reassigned


B. Creates a constant that cannot be reassigned
C. Declares a global variable
D. Creates a variable with block scope

Answer: B
Explanation: The const keyword is used to declare constants. These cannot be reassigned
once initialized, but note that objects declared with const can still be mutated.

4. What is the correct syntax to create an object in JavaScript?


A. let obj = {};
B. let obj = [];
C. let obj = function();
D. let obj = Object();

Answer: A
Explanation: The syntax let obj = {}; correctly initializes an empty object in JavaScript.

5. How do you add a new property to an object in JavaScript?

A. object.addProperty('key', value)
B. object['key'] = value
C. object.add('key', value)
D. object.set('key', value)

Answer: B
Explanation: You can add a new property to an object using bracket notation like
object['key'] = value.

6. What does the this keyword refer to in JavaScript?

A. The current function


B. The global object
C. The object that is executing the current function
D. The last declared variable

Answer: C
Explanation: The this keyword refers to the object that is executing the current function,
depending on the context in which the function is called.

7. Which of the following is a correct way to define a function in JavaScript?

A. function myFunction() {}
B. function = myFunction() {}
C. def myFunction() {}
D. function: myFunction() {}
Answer: A
Explanation: The correct way to define a function in JavaScript is function myFunction()
{}.

8. What does the return statement do in a function?

A. Stops the function execution and returns a value


B. Creates a new function
C. Prevents the function from executing
D. Logs the value to the console

Answer: A
Explanation: The return statement stops the execution of a function and sends a specified
value back to the calling code.

9. Which of the following is used to loop over an array in JavaScript?

A. forEach()
B. loop()
C. repeat()
D. iterate()

Answer: A
Explanation: forEach() is an array method in JavaScript that allows you to iterate over the
elements of an array.

10. What is the result of typeof null in JavaScript?

A. "null"
B. "object"
C. "undefined"
D. "array"

Answer: B
Explanation: The typeof operator returns "object" for null, which is a quirk in JavaScript
that can lead to confusion.
11. How would you create an array in JavaScript?

A. let arr = {1, 2, 3}


B. let arr = [1, 2, 3]
C. let arr = (1, 2, 3)
D. let arr = array(1, 2, 3)

Answer: B
Explanation: Arrays are created in JavaScript using square brackets like let arr = [1, 2,
3];.

12. Which method would you use to convert a string to an integer in JavaScript?

A. parseInt()
B. toInteger()
C. convertToInt()
D. stringToInt()

Answer: A
Explanation: The parseInt() function is used to convert a string to an integer in JavaScript.

13. What is the purpose of JSON.stringify() in JavaScript?

A. To convert an object into a JSON string


B. To parse a JSON string into an object
C. To sort an object
D. To handle errors in JSON

Answer: A
Explanation: JSON.stringify() is used to convert a JavaScript object into a JSON string.

14. What does the push() method do in JavaScript?

A. Adds a new element to the beginning of an array


B. Adds a new element to the end of an array
C. Removes the last element of an array
D. Removes the first element of an array
Answer: B
Explanation: The push() method adds one or more elements to the end of an array.

15. Which operator is used for strict equality comparison in JavaScript?

A. ==
B. ===
C. =
D. !=

Answer: B
Explanation: The === operator checks for strict equality, meaning it compares both the value
and the type.

16. What is a closure in JavaScript?

A. A function inside an object


B. A function that can access variables from its outer scope
C. A way to close a program
D. A method that closes the console

Answer: B
Explanation: A closure is a function that has access to its own scope, the scope in which it was
created, and the global scope.

17. What does the map() method do in JavaScript?

A. Filters out elements from an array


B. Creates a new array with the results of calling a function on every element of the array
C. Modifies an array in place
D. Sorts the elements of an array

Answer: B
Explanation: The map() method creates a new array with the results of calling a provided
function on every element in the calling array.

18. Which of the following will trigger an error in JavaScript?


A. let x = 10; x = 20;
B. const y = 30; y = 40;
C. var z = 50; z = 60;
D. let a = 70; a = 80;

Answer: B
Explanation: const is used to declare constants, so attempting to reassign a value to a const
variable will result in an error.

19. How do you handle errors in JavaScript?

A. catch block
B. try...catch statement
C. error() function
D. handleError() function

Answer: B
Explanation: Errors in JavaScript are handled using try...catch blocks, where code that
may throw an error is placed in the try block, and error handling is done in the catch block.

20. Which of the following methods is used to remove the last element of an array in
JavaScript?

A. pop()
B. shift()
C. unshift()
D. splice()

Answer: A
Explanation: The pop() method removes the last element from an array and returns that
element.

21. What is the correct syntax for a JavaScript class?

A. class MyClass {}
B. class = MyClass {}
C. class MyClass[]
D. function class MyClass {}

Answer: A
Explanation: The correct syntax for defining a class in JavaScript is class MyClass {}.

22. Which of the following methods is used to execute code asynchronously in


JavaScript?

A. setTimeout()
B. setInterval()
C. async/await
D. all()

Answer: C
Explanation: async/await is used to handle asynchronous operations in JavaScript, allowing
you to write asynchronous code in a synchronous-like manner.

23. What is the difference between null and undefined in JavaScript?

A. null is a type, undefined is a value


B. null is an object, undefined is a primitive value
C. null is a primitive value, undefined is an object
D. There is no difference

Answer: B
Explanation: null is an object, whereas undefined represents the absence of a value or a
variable that has not been assigned a value.

24. Which event is used to trigger an action when an element is clicked in JavaScript?

A. onmouseover
B. onkeydown
C. onclick
D. onload

Answer: C
Explanation: The onclick event is triggered when an element is clicked.
25. What does JSON.parse() do in JavaScript?

A. Converts a JavaScript object to a JSON string


B. Converts a JSON string into a JavaScript object
C. Creates a JSON string
D. Throws an error if the input is not valid JSON

Answer: B
Explanation: JSON.parse() is used to convert a valid JSON string into a JavaScript object.

26. What will console.log(typeof NaN) output?

A. "number"
B. "NaN"
C. "undefined"
D. "object"

Answer: A
Explanation: NaN stands for "Not-A-Number," but it is still considered a type of number in
JavaScript.

27. How can you prevent a form from submitting in JavaScript?

A. form.preventDefault();
B. event.preventDefault();
C. form.stopSubmit();
D. event.stopSubmit();

Answer: B
Explanation: event.preventDefault() is used to prevent the default action of an event,
such as form submission.

28. Which operator is used to concatenate strings in JavaScript?

A. +
B. &
C. concat()
D. +=

Answer: A
Explanation: The + operator is used to concatenate strings in JavaScript.

29. Which method can be used to check if an object contains a specific property in
JavaScript?

A. object.contains(key)
B. object.hasOwnProperty(key)
C. object.include(key)
D. object.getProperty(key)

Answer: B
Explanation: The method hasOwnProperty(key) is used to check if an object has a specific
property.

30. What will the expression [] == ![] return?

A. true
B. false
C. undefined
D. null

Answer: A
Explanation: This expression evaluates to true due to type coercion in JavaScript. An empty
array [] is loosely equal to false, and ![] evaluates to false.

31. What does the splice() method do in JavaScript?

A. Adds elements to the end of an array


B. Adds elements to the beginning of an array
C. Removes elements from an array
D. Both adds and removes elements from an array
Answer: D
Explanation: The splice() method allows you to add or remove elements from any position
in an array.

32. What is the result of console.log(0.1 + 0.2 == 0.3) in JavaScript?

A. true
B. false
C. undefined
D. NaN

Answer: B
Explanation: Due to floating-point precision issues in JavaScript, 0.1 + 0.2 does not exactly
equal 0.3, so the result is false.

33. Which of the following is used to add an event listener to an element in JavaScript?

A. element.addEventListener()
B. element.onEvent()
C. element.bindEvent()
D. element.listenEvent()

Answer: A
Explanation: addEventListener() is used to attach an event handler to a DOM element in
JavaScript.

34. How do you create a copy of an array in JavaScript?

A. array.copy()
B. array.clone()
C. array.slice()
D. array.duplicate()

Answer: C
Explanation: The slice() method can be used to create a shallow copy of an array.
35. Which of the following is the correct way to declare a class in JavaScript?

A. class MyClass {}
B. new class MyClass {}
C. class = MyClass {}
D. class MyClass[] {}

Answer: A
Explanation: The correct syntax for declaring a class in JavaScript is class MyClass {}.

36. How do you declare a function in JavaScript?

A. function myFunction() {}
B. def myFunction() {}
C. func myFunction() {}
D. declare function myFunction() {}

Answer: A
Explanation: Functions in JavaScript are declared using the function keyword followed by
the function name and parentheses.

37. What is the purpose of bind() in JavaScript?

A. To bind an event to an element


B. To set the value of this for a function
C. To attach a callback function
D. To link two functions together

Answer: B
Explanation: The bind() method creates a new function that, when invoked, has its this
value set to the provided value, with a given sequence of arguments.

38. What does the slice() method do in JavaScript?

A. Adds elements to the beginning of an array


B. Returns a portion of an array
C. Removes the last element of an array
D. Modifies the original array in place
Answer: B
Explanation: The slice() method is used to return a shallow copy of a portion of an array
without modifying the original array.

39. What will be the result of console.log(1 + "1")?

A. 11
B. 2
C. 1
D. NaN

Answer: A
Explanation: In JavaScript, the + operator will convert 1 to a string and concatenate it with "1",
resulting in "11".

40. How can you convert a string to lowercase in JavaScript?

A. string.toLowerCase()
B. string.lowerCase()
C. string.makeLower()
D. string.toLower()

Answer: A
Explanation: The toLowerCase() method converts a string to lowercase in JavaScript.

41. What will console.log([] == false) output?

A. true
B. false
C. undefined
D. null

Answer: A
Explanation: An empty array [] is loosely equal to false due to type coercion in JavaScript.

42. What is the purpose of the setInterval() method?


A. To stop a function from executing
B. To execute a function repeatedly at specified intervals
C. To delay the execution of a function
D. To repeat a function once

Answer: B
Explanation: setInterval() is used to repeatedly execute a function at fixed intervals,
specified in milliseconds.

43. Which method can be used to stop the execution of setInterval()?

A. clearInterval()
B. stopInterval()
C. cancelInterval()
D. endInterval()

Answer: A
Explanation: The clearInterval() method is used to stop a function from executing
repeatedly after being set with setInterval().

44. What is the output of console.log("10" - 5)?

A. 10
B. 5
C. 15
D. NaN

Answer: B
Explanation: The - operator in JavaScript triggers type coercion, converting the string "10" to
the number 10 and then subtracting 5, resulting in 5.

45. How can you declare a JavaScript variable that is globally scoped inside a function?

A. global var myVar;


B. this.myVar = value;
C. var myVar = value;
D. window.myVar = value;
Answer: B
Explanation: To declare a globally scoped variable inside a function, you can assign it to this,
as this refers to the global object when inside a function (in non-strict mode).

46. Which method is used to search for a substring within a string in JavaScript?

A. indexOf()
B. substring()
C. search()
D. includes()

Answer: A
Explanation: The indexOf() method returns the index of the first occurrence of a specified
substring in a string.

47. What does the delete operator do in JavaScript?

A. Deletes a variable
B. Deletes a property from an object
C. Deletes an element from an array
D. Deletes a function

Answer: B
Explanation: The delete operator is used to remove a property from an object, but it does not
affect variables or array elements directly.

48. What is the default value of a JavaScript variable that is declared but not assigned a
value?

A. 0
B. null
C. undefined
D. NaN

Answer: C
Explanation: In JavaScript, variables that are declared but not initialized have the default value
undefined.
49. Which of the following is an example of a falsy value in JavaScript?

A. "hello"
B. 1
C. true
D. "" (empty string)

Answer: D
Explanation: The empty string "" is a falsy value in JavaScript, meaning it evaluates to false
in a boolean context.

50. Which of the following methods is used to parse a query string into an object in
JavaScript?

A. URLSearchParams
B. queryParser()
C. parseQueryString()
D. getQueryParams()

Answer: A
Explanation: URLSearchParams is used to parse query strings into an object that can be
easily accessed and manipulated.

You might also like