Software Development Exam  >  Software Development Notes  >  JavaScript for Web Development  >  Assignment: JavaScript Variables

Assignment: JavaScript Variables | JavaScript for Web Development - Software Development PDF Download

Multiple Choice Questions (MCQs)

Q.1. Which keyword is used to declare a variable in JavaScript?
(a)
let
(b) variable
(c) const
(d) var

Ans. (d)

Q.2. What will be the output of the following code snippet?
var x = 5;
var y = 3;
var result = x + y;
console.log(result);
(a)
5
(b) 3
(c) 8
(d) "8"

Ans. (c)

Q.3. Which of the following is NOT a valid variable type in JavaScript?
(a)
string
(b) boolean
(c) number
(d) character

Ans. (d)

Q.4. What is the value of the variable z after executing the following code?

let x = 10;

if (x > 5) {

  let z = 20;

}

console.log(z);/
(a)
10
(b) 20
(c) undefined
(d) ReferenceError

Ans. (d)

Q.5. Which keyword is used to declare a constant variable in JavaScript?
(a)
const
(b) constant
(c) let
(d) var

Ans. (a)

High Order Thinking Questions (HOTS)

Q.1. Explain the difference between let, var, and const in JavaScript.

  • 'var' has function scope and can be re-declared and updated throughout the function.
  • 'let' has block scope and can be updated but not re-declared within the block.
  • 'const' has block scope and cannot be updated or re-declared once assigned.

Q.2. Can you change the value of a constant variable in JavaScript? Why or why not?

No, you cannot change the value of a constant variable in JavaScript. Once assigned, the value of a constant variable cannot be modified.

Q.3. What is variable scope in JavaScript? Explain with an example.

Variable scope in JavaScript refers to the visibility or accessibility of variables within different parts of the code. For example:

function example() {

  let x = 5;  // x is only accessible within the function

  console.log(x);

}

example();

console.log(x);  // ReferenceError: x is not defined

Q.4. Identify and explain the error in the following code:
let x = 5;

const y = 10;

x = 8;

y = 12;

The error in the code is that you cannot reassign a value to a constant variable. In the given code, 'y' is declared as a constant, and later an attempt is made to assign a new value ('12') to it. This will result in a TypeError.

Q.5. What will be the output of the following code snippet?
let x = 5;

if (true) {

  let x = 10;

  console.log(x);

}

console.log(x);

The output of the code snippet will be:
10

5

Inside the block, the value of 'x' is redeclared using 'let' and assigned as '10'. Outside the block, the original value of 'x' (i.e., '5') is printed.

Fill in the blanks

1. The keyword used to declare a variable in JavaScript is ___________.

let

2. In JavaScript, the '===' operator is used for ___________ equality comparison.

strict

3. The process of assigning a value to a variable is called ___________.

assignment

4. JavaScript has ___________ variable types, including string, number, boolean, etc.

multiple

5. The scope of a variable determines its ___________ in the program.

visibility

True/False

1. Variables declared with the var keyword have function scope.

True

2. The let keyword allows re-declaration of variables in the same scope.

False

3. In JavaScript, variables are case-sensitive. 

True

4. Block scope refers to the visibility of variables within a block of code enclosed in curly braces. 

True

5. Global variables are accessible throughout the entire JavaScript program.

True

Hands-On Questions

Q.1. Declare a variable name and assign it a string value containing your name.

let name = "John Doe";

Q.2. Write a JavaScript program to calculate and display the sum of two numbers using variables. (Hint: Declare variables for the numbers and a variable to store the sum)

let num1 = 5;

let num2 = 3;

let sum = num1 + num2;

console.log("The sum is: " + sum);

Q.3. Write a JavaScript program to swap the values of two variables. (Hint: Use a temporary variable to hold one value during the swap)

let a = 10;

let b = 20;

let temp;

console.log("Before swap: a =", a, "and b =", b);

temp = a;

a = b;

b = temp;

console.log("After swap: a =", a, "and b =", b);

Q.4. Write a JavaScript program to convert a temperature in Celsius to Fahrenheit. (Hint: Use variables to store the temperature values and the conversion formula)

let celsius = 25;

let fahrenheit = (celsius * 9/5) + 32;

console.log(celsius + "°C is equal to " + fahrenheit + "°F");

Q.5. Write a JavaScript program that prompts the user for their name and displays a personalized greeting using their name. (Hint: Use a variable to store the user's name and concatenate it with a greeting string)

let name = prompt("What is your name?");

console.log("Hello, " + name + "! Welcome to our website.");

The document Assignment: JavaScript Variables | JavaScript for Web Development - Software Development is a part of the Software Development Course JavaScript for Web Development.
All you need of Software Development at this link: Software Development
51 videos|28 docs|12 tests

Top Courses for Software Development

51 videos|28 docs|12 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Viva Questions

,

Assignment: JavaScript Variables | JavaScript for Web Development - Software Development

,

Semester Notes

,

Extra Questions

,

Important questions

,

video lectures

,

MCQs

,

practice quizzes

,

Exam

,

Objective type Questions

,

Free

,

ppt

,

Previous Year Questions with Solutions

,

study material

,

pdf

,

Assignment: JavaScript Variables | JavaScript for Web Development - Software Development

,

past year papers

,

mock tests for examination

,

Assignment: JavaScript Variables | JavaScript for Web Development - Software Development

,

shortcuts and tricks

,

Summary

,

Sample Paper

;