Software Development Exam  >  Software Development Notes  >  JavaScript for Web Development  >  Assignment: JavaScript Strings

Assignment: JavaScript Strings | JavaScript for Web Development - Software Development PDF Download

Multiple Choice Questions (MCQs)

Q.1. Which of the following is NOT a valid way to create a JavaScript string?
(a)
var str = "Hello";
(b) var str = new String("Hello");
(c) var str = String("Hello");
(d) var str = String.new("Hello");

Ans. (d)

Q.2. What will be the output of the following code?
var str = "Hello, World!";

console.log(str.length);
(a) 6
(b) 12
(c) 13
(d) 14

Ans. (c)

Q.3. Which method is used to extract a specific part of a string and return it as a new string?
(a)
split()
(b) slice()
(c) replace()
(d) concat()

Ans. (b)

Q.4. What will be the output of the following code?
var str = "Hello, World!";

console.log(str.indexOf("World"));
(a) -1
(b) 0
(c) 7
(d) 8

Ans. (c)

Q.5. Which method is used to convert a string to uppercase letters?
(a) 
toLowerCase()
(b) trim()
(c) toUpperCase()
(d) concat()

Ans. (c)

High Order Thinking Questions (HOTS)

Q.1. Write a JavaScript function that takes a string as input and returns the reverse of that string.

function reverseString(str) {

  return str.split("").reverse().join("");

}

Q.2. Write a JavaScript function that counts the number of occurrences of a specific character in a string.

function countOccurrences(str, char) {

  var count = 0;

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

    if (str[i] === char) {

      count++;

    }

  }

  return count;

}

Q.3. Write a JavaScript function that checks whether a given string is a palindrome or not.

function isPalindrome(str) {

  var reversedStr = str.split("").reverse().join("");

  return str === reversedStr;

}

Q.4. Write a JavaScript function that removes all white spaces from a string.

function removeSpaces(str) {

  return str.replace(/\s/g, "");

}

Q.5. Write a JavaScript function that capitalizes the first letter of each word in a string.

function capitalizeWords(str) {

  var words = str.split(" ");

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

    words[i] = words[i][0].toUpperCase() + words[i].substr(1);

  }

  return words.join(" ");

}

Fill in the Blanks

1. The length property of a string returns the ______ of the string.

length

2. The ______ method is used to combine the text of two or more strings and return a new string.

concat()

3. The ______ method searches a string for a specified value and returns the position of the match.

indexOf()

4. The ______ method is used to convert a string to lowercase letters.

toLowerCase()

5. The ______ method extracts a part of a string and returns a new string.

slice()

True/False

1. In JavaScript, strings are immutable, which means you cannot change the value of a string once it is created. 

True

2. The concat() method is used to add new elements to the end of a string. 

False

3. The toUpperCase() method converts a string to lowercase letters.

False

4. The charAt() method returns the character at a specified index in a string. 

True

5. The includes() method checks whether a string contains a specified value.

True

Hands-On Questions

Q.1. Write JavaScript code to check if a given string is a palindrome.

function isPalindrome(str) {

  var reversedStr = str.split("").reverse().join("");

  return str === reversedStr;

}

Q.2. Write JavaScript code to count the number of vowels in a given string.

function countVowels(str) {

  var count = 0;

  var vowels = "aeiou";

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

    if (vowels.includes(str[i].toLowerCase())) {

      count++;

    }

  }

  return count;

}

Q.3. Write JavaScript code to capitalize the first letter of a given string.

function capitalizeFirstLetter(str) {

  return str.charAt(0).toUpperCase() + str.slice(1);

}

Q.4. Write JavaScript code to remove all special characters from a given string.

function removeSpecialCharacters(str) {

  return str.replace(/[^a-zA-Z0-9 ]/g, "");

}

Q.5. Write JavaScript code to reverse the words in a given string.

function reverseWords(str) {

  return str.split(" ").reverse().join(" ");

}

The document Assignment: JavaScript Strings | JavaScript for Web Development - Software Development is a part of the Software Development Course JavaScript for Web Development.
All you need of Software Development at this link: Software Development
51 videos|28 docs|12 tests

Top Courses for Software Development

51 videos|28 docs|12 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

video lectures

,

Extra Questions

,

shortcuts and tricks

,

Assignment: JavaScript Strings | JavaScript for Web Development - Software Development

,

Sample Paper

,

Free

,

Semester Notes

,

mock tests for examination

,

study material

,

Assignment: JavaScript Strings | JavaScript for Web Development - Software Development

,

Summary

,

Viva Questions

,

practice quizzes

,

Important questions

,

Previous Year Questions with Solutions

,

pdf

,

ppt

,

Exam

,

MCQs

,

past year papers

,

Assignment: JavaScript Strings | JavaScript for Web Development - Software Development

,

Objective type Questions

;