Chapter 2 Beginner 54 Questions

Practice Questions — JavaScript in the Browser - Script Tags and Console

← Back to Notes
10 Easy
12 Medium
10 Hard

Topic-Specific Questions

Question 1
Easy
What appears in the console when this HTML page loads?
<script>
  console.log("First");
</script>
<p>Hello</p>
<script>
  console.log("Second");
</script>
Script tags execute in order as the browser reads the page.
First
Second
Question 2
Easy
What is the output?
console.log("A");
console.log("B");
console.log("C");
console.log statements execute in order.
A
B
C
Question 3
Easy
What is the output?
console.log(2, 4, 6, 8);
console.log can take multiple arguments separated by commas.
2 4 6 8
Question 4
Easy
What is the output?
console.log("10" + 20);
console.log(10 + 20);
The first line has a string, the second has two numbers.
1020
30
Question 5
Medium
What happens when this page loads?
<!DOCTYPE html>
<html>
<head>
  <script>
    document.getElementById("msg").textContent = "Hello!";
  </script>
</head>
<body>
  <p id="msg">Original</p>
</body>
</html>
The script runs in the head, before the body elements exist.
A TypeError appears in the console: Cannot set properties of null. The paragraph shows "Original".
Question 6
Medium
What is the output?
let x = prompt("Enter a number:");
console.log(typeof x);

Assume the user types 42 and clicks OK.

What type does prompt() always return?
string
Question 7
Medium
What is the output?
let result = confirm("Are you sure?");
console.log(result);
console.log(typeof result);

Assume the user clicks Cancel.

confirm() returns a boolean value.
false
boolean
Question 8
Medium
What is the output?
let name = prompt("Enter name:");
console.log(name);
console.log(typeof name);

Assume the user clicks Cancel without typing anything.

What does prompt() return when the user cancels?
null
object
Question 9
Easy
Aarav wrote this but nothing runs. Find the bug:
<script src="app.js">
  console.log("Hello!");
</script>
What happens when a script tag has both src and inline code?
The inline console.log("Hello!") is ignored because the script tag has a src attribute. When src is present, inline code is not executed.
Question 10
Hard
What is the output?
<script>
  var a = 10;
</script>
<script>
  console.log(a);
  var b = 20;
</script>
<script>
  console.log(a + b);
</script>
All script tags on a page share the same global scope.
10
30
Question 11
Easy
Write an HTML page that uses console.log to print your name, console.warn to print a warning, and console.error to print an error message.
Use three different console methods.
<!DOCTYPE html>
<html>
<body>
  <script>
    console.log("My name is Rohan");
    console.warn("This is a warning message");
    console.error("This is an error message");
  </script>
</body>
</html>
Question 12
Hard
What is the output?
console.log(1);
setTimeout(function() {
  console.log(2);
}, 0);
console.log(3);
setTimeout always runs its callback after the current code finishes, even with 0 delay.
1
3
2
Question 13
Medium
Priya wants to add two numbers from prompts, but gets concatenation instead of addition. Fix the bug:
let a = prompt("First number:");
let b = prompt("Second number:");
console.log("Sum: " + (a + b));

User enters 5 and 3. Output: Sum: 53

prompt() returns strings. How do you convert them to numbers?
let a = Number(prompt("First number:"));
let b = Number(prompt("Second number:"));
console.log("Sum: " + (a + b));
Output: Sum: 8
Question 14
Hard
What is the output?
console.log(typeof alert);
console.log(typeof prompt);
console.log(typeof console);
alert and prompt are functions. What about console itself?
function
function
object
Question 15
Medium
Write code that asks the user for their name using prompt, then displays a greeting on the web page (not in the console) inside a paragraph with id 'greeting'.
Use prompt() to get the name, then .textContent to display it.
<p id="greeting"></p>
<script>
  let name = prompt("What is your name?");
  document.getElementById("greeting").textContent = "Hello, " + name + "!";
</script>
Question 16
Easy
What is the output?
console.log("Hello");
// console.log("World");
console.log("JavaScript");
Lines starting with // are comments — they do not execute.
Hello
JavaScript
Question 17
Medium
What is the output?
/* 
console.log("A");
console.log("B");
*/
console.log("C");
/* and */ create multi-line comments.
C
Question 18
Hard
Vikram wrote this code. The page loads, the button appears, but clicking it does nothing. No errors in the console either. Find the bug:
<!DOCTYPE html>
<html>
<body>
  <button onclick="showMessage">Click Me</button>
  <p id="msg"></p>
  <script>
    function showMessage() {
      document.getElementById("msg").textContent = "Button clicked!";
    }
  </script>
