πŸ“Š JavaScript
Q. β€œAn expression that can legally appear on the left side of an assignment expression.” is a well known explanation for variables, properties of objects, and elements of arrays. They are called
  • (A) Definition
  • (B) Properties
  • (C) Prototypes
  • (D) Lvalue
πŸ’¬ Discuss
βœ… Correct Answer: (D) Lvalue

Explanation:

An "Lvalue" (short for "left-hand value") is an expression that refers to a location in memory that can store a value. It's an expression that can legally appear on the left side of an assignment expression, meaning it can hold or receive a value. For example, variables, properties of objects, and array elements are Lvalues because you can assign values to them.

πŸ“Š JavaScript
Q. Among the following, which one is a ternary operator?
  • (A) ?:
  • (B) -
  • (C) :
  • (D) +
πŸ’¬ Discuss
βœ… Correct Answer: (A) ?:

Explanation:

The ?: operator is known as the ternary operator, and it is a shorthand for an if-else statement. It takes three operands:

condition ? expression_if_true : expression_if_false;

For example:

let result = (x > 10) ? "Greater" : "Smaller";

This means that if x > 10 is true, result will be "Greater", otherwise, it will be "Smaller".

πŸ“Š JavaScript
Q. Which of the operator is used to test if a particular property exists or not?
  • (A) exist
  • (B) exists
  • (C) in
  • (D) within
πŸ’¬ Discuss
βœ… Correct Answer: (C) in

Explanation:

The in operator is used to test if a property exists in an object or in an array. For example:

let obj = { name: "John", age: 30 };

console.log("name" in obj); // true
console.log("address" in obj); // false

This checks if the property (e.g., "name") is present in the object (obj).

πŸ“Š JavaScript
Q. What kind of an expression is β€œnew Point(2,3)”?
  • (A) Invocation Expression
  • (B) Constructor Calling Expression
  • (C) Primary Expression
  • (D) Object Creation Expression
πŸ’¬ Discuss
βœ… Correct Answer: (D) Object Creation Expression

Explanation:

The expression new Point(2, 3) is an example of an object creation expression. It uses the new keyword to create an instance of the Point class (or constructor function), initializing it with the arguments 2 and 3. This is typically used to instantiate new objects in object-oriented programming.

πŸ“Š JavaScript
Q. The expression of calling ( or executing ) a function or method in JavaScript is called
  • (A) Property Access Expression
  • (B) Functional expression
  • (C) Primary expression
  • (D) Invocation expression
πŸ’¬ Discuss
βœ… Correct Answer: (D) Invocation expression

Explanation:

In JavaScript, invocation expressions are used to call or execute a function or method. When you write an expression like myFunction(), you're invoking or executing the function named myFunction.

Let's break down the concept:

Expression: In programming, an expression is any valid combination of variables, constants, operators, and functions that can be evaluated to produce a value.

Invocation Expression: An invocation expression specifically refers to the act of calling or invoking a function. It involves using parentheses () to trigger the function or method to execute its code.

For example:

function greet(name) {
console.log("Hello, " + name);
}

greet("Alice"); // This is an invocation expression

Here, greet("Alice") is an invocation expression, which calls the greet function and passes "Alice" as an argument.

Key Points:

  • Function invocation: When you use () after a function name, you're invoking the function.
  • Execution: The function code runs only when it's invoked.

In contrast, other types of expressions include:

  • Property Access Expression: Accessing a property of an object, e.g., obj.property.
  • Functional Expression: This refers to expressions that define functions (e.g., a function declaration or function expression).
  • Primary Expression: Refers to basic expressions like variables or literals, e.g., 5, "Hello", or x.

Thus, an invocation expression is specifically about calling a function or method to perform an action or return a result.

πŸ“Š JavaScript
Q. Consider the following statements.
In order to check if the pattern matches with the string β€œt”, the statement is
Code:
var t= "Example: 4, 5, 6"; // text
var p = /\d+/g // Matches all instances of one or more digits
  • (A) p.test(t)
  • (B) t.test(p)
  • (C) t.equals(p)
  • (D) t==p
πŸ’¬ Discuss
βœ… Correct Answer: (A) p.test(t)

Explanation:

  • p.test(t) is the correct method to check if a regular expression (p) matches the string (t).
  • In this case, p is the regular expression /\d+/g, which matches one or more digits, and t is the string "Example: 4, 5, 6".
  • The test() method is used with regular expressions to check if the pattern matches any part of the string. It returns true if the pattern is found, and false otherwise.

Example:

var t = "Example: 4, 5, 6"; // text
var p = /\d+/g; // Matches one or more digits

console.log(p.test(t)); // true, because digits 4, 5, and 6 are found in the string

