Multiple Choice Question for Grand JS Quiz

(Chap # 1 -Alert)

Answer: b) Displays a pop-up message box

Explanation: The alert() function displays a pop-up message box with text.

(1) What does the alert() function do in JavaScript?

a) Logs a message to the console
b) Displays a pop-up message box
c) Stores data in local storage
d) Prompts the user for input


Answer: c) alert("Welcome!")

Explanation: alert("message") is the correct syntax for showing a message in an alert box.

(2) Which of the following is the correct syntax to show an alert with the message "Welcome!"?

a) alertBox("Welcome!");
b) popup("Welcome!");
c) alert("Welcome!");
d) msg("Welcome!");


Answer: b) It shows 55

Explanation: The number 5 is converted to a string, so "5" + 5 becomes "55".

(3) What happens when you use alert("5" + 5);?

a) It shows 10
b) It shows 55
c) It shows an error
d) It shows undefined


Answer: c) Hello World!

Explanation: Strings are concatenated with +, including the space " ".

(4) What is the result of this code? alert("Hello" + " " + "World!");

a) An error
b) HelloWorld!
c) Hello World!
d) Hello + World!


Answer: b) It returns a value

Explanation: alert() doesn’t return a value; it just shows a message.

(5) Which of these is not true about alert()?

a) It stops code execution until dismissed
b) It returns a value
c) It shows a message to the user
d) It’s commonly used for debugging


(Chap # 2 - Variables for Strings)


Answer: c) var name = "Ali";

Explanation: In JavaScript, strings are enclosed in quotes and declared using var, let, or const.

(1) How do you declare a string variable in JavaScript?

a) string name = "Ali";
b) var name = Ali;
c) var name = "Ali";
d) var name := 'Ali';


Answer: c) Hello!

Explanation: The variable message contains the string "Hello!", which is shown in the alert.

(2) What will the following code output in the alert box?

var message = "Hello!"; alert(message);


a) undefined
b) message
c) Hello!
d) null


Answer: b) var msg = 'Hello!';

Explanation: Strings must be enclosed in quotes. Both single and double quotes work.

(3) Which of the following is a valid string assignment in JavaScript?

a) var msg = Hello!;
b) var msg = 'Hello!';
c) var msg = Hello;
d) var msg = @Hello;


Answer: c) Hi Sara

Explanation: The + operator concatenates strings. "Hi" + " " + "Sara" becomes "Hi Sara".

(4) What is the output of this code?

var greeting = "Hi"; var name = "Sara"; alert(greeting + " " + name);


a) HiSara
b) greeting name
c) Hi Sara
d) Hi + Sara


Answer: b) Declares a string variable

Explanation: This line declares a variable msg and assigns it a string value.

(5) What does this line of code do?

var msg = "Welcome to JavaScript!";


a) Displays a message
b) Declares a string variable
c) Adds two strings
d) Prompts the user


Answer: b) var 1name = "Ali";

Explanation: Variable names cannot start with a number.

(6) Which of the following is NOT a valid way to name a string variable?

a) var firstName = "Ali";
b) var 1name = "Ali";
c) var name_1 = "Ali";
d) var nameOne = "Ali";


Answer: b) It shows undefined

Explanation: Due to hoisting, city exists but has undefined until it is assigned a value.

(7) What happens if you use a variable before declaring it like this?

alert(city); var city = "Lahore";


a) It shows "Lahore"
b) It shows undefined
c) It shows an error
d) It shows "city"


Answer: b) 205

Explanation: Both are strings, so the + operator concatenates them into "205".

(8) What will this output?

var x = "20"; var y = "5"; alert(x + y);


a) 25
b) 205
c) x + y
d) Error


Answer: b) Text data enclosed in quotes

Explanation: A string is any sequence of characters enclosed in single or double quotes.

(9) Which statement best describes a string in JavaScript?

a) A number enclosed in quotes
b) Text data enclosed in quotes
c) Any variable used in HTML
d) A mathematical operator


Answer: a) My favorite animal is a cat.

Explanation: The string "My favorite animal is a " + animal + "." becomes "My favorite animal is a cat."

(10) Choose the correct output of this code:

var animal = "cat"; alert("My favorite animal is a " + animal + ".");


a) My favorite animal is a cat.
b) My favorite animal is a animal.
c) animal
d) cat


