0% found this document useful (0 votes)
4 views11 pages

Question 40

The document contains a series of programming questions and answers primarily focused on JavaScript concepts, including string manipulation, event handling, and data types. Each question is followed by an explanation to clarify the reasoning behind the answer. The content is structured in a quiz format, covering various programming scenarios and best practices.

Uploaded by

serenelightt
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)
4 views11 pages

Question 40

The document contains a series of programming questions and answers primarily focused on JavaScript concepts, including string manipulation, event handling, and data types. Each question is followed by an explanation to clarify the reasoning behind the answer. The content is structured in a quiz format, covering various programming scenarios and best practices.

Uploaded by

serenelightt
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/ 11

Question 40

Answer: A. str == ""


Explanation: To check if a string is empty, compare it to an empty string "".

Question 39

Answer: B. window
Explanation: The window object is used to display popup windows
like alert(), confirm(), or prompt().

Question 38

Answer:

• sampleStudent = "HTML Student"

• sampleCourse.name = "JavaScript"

• sampleCourse.grade = 100
Explanation: Primitive types (like sampleStudent) are passed by value, while
objects (like sampleCourse) are passed by reference.

Question 37

Answer:

• Page load: Add changeColor(); inside a window.onload or <body


onload="changeColor()">.

• Button click: Use onclick="changeColor()" on the button.


Note: The provided changeColor() function has syntax errors
(e.g., win, Man, table are undefined).

Question 36

Answer:

javascript

Copy

