quiz Informatique · 21 questions

Fundamentals of JavaScript and PHP

help_outline 21 questions
timer ~11 min
auto_awesome AI-generated
0 / 21
Score : 0%
1

Which statement correctly describes the scope of a variable declared with let in JavaScript?

2

What will be the result of the expression Number("abc") in JavaScript?

3

If a JavaScript function uses document.write after the page has fully loaded, what is the effect?

4

Which of the following is true about the const declaration in JavaScript?

5

What does the JavaScript expression isNaN("Ali") evaluate to?

6

Given the code snippet: let x = 5; x++; What is the value of x after execution?

7

Which method of the Math object returns a random number between 0 (inclusive) and 1 (exclusive)?

8

What will be the output of the following JavaScript code? console.log('Hello' + 5);

9

In JavaScript, which event attribute is used to execute code when a user clicks on an element?

10

What does the JavaScript expression document.getElementById('titre').innerHTML return?

11

Which of the following PHP functions is used to safely include a file and halt execution if the file cannot be loaded?

12

What will be the result of the PHP expression strcmp("apple", "banana")?

13

Which PHP function converts a string to an integer, ignoring any non-numeric trailing characters?

14

What does the PHP function date('Y-m-d') return?

15

Which MySQLi function retrieves the number of rows returned by a SELECT query?

16

In JavaScript, which method of the Date object returns the month index (0‑January, 11‑December)?

17

What will be the output of the PHP expression strlen("Hello")?

18

Which JavaScript method is used to remove whitespace from both ends of a string?

19

When validating a form in JavaScript, which property indicates whether a checkbox is checked?

20

What is the effect of calling video.play() on an HTML5 video element?

21

Which PHP function checks whether a variable is set and not null?

menu_book

Fundamentals of JavaScript and PHP

Review key concepts before taking the quiz

Introduction to Core JavaScript Concepts

Welcome to the Fundamentals of JavaScript course. This module covers the most common questions that appear in beginner quizzes, focusing on variable scope, type conversion, DOM manipulation, constant declarations, numeric checks, arithmetic operators, the Math object, and string concatenation. Mastering these topics will improve your ability to write clean, predictable code and boost your SEO ranking for related programming queries.

Understanding let and Block Scope

The let keyword introduced in ES6 provides block‑level scoping. Unlike var, which is function‑scoped, a variable declared with let is only accessible inside the curly braces ({ }) where it is defined.

  • It is not hoisted to the top of the function.
  • Attempting to reference it before its declaration results in a ReferenceError.
  • Each iteration of a for loop creates a fresh binding.

Correct answer: "It is accessible only within the block where it is defined".

Converting Strings to Numbers with Number()

The global Number() function attempts to parse a string into a numeric value. If the string cannot be interpreted as a valid number, the result is NaN (Not‑a‑Number).

  • Number("123")123
  • Number("12.3")12.3
  • Number("abc")NaN

Correct answer: NaN.

Using document.write After Page Load

The document.write() method was designed for writing content during the initial page parsing. When it is called after the document has finished loading, the browser discards the existing DOM and replaces it with the new content.

  • This behavior can unintentionally erase the entire page.
  • Modern best practices recommend using DOM methods like appendChild or innerHTML instead.

Correct answer: "It replaces the entire existing page content".

Characteristics of the const Declaration

The const keyword creates a block‑scoped variable that cannot be reassigned. However, if the constant holds an object or an array, the internal properties remain mutable.

  • Reassignment of the identifier itself throws a TypeError.
  • Object properties can still be added, modified, or deleted.
  • Primitive values (strings, numbers, booleans) are immutable by nature.

Correct answer: "The variable cannot be reassigned, but its object properties can be modified".

Checking for Non‑Numeric Values with isNaN()

The global isNaN() function determines whether a value, after being coerced to a number, is NaN. When a non‑numeric string such as "Ali" is passed, the coercion results in NaN, so isNaN("Ali") returns true.

  • isNaN("123")false
  • isNaN(123)false
  • isNaN(NaN)true

Correct answer: true.

Post‑Increment Operator (x++)

The post‑increment operator returns the original value of the variable and then increments it by one. In the snippet let x = 5; x++;, x becomes 6 after the statement executes.

  • Pre‑increment (++x) increments first, then returns the new value.
  • Both operators are equivalent when the result of the expression is not used.

Correct answer: 6.

Generating Random Numbers with Math.random()

The Math object provides a suite of mathematical utilities. Math.random() returns a floating‑point number in the range 0 (inclusive) to 1 (exclusive).

  • To obtain a random integer between min and max, use:
    Math.floor(Math.random() * (max - min + 1)) + min;
  • Never use Math.randomNumber() or Math.rand()—they do not exist.

Correct answer: Math.random().

String Concatenation with the + Operator

When the + operator encounters a string operand, JavaScript coerces the other operand to a string and concatenates them. Therefore, console.log('Hello' + 5); prints "Hello5" to the console.

  • Numeric addition occurs only when both operands are numbers.
  • Using template literals (e.g., `Hello ${value}`) is a modern, readable alternative.

Correct answer: "Hello5".

Summary and Best Practices

By mastering these fundamental concepts, you can avoid common pitfalls and write more reliable JavaScript code. Remember:

  • Prefer let and const over var for clearer scope control.
  • Use Number() and isNaN() to validate user input.
  • Avoid document.write in production; manipulate the DOM directly.
  • Leverage Math.random() for randomness, and always round or floor the result as needed.
  • When concatenating strings, be aware of implicit type conversion and consider template literals for readability.

These guidelines not only improve code quality but also increase the likelihood that your tutorials and articles rank well for queries such as "JavaScript let scope", "Number conversion NaN", and "how to use Math.random".

Stop highlighting.
Start learning.

Join students who have already generated over 50,000 quizzes on Quizly. It's free to get started.