(Chap # 3 - Variables for Numbers)


Answer: b) var num = 5;

Explanation: Numbers should be assigned without quotes in JavaScript.

(1) What is the correct way to declare a number variable in JavaScript?

a) var num = "5";
b) var num = 5;
c) var num = '5';
d) number num = 5;


Answer: b) 30

Explanation: 10 + 20 = 30 since both are numbers.

(2) What will the following code output?

var a = 10; var b = 20; alert(a + b);


a) 1020
b) 30
c) a + b
d) undefined


Answer: b) 55

Explanation: Number 5 is converted to a string, so it becomes "5" + "5" = "55".

(3) What happens if you add a number and a string like this?

var x = 5; var y = "5"; alert(x + y);


a) 10
b) 55
c) NaN
d) Error


Answer: c) They can be decimal numbers

Explanation: JavaScript number variables can be integers or floating-point numbers.

(4) Which statement is true about number variables?

a) They must be whole numbers only
b) They cannot be negative
c) They can be decimal numbers
d) They must be enclosed in quotes


Answer: a) 20

Explanation: The multiplication 10 * 2 results in 20.

(5) What is the output of the following code?

var result = 10 * 2; alert(result);


a) 20
b) 102
c) *
d) Error


Answer: a) 108

Explanation: Both variables hold numbers, so 100 + 8 = 108.

(6) What will be shown by this code?

var price = 100; var tax = 8; alert(price + tax);


a) 108
b) 1008
c) price + tax
d) Error


Answer: b) 5 - 3

Explanation: Subtraction in JavaScript is done with the - operator, like in 5 - 3.

(7) How do you subtract numbers in JavaScript?

a) subtract(5, 3);
b) 5 - 3
c) 5 minus 3
d) 5 subtract 3


Answer: b) 15

Explanation: x is reassigned to 10 + 5, which is 15.

(8) What will be the value of x after this code?

var x = 10; x = x + 5;


a) 10
b) 15
c) 5
d) x + 5


Answer: a) 1.5

Explanation: 3 / 2 results in 1.5 (JavaScript supports decimals).

(9) What will the following code output?

var a = 3; var b = 2; alert(a / b);


a) 1.5
b) 1
c) 0
d) Error


Answer: c) "20"

Explanation: "20" is a string, not a number—even though it looks like one.

(10) Which of the following is not a number in JavaScript?

a) 5.5
b) -10
c) "20"
d) 0


(Chap # 4 - Legal and Illegal variables)


Answer: c) _userName

Explanation: Legal variable names can start with letters, $, or _. 1stName starts with a number, user-name contains a hyphen, and var is a reserved keyword.

(1) Which of the following variable names is legal in JavaScript?

a) 1stName
b) user-name
c) _userName
d) var


Answer: c) It causes a syntax error

Explanation: Spaces are not allowed in variable names.

(2) What will happen with this code?

var my name = "Ali";


a) It runs successfully
b) It shows "my name"
c) It causes a syntax error
d) It assigns a string to the variable


Answer: c) let

Explanation: let is a reserved keyword in JavaScript and cannot be used as a variable name.

(3) Which of these is not a valid variable name?

a) first_name
b) $user2
c) let
d) userName3


Answer: c) It includes a hyphen

Explanation: Hyphens - are interpreted as the minus operator, making user-name invalid.

(4) Why is user-name an illegal variable name?

a) It is a reserved word
b) It starts with a symbol
c) It includes a hyphen
d) It is too short


Answer: d) camelCase

Explanation: JavaScript typically uses camelCase for variable names, e.g., userName.

(5) Which naming convention is most commonly used in JavaScript variable names?

a) PascalCase
b) snake_case
c) kebab-case
d) camelCase


Answer: c) fullName

Explanation: camelCase starts with a lowercase letter and capitalizes the first letter of each subsequent word.

(6) Which of the following variable names follows the camelCase convention?

a) FullName
b) full_name
c) fullName
d) Fullname


Answer: c) It throws a syntax error

Explanation: Reserved keywords like function cannot be used as variable names; it causes a syntax error.

(7) What happens if you try to use a reserved keyword as a variable name?

Example: var function = 10;


a) It runs fine
b) It ignores the keyword
c) It throws a syntax error
d) It treats it as a string


Answer: b) _

