Chapter 7 : JSON, Iterators, and Generators
JSON (JavaScript Object Notation) is a popular format for storing and exchanging data. This section will cover how to work with JSON in JavaScript, including parsing and stringifying data. You'll also learn about iterators and generators, which provide ad
Ques 1: What does JSON stand for in JavaScript?
A. JavaScript Object Notation
B. JavaScript Optimized Notation
C. JavaScript Online Notation
D. JavaScript Object Node
Ques 2: Which of the following is the correct format for a JSON object?
A. {"name": "John", "age": 30}
B. {"name" = "John", age: 30}
C. {name: "John", age = 30}
D. {"name" : "John", "age": 30}
Ques 3: What is the output of JSON.stringify({name: "Alice", age: 25})?
A. {"name": "Alice", "age": 25}
B. [{name: "Alice", age: 25}]
C. {name: Alice", age: 25}"
D. {"name": "Alice", "age"}
Ques 4: Which method is used to parse a JSON string into a JavaScript object?
A. JSON.parse()
B. JSON.stringify()
C. JSON.toObject()
D. JSON.decode()
Ques 5: Which of the following is not a valid JSON data type?
A. Function
B. Object
C. Array
D. String
Ques 6: How do you convert a JavaScript object to a JSON string?
A. JSON.stringify()
B. JSON.toString()
C. JSON.parse()
D. Object.toJSON()
Ques 7: What will JSON.parse('{"name":"Bob", "age":25}') return?
A. {name: "Bob", age: 25}
B. {name: 'Bob', age: 25}
C. {"name": "Bob", "age": 25}
D. null
Ques 8: What is the primary purpose of using JSON in JavaScript?
A. To convert data into an object
B. To serialize and deserialize data
C. To handle asynchronous functions
D. To manipulate DOM elements
Ques 9: Which of the following will correctly parse a JSON array in JavaScript?
A. JSON.parse('[1, 2, 3]')
B. JSON.array('[1, 2, 3]')
C. JSON.parseArray('[1, 2, 3]')
D. JSON.load('[1, 2, 3]')
Ques 10: How do you check if a variable contains valid JSON in JavaScript?
A. try...catch block
B. isValidJSON()
C. JSON.isValid()
D. JSON.check()
Ques 11: What does the JSON.stringify() method do with undefined values?
A. Removes them from the output
B. Converts them into null
C. Throws an error
D. Keeps them as undefined
Ques 12: How would you define an iterator in JavaScript?
A. An object that defines the iteration behavior
B. A function that returns a list of elements
C. A special type of function for asynchronous code
D. A loop that iterates over arrays
Ques 13: Which method is used to obtain the next value from an iterator?
A. next()
B. advance()
C. moveNext()
D. get()
Ques 14: What will const iterator = [1, 2, 3].values(); iterator.next() return?
A. { value: 1, done: false }
B. { value: 1, done: true }
C. { value: 2, done: false }
D. { value: undefined, done: true }
Ques 15: How do you create a generator function in JavaScript?
A. By using function* syntax
B. By defining a function inside a class
C. By adding yield to an existing function
D. By using async function syntax
Ques 16: Which of the following will stop the execution of a generator function and return a value?
A. yield
B. return
C. break
D. exit
Ques 17: What does the yield keyword do in a generator function?
A. Pauses the function and returns a value
B. Executes the function in parallel
C. Calls another function
D. Returns the value of a variable
Ques 18: How do you call a generator function in JavaScript?
A. generatorFunction()
B. generatorFunction().next()
C. generatorFunction().call()
D. next(generatorFunction)
Ques 19: What does the done property of an iterator represent?
A. Whether the iteration has finished
B. The current value of the iterator
C. The number of steps taken in the iteration
D. The next value to be returned
Ques 20: How can you iterate over an object's properties in JavaScript?
A. Using Object.keys() and forEach()
B. Using Object.values() and forEach()
C. Using Object.entries() and for...in loop
D. All of the above
Ques 21: Which of the following is an example of a generator function in JavaScript?
A. function* generateNumbers() { yield 1; }
B. function generateNumbers() { return 1; }
C. function* generateNumbers() { return 1; }
D. function generateNumbers() { yield 1; }
Ques 22: How do you iterate over an array using an iterator in JavaScript?
A. for...of loop
B. forEach()
C. map()
D. while()
Ques 23: What will JSON.stringify({name: "Alice", skills: undefined}) return?
A. {"name":"Alice","skills":null}
B. {"name":"Alice"}
C. {"name":"Alice","skills":undefined}
D. {}"
Ques 24: Which of the following is true about the next() method in an iterator?
A. It returns an object with value and done
B. It executes the iterator function immediately
C. It always returns the final value
D. It is used to pause the generator function
Ques 25: How do you resume a generator function from where it was paused?
A. Using next()
B. Using yield
C. Using continue()
D. Using resume()