0% found this document useful (0 votes)
16 views9 pages

Jsquiz

Uploaded by

tareeba817
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views9 pages

Jsquiz

Uploaded by

tareeba817
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

QUIZES :

Quiz 1: Variables and Data Types

1. What will be the output of the following code?

let x = "5";

let y = 5;

console.log(x == y);

True
false
undefined
error

2. What is the type of the variable ‘a’ in the following code?

let a = 10;

a = "Hello";

number
string
boolean
undefined

3. Which of the following is NOT a valid JavaScript data type?

object
Array
Date
Number

4. What will be the result of this code?

let a = null;

let b;

console.log(a == b);

true
false
null
undefined
5. What is the output of the following code?

let str = `Hello ${2 + 2}`;

console.log(str);

"Hello 4"
"Hello ${2 + 2}"
"Hello 22"
4

Quiz 2 : Operators

1. What is the result of 5 + '5' in JavaScript?

10
55
undefined
error

2. Which operator is used to check for equality without type coercion?

==
===
!=
!==

3. What does the following code return?

console.log(7 % 3);

1
2
3
0

4. What is the result of this expression: true && false || true?

true
false
undefined
error

5. What does the typeof operator do?

Checks the type of a variable at runtime


Changes the type of a variable
Compares two variables
Returns the length of a variable
Quiz 3: Control Flow

1 . Which keyword is used to exit a loop early?

a) stop
b) exit
c) break
d) continue

2 . What will be the output of the following code?

let x = 10;

if (x > 5) {

console.log("Greater");

} else if (x == 5) {

console.log("Equal");

} else {

console.log("Less");

a) "Greater"
b) "Equal"
c) "Less"
d) undefined

3 . Which loop is guaranteed to execute at least once?

a) for
b) while
c) do...while
d) foreach

4 . What does the continue keyword do in a loop?

a) Exits the loop


b) Skips the current iteration and continues with

the next iteration


c) Ends the loop entirely
d) Restarts the loop from the beginning
5 . Which of the following statements is true about a for loop?

a) It executes a block of code a specified number of times


b) It can only be used with arrays
c) It executes code while a condition is true
d) It executes code based on a boolean value

Quiz 4 : Functions

1. What is the default value of a parameter if it is not provided?

a) null
b) undefined
c) 0
d) ""

2 . How do you define a function in JavaScript?

a) function myFunction() {}
b) def myFunction() {}
c) func myFunction() {}
d) function: myFunction() {}

3 .What does the return statement do in a function?

a) Ends the function and returns a value to the caller


b) Continues the function
c) Calls another function
d) Defines a new variable

4 .Which of the following is a correct way to call a function named calculateArea?

a) calculateArea()
b) calculateArea[]
c) calculateArea{}
d) call calculateArea()

5 .What will be the output of the following code?

function add(a, b) {

return a + b;

console.log(add(5, 10));
a) 5
b) 10
c) 15
d) undefined

Quiz 5 : Object and Array

1 .How do you access a property of an object named person with a property name?

a) person.name
b) person[name]
c) person->name
d) person{name}

2 .What will be the output of this code?

let arr = [1, 2, 3, 4];

console.log(arr[2]);

a) 1
b) 2
c) 3
d) 4

3 .How do you add a new property to an object in JavaScript?

a) object.addProperty(name, value)
b) object.property = value
c) object.insert(name, value)
d) object.set(name, value)

4 .What is the result of this code?

let obj = {a: 1, b: 2};

obj.b = 3;

console.log(obj.b);

a) 1
b) 2
c) 3
d) undefined

5 .How can you iterate over the elements of an array arr?


a) for (let i in arr)
b) for (let i of arr)
c) arr.forEach(element => { })
d) All of the above

Quiz 6: DOM Manipulation

How do you select an element with the ID myElement?

a) document.querySelector('#myElement')
b) document.getElementById('myElement')
c) document.select('#myElement')
d) document.getElementByClassName('myElement')

Which method is used to change the content of an HTML element?

a) element.innerHTML
b) element.setContent()
c) element.updateText()
d) element.changeContent()