Explanation: A variable name can start with a letter, $, or _.

(8) Which of the following can be used at the beginning of a JavaScript variable name?

a) @
b) _
c) #
d) -


Answer: c) userAge

Explanation: userAge clearly indicates the purpose of the variable and follows naming conventions.

(9) Which variable name is most appropriate and readable for storing a user’s age?

a) a
b) x
c) userAge
d) data123


Answer: c) No official limit, but practical limits apply

Explanation: JavaScript doesn't have an official limit, but long variable names are discouraged for readability.

(10) What is the maximum length a JavaScript variable name can be?

a) 32 characters
b) 64 characters
c) No official limit, but practical limits apply
d) 255 characters


(Chap # 5 - Math Expressions – Familiar Operators)


Answer: c) Adds the numbers

Explanation: The + operator performs addition when both operands are numbers.

(1) What does the + operator do when used between two numbers in JavaScript?

a) Divides the numbers
b) Concatenates the numbers
c) Adds the numbers
d) Subtracts the numbers


Answer: a) 6

Explanation: 10 - 4 evaluates to 6.

(2) What is the result of the following expression?

var result = 10 - 4;


a) 6
b) 14
c) -6
d) undefined


Answer: b) *

Explanation: JavaScript uses the asterisk * for multiplication.

(3) Which operator is used for multiplication in JavaScript?

a) x
b) *
c) ×
d) multiply


Answer: a) 5

Explanation: 10 / 2 equals 5.

(4) What is the output of this code?

var a = 10;
var b = 2;
alert(a / b);


a) 5
b) 20
c) 0.2
d) 12


Answer: a) %

Explanation: The % operator returns the remainder after division.

(5) Which of the following is the modulus (remainder) operator in JavaScript?

a) %
b) &
c) #
d) @


Answer: a) 10

Explanation: x is reassigned as 7 + 3, which equals 10.

(6) What is the value of x after this code?

var x = 7;
x = x + 3;


a) 10
b) 73
c) x + 3
d) Error


Answer: b) 150

Explanation: 200 - 50 equals 150.

(7) What will be shown in the alert box?

var price = 200;
var discount = 50;
alert(price - discount);


a) 250
b) 150
c) 100
d) Error


Answer: c) Multiplies 3 by 4

Explanation: The * operator performs multiplication.

(8) What does this line of code do?

var result = 3 * 4;


a) Adds 3 and 4
b) Divides 3 by 4
c) Multiplies 3 by 4
d) Finds the remainder of 3/4


Answer: d) All of the above

Explanation:
10 - 8 = 2
2 + 0 = 2
10 % 4 = 2 (since 4 * 2 = 8, remainder = 2)

(9) Which expression will give you 2 as the result?

a) 10 - 8
b) 2 + 0
c) 10 % 4
d) All of the above


Answer: a) 7

Explanation: x is updated to 12 - 5 = 7.

(10) What will be the result of this code?

var x = 12;
x = x - 5;
alert(x);


a) 7
b) 17
c) -5
d) Error


(Chap # 6 - Math Expressions – Unfamiliar Operators)


Answer: b) Increases the variable’s value by 1

Explanation: The ++ operator is a shorthand for incrementing a variable by 1.

(1) What does the ++ operator do in JavaScript?

a) Adds 2 to a variable
b) Increases the variable’s value by 1
c) Appends a number
d) Multiplies the variable by 2


Answer: b) 6

Explanation: x++ increases the value of x by 1 after its current value is used.

(2) What is the output of this code?

var x = 5;
x++;
alert(x);


a) 5
b) 6
c) x++
d) Error


Answer: c) Decreases the value by 1

Explanation: The -- operator decrements the variable’s value by 1.

(3) What does -- do in JavaScript?

a) Divides the value by 2
b) Sets the value to 0
c) Decreases the value by 1
d) Deletes the variable


Answer: b) 11

Explanation: ++a is a pre-increment. It adds 1 before displaying, so 10 becomes 11.

(4) What is the output of this code?

var a = 10;
alert(++a);


a) 10
b) 11
c) ++a
d) undefined


Answer: a) 7

Explanation: b-- is a post-decrement, so it shows 7 and then changes b to 6.

(5) What will be shown in the alert box?

var b = 7;
alert(b--);


a) 7
b) 6
c) 8
d) Error


