WorksheetsJavascript basics 6
Total questions: 29
Worksheet time: 58mins
Name
Class
Date
1.
What will be the result of the following code? console.log(3 + '5');
a)
8
b)
35
c)
53
d)
15
2.
How can you check if a variable x is of type "string"?
a)
typeof(x) === 'string'
b)
x.type === 'string'
c)
x instanceof String
d)
x.typeOf(String)
3.
What is the purpose of the === operator in JS?
a)
It performs type coercion.
b)
It assigns a value to a variable.
c)
It checks for equality without type conversion.
d)
It concatenates two strings.
4.
Which of the following is not a valid way to declare a variable in JS?
a)
let x = 5;
b)
const y = 10;
c)
var z = 15;
d)
variable w = 20;
5.
What will the following code snippet print to the console?const numbers = [1, 2, 3, 4, 5];
numbers.forEach(function(num) {
console.log(num * 2);
});
a)
1 2 3 4 5
b)
2 4 6 8 10
c)
1 4 9 16 25
d)
2 3 4 5 6
6.
What is the output of the following code snippet? let a = 10;
if (a > 5) {
console.log('Hello');
} else {
console.log('World');
}
a)
Hello
b)
World
c)
Hello World
d)
No output
7.
How do you write an if statement that checks if a number x is positive?
a)
if (x >= 0) { }
b)
if (x > 0) { }
c)
if (x < 0) { }
d)
if (x == 0) { }
8.
Which of the following options correctly declares a function named multiply that takes two parameters and returns their product?
a)
function multiply(a, {
return a + b;
}
b)
function multiply(a, {
a * b;
}
c)
function multiply(a, {
return a * b;
}
d)
multiply = (a, => a * b;
9.
What is the purpose of the switch statement in JS?
a)
It defines a function.
b)
It creates a loop.
c)
It performs type conversion.
d)
It allows you to choose between different actions based on different conditions.
10.
Which loop construct is ideal when you want to loop through the properties of an object?
a)
for loop
b)
while loop
c)
do...while loop
d)
for...in JS?oop
11.
What will the following code snippet print to the console? let x = 10;
x += 5;
x *= 2;
console.log(x);
a)
15
b)
20
c)
25
d)
30
12.
How can you check if a given variable y is a number in JS?
a)
typeOf(y) === 'number'
b)
y.type === 'number'
c)
y instanceof Number
d)
typeof y === 'number'
13.
What does the || operator do in JS?
a)
It performs addition.
b)
It performs division.
c)
It performs logical OR.
d)
It performs logical AND.
14.
Which statement is used to exit a function and return a value?
a)
end
b)
return
c)
exit
d)
break
15.
What will be the output of the following code snippet? - let str = 'Hello, World!';
console.log(str.length);
a)
12
b)
13
c)
14
d)
15
16.
What is the value of result after the following code snippet? - let result = 0;
for (let i = 1; i <= 5; i++) {
result += i;
}
a)
5
b)
10
c)
15
d)
25
17.
How do you write a while loop that prints even numbers from 2 to 10 (inclusive)?
a)
let i = 2;
while (i <= 10) {
console.log(i);
i++;
}
b)
let i = 2;
while (i < 10) {
console.log(i);
i += 2;
}
c)
let i = 1;
while (i <= 10) {
console.log(i);
i += 2;
}
d)
let i = 1;
while (i <= 10) {
if (i % 2 === 0) {
console.log(i);
}
i++;
}
18.
Which of the following correctly defines an arrow function that calculates the cube of a number n?
a)
const cube = n => { n * n * n; }
b)
const cube = n => n * n * n;
c)
const cube = (n) => n * n * n
d)
cube = (n) => n * n * n;
19.
What is the purpose of the NaN value in JS?
a)
It represents the value "not null".
b)
It is used to declare undefined variables.
c)
It stands for "not a number" and indicates an unrepresentable value resulting from an invalid calculation.
d)
It is a string representing "no value".
20.
Which of the following is a correct way to create an object with properties name and age?
a)
const person = [name: 'John', age: 30];
b)
const person = (name: 'John', age: 30);
c)
const person = { name: 'John', age: 30; }
d)
const person = { 'John', 30 };
21.
What will be the result of the following code? - console.log(true && false);
a)
True
b)
false
c)
undefined
d)
null
22.
How can you check if a given variable z is defined (not undefine?
a)
z !== undefined
b)
z === undefined
c)
z.isDefined()
d)
z.checkDefined()
23.
What does the ! operator do in JS?
a)
It performs bitwise NOT.
b)
It performs logical NOT.
c)
It performs addition.
d)
It performs multiplication.
24.
Which statement is used to iterate over the elements of an array?
a)
for (let i = 0; i < array.length; i++) { }
b)
for each (element in JS?rray) { }
c)
foreach (let element of array) { }
d)
for (element of array) { }
25.
What will the following code snippet print to the console? - let arr = [2, 4, 6, 8, 10];
let sum = 0;
for (let num of arr) {
sum += num;
}
console.log(sum);
a)
15
b)
20
c)
30
d)
25
26.
What is the value of result after the following code snippet? - let result = 1;
for (let i = 1; i <= 5; i++) {
result *= i;
}
a)
5
b)
10
c)
15
d)
120
27.
How do you write a do...while loop that prints odd numbers from 1 to 9 (inclusive)?
a)
let i = 1;
do {
console.log(i);
i++;
} while (i < 10);
b)
let i = 2;
do {
console.log(i);
i += 2;
} while (i <= 10);
c)
let i = 0;
do {
console.log(i);
i += 2;
} while (i < 10);
d)
let i = 1;
do {
if (i % 2 !== 0) {
console.log(i);
}
i++;
} while (i < 10);
28.
Which of the following correctly defines an arrow function that calculates the sum of two numbers a and b?
a)
const sum = (a, => { a * b; }
b)
const sum = (a, => a * b;
c)
const sum = (a, => { return a + b; }
d)
const sum = (a, => return a + b;
29.
What is the purpose of the null value in JS?
a)
It represents the value "not a number".
b)
It is used to define empty strings.
c)
It stands for "no value" or "absence of a value".
d)
It is a placeholder for undefined variables.
100 %
