Q
Which literal correctly defines an object with a method?

Answer & Solution

Answer: Option B
Solution:
An object with a method can be defined with the method as a property with a function value.
Related Questions on Average

Which of the following correctly defines a method in an object literal?

A). { name: 'Alice', greet: () => 'Hello' }

B). { name: 'Alice', greet() { return 'Hello'; } }

C). { name: 'Alice', greet: function() { return 'Hello'; } }

D). All of the above

Which of the following is a valid hexadecimal literal?

A). 0xG1

B). 0x1G

C). 0x1A

D). 0x1Z

Which of these is a valid Boolean literal?

A). 1

B). 0

C). 1

D). 0

How would you define an empty object literal?

A). []

B). {}

C). empty

D). Object()

What is the correct way to declare a string literal?

A). 'Hello'

B). Hello

C). Hello

D). All of the above

Which of these is a correct array literal?

A). {}

B). []

C). [[]]

D). Array()

What is the correct syntax for a template literal?

A). 'Hello, ${name}!'

B). Hello, ${name}!

C). Hello, ${name}!

D). templateHello, ${name}!

Which syntax is used to create a BigInt literal?

A). BigInt(12345)

B). 12345n

C). bigint 12345

D). 12345N

Which literal can contain mixed data types?

A). Object

B). Array

C). String

D). Number

Which regular expression literal matches a string containing 'abc'?

A). /abc/

B). /a.b.c/

C). /ab*c/

D). /abc/i