Master Javascript Input and Output MCQs

Devanshu Agarwal

Written by Devanshu Agarwal /

Introduction

JavaScript is an incredibly versatile language. You can use it for a variety of programming tasks, including handling input and output. Whether you're creating a simple web page or developing a complex web application, effectively managing input and output is essential for success. In this blog post, we'll explore some of the most common input-output questions in JavaScript.

We'll provide you with code snippets that display the questions, followed by a set of multiple choice answers. You'll get a chance to select your answer, and we'll provide you with a detailed explanation of the correct answer. By the end of this post, you'll have a better understanding of how to handle input and output in JavaScript, and you'll be better prepared for your next coding challenge.

let's get started with the questions.

Question 1

Which of the following is the correct syntax for outputting "Hello World" to the console in JavaScript?

A. console.log("Hello World")

B. console.write("Hello World")

C. console.print("Hello World")

D. console.writeLine("Hello World")

Click to see the answer!

The correct answer is A. console.log("Hello World"). The console.log() method is used to output text to the console in JavaScript. This method can be used to output strings, numbers, variables, and more.

Question 2

What will be the output of the following code?

JS
let x = 5;
let y = 3;
console.log("The value of x is " + x + " and the value of y is " + y);

A. The value of x is 5 and the value of y is 3

B. The value of x is 5 and the value of y is undefined

C. The value of x is undefined and the value of y is 3

D. The value of x is undefined and the value of y is undefined

Click to see the answer!

The correct answer is A. The output of the code will be: "The value of x is 5 and the value of y is 3". In this code, we're using the + operator to concatenate the strings and the variables. This is a common way to output variables to the console in JavaScript.

Question 3

You are given the following code:

JS
let a = 10;
let b = "20";
console.log(a + b);

What will be the output of this code?

A. 1020

B. "30"

C. 30

D. "1020"

Click to see the answer!

The correct answer is D. The output of the code will be the string "1020". In this code, the + operator is used to concatenate the variables a and b, but since b is a string, JavaScript will treat the + operator as a concatenation operator, resulting in the string "1020".

Question 4

Consider the following code:

JS
const arr = [10, 20, 30];
console.log(arr[3]);

What will be the output of this code?

A. 10

B. 20

C. 30

D. undefined

Click to see the answer!

The correct answer is D. The output of the code will be undefined. In this code, we're trying to access the value of an element at index 3 of the array arr. However, since arr only has three elements (at indices 0, 1, and 2), the value at index 3 does not exist, and attempting to access it will return undefined.

Question 5

Consider the following code:

JS
const person = {
  name: "John",
  age: 30,
  city: "New York"
};

for (let prop in person) {
  console.log(prop);
}

What will be the output of this code?

A. "name", "age", "city"

B. ["name", "age", "city"]

C. { "name": "John", "age": 30, "city": "New York" }

D. Nothing will be outputted

Click to see the answer!

The correct answer is A. The output of the code will be the strings "name", "age", and "city" on separate lines. In this code, we're using a for...in loop to iterate over the properties of the person object. The loop iterates over each property name, and logs it to the console using console.log(prop).

Question 6

Consider the following code:

JS
let x = 10;

function foo() {
  console.log(x);
  let x = 20;
}

foo();

What will be the output of this code?

A. 10

B. 20

C. undefined

D. This code will throw an error

Click to see the answer!

The correct answer is D. This code will throw an error. In this code, we're declaring a variable x with the value of 10 outside the foo function, and then trying to log its value within the foo function before it has been declared in that function. When the function is called, the variable x inside the function is declared with a value of 20, but the console.log(x) statement that references the x variable is executed before this declaration, causing a reference error.

Question 7

Consider the following code:

JS
let x = [1, 2, 3];
let y = x;
x.push(4);

console.log(y);

What will be the output of this code?

A. [1, 2, 3, 4]

B. [1, 2, 3]

C. [1, 2, 3, [4]]

D. This code will throw an error

Click to see the answer!

The correct answer is A. The output of the code will be [1, 2, 3, 4]. In this code, we're declaring an array x with the values of 1, 2, and 3. We then declare a new variable y and set it equal to x. Since y is a reference to x, when we push 4 onto x, it also gets added to y. Finally, we log the value of y to the console, which will now include the value 4.

Question 8

Consider the following code:

JS
let obj = {
  x: 1,
  y: 2,
  z: 3
};

let keys = Object.keys(obj);
console.log(keys.length);

What will be the output of this code?

A. 1

B. 2

C. 3

D. This code will throw an error

Click to see the answer!

The correct answer is C. The output of the code will be 3. In this code, we're declaring an object obj with three properties: x, y, and z. We then declare a variable keys and set it equal to an array of the object's keys using the Object.keys method. Finally, we log the length of the keys array to the console, which is 3 since there are three keys in the obj object.

Question 9

Consider the following code:

JS
let arr = [1, 2, 3, 4, 5];
let result = arr.reduce((total, current) => {
  return total + current;
}, 0);

console.log(result);

What will be the output of this code?

A. 10

B. 15

C. 20

D. This code will throw an error

Click to see the answer!

The correct answer is B. The output of the code will be 15. In this code, we're declaring an array arr with the values of 1, 2, 3, 4, and 5. We then use the reduce method to add up all the values in the array and store the result in the variable result. The reduce method takes two arguments: a callback function that specifies how to combine the values, and an initial value for the total (in this case, 0). The callback function takes two arguments: the total so far, and the current value being iterated over. In each iteration, the current value is added to the total. Finally, we log the value of result to the console, which will be the sum of all the values in the arr array, which is 15.

Question 10

Consider the following code:

JS
let str = 'hello world';
let result = str.substring(6, 0);

console.log(result);

What will be the output of this code?

A. 'world'

B. 'hello'

C. This code will throw an error

D. Undefined

Click to see the answer!

The correct answer is D. The output of the code will be undefined. In this code, we're declaring a string variable str with the value of 'hello world'. We then use the substring method to extract a portion of the string, starting from index 6 and ending at index 0. However, the second argument to substring should be greater than the first argument, so this code will return an empty string '' instead of 'world' or 'hello'. Finally, we log the value of result to the console, which will be undefined since there is no valid substring to return.

Conclusion

Congratulations on making it to the end of our list of input-output questions for JavaScript! We hope that these questions have helped you to better understand how input and output works in JavaScript and have given you a chance to test your knowledge of the language.

Remember, it's important to understand how input and output works in JavaScript in order to write more efficient, bug-free code. By mastering the concepts we've covered in this post, you'll be well on your way to becoming a more proficient JavaScript developer.

We hope you found this post helpful, and be sure to check out our other articles on JavaScript and programming in general. Thanks for reading, and happy coding!