Input/Output interview questions

This page consists of all the Input/Output related questions based on various topics related to frontend.

Click on the question title to see the code and solution.

Question 1: What will get printed in console.log()? Asked in: Nagarro Axtria
Hints: JS var-let-const use strict
1
2
3
4
5
6
7
8
9
10
11
12
13
14
var x = 1;
var x = 2;
console.log(x); // ?

"use strict"
let x = 1;
let x = 2;
console.log(x); // ?

"use strict"
var x = 1;
var x = 2;
console.log(x); // ?

Question 2: Is this statement correct? If yes, what will be the output here? Asked in: Nagarro HCL GreyHR
Hints: JS ES6 arrow functions
1
2
3
let bob = a => a + 100;
console.log(bob(2)); //?

Question 3: What value will be displayed in the console?
Hints: JS arrays
1
2
3
4
5
6
var arr = [];
arr[0] = 'a';
arr[1] = 'b';
arr.foo = 'c';
console.log(arr.length);

Question 4: What will happen, if we declare an array using a const and try to change one of its values? Asked in: Walmart Publicis Sapient
Hints: JS ES6 const
1
2
3
4
const s = [5, 6, 7];
s[2] = 45;
console.log(s);

Question 5: What will be the output here, and how does Object.create() works?
Hints: JS Object creation in JS
1
2
3
4
5
var obj = {a: 5};
var obj2 = Object.create(obj);
delete obj2.a;
console.log(obj2.a);    // ?

Question 6: What will be the output?
Hints: JS arrays
1
2
3
4
let arr = [];
arr[0] = 'a';
console.log(arr['0'] === arr[0]);   // true or false

Question 7: What will be the length of an array?
Hints: JS arrays
1
2
3
let arr = [1 , , 3];
console.log(arr.length);   // ?

Question 8: What will be the output here, and how does (...) works?
Hints: JS ES6 Spread operator
1
2
3
4
5
6
function sum(x, y, z) {
  return x + y + z;
}
const numbers = [1, 2, 3];
console.log(sum(...numbers));   //?

Question 9: What will be the output here and why?
Hints: JS this
1
2
3
4
5
6
7
8
9
10
var hero = {
    _name: 'John Doe',
    getSecretIdentity: function () {
        return this._name;
    }
};
var stoleSecretIdentity = hero.getSecretIdentity;
console.log(stoleSecretIdentity());     // ?
console.log(hero.getSecretIdentity());      // ?

Question 10: How does bind() works, and what will be the output here? Asked in: Publicis Sapient HCL Nagarro
Hints: JS this bind()
1
2
3
4
5
6
7
8
9
10
11
var obj = {
    x: 2,
    foo: function () {
        console.log(this.x);
    }
}

// using bind()
var baz = obj.foo.bind({x: 3});
baz(); 

Question 11: What will be the output, if we use delete operator to delete an item from an array?
Hints: JS delete
1
2
3
4
const arr = [10, 20, 30, 40];
console.log(delete arr[2]);
console.log(arr);

Question 12: What will be the output here?
Hints: JS event-loop
1
2
3
4
5
6
7
8
9
10
let a = true;

setTimeout(() => {
  a = false;
}, 2000);

while(a) {
  console.log("Hello");
}

Question 13: What will be the output here, if we use the same name for a function and a variable: Part 1?
Hints: JS scope hoisting
1
2
3
4
5
6
7
8
9
10
var a = 1;

function b() {
  a = 10;
  function a() {}
  return;
}
b();
console.log(a); // ?

Question 14: What will be the output here, if we use the same name for a function and a variable: Part 2?
Hints: JS scope hoisting
1
2
3
4
5
6
7
8
9
10
var a = 1;

function b() {
  a = 10;
  return;
  function a() {}
}
b();
console.log(a); // ?

Question 15: What will be the output here, if we use the same name for a function and a variable: Part 3?
Hints: JS scope hoisting
1
2
3
4
5
6
7
8
9
10
var a = 1;

function b() {
  a = 10;
  a = function() {}
  return;
}
b();
console.log(a); // ?

Question 16: What will happen, if we return an object explicitly from a constructor function?
Hints: JS constructor-function
1
2
3
4
5
6
7
8
9
function Car() {
  this.name = name; 
  this.color = color;

  return {foo: 1, bar: 2};
}

var bmw = new Car(); 

Question 17: What will happen, if we try to create an object using Object.create()?
Hints: JS Object.create()
1
2
3
4
5
6
var obj = Object.create(null);
var obj2 = Object.create();

console.log(obj);   // ?
console.log(obj2);  // ?

Question 18: What will be the output, if we create an an arrow function inside a Class?
Hints: JS Class arrow function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class User {
  constructor() {
    this.name = "foo";
  }

  getName() {
    console.log('get name', this.name)
  }

  getAge = () => {
    console.log('get age', this.name)
  }
}

var user1 = new User();

console.log(user1);   // ?

user1.getName();    // ?
user1.getAge();   // ?

Question 19: What will be the output here?
Hints: JS this execution context arguments
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var a = 5;
function length() {
  console.log('this : ', this.a);
}

var obj = {
  a: 10,
  method: function(a) {
    length(); 
    console.log(arguments)
    arguments[0]();
  }
}

obj.method(length, 2);

Question 20: What will be the output here?
Hints: JS this execution context arguments
1
2
3
4
5
6
7
(function(a){
  return (function(){
    console.log(a);
    a = 23;
  })()
})(45);

Question 21: What will be the value of `this` inside the following functions?
Hints: JS this execution context
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const a = function(){
  console.log(this);

  const b = {
    func1: function(){
      console.log(this);
    }
  }

  const c = {
    func2: ()=>{
      console.log(this);
    }
  }

  b.func1();
  c.func2();
}

a();

Question 22: Which of these selectors will get higher priority?
Hints: CSS Specificity
// html
<div>
  <p>Hello World!</p>
</div>

// css
div p {
  color: red;
}

p {
  color: blue
}