</body>
</html>
Look at the onclick attribute. How do you call a function?
The onclick should be onclick="showMessage()" with parentheses. Without (), the function is referenced but never called.
Question 19
Hard
What is the output in the console?
console.log("Start");
alert("Pause!");
console.log("End");
alert() blocks code execution until the user clicks OK.
Start appears in the console. Then the alert popup shows. After the user clicks OK, End appears in the console.
Question 20
Hard
Write an HTML page with a button. When clicked, use console.time and console.timeEnd to measure how long it takes to concatenate the string 'x' to itself 100000 times, and log the final string length.
Use console.time('label') before, console.timeEnd('label') after, and a for loop.
<button onclick="measure()">Run Test</button>
<script>
  function measure() {
    console.time("concat");
    let str = "";
    for (let i = 0; i < 100000; i++) {
      str += "x";
    }
    console.timeEnd("concat");
    console.log("String length: " + str.length);
  }
</script>

Mixed & Application Questions

Question 1
Easy
What is the output?
console.log("Hello" + " " + "Aarav");
The + operator joins strings.
Hello Aarav
Question 2
Easy
Write code using console.log that prints the numbers 1 through 5, each on a separate line.
Use five console.log statements.
console.log(1);
console.log(2);
console.log(3);
console.log(4);
console.log(5);
Question 3
Medium
What does this HTML page display on the web page (not the console)?
<!DOCTYPE html>
<html>
<body>
  <p id="demo">Old Text</p>
  <script>
    console.log("New Text");
  </script>
</body>
</html>
console.log does not change the web page.
The web page displays Old Text. The console shows New Text.
Question 4
Medium
What is the output?
let a = "5";
let b = "10";
console.log(a + b);
console.log(Number(a) + Number(b));
String + string concatenates. Number() converts strings to numbers.
510
15
Question 5
Medium
Write an HTML page with an external JavaScript file. The HTML should have a paragraph with id 'output'. The external JS file should set the text to 'Loaded from external file!'.
Create two files: an HTML file with a script src tag, and a .js file.
<!-- index.html -->
<!DOCTYPE html>
<html>
<body>
  <p id="output"></p>
  <script src="script.js"></script>
</body>
</html>

// script.js
document.getElementById("output").textContent = "Loaded from external file!";
Question 6
Medium
What is the output?
console.log(parseInt("42px"));
console.log(parseInt("abc"));
console.log(Number("42px"));
console.log(Number("abc"));
parseInt reads digits until it hits a non-digit. Number requires the entire string to be numeric.
42
NaN
NaN
NaN
Question 7
Medium
Ananya wrote this HTML file but the browser shows the JavaScript code as text on the page instead of running it. Find the bug:
<!DOCTYPE html>
<html>
<body>
  <p>
    console.log("Hello World");
  </p>
</body>
</html>
JavaScript code needs a special tag to execute.
The code is inside a <p> tag, not a <script> tag. It should be:
<script>
  console.log("Hello World");
</script>
Question 8
Hard
What is the output?
let x = prompt("Enter value:");

if (x === null) {
  console.log("Cancelled");
} else if (x === "") {
  console.log("Empty");
} else {
  console.log("Value: " + x);
}

The user clicks OK without typing anything.

When the user clicks OK without typing, prompt returns an empty string, not null.
Empty
Question 9
Hard
What is the output?
console.log("A");

setTimeout(function() {
  console.log("B");
}, 1000);

setTimeout(function() {
  console.log("C");
}, 0);

console.log("D");
All synchronous code runs first. Then setTimeout callbacks run based on their delay.
A
D
C
B
Question 10
Hard
Write an HTML page that uses console.table to display an array of 3 student objects. Each student should have name, age, and grade properties.
Create an array of objects and pass it to console.table().
<script>
  let students = [
    { name: "Aarav", age: 14, grade: "9th" },
    { name: "Priya", age: 15, grade: "10th" },
    { name: "Vikram", age: 16, grade: "11th" }
  ];
  console.table(students);
</script>
Question 11
Easy
What is the output?
console.log(typeof console.log);
console.log(typeof alert);
console.log and alert are both built-in functions.
function
function
Question 12
Hard
Write an HTML page with a text input and a button. When clicked, use prompt-like behavior without actually using prompt: read the input value, and if it is empty, show a red error message 'Name cannot be empty!'. If it has a value, show a green success message 'Welcome, [name]!'.
Check if the input value is an empty string. Use style.color to change text color.
<input type="text" id="nameInput" placeholder="Enter name">
<button onclick="validate()">Submit</button>
<p id="msg"></p>
<script>
  function validate() {
    let name = document.getElementById("nameInput").value;
    let msg = document.getElementById("msg");
    if (name === "") {
      msg.textContent = "Name cannot be empty!";
      msg.style.color = "red";
    } else {
      msg.textContent = "Welcome, " + name + "!";
      msg.style.color = "green";
    }
  }
