web123456

js output class interview questions (2)

Article Catalog

  • 1. What is the output of the following code?
  • 2. What is the output of the following code?
  • 3. What are the three stages of event communication?
  • 4. What is the output of the following code?
  • 5. What is the output of the following code?
  • 6. What is the output of the following code?
  • 7. What is the output of the following code?
  • 8. What is the output of the following code?
  • 9. What is the output of the following code?
  • 10. What is the output of the following code?

1. What is the output of the following code?

function Person(firstName, lastName) {
  this.firstName = firstName;
  this.lastName = lastName;
}

const member = new Person("Lydia", "Hallie");
Person.getFullName = () => this.firstName + this.lastName;

console.log(member.getFullName());
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

A: TypeError
B: SyntaxError
C: Lydia Hallie
D: undefined undefined

Answer: A

You cannot add properties to a constructor as you can with regular objects. If you want to add functionality to all objects at once, you must use a prototype. So in this case it should be written like this:

Person.prototype.getFullName = function () {
  return `${this.firstName} ${this.lastName}`;
}
  • 1
  • 2
  • 3

This would make () available, so why is this the right thing to do? Suppose we add this method to the constructor itself. Maybe not every Person instance needs this method. This would waste a lot of memory space because they would still have the property, which takes up memory space for each instance. Instead, if we just add it to the prototype, we only have to put it in one location in memory, but they can all access it!

2. What is the output of the following code?

function Person(firstName, lastName) {
  this.firstName = firstName;
  this.lastName = lastName;
}

const lydia = new Person("Lydia", "Hallie");
const sarah = Person("Sarah", "Smith");

console.log(lydia);
console.log(sarah);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
'
(of a computer) run

A: Person {firstName: “Lydia”, lastName: “Hallie”} and undefined
B: Person {firstName: “Lydia”, lastName: “Hallie”} and Person {firstName: “Sarah”, lastName: “Smith”}
C: Person {firstName: “Lydia”, lastName: “Hallie”} and {}
D:Person {firstName: “Lydia”, lastName: “Hallie”} and ReferenceError

Answer: A

For Sarah, we didn't use the new keyword. When you use new, it refers to the new empty object we created. However, if you don't add new it refers to the global object!

We specified equal to 'Sarah and equal to Smith. What we actually did was define theglobal.firstName = 'Sarah' and = 'Smith. sarah itself returns undefined.

3. What are the three stages of event communication?

A: Target > Capture > bubbling
B: bubble > target > capture
C: target > bubble > capture
D: capture > target > bubble

Answer: D

During the capture phase, the event passes down through the parent element to the target element. Then it reaches the target element and the bubbling begins.

4. What is the output of the following code?

function sum(a, b) {
  return a + b;
}

sum(1, "2");
  • 1
  • 2
  • 3
  • 4
  • 5
'
(of a computer) run

A: NaN
B: TypeError
C: “12”
D: 3

Answer: C

JavaScriptis a dynamically typed language: we don't specify the type of certain variables. Without your knowledge, values can be automatically converted to another type, called implicit type conversion. Forced conversion from one type to another.

In this example, JavaScript converts the number 1 to a string to make sense of the function and return the value. When you add a number type (1) to a string type ('2'), the number is treated as a string. We can concatenate strings like "Hello" + "World", so what happens here is that "1" + "2 " returns "12".

5. What is the output of the following code?

let number = 0;
console.log(number++);
console.log(++number);
console.log(number);
  • 1
  • 2
  • 3
  • 4
'
(of a computer) run

A: 1 1 2
B: 1 2 2
C: 0 2 2
D: 0 1 2

Answer: C

The suffixed unary operator ++:

  • Return value (return 0)

  • Value added (figure now 1)
    Prefix monadic operators ++:

  • Value added (figure now 2)

  • Return value (return 2)

So return 0 2 2.

6. What is the output of the following code?

function getPersonInfo(one, two, three) {
  console.log(one);
  console.log(two);
  console.log(three);
}

const person = "Lydia";
const age = 21;

getPersonInfo`${person} is ${age} years old`;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
'
(of a computer) run

A: Lydia 21 ["", “is”, “years old”]
B: ["", “is”, “years old”] Lydia 21
C: Lydia ["", “is”, “years old”] 21

Answer: B

If a labeled template string is used, the value of the first argument is always an array of string values. The remaining arguments get the value of the expression passed into the template string!

7. What is the output of the following code?

function checkAge(data) {
  if (data === { age: 18 }) {
    console.log("You are an adult!");
  } else if (data == { age: 18 }) {
    console.log("You are still an adult.");
  } else {
    console.log(`Hmm.. You don't have an age I guess`);
  }
}

checkAge({ age: 18 });
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
'
(of a computer) run

A: You are an adult!
B: You are still an adult.
C: Hmm… You don’t have an age I guess

Answer: C

In comparing equality, primitive types are compared by their values, while objects are compared by their references.JavaScript checks whether objects have references to the same locations in memory.

The object we pass as a parameter and the object we use to check for equality are located in different places in memory, so their references are different.

This is why { age: 18 } === { age: 18 } and { age: 18 } == { age: 18 } return false.

8. What is the output of the following code?

function getAge(...args) {
  console.log(typeof args);
}

getAge(21);
  • 1
  • 2
  • 3
  • 4
  • 5
'
(of a computer) run

A: “number”
B: “array”
C: “object”
D: “NaN”

Answer: C

extended operator (...)args) returns an array with arguments. An array is an object, so typeof args returns object.

9. What is the output of the following code?

function getAge() {
  "use strict";
  age = 21;
  console.log(age);
}

getAge();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

A: 21
B: undefined
C: ReferenceError
D: TypeError

Answer: C

Using ``use strict'' ensures that global variables are not accidentally declared. We never declared the variable age because we used ``use strict'' and it would have raised a ReferenceError. If we hadn't used ``use strict'', it would have worked because the property age` would have been added to the global object.

10. What is the output of the following code?

const sum = eval("10*10+5");
  • 1
'
(of a computer) run

A: 105
B: “105”
C: TypeError
D: “10*10+5”

Answer: A

eval evaluates the code passed as a string. If it is an expression, as it is in this case, it will evaluate the expression. An expression of 10 * 10 + 5 evaluates to 105.