JS SA Solution
JS SA Solution
What will be the output on console when the below JavaScript code is run?
function updateMyNumber() {
var myNumber = 222;
myNumber = myNumber + 111;
}
updateMyNumber();
console.log(myNumber);
a) 222
b) 111
c) 333
d) Compilation Error
Correct Answer:
Option (b) 111
In JavaScript, variables declared using var inside a function are function-scoped, meaning they
are local to that function. They do not affect the variable with the same name outside the
function.
Step-by-Step Analysis:
Question 2:
What will be the output in the console for the following code?
var i = 999;
function myFunc() {
for (var i = 1; i <= 10; i++) {}
console.log(i);
}
myFunc();
a) Error
b) 10
c) 11
d) 999
Correct Answer:
Option (c) 11
Step-by-Step Breakdown:
1. var i = 999;
→ Declares a global variable i and assigns it 999.
2. function myFunc() { ... }
o Inside this function, var i = 1; in the for loop shadows the global i.
o The loop executes as:
▪ i = 1 to i = 10
▪ Then i becomes 11, loop condition i <= 10 fails, and loop exits
o console.log(i); prints the local i, which is now 11.
3. Global i = 999 remains unchanged.
• var is function-scoped, so the i inside the function is not the same as the global i.
• After a for loop with var, the loop variable retains its final value.
• let would behave differently (block-scoped), but this uses var.
Question 3:
Which of the following regular expressions will correctly match Indian PAN and fail if
provided input is not valid?
(Hint: Valid PAN formats include ABCDE1234F, VWXYZ6789Q)
a) /^[A-Z]{5}[\d]{4}[A-Z]$/
b) /^[A-Z]{5}[\d]{4}[A-Z]?$/
c) /^[\w]{5}[\d]{4}[A-Z]{1}$/
d) /^[A-Z]{5}[0-9]{4}[A-Z]{1}$/
Correct Answer:
Option (a) /^[A-Z]{5}[\d]{4}[A-Z]$/
Also valid: Option (d) /^[A-Z]{5}[0-9]{4}[A-Z]{1}$/
An Indian PAN card number is a 10-character alphanumeric string with this format:
• First 5 characters: uppercase alphabets (A-Z)
• Next 4 characters: digits (0-9)
• Last character: an uppercase alphabet (A-Z)
Examples:
• ABCDE1234F
• VWXYZ6789Q
Option-by-Option Analysis:
Final Answer:
Both Option (a) /^[A-Z]{5}[\d]{4}[A-Z]$/ and Option (d) /^[A-Z]{5}[0-9]{4}[A-
Z]{1}$/ are correct.
Question 4:
Which of the following REGEX will properly validate an Indian Mobile Number (10
digits, should start with 6/7/8/9)?
a) /^[6789]\d{9}$/
b) /^\d{10}$/
c) /^[^6-9]\d{9}$/
d) /^[6-9][0-9]{9}$/
Correct Answer:
Both Option (a) /^[6789]\d{9}$/ and Option (d) /^[6-9][0-9]{9}$/ are correct
If only one must be selected, prefer:
Option (d) – cleaner and more semantically accurate.
Option Analysis:
Both (a) and (d) are correct, though (d) is more readable and explicitly matches the
requirement.
Key Notes:
Question 5:
What will be the output of the following JavaScript snippet?
Options:
a) true
b) s3
c) The best things3 are free in life
d) things3
Correct Answer:
Option (d) things3
/[a-z]+[0-9]/
Code Execution:
Key Points:
Question 6:
What will be the console output for the code given below?
function good(greet) {
console.log("Good");
return greet;
}
function call(greet) {
console.log("Hi");
good(greet)();
}
function greet() {
console.log("Morning");
}
call(greet);
Options:
a) Hi Good
b) Hi Good Morning
c) Hi Morning Good
d) Hi Morning
Correct Answer:
Option (b) Hi Good Morning
Step-by-Step Execution:
1. call(greet); is executed.
o Enters the call function.
o Prints "Hi" → Console: Hi
o Calls good(greet).
2. Inside good(greet):
o Prints "Good" → Console: Hi Good
o Returns the greet function (i.e., returns a reference to the function greet)
o So: good(greet)() → now becomes greet() (a function call)
3. greet():
o Prints "Morning" → Console: Hi Good Morning
Hi
Good
Morning
Question 7:
What will be printed on the console when the below JavaScript code is run?
Options:
a) Compilation Error
b) Hello
c) World
d) Hello World!
Correct Answer:
Option (c) World
Code Breakdown:
Then:
Console Output:
Hello
World!
Question 8:
What will be the output of the following JavaScript code snippet?
function emailFormat(email){
try {
if(email.match(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/)) {
console.log("Matching the email format!!");
return true;
} else {
throw new Error("Email invalid!!");
}
} catch(error) {
console.log(error.message);
} finally {
console.log("Into the finally block!!");
}
}
console.log(emailFormat("hello.123@example.com"));
Options:
Correct Answer:
Option (c) Matching the email format!! Into the finally block!! true
1. email.match(...)
hello.123@example.com matches the regex → Valid email
2. Inside try block:
o Logs: "Matching the email format!!"
o Executes return true
3. Before returning, finally block executes:
o Logs: "Into the finally block!!"
4. The function returns true, which is passed to:
console.log(emailFormat("hello.123@example.com"));
Logs: true
Question 9:
Which of the following test case will fail?
main.js file:
// Test Case 2
it("Test Case 2", function(){
expect(modulus(15, 8)).toBeGreaterThan(4);
});
// Test Case 3
it("Test Case 3", function(){
expect(modulus(15, 15)).toBeTruthy();
});
// Test Case 4
it("Test Case 4", function(){
expect(modulus(15, 15)).toBeDefined();
});
});
Test Case 1:
modulus(15, 7) → 15 % 7 = 1
expect(1).toEqual(1) → Pass
Test Case 2:
modulus(15, 8) → 15 % 8 = 7
expect(7).toBeGreaterThan(4) → Pass
Test Case 3:
modulus(15, 15) → 15 % 15 = 0
expect(0).toBeTruthy() → **Fails**
Question 10:
Which of the following regular expressions will be valid for all of the following list of
words?
Jack, John, James, Jasper, Judie
Options:
a) /[J|j][a-z]{3,5}/
b) /[a-z]+/
c) /[j|J][a-z]{4,6}/
d) /j[A-z]{4,6}/
Correct Answer:
Words:
• Jack → 4 letters
• John → 4 letters
• James → 5 letters
• Jasper → 6 letters
• Judie → 5 letters
• Match J or j
• Followed by 4 to 6 lowercase letters
Option-by-Option Analysis:
⚠ A-z is not safe — use [A-Za-z] instead when matching only letters.
Final Answer:
Question 11:
What will be the output in the console for the following JavaScript code?
function man() {
power = 10;
console.log("Man's power is:", power);
}
function bat(spider) {
power = 30;
try {
spider();
} catch(e) {
power = 20;
}
console.log("Bat's power is:", power);
}
function spider() {
bat(man);
}
spider();
Options:
Correct Answer:
1. Global:
o power = 666 initially.
2. Call spider()
o Inside spider(), it calls bat(man).
3. Inside bat(man):
o First line: power = 30;
➔ Now power = 30
4. Inside bat(man), spider() is actually man (passed as argument):
o man() is called inside the try block.
5. Inside man():
o Sets power = 10;
o Logs: "Man's power is: 10"
6. Back to bat() after spider() (which is man()) executes:
o No exception was thrown → so catch block does NOT execute.
o Now, console.log("Bat's power is:", power);
o power is now 10 (set by man() earlier).
7. Final console output:
Key Observations:
• bat(man) calls man() normally → no error → catch is skipped.
• power was modified globally at each step.
• No nested or hidden errors.
Question 12:
Which of the following statements are true about arrow functions?
Correct Answers:
Option (b) Curly Brackets {} are mandatory for multiple lines of code.
Option (c) Parentheses () are optional for single parameter.
Arrow functions (=>) are a concise syntax for writing function expressions.
Option-by-Option Analysis:
Key Points:
Final Answer:
Options (b) and (c) are true.
Curly Brackets {} are mandatory for multiple lines, and Parentheses () are optional for
a single parameter.
Question 13:
Which of the following Jasmine matchers should be used so that the test case passes?
main.js
mainSpec.js
Options:
a) toBeTruthy()
b) toBeFalsy()
c) toBeEqualTo()
d) toEqual()
Correct Answer:
Matcher Description
toEqual() Checks deep equality for primitive values and objects (like ===)
toBe() Checks strict equality (===) — also works here, but not an option
toBeTruthy() Passes if the result is truthy — does not check exact value
toBeFalsy() Passes if the result is falsy — clearly wrong in this case
toBeEqualTo() Invalid — not a real Jasmine matcher
expect(helloworld()).toEqual('Hello World');
Question 14:
What will be the output in the console for the following code?
let i = 666;
function myFunc() {
for (let i = 1; i < 10; i++) {
}
console.log(i);
}
myFunc();
Options:
a) Error
b) 666
c) 10
d) 11
Correct Answer:
Step-by-Step Execution:
1. let i = 666;
→ Global variable i = 666.
2. function myFunc() defines a function that:
Key Point:
• let inside a loop does not leak outside the loop block.
• Trying to use that variable outside will cause an error.
Question 15:
What will be the output in the console for the following code?
function man() {
try {
throw new Error();
power = 100;
} catch(e) {
power = 200;
} finally {
power = 300;
}
}
bat(man, 100);
Correct Answer:
Step-by-Step Execution:
1. Global Context:
• No var, let, or const declared → All power = ... statements affect a global variable
called power.
At this point:
• power = 100
The final output is 400, due to the finally block in the bat() function overriding all earlier
changes.
Question 16:
What will be the output in the console for the following code?
function man() {
try {
throw new Error();
power = 100;
} catch(e) {
power = 200;
} finally {
power = 300;
}
}
bat(man, 100);
Correct Answer:
Step-by-Step Explanation:
• No variable power is declared with let, const, or var in the global scope.
• So all power = ... assignments create/modify a global variable named power.
• So after man():
o Global power = 300
• This declares a new block-scoped power inside finally, which is local and doesn't affect
the global power.
• Hence, global power remains 300.
🔹 Final Step:
console.log(power); // prints global `power`, which is 300
Key Takeaways:
• let power = 400; inside finally shadows the global power only within that block.
• The global power remains unchanged by this let declaration.
• man() modifies global power to 300.
Question 17:
What will be the output in the console for the following JavaScript code?
javascript
CopyEdit
let power = 999;
function elephant(spider) {
let power = 50;
try {
spider();
} catch (e) {
power = 70;
}
console.log("Elephant's power is:", power);
}
function spider() {
elephant(tiger);
}
function tiger() {
power = 30;
console.log("Tiger's power is:", power);
}
spider();
Correct Answer:
Step-by-Step Execution:
2. Call spider()
3. Inside elephant(tiger)
javascript
CopyEdit
let power = 50;
try {
spider(); // here 'spider' is 'tiger'
} catch (e) {
power = 70;
}
console.log("Elephant's power is:", power);
4. Inside tiger()
javascript
CopyEdit
power = 30;
console.log("Tiger's power is:", power);
• Which power?
o No let, const, or var is declared inside tiger().
o So it modifies the global power (from 999 to 30).
Prints:
• No error is thrown inside tiger(), so the catch block inside elephant() does not
execute.
Final Output:
Question 18:
What will be printed in the console on successful execution of the below code
snippet?
function test() {
console.log(a);
console.log(foo());
var a = 1;
}
function foo() {
return 2;
}
test();
Options:
Correct Answer:
Step-by-Step Execution:
• var declarations are hoisted (moved to the top of their scope), but their initialization
remains in place.
• Function declarations like foo() are fully hoisted with their definitions.
function test() {
var a; // hoisted and initialized as undefined
console.log(a); // prints undefined
console.log(foo()); // prints 2
a = 1; // assignment happens here
}
2. Step-by-Step:
• console.log(a);
o a is declared (var a) but not assigned yet → prints undefined
• console.log(foo());
o foo() is hoisted and fully available → returns 2, so it prints 2
Summary:
Line Output
console.log(a); undefined
console.log(foo()); 2
Final Console Output:
undefined
2
Question 19:
What will be the output in the console for the following JavaScript code?
function man() {
power = 10;
console.log("Man's power is:", power);
}
function bat(spider) {
var power = 30;
try {
spider();
} catch(e) {
power = 20;
}
console.log("Bat's power is:", power);
}
function spider() {
bat(man);
}
spider();
Correct Answer:
Step-by-Step Execution:
• spider() is called.
2. Inside spider(), it calls bat(man).
3. Inside bat(man):
var power = 30;
try {
spider(); // here 'spider' is 'man'
} catch(e) {
power = 20;
}
console.log("Bat's power is:", power);
4. Inside man():
power = 10;
console.log("Man's power is:", power);
• Important:
o power = 10; — No var, let, or const is declared inside man().
o So global power (not the local power inside bat) is created/modified to 10.
• Then prints:
5. Back to bat(man):
Question 20:
What will be the output of the below JavaScript code?
Options:
a) [1, 4, 9, 16]
b) []
c) [1, 2, 3, 4]
d) Compilation Error
Correct Answer:
Step-by-Step Explanation:
1. Code Behavior
• Then console.log(counts);
o Prints the original counts array: [1, 2, 3, 4]
Important Concept:
But in the given code, no reassignment happens, so counts stays [1, 2, 3, 4].
[1, 2, 3, 4]