Answer: b) 6

Explanation: x is incremented twice: 4 → 5 → 6.

(6) What is the final value of x?

var x = 4;
x++;
x++;


a) 5
b) 6
c) 4
d) 8


Answer: b) 7

Explanation: Pre-decrement --y subtracts 1 before alerting: 8 → 7.

(7) What will be the output of this code?

var y = 8;
alert(--y);


a) 8
b) 7
c) 9
d) undefined


Answer: b) x += 1

Explanation: x += 1 is a shorthand for x = x + 1.

(8) Which statement is equivalent to x = x + 1?

a) x =+ 1
b) x += 1
c) x = ++1
d) x == 1


Answer: a) x++ increments after use, ++x increments before use

Explanation: Post-increment (x++) increases value after it's used; pre-increment (++x) increases it before use.

(9) What is the difference between x++ and ++x?

a) x++ increments after use, ++x increments before use
b) x++ doubles the value, ++x adds 1
c) x++ is invalid syntax
d) No difference at all


Answer: a) 5

Explanation: a++ assigns 5 to b first, then increments a to 6.

(10) What will be the final result?

var a = 5;
var b = a++;
alert(b);


a) 5
b) 6
c) a
d) Error


(Chap # 7 - Math Expressions – Eliminating Ambiguity)


Answer: b) It determines the order in which operations are evaluated

Explanation: Operator precedence controls which operations are performed first when multiple operators are used.

(1) Why is operator precedence important in JavaScript?

a) It makes code look cleaner
b) It determines the order in which operations are evaluated
c) It removes the need for semicolons
d) It allows string concatenation


Answer: d) 25

Explanation: Multiplication has higher precedence than addition, so 5 * 2 = 10, then 10 + 15 = 25.

(2) What is the output of this expression?

var result = 10 + 5 * 2;


a) 30
b) 20
c) 10
d) 25


Answer: c) Using parentheses ()

Explanation: Parentheses change the default order of operations, forcing inner operations to be evaluated first.

(3) How can you change the order of operations in an expression?

a) Using semicolons
b) Using square brackets []
c) Using parentheses ()
d) Using quotation marks


Answer: a) 30

Explanation: Parentheses force 10 + 5 = 15, then 15 * 2 = 30.

(4) What is the output of this expression?

var result = (10 + 5) * 2;


a) 30
b) 25
c) 20
d) 15


Answer: c) Parentheses ()

Explanation: Parentheses always have the highest precedence and are evaluated first.

(5) Which has the highest precedence in JavaScript?

a) Addition +
b) Subtraction -
c) Parentheses ()
d) Multiplication *


Answer: b) 13

Explanation: Multiplication first: 2 * 3 = 6; then 8 + 6 = 14; finally 14 - 1 = 13.

(6) What is the result of this expression?

var result = 8 + 2 * 3 - 1;


a) 29
b) 13
c) 15
d) 9


Answer: b) 4 * 3

Explanation: Parentheses are evaluated first: 4 * 3 = 12.

(7) Which part of the expression is evaluated first in this code?

var result = 5 + (4 * 3) - 2;


a) 5 + 4
b) 4 * 3
c) 5 - 2
d) Entire expression at once


Answer: a) Left to right

Explanation: Operators of equal precedence (- and +) are evaluated left to right: 20 - 3 = 17, then 17 + 2 = 19.

(8) In what order is this expression evaluated?

var x = 20 - 3 + 2;


a) Left to right
b) Right to left
c) Parentheses first
d) No specific order


Answer: c) Add parentheses

Explanation: Parentheses help remove ambiguity: e.g., base + (tax / 100 * base) or better structured as needed.

(9) What is the best way to make this expression clearer and less error-prone?

var total = base + tax / 100 * base;


a) Use comments
b) Use parseInt()
c) Add parentheses
d) Use shorter variable names


Answer: a) 15

Explanation: (2 + 3) = 5, (4 - 1) = 3, then 5 * 3 = 15.

(10) What will this output?

var value = (2 + 3) * (4 - 1);


a) 15
b) 10
c) 5
d) 20


(Chap # 8 - Concatenating Text Strings)


Answer: c) Concatenates (joins) the strings

Explanation: In JavaScript, the + operator joins strings when used with string values.

(1) What does the + operator do when used between two strings?