Why the other options are incorrect:

  • (B) t.test(p): This is incorrect because test() is a method of regular expression objects, not strings.
  • (C) t.equals(p): This is incorrect because there is no equals() method for strings in JavaScript to compare a string and a regular expression.
  • (D) t == p: This is incorrect because == is used for comparison, but it doesn't check if a regular expression matches a string.

So, the correct answer is (A) p.test(t).

πŸ“Š JavaScript
Q. The property of a primary expression is
  • (A) contains only keywords
  • (B) contains variable references alone
  • (C) stand-alone expressions
  • (D) basic expressions containing all necessary functions
πŸ’¬ Discuss
βœ… Correct Answer: (C) stand-alone expressions

Explanation:

A primary expression in JavaScript refers to the simplest kind of expression that doesn't depend on any other expressions to be evaluated. It typically includes:

  • Literals: Like numbers (5), strings ("Hello"), booleans (true or false).
  • Variable references: Like x or y.
  • Function calls: Like someFunction().
  • Object or array literals: Like { key: "value" } or [1, 2, 3].

Essentially, primary expressions are self-contained expressions that are the building blocks of more complex expressions.

For example:

  • 5 is a primary expression.
  • "Hello" is a primary expression.
  • x (a variable reference) is a primary expression.
  • someFunction() (a function call) is a primary expression.

Why the other options are incorrect:

  • (A) contains only keywords: Keywords like if, for, and function are not primary expressionsβ€”they are part of the syntax of control structures or declarations.
  • (B) contains variable references alone: While variable references are primary expressions, this is too narrow. Primary expressions can include more than just variables.
  • (D) basic expressions containing all necessary functions: This is also too vague and inaccurate. Primary expressions do not necessarily "contain all necessary functions."

So, (C) stand-alone expressions is the most accurate description.

πŸ“Š JavaScript
Q. A function definition expression can be called as
  • (A) Function calling
  • (B) Function prototype
  • (C) Function declaration
  • (D) Function literal
πŸ’¬ Discuss
βœ… Correct Answer: (D) Function literal

Explanation:

A function literal (also called a function expression) is a way of defining a function directly within an expression. It's called a "literal" because you're creating a function object literally in your code. This is often used when passing a function as an argument or when assigning it to a variable.

For example:

const add = function(a, b) {
return a + b;
};

Here, function(a, b) { return a + b; } is a function literal. It's an anonymous function (not named) created directly as an expression and assigned to the variable add.

Why the other options are incorrect:

(A) Function calling: Refers to invoking or executing a function, not defining it.

(B) Function prototype: Refers to the definition of a function's interface (i.e., the function signature), typically seen in constructor functions or classes.

(C) Function declaration: Refers to a function defined using the function keyword with a name, like this:

This is different from a function expression (or literal), as the function is not immediately part of an expression but rather declared in a statement.

So, the correct term for a function definition in an expression form is (D) Function literal.

function add(a, b) {
return a + b;
}

πŸ“Š JavaScript
Q. What will be the output of the following Javascript code?
Code:
var str1 = ”4321”;
var intval = 4321;
alert( str1 + intval );
  • (A) 4321
  • (B) 43214321
  • (C) 8642
  • (D) 12341234
πŸ’¬ Discuss
βœ… Correct Answer: (B) 43214321

Explanation:

In the given code:

var str1 = "4321";    // string
var intval = 4321; // number
alert( str1 + intval ); // concatenates str1 and intval

The key point here is that JavaScript uses type coercion when you perform the + operation between a string and a number. Since one of the operands (str1) is a string, JavaScript converts the other operand (intval) to a string as well. Then, the two strings are concatenated.

  • str1 is the string "4321".
  • intval is the number 4321, which is coerced into the string "4321".

When these two strings are concatenated:

"4321" + "4321" => "43214321"

Thus, the output of alert(str1 + intval) will be the string "43214321".

Why the other options are incorrect:

  • (A) 4321: This is incorrect because it would require a mathematical operation, not string concatenation.
  • (C) 8642: This would happen if we were performing arithmetic addition, but the + operator here is used for string concatenation.
  • (D) 12341234: This is incorrect because it's not related to the given values or any possible operation with those values.

So, the correct output is (B) 43214321.

πŸ“Š JavaScript
Q. Consider the following code snippet :
What does the last statement return ?
Code:
function constfunctions() 
{
    var functions = [];
    for(var k = 0; k < 10; k++)
        functions [k] = function() { return k; };
    return functions ;
}
var functions = constfunctions();
functions [5]()
  • (A) 10
  • (B) 9
  • (C) 0
  • (D) All of above
πŸ’¬ Discuss
βœ… Correct Answer: (A) 10