function safeRoot(a, b) {
if (a >= 0) return Math.pow(a, 1 / b);

else if (b % 2 === 0) return "Result is an imaginary number";

else return -Math.pow(-a, 1 / b);

Explanation: The function must handle non-negative a, even/odd b for negative a.

Question 35

Answer: B. onmouseenter, D. onmouseover


Explanation: These events trigger when the mouse enters/hovers over the
image. onmouseout would revert the image.

Question 34

Answer:

• console.log(y) → Error (const y cannot be reassigned).

• console.log(3 * x) → 54 (from catch).

• console.log(4 * x) → 72 (from finally).


Final Output: 54, 72.

Question 33

Answer:

javascript

Copy

var numbers = [];

for (var i = 0; i < 10; i++) {

numbers.push(Math.round(Math.abs(Math.random() * 10)));

var sum = 0;

for (var j = 0; j < 10; j = j + 2) {

sum += numbers[j];
}

console.log(sum);

Explanation: Fill array with random numbers, then sum every other element.

Question 32

Answer: D. document.getElementById("tester").innerHTML = randomQuote();


Explanation: innerHTML updates the content of an element.

Question 31

Answer: C. In a .js file


Explanation: External JavaScript files promote reusability and maintainability.

Question 30

Answer:

javascript

Copy

function validGraphic(height, width) {

if (height >= 50 && height < 100) {

valid = true;

Explanation: The function checks if height is between 50 and 100.

Question 29

Answer:

• n = 10

• c = 55

• a=0
• d=5
Explanation:

• c = 50 + 5 = 55

• a = 50 % 2 = 0

• d = 55 / 11 = 5

• n = 5 * 2 = 10.

Question 28

Answer: D. noscript
Explanation: The <noscript> tag displays content when JavaScript is disabled.

Question 27

Answer:

• "ABCD" → False

• 1234 → True

• 28 * 59 → False
Explanation: The function checks if pin is a 4-digit number (implicitly numeric).

Question 25

Answer:

• var age = 23; → Number

• var except = false; → Boolean

• var initial = '0'; → String

• var salary = 123.5; → Number

• var zip = "E1086"; → String

Question 24

Answer:

javascript
Copy

if (currentDate.getFullYear() - orderDate.getFullYear() <= 3) {

console.log("Customer " + customer.customerName + " placed order " +


dateDiff(currentDate, orderDate));

Explanation: Checks if the order date is within the last 3 years.

Question 23

Answer: A. document.getElementById("para").innerHTML += room[i];


Explanation: Appends each room type to the paragraph.

Question 22

Answer:

javascript

Copy

function showArea(length, width) {

alert(length * width);

Explanation: The correct standalone function for an external .js file.

Question 21

Answer:

javascript

Copy

if (day === "wednesday") discount = 0.1;

else discount = 0;

Explanation: Applies a 10% discount only on Wednesday.

Question 20
Answer: B. 1010
Explanation: String concatenation occurs ("16" + "10" = "1610").

Question 19

Answer:

javascript

Copy

function calculate(operation, a, b) {

switch (operation) {

case 'multiply': return a * b;

case 'divide': return a / b;

Explanation: The function routes operations to the correct calculation.

Question 18

Answer:

javascript

Copy

for (i = 0; i < list.length; i++) {

if (list[i] == null) continue;

if (list[i] == 'orange') {

alert('found');

break;

Explanation: continue skips null, break exits the loop when orange is found.
Question 17

Answer:

• Form POST requests are cached. → False

• GET requests have restricted data length. → True

• POST requests are stored in browser history. → False

• Use GET for sensitive data. → False

Question 16

Answer:

• ceil = 101

• floor = 100

• round = 101

Question 15

Answer:

javascript

Copy

document.getElementById("title").innerHTML = "Wish List";

document.getElementById("showList").innerHTML = text;

Explanation: Updates the title and list dynamically.

Question 13

Answer:

javascript

Copy

for (let i = start; i >= 0; i--) {

console.log(i);

}
Explanation: Counts down from start to 0.

Question 12

Answer: B. getElementById
Explanation: Accesses the element with id="section1".

Question 11

Answer:

javascript

Copy

function showList() {

var list = document.getElementsByTagName("li");

for (var i = 0; i < list.length; i++) {

document.getElementById("list").innerHTML += list[i].textContent + "<br>";

Explanation: Collects all <li> elements and displays their text.

Question 10

Answer: A. An exception is thrown and caught.


Explanation: const sum cannot be reassigned, causing an error.

Question 9

Answer:

javascript

Copy

if (age >= 24 && age < 36) category = 'CAT2';

else if (age >= 36 && age < 46) category = 'CAT2';

else category = 'CAT3';


Explanation: Assigns categories based on age ranges.

Question 8

Answer:

javascript

Copy

function ticketPrice(age) {

if (age < 5 || age >= 65) return 0;

else if (age >= 5 && age <= 17) return 10;

else return 20;

Explanation: Implements the pricing rules.

Question 7

Answer: A.

javascript

Copy

output.innerHTML = input.value;

input.hidden = true;

Explanation: Updates the paragraph and hides the input box.

Question 6

Answer: The form will not submit because it is missing the submission method.
Explanation: The button lacks type="submit" or form event handling.

Question 5

Answer:

javascript
Copy

var newImgElement = document.createElement("img");

newImgElement.src = "_images/photo.jpg";

document.body.prepend(newImgElement);

Explanation: Creates and inserts an image at the start of the body.

Question 4

Answer: C. Change line 10 to var option =


document.forms.orderForm["heatIndex"].value;
Explanation: Accesses the selected value correctly.

Question 3

Answer:

javascript

Copy

function Student(first, last, major, year) {

this.firstName = first;

this.lastName = last;

this.major = major;

this.year = year;

this.info = function() {

document.write("<p>You are registered as " + this.firstName + " " + this.lastName +


"</p>" + "<p>You are a " + this.year + " majoring in " + this.major + "</p>");

};

Explanation: Constructor function for student registration.

Question 2
Answer: [100, 40, 60, 10]
Explanation:

• shift() removes 20 → [40, 60, 80]

• pop() removes 80 → [40, 60]

• push(10) → [40, 60, 10]

• unshift(100) → [100, 40, 60, 10].

Question 1

Answer: D. 75
Explanation: x *= 5 is equivalent to x = x * 5 → 15 * 5 = 75.

You might also like