Computer Knowledge
Java Core Classes and Threads
1,935 Questions
Java core classes and threads form the foundation of object oriented programming and are crucial for IT officer and programming exams. This includes concepts like string mutability, collections, and multithreading. Solve these questions to test your practical coding and theoretical knowledge.
String and StringBuffer classesThread execution methodsJava collections frameworkCharacter streams input outputVariable serialization rules
Java Core Classes and Threads Questions
What is the purpose of the Object.keys() method in JavaScript?
-
To return an array of all the keys in an object
-
To return an array of all the values in an object
-
To return the number of keys in an object
-
To return the number of values in an object
A
Correct answer
Explanation
The Object.keys() method returns an array of all the keys in an object, in the same order as they were defined.
Which of the following is not a valid way to access the properties of an object in JavaScript?
-
object.property
-
object['property']
-
object[property]
-
object.getProperty()
D
Correct answer
Explanation
The object.getProperty() syntax is not a valid way to access the properties of an object in JavaScript. The correct syntax is object.property, object['property'], or object[property].
What is the purpose of the Array.from() method in JavaScript?
-
To convert an array-like object to an array
-
To convert an array to an array-like object
-
To create a new array with the same elements as an existing array
-
To sort the elements of an array
A
Correct answer
Explanation
The Array.from() method converts an array-like object (such as a string, a NodeList, or an HTMLCollection) to an array.
Which of the following is not a valid way to iterate over the elements of an array in JavaScript?
-
for (let i = 0; i < array.length; i++) { ... }
-
for (const element of array) { ... }
-
array.forEach((element, index) => { ... })
-
array.map((element, index) => { ... })
D
Correct answer
Explanation
The array.map() method is not a valid way to iterate over the elements of an array in JavaScript. It is a method that creates a new array with the results of calling a specified function on each element of the original array.
What is the purpose of the Object.assign() method in JavaScript?
-
To copy the properties of one object to another
-
To merge two objects into a new object
-
To create a new object with the same properties as an existing object
-
To delete the properties of an object
A
Correct answer
Explanation
The Object.assign() method copies the properties of one object to another. It can be used to merge two objects into a new object, or to create a new object with the same properties as an existing object.
Which of the following is not a valid way to access the elements of an object in JavaScript?
-
object.property
-
object['property']
-
object[property]
-
object.getProperty()
D
Correct answer
Explanation
The object.getProperty() syntax is not a valid way to access the properties of an object in JavaScript. The correct syntax is object.property, object['property'], or object[property].
What is the purpose of the Array.prototype.reduce() method in JavaScript?
-
To reduce an array to a single value
-
To reduce an array to a new array
-
To reduce an array to an object
-
To reduce an array to a string
A
Correct answer
Explanation
The Array.prototype.reduce() method reduces an array to a single value. The value is determined by a reduce function that is passed to the method.
What is the purpose of the Object.freeze() method in JavaScript?
-
To prevent an object from being modified
-
To prevent an object from being deleted
-
To prevent an object from being accessed
-
To prevent an object from being serialized
A
Correct answer
Explanation
The Object.freeze() method prevents an object from being modified. Once an object is frozen, its properties cannot be added, removed, or changed.
Which of the following is not a valid way to iterate over the properties of an object in JavaScript?
-
for (const property in object) { ... }
-
for (const [property, value] of Object.entries(object)) { ... }
-
object.forEach((property, value) => { ... })
-
object.map((property, value) => { ... })
C
Correct answer
Explanation
The object.forEach((property, value) => { ... }) syntax is not a valid way to iterate over the properties of an object in JavaScript. The correct syntax is for (const property in object) { ... }, for (const [property, value] of Object.entries(object)) { ... }, or object.map((property, value) => { ... }).
Which lifecycle method is called when a React component is first mounted?
-
componentDidMount()
-
componentWillMount()
-
render()
-
constructor()
A
Correct answer
Explanation
The componentDidMount() lifecycle method is called immediately after a component is mounted (inserted into the DOM). It is commonly used to perform side effects such as fetching data, setting up event listeners, or integrating with third-party libraries.
Which JavaScript method is used to send an asynchronous HTTP request to a server?
-
fetch()
-
XMLHttpRequest()
-
send()
-
open()
A
Correct answer
Explanation
The fetch() method is a modern and simplified way to make asynchronous HTTP requests in JavaScript.
What is the syntax for using the fetch() method?
-
fetch(url)
-
fetch(url, options)
-
fetch(url, data)
-
fetch(url, headers)
B
Correct answer
Explanation
The fetch() method takes two arguments: the URL of the resource to fetch and an optional options object.
What is the default HTTP method used by the fetch() method?
A
Correct answer
Explanation
The default HTTP method used by the fetch() method is GET, which is used to retrieve data from a server.
What is the purpose of the JSON.stringify() method?
-
To convert a JavaScript object to a JSON string
-
To convert a JSON string to a JavaScript object
-
To parse a JSON string
-
To validate a JSON string
A
Correct answer
Explanation
The JSON.stringify() method converts a JavaScript object or value to a JSON string.
What is the purpose of the JSON.parse() method?
-
To convert a JavaScript object to a JSON string
-
To convert a JSON string to a JavaScript object
-
To parse a JSON string
-
To validate a JSON string
B
Correct answer
Explanation
The JSON.parse() method converts a JSON string to a JavaScript object.