a) Adds numbers
b) Divides the strings
c) Concatenates (joins) the strings
d) Multiplies the strings


Answer: b) Ali Khan

Explanation: It concatenates the first and last name with a space between them.

(2) What will the following code output?

var firstName = "Ali";
var lastName = "Khan";
alert(firstName + " " + lastName);


a) AliKhan
b) Ali Khan
c) "Ali" "Khan"
d) undefined


Answer: b) "Hi" + "There" + "Friend"

Explanation: The + operator joins multiple strings in order.

(3) Which of the following is a correct way to concatenate three strings?

a) "Hi" & "There" & "Friend"
b) "Hi" + "There" + "Friend"
c) concat("Hi", "There", "Friend")
d) "Hi".join("There").join("Friend")


Answer: c) Total: 105

Explanation: String + number = string. So "Total: " + 10 becomes "Total: 10", then "Total: 10" + 5 = "Total: 105".

(4) What will be the result of this code?

var msg = "Total: " + 10 + 5;
alert(msg);


a) 15
b) Total: 15
c) Total: 105
d) Error


Answer: b) Add numbers inside parentheses

Explanation: Parentheses change the evaluation order. Example: "Total: " + (10 + 5) → "Total: 15"

(5) How can you force JavaScript to add numbers before concatenating with a string?

a) Use Number()
b) Add numbers inside parentheses
c) Use a comma
d) Use semicolon


Answer: a) HelloJohn

Explanation: The strings "Hello" and "John" are concatenated without a space between them.

(6) What will the following code display?

var greeting = "Hello";
var name = "John";
alert(greeting + name);


a) HelloJohn
b) Hello John
c) Hello + John
d) undefined


Answer: b) "Hello" + " " + "World"

Explanation: You need to explicitly add a space between the strings using " ".

(7) How would you add a space between "Hello" and "World" when concatenating them?

a) "Hello" + "World"
b) "Hello" + " " + "World"
c) "Hello" - "World"
d) "Hello".add("World")


Answer: a) The sum is: 510

Explanation: The + operator concatenates the string with num1, then num2, forming "The sum is: 510".

(8) What will this code display?

var num1 = 5;
var num2 = 10;
var message = "The sum is: " + num1 + num2;
alert(message);


a) The sum is: 510
b) The sum is: 15
c) The sum is: 5 10
d) Error


Answer: a) concat()

Explanation: The concat() method joins two or more strings in JavaScript.

(9) Which method is used to concatenate two strings in JavaScript?

a) concat()
b) add()
c) merge()
d) combine()


Answer: c) 321

Explanation: "3" + 2 = "32", then "32" + 1 = "321".

(10) What does this expression return?

var result = "3" + 2 + 1;
alert(result);


a) 6
b) 32
c) 321
d) 3 2 1


Answer: a) Good Morning

Explanation: The code concatenates "Good" and "Morning" with a space between.

(11) What will this code output?

var a = "Good";
var b = "Morning";
alert(a + " " + b);


a) Good Morning
b) GoodMorning
c) MorningGood
d) Error


Answer: b) 2030

Explanation: "20" + "30" results in the string "2030".

(12) What will this expression evaluate to?

var x = "20";
var y = "30";
alert(x + y);


a) 50
b) 2030
c) Error
d) 20 30


Answer: a) Hello52

Explanation: "Hello" + 5 = "Hello5", then "Hello5" + 2 = "Hello52".

(13) What is the result of the following code?

var str = "Hello" + 5 + 2;
alert(str);


a) Hello52
b) Hello7
c) Hello
d) Error


Answer: a) John Doe is 30 years old.

Explanation: This concatenates all parts to form the sentence.

(14) What will this code display?

var firstName = "John";
var lastName = "Doe";
var age = 30;
alert(firstName + " " + lastName + " is " + age + " years old.");


a) John Doe is 30 years old.
b) JohnDoe is30 years old.
c) John 30 years old.
d) Error


Answer: c) The array elements are joined together as a string

Explanation: Example: "Hello" + [1, 2, 3] results in "Hello123".

(15) What happens when you concatenate a string and an array in JavaScript?

a) The string is added to the beginning of the array
b) The array is added to the end of the string
c) The array elements are joined together as a string
d) An error occurs


