Computer Knowledge
Programming Languages and Compilers
2,284 Questions
Programming languages and compilers involve the rules, syntax, and semantics used to write and execute software programs. Key areas include scripting languages, object oriented concepts, and parsing algorithms like top down parsers. Practice these computer science questions to build proficiency for technical and computer knowledge exams.
Object oriented languagesScripting languagesCompilers and parsersProgramming syntax
Programming Languages and Compilers Questions
What is the purpose of the join() method in JavaScript arrays?
-
To join all the elements of an array into a single string
-
To add an element to the end of an array
-
To remove the last element from an array
-
To sort the elements of an array
A
Correct answer
Explanation
The join() method joins all the elements of an array into a single string, separated by a specified separator.
Which of the following is not a built-in method of JavaScript objects?
-
hasOwnProperty()
-
toLocaleString()
-
valueOf()
-
forEach()
D
Correct answer
Explanation
The forEach() method is not a built-in method of JavaScript objects. It is a method of the Object.prototype object, which is added to all objects by default.
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 define a JavaScript object?
-
const object = {};
-
const object = new Object();
-
const object = { property: value };
-
const object = [property: value]
D
Correct answer
Explanation
The const object = [property: value] syntax is not a valid way to define a JavaScript object. The correct syntax is const object = {};, const object = new Object();, or const object = { property: value };.
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.
Which of the following is not a valid way to create a new JavaScript object?
-
const object = {};
-
const object = new Object();
-
const object = { property: value };
-
const object = [];
D
Correct answer
Explanation
The const object = []; syntax is not a valid way to create a new JavaScript object. The correct syntax is const object = {};, const object = new Object();, or const object = { property: value };.
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) => { ... }).
What is the primary programming language used for scripting in Unity?
A
Correct answer
Explanation
Unity primarily uses C# for scripting, although it also supports other languages like JavaScript and Boo.
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.