What does the addEventListener method do?

a) Adds an event to an element


b) Removes an event from an element
c) Adds a listener to an event
d) Changes the event type

How do you remove an element from the DOM?

a) element.remove()
b) document.removeChild(element)
c) element.delete()
d) document.deleteChild(element)

What is the purpose of event.preventDefault()?

a) Stops the default action of an event


b) Cancels an event
c) Prevents an event from being triggered
d) Delays an event

EXERCISES :
Variables and Data Types
1. Create a variable age and assign your age to it. Then, create another variable isAdult that
checks if age is greater than or equal to 18. Output isAdult.
2. Write a function convertToString that takes a number as an argument and returns it as a
string.
3. Create an array with the following elements: "apple", "banana", and "cherry". Write a
function to print each element of the array.
4. Write a function isString that takes an argument and returns true if the argument is a
string, and false otherwise.
5. Declare an object person with properties name (string), age (number), and isEmployed
(boolean). Print each property.

Operators

1. Write a function calculateSum that takes two numbers and returns their sum.
2. Create a function checkGreaterThan that takes two numbers and returns true if the first
number is greater than the second number, otherwise false.
3. Use the ternary operator to check if a number is even or odd.
4. Write a function logicalOperators that takes two boolean values and returns the result of
their logical AND, OR, and NOT operations.
5. Write a program that uses a switch statement to print the name of the day based on a
number (1 for Monday, 2 for Tuesday, etc.).

Control Flow

1. Write a function countdown that takes a number and prints every number from that
number down to 1.
2. Create a program that uses a for loop to sum the numbers from 1 to 100 and prints the
result.
3. Write a while loop that prints all even numbers between 1 and 50.
4. Create a function printMultiples that takes a number and prints all its multiples up to 100
using a for loop.
5. Write a program using a do...while loop to generate and print a random number between 1
and 10 until it generates the number 7.

Functions

1. Write a function greet that takes a name as a parameter and returns a greeting string.
2. Create a function factorial that computes the factorial of a given number.
3. Write a function multiply that takes two numbers and returns their product.
4. Create a function isPalindrome that checks if a given string is a palindrome.
5. Write a function calculateAverage that takes an array of numbers and returns their
average.

Object and Array

1. Create an object car with properties make, model, and year. Write a function to print
these properties.
2. Write a function addProperty that takes an object, a property name, and a value, and
adds the property to the object.
3. Create an array numbers with 5 elements. Write a function that prints the sum of all
elements in the array.
4. Write a function removeElement that takes an array and an element, and removes the
element from the array if it exists.
5. Create a function mergeObjects that takes two objects and merges them into one. If there
are duplicate properties, the second object’s properties should overwrite the first
object’s properties.

MOD Manipulation

1. Write a script that changes the text content of an element with the ID myElement to
"Hello, World!".
2. Create a button that, when clicked, changes the background color of the page to a
random color.
3. Write a function that hides an element with a given class name when a button is clicked.
4. Create a form with an input field and a submit button. Write a JavaScript function that
alerts the value of the input field when the form is submitted.
5. Write a script that dynamically creates and appends a new div element with the text
"New Element" to the body of the document.

ANSWERS OF QUIZ:
QUIZ 1 :

1. true
2. string
3. Date (Note: Date is an object, not a primitive data type.)
4. true
5. “Hello 4"

QUIZ 2 :

1. 55
2. ===
3. 1
4. true
5. Checks the type of a variable at runtime

QUIZ 3 :

1. break
2. “Greater”
3. do...while
4. Skips the current iteration and continues with the next iteration
5. It executes a block of code a specified number of times
QUIZ 4 :

1. undefined
2. function myFunction() {}
3. Ends the function and returns a value to the caller
4. calculateArea()
5. 15

QUIZ 5:

1. person.name
2. 3
3. object.property = value
4. 3
5. All of above

QUIZ 6 :

1. document.getElementById('myElement')
2. element.innerHTML
3. Adds a listener to an event
4. element.remove()
5. Stops the default action of an event

You might also like