(Chap # 9 - Prompts)


(1) What is the purpose of the prompt() method in JavaScript?

A) To display a message in the console
B) To ask the user for input
C) To display an alert box
D) To create a new variable

Answer: B) To ask the user for input

Explanation: The prompt() method is used to display a dialog box asking the user for input.



(2) What will this code do?

var name = prompt("What is your name?");
alert("Hello, " + name);

A) It asks the user to enter their name and then displays a greeting
B) It prints "Hello, "
C) It causes an error
D) It creates a new variable name without asking for input

Answer: A) It asks the user to enter their name and then displays a greeting

Explanation: The prompt() method asks the user for input and stores it in the name variable. Then, the alert() displays a greeting with the entered name.



(3) What type of value does the prompt() method return?

A) String
B) Boolean
C) Number
D) Array

Answer: A) String

Explanation: The prompt() method always returns the input as a string, even if the user enters a number.



(4) What will be displayed if the user enters "John" in the following code?

var age = prompt("How old are you?");
alert("You are " + age + " years old.");

A) You are John years old.
B) You are 25 years old.
C) You are NaN years old.
D) Error

Answer: A) You are John years old.

Explanation: If the user enters "John", it will be treated as a string, and the message will be "You are John years old.".



(5) How can you ensure that a number entered by the user in prompt() is treated as a number, not a string?

A) Use parseInt() or parseFloat()
B) Use Number()
C) Use Math.floor()
D) Both A and B

Answer: D) Both A and B

Explanation: You can use parseInt() or parseFloat() to convert the user input into a number, or use Number() for automatic conversion.



(6) What will this code display if the user enters "25"?

var number = prompt("Enter a number:");
var result = number * 2;
alert(result);

A) 50
B) NaN
C) 25
D) 25*2

Answer: B) NaN

Explanation: The value returned by prompt() is always a string, so multiplying a string ("25") by 2 results in NaN (Not a Number).



(7) How can you convert the string "30" into a number in JavaScript?

A) parseInt("30")
B) parseFloat("30")
C) Number("30")
D) All of the above

Answer: D) All of the above

Explanation: All of the methods (parseInt(), parseFloat(), and Number()) can be used to convert a string to a number.



(8) What will happen if the user presses "Cancel" on the prompt dialog box?

A) It returns null
B) It returns an empty string ""
C) It returns undefined
D) It causes an error

Answer: A) It returns null

Explanation: When the user clicks "Cancel", the prompt() method returns null.



(9) How would you ask the user for their age and ensure that the entered value is a valid number?

A)
var age = prompt("What is your age?");
if (isNaN(age)) {
  alert("Please enter a valid number.");
}
B)
var age = prompt("What is your age?");
if (typeof age === "number") {
  alert("Valid number entered.");
}
C)
var age = prompt("What is your age?");
if (age === "NaN") {
  alert("Please enter a valid number.");
}
D)
var age = prompt("What is your age?");
alert(age);

Answer: A

Explanation: Using isNaN() checks whether the entered value is not a number, ensuring the input is valid.



(10) What will this code do?

var userInput = prompt("Enter a number:");
var squared = userInput ** 2;
alert("Squared value: " + squared);

A) It alerts the squared value of the input
B) It causes an error
C) It squares the input as a string
D) It shows the input without changes

Answer: A) It alerts the squared value of the input

Explanation: If the user enters a number, it will be squared and the result will be displayed. If the input is not a number, it might result in NaN due to the way prompt() returns a string.



(Chap # 10 - if Statements )


(1) What is the purpose of an if statement in JavaScript?

A) To perform a task repeatedly
B) To declare variables
C) To make decisions based on a condition
D) To define functions

Answer: C) To make decisions based on a condition

Explanation: An if statement allows JavaScript to execute a block of code only if a specified condition is true.



(2) Which of the following is the correct syntax for an if statement?

A) if (condition) { statement; }
B) if (condition): statement;
C) if { condition } statement;
D) if condition { statement; }

Answer: A) if (condition) { statement; }

Explanation: The correct syntax for an if statement in JavaScript is if (condition) { statement; }.



(3) What will be the output of the following code?

var x = 10;
if (x > 5) {
alert("x is greater than 5");
}

A) x is greater than 5
B) x is less than or equal to 5
C) Error
D) undefined

Answer: A) x is greater than 5