</script>

Multiple Choice Questions

MCQ 1
Which tag is used to add JavaScript to an HTML page?
  • A. <javascript>
  • B. <js>
  • C. <script>
  • D. <code>
Answer: C
C is correct. The <script> tag is the standard way to include JavaScript in HTML. The other tags (<javascript>, <js>) do not exist. <code> is for displaying code as text.
MCQ 2
What keyboard shortcut opens the browser DevTools in Chrome?
  • A. Ctrl + D
  • B. F12
  • C. Ctrl + F
  • D. Alt + Tab
Answer: B
B is correct. F12 opens DevTools in Chrome, Edge, and Firefox. Ctrl + Shift + J also works in Chrome to open the Console directly. Ctrl + D bookmarks the page, and Ctrl + F opens Find.
MCQ 3
What does console.log('Hello') do?
  • A. Displays 'Hello' on the web page
  • B. Shows an alert popup saying 'Hello'
  • C. Prints 'Hello' to the browser's developer console
  • D. Saves 'Hello' to a file
Answer: C
C is correct. console.log() outputs to the Developer Console only (F12), not to the web page (A), not as a popup (B), and not to a file (D).
MCQ 4
What does prompt() return when the user clicks Cancel?
  • A. undefined
  • B. false
  • C. An empty string
  • D. null
Answer: D
D is correct. Clicking Cancel on prompt() returns null. Clicking OK with nothing typed returns an empty string "". This distinction is important for handling user input.
MCQ 5
What happens to inline code when a script tag has a src attribute?
  • A. Both the external file and inline code run
  • B. The inline code runs first, then the external file
  • C. The inline code is ignored
  • D. An error occurs
Answer: C
C is correct. When a <script> tag has a src attribute, any code between the opening and closing tags is completely ignored. To run both, you need two separate script tags.
MCQ 6
What does the defer attribute do on a script tag?
  • A. Prevents the script from running
  • B. Runs the script immediately, before the page loads
  • C. Downloads the script in the background and runs it after the page is fully loaded
  • D. Makes the script run twice
Answer: C
C is correct. The defer attribute tells the browser to download the script file while continuing to build the page, then execute the script only after the entire HTML has been parsed. This prevents the script from blocking page rendering.
MCQ 7
Which console method displays data in a table format?
  • A. console.log()
  • B. console.table()
  • C. console.grid()
  • D. console.display()
Answer: B
B is correct. console.table() displays arrays and objects in a formatted table in the console. console.grid() and console.display() do not exist.
MCQ 8
What happens when document.write() is called after the page has finished loading?
  • A. It appends text to the end of the page
  • B. It does nothing
  • C. It erases the entire page and replaces it with the new content
  • D. It shows an error
Answer: C
C is correct. When called after the page has loaded (e.g., from a button click), document.write() overwrites the entire page. This destructive behavior is why it should be avoided in modern JavaScript.
MCQ 9
What is the correct way to link an external JavaScript file?
  • A. <script href='app.js'></script>
  • B. <script src='app.js'></script>
  • C. <script file='app.js'></script>
  • D. <link rel='script' href='app.js'>
Answer: B
B is correct. The src attribute on the <script> tag specifies the external JavaScript file. href is used for links and stylesheets, not scripts. There is no file attribute for script tags.
MCQ 10
What is the output of: console.log(parseInt('42abc'))?
  • A. NaN
  • B. 42abc
  • C. 42
  • D. Error
Answer: C
C is correct. parseInt() reads digits from the beginning of the string and stops when it hits a non-digit character. It successfully parses 42 from '42abc'. If the string started with non-digits (like 'abc42'), the result would be NaN.
MCQ 11
What does confirm() return?
  • A. The text the user typed
  • B. 'OK' or 'Cancel' as strings
  • C. true or false
  • D. 1 or 0
Answer: C
C is correct. confirm() returns true when the user clicks OK and false when they click Cancel. It does not return strings or numbers.
MCQ 12
What is the output of: console.log(1); setTimeout(() => console.log(2), 0); console.log(3);
  • A. 1, 2, 3
  • B. 1, 3, 2
  • C. 2, 1, 3
  • D. 3, 2, 1
Answer: B
B is correct. Synchronous code runs first: 1 then 3. The setTimeout callback is placed in the event queue and only runs after all synchronous code completes, even with a 0ms delay. So 2 prints last.
MCQ 13
Why is the