JavaScript Introduction II
39 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is a defining feature of variables declared with const?

  • They have block scope. (correct)
  • They can be reassigned later.
  • They can be redeclared multiple times.
  • They must have a global scope.
  • How can a property of a JavaScript object be accessed?

  • By calling objectName[propertyName] method.
  • Invoking objectName.propertyName() function.
  • Using the method objectName->propertyName.
  • Using objectName.propertyName or objectName['propertyName'] (correct)
  • What does the function toCelsius(fahrenheit) do?

  • Returns the input temperature as is.
  • Calculates the freezing point of water.
  • Converts Fahrenheit to Celsius. (correct)
  • Converts Celsius to Fahrenheit.
  • Which of the following statements about JavaScript objects is false?

    <p>They are immutable in nature.</p> Signup and view all the answers

    What result does person.fullName() return?

    <p>A concatenation of firstName and lastName.</p> Signup and view all the answers

    What is true regarding HTML events?

    <p>They encompass both browser and user actions.</p> Signup and view all the answers

    Which method correctly calls an object method?

    <p>objectName.methodName();</p> Signup and view all the answers

    Which of the following is NOT a characteristic of const variables?

    <p>They can be reassigned.</p> Signup and view all the answers

    In JavaScript, an object allows for the grouping of which of the following?

    <p>Related data (properties) and behavior (methods).</p> Signup and view all the answers

    To access the last name of a person stored in an object, which of the following syntaxes is correct?

    <p>person.lastName;</p> Signup and view all the answers

    What does the slice() method do when a second parameter is omitted?

    <p>Slices out the rest of the string</p> Signup and view all the answers

    What is the output of the code: let part = str.slice(-12); where str is 'Apple, Banana, Kiwi'?

    <p>Banana, Kiwi</p> Signup and view all the answers

    How does substr() differ from slice() in terms of the second parameter?

    <p>It specifies the length of the extracted part</p> Signup and view all the answers

    What will the following code output: let part = str.substr(-4); where str is 'Apple, Banana, Kiwi'?

    <p>Kiwi</p> Signup and view all the answers

    What is a key characteristic of the replace() method?

    <p>It only replaces the first match found</p> Signup and view all the answers

    If str is 'Apple, Banana, Kiwi', what will str.slice(7, 13) return?

    <p>Banana</p> Signup and view all the answers

    What happens if the first parameter of slice() is negative?

    <p>It starts counting from the end of the string</p> Signup and view all the answers

    What will be the output of the following code: let part = str.substr(7, 6); if str is 'Apple, Banana, Kiwi'?

    <p>Banana</p> Signup and view all the answers

    What is the result of calling str.replace('a', 'o') on the string 'Apple, Banana, Kiwi'?

    <p>Apple, Bonana, Kiwi</p> Signup and view all the answers

    What does the slice() method return if both parameters are negative, e.g., str.slice(-5, -1)?

    <p>Kiwi</p> Signup and view all the answers

    What will be the output of the following code: let text = 'MSA students and MSA staff!'; let newText = text.replace('MSA', 'CS'); console.log(newText);?

    <p>CS students and MSA staff!</p> Signup and view all the answers

    Which of the following statements correctly uses the concat() method?

    <p>let fullName = text1.concat(' ', text2);</p> Signup and view all the answers

    What does the toUpperCase() method do?

    <p>It converts all characters in a string to upper case.</p> Signup and view all the answers

    What is the purpose of the trim() method in string manipulation?

    <p>It removes whitespace from both sides of a string.</p> Signup and view all the answers

    In which scenario would you use the split() method?

    <p>To convert a string to an array based on a delimiter.</p> Signup and view all the answers

    How would you correctly call the toLowerCase() method on a string variable text1?

    <p>text1.toLowerCase();</p> Signup and view all the answers

    What does the output of the following code produce: let text1 = ' welcome!'; let text2 = text1.trim(); console.log(text2);?

    <p>welcome!</p> Signup and view all the answers

    What is expected when using the split() method with a pipe character as the delimiter, given the string 'a|b|c|d|e|f'?

    <p>['a', 'b', 'c', 'd', 'e', 'f']</p> Signup and view all the answers

    Which method would you use to remove whitespace only from the start of a string?

    <p>trimStart()</p> Signup and view all the answers

    If the below code is executed, what will be the value of myArray? let text = 'a b c d e f'; const myArray = text.split(' ');

    <p>['a', 'b', 'c', 'd', 'e', 'f']</p> Signup and view all the answers

    What does the 'onchange' event signify in HTML?

    <p>An HTML element has been changed</p> Signup and view all the answers

    Which of the following is NOT a valid HTML event?

    <p>onhover</p> Signup and view all the answers

    Which method is used to determine the length of a string in JavaScript?

    <p>length</p> Signup and view all the answers

    What does the 'substr(start, length)' function do in JavaScript?

    <p>Returns a substring based on a specified length</p> Signup and view all the answers

    What will the following code return: let text = 'Hello'; text[1];?

    <p>e</p> Signup and view all the answers

    Which of the following correctly uses template literals for variable substitution?

    <p>let message = <code>Welcome ${firstName}</code>;</p> Signup and view all the answers

    Which event should you use to execute a function after a page has finished loading?

    <p>onload</p> Signup and view all the answers

    What will the output of the following code be: let greeting = 'Hello World'; console.log(greeting.slice(0, 5));?

    <p>Hello</p> Signup and view all the answers

    Which string declaration is correct in JavaScript?

    <p>let site = 'w3schools.com';</p> Signup and view all the answers

    Study Notes

    JavaScript - Introduction - II

    • Constant Variables: Constants cannot be re-declared or reassigned. They have block scope.
    • Functions in JavaScript: A function is a block of code used for a particular task. It is executed when called or invoked. Functions can accept parameters.
    • JavaScript Objects: A JavaScript object is a versatile collection of key-value pairs (properties) and actions (methods). You use objects to group related data and behaviors. Objects are variables that have properties and functions.
    • Retrieving Object Properties: Accessing an object's property can be done in two ways: objectName["propertyName"] or objectName.propertyName

    HTML Events

    • HTML Events: Events can be actions resulting from a user or happening within the browser, such as page loading, user clicks, mouse movements, form input changes, and button clicks. Specific JavaScript code is often associated with these events.

    Strings in JavaScript

    • String Definition: Strings in JavaScript can use single or double quotes.
    • Quotes within Strings: Nested quotes are possible as long as the enclosing quotes differ in style.
    • Escape Characters: Special characters like apostrophes or backslashes can be included, using "".
    • String Properties and Methods: JavaScript strings have properties such as .length and methods for various operations like .slice(), .substring(), .substr(), .replace(), .concat(),.toUpperCase(), .toLowerCase(), .trim(), .trimStart(), .trimEnd(), .split()

    String Functions

    • slice(start, end): A string function that extracts a portion of the string. If the end parameter is omitted, the rest of the string is extracted. The extracted string does not include the character at the end position
    • substring(start, end): Extracts a portion of a string from one index to another. It treats negative values as zero and does not include the final index
    • substr(start, length): Extracts characters starting at start for a specific length.

    Variable Substitutions

    • Variable substitutions in strings use template literals (\${expression}``) to substitute variables directly
    • String concatenation can use the concat() method or a plus (+) operator

    Additional notes

    • The example code for various JavaScript string functions has been omitted but is readily accessible via the example links given.
    • The presentation contained various examples of these JavaScript concepts.

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Related Documents

    Javascript II Lecture Notes PDF

    Description

    This quiz explores advanced concepts in JavaScript, including constant variables, functions, and objects. You'll learn how to retrieve object properties and understand HTML events alongside string handling in JavaScript. Dive deeper into the fundamentals of programming with this engaging assessment.

    More Like This

    JavaScript Objects and Arrays Quiz
    10 questions
    JavaScript Module 2: Objects
    10 questions
    JavaScript Objects and jQuery Basics
    37 questions
    Use Quizgecko on...
    Browser
    Browser