Explanation: Since the condition x > 5 is true, the code inside the if block is executed, showing the message "x is greater than 5".



(4) What is the result of this condition?

var num = 20;
if (num == 20) {
alert("The number is 20");
}

A) The number is 20
B) Error
C) undefined
D) The number is not 20

Answer: A) The number is 20

Explanation: The condition num == 20 is true, so the code inside the if block is executed, and the message "The number is 20" is shown.



(5) Which comparison operator should be used to check if two values are equal in both value and type?

A) ==
B) !=
C) ===
D) >

Answer: C) ===

Explanation: The === operator checks both value and type equality, making it a stricter comparison than ==, which only compares values.



(6) What will happen if the condition in an if statement evaluates to false?

A) The code inside the if block will execute
B) The code inside the if block will not execute
C) The if statement will throw an error
D) The program will stop

Answer: B) The code inside the if block will not execute

Explanation: If the condition is false, the code inside the if block will not execute.



(7) Which of the following statements is true?

A) An if statement can have multiple conditions combined using logical operators
B) An if statement always requires a else block
C) An if statement can only check for equality
D) if statements are used to repeat code multiple times

Answer: A) An if statement can have multiple conditions combined using logical operators

Explanation: You can combine multiple conditions in an if statement using logical operators like && (AND) and || (OR).



(8) What will be the output of this code?

var age = 16;
if (age >= 18) {
alert("You are an adult.");
} else {
alert("You are a minor.");
}

A) You are an adult.
B) You are a minor.
C) undefined
D) Error

Answer: B) You are a minor.

Explanation: Since age is 16, which is less than 18, the else block is executed, displaying "You are a minor.".



(9) What will be the result of this code?

var temperature = 30;
if (temperature > 20) {
alert("It's hot outside.");
} else if (temperature === 20) {
alert("It's warm outside.");
} else {
alert("It's cold outside.");
}

A) It's hot outside.
B) It's warm outside.
C) It's cold outside.
D) Error

Answer: A) It's hot outside.

Explanation: Since temperature is 30, which is greater than 20, the first condition is true, and "It's hot outside." is displayed.



(10) What will this code output?

var number = -5;
if (number < 0) {
alert("Negative number");
} else if (number > 0) {
alert("Positive number");
} else {
alert("Zero");
}

A) Negative number
B) Positive number
C) Zero
D) undefined

Answer: A) Negative number

Explanation: Since number is -5, the first condition number < 0 is true, and the message "Negative number" is displayed.



(11) What will this code output?

var num = 0;
if (num) {
alert("Truthy value");
} else {
alert("Falsy value");
}

A) Truthy value
B) Falsy value
C) undefined
D) Error

Answer: B) Falsy value

Explanation: In JavaScript, 0 is considered a falsy value, so the else block will execute, showing "Falsy value".



(12) What will happen if you omit the else block in an if statement?

A) The code will throw an error
B) The code will execute without any issues
C) The if statement will always execute
D) The program will stop

Answer: B) The code will execute without any issues

Explanation: An if statement can exist without an else block. If the condition is false, simply nothing happens, and the code continues.



(13) What will the following code display?

var number = -10;
if (number) {
alert("Positive or Non-zero number");
} else {
alert("Zero");
}

A) Positive or Non-zero number
B) Zero
C) undefined
D) Error

Answer: A) Positive or Non-zero number

Explanation: The value -10 is considered a truthy value in JavaScript, so the code will display "Positive or Non-zero number".



(14) Which of the following statements correctly describes the use of else if?

A) else if is used to check multiple conditions sequentially
B) else if is used only once in the program
C) else if does not allow checking multiple conditions
D) else if can only be used inside loops

Answer: A) else if is used to check multiple conditions sequentially

Explanation: The else if statement allows for checking additional conditions after the initial if, enabling multiple conditions to be tested sequentially.



(15) What will the following code output?

var num1 = 20;
var num2 = 30;
if (num1 < num2) {
alert("num1 is less than num2");
} else {
alert("num1 is greater than num2");
}

A) num1 is less than num2
B) num1 is greater than num2
C) undefined
D) Error

Answer: A) num1 is less than num2

Explanation: Since num1 (20) is less than num2 (30), the condition num1 < num2 is true, and the message "num1 is less than num2" is displayed.




Prepared by "Ismail Shah"