Font size
WorksheetsJAVASCRIPT-STRING,ARRAYS,FUNCTIONS
Total questions: 75
Worksheet time: 58mins
What does the following code snippet do? function add(x, y) { return x + y; }
console.log(add(5, 3));
(a)
NOT a valid way to pass arguments to a function?
(a)
What will the following code output?
function foo() { var x = 10; if (true) { var x = 20; console.log(x); } console.log(x); } foo();
(a)
What is the result of the following code?
function sayName(name) { console.log("Hello, " + name); } var greeting = sayName("John"); console.log(greeting);
(a)
What will be the output of the following code?
let arr = [1, 2, 3]; console.log(arr[3]);
(a)
What will be the output of the following code?
let arr = [1, 2, 3]; arr.length = 1; console.log(arr[2]);
(a)
What will be the output of the following code?
let arr = [1, 2, 3]; arr[5] = 6; console.log(arr.length);
(a)
What will be the output of the following code?
let arr = [1, 2, 3]; arr.length = 0; console.log(arr[0]);
(a)
What keyword is used to define a function in JavaScript?
A) function
B) define
C) func
D) def
What is the scope of variables declared using the var keyword within a function?
A) Global scope
B) Local scope
C) Function scope
D) Block scope
What method returns the first index at which a given element can be found in the array, or -1 if it is not present?
A) indexOf()
B) lastIndexOf()
C) findIndex()
D) includes()
Which method is used to find the position of a specified value (substring) within a string?
A) indexOf()
B) search()
C) positionOf()
D) find()
What does the bind() method do in JavaScript?
A) Binds a function to an object.
B) Binds a function to its parameters.
C) Binds a function to a specific context, creating a new function.
D) Binds a function to its return value.
What does the typeof operator return when used on a function?
A) "function"
B) "object"
C) "undefined"
D) "number"
What does the setTimeout() function do in JavaScript?
Delays the execution of a function by a specified number of milliseconds.
Calls a function repeatedly at a fixed interval.
Halts the execution of a function for a specified number of milliseconds.
Checks if a function is defined.
Which of the following statements is true about JavaScript functions?
A) JavaScript functions cannot be nested.
B) JavaScript functions can return multiple values.
C) JavaScript functions must have a return type specified.
D) JavaScript functions are objects.
What does the map() method do when called on an array in JavaScript?
Modifies the original array.
Creates a new array with the results of calling a provided function on every element in the calling array.
Removes elements from the array.
Sorts the array.
What method reverses the order of the elements in an array in place?
A) reverse()
B) sort()
C) flip()
D) invert()
What is a higher-order function?
A) A function that is defined with the higher keyword.
B) A function that operates on other functions, either by taking them as arguments or by returning them.
C) A function that has a higher execution priority.
D) A function that has a higher number of lines of code.
Which method is used to extract a section of a string and returns a new string?
A) extract()
B) slice()
C) split()
D) cut()
What is the difference between function declarations and function expressions in JavaScript?
A) Function declarations can be hoisted, while function expressions cannot.
B) Function expressions can be hoisted, while function declarations cannot.
C) There is no difference.
D) Function expressions are shorter in syntax compared to function declarations.
The join() method joins all elements of an array into a string, using which separator by default?
A) Comma (,)
B) Pipe (|)
C) Space ( )
D) Underscore (_)
Which method removes the last element from an array and returns that element?
A) push()
B) pop()
C) shift()
D) unshift()
The Array.isArray() method returns true if the argument is an array, otherwise:
A) It returns false
B) It returns null
C) It returns undefined
D) It throws an error
The toUpperCase() method returns the calling string value converted to uppercase. What happens if the string is already in uppercase?
A) It converts it to lowercase.
B) It returns the string as it is.
C) It throws an error.
D) It returns null.
How do you call a function named myFunction in JavaScript?
A) call myFunction()
B) myFunction.call()
C) invoke myFunction()
D) myFunction()
In JavaScript, what is a callback function?
A function that is called automatically when a certain event occurs.
A function that is passed as an argument to another function and is executed after some operation has been completed.
A function that is called inside a loop.
A function that is executed before any other function in a program.
What method returns the length of a string?
A) size()
B) length()
C) count()
D) getSize()
What is the purpose of the apply() method in JavaScript?
It applies a function to an object.
It calls a function with a given this value and arguments provided as an array.
It applies CSS styles to HTML elements.
It creates a new function.
What is the purpose of the return statement in a function?
A) To stop the execution of the function.
B) To define the function's parameters.
C) To specify the function's name.
D) To specify the value the function should return.
What method combines the text of two strings and returns a new string?
A) append()
B) merge()
C) concat()
D) combine()
The charAt() method returns the character at a specified index within a string. If the index is out of range, what does it return?
A) null
B) undefined
C) Empty string
D) Error
What method returns a new array containing all elements of the calling array for which the provided filtering function returns true?
A) filter()
B) map()
C) reduce()
D) forEach()
In JavaScript, functions are first-class citizens. What does this mean?
A) Functions are treated as objects.
B) Functions can only be defined at the top of the code.
C) Functions can only be used in specific contexts.
D) Functions can't be passed as arguments to other functions.
The trim() method removes whitespace from both ends of a string. Which of the following whitespace characters does it remove?
A) Space
B) Tab
C) Newline
D) All of the above
Which of the following methods does not modify the original array?
A) splice()
B) slice()
C) concat()
D) map()
Which of the following is true about arrow functions in JavaScript?
Arrow functions do not have their own this context.
Arrow functions cannot accept arguments.
Arrow functions are always anonymous.
Arrow functions cannot be used as callbacks.
What method adds one or more elements to the end of an array and returns the new length of the array?
A) push()
B) pop()
C) shift()
D) unshift()
Which of the following methods does not modify the original string but returns a new string?
A) substr()
B) substring()
C) slice()
D) splice()
The replace() method replaces a specified value with another value in a string. By default, it replaces only the first occurrence. How to make it replace all occurrences?
A) Use a regular expression with the global flag (/g).
B) Pass an additional parameter specifying 'all'.
C) There's no way to replace all occurrences.
D) Use the replaceAll() method.
What method is used to split a string into an array of substrings, and returns the new array?
A) split()
B) divide()
C) slice()
D) break()
Which array method does not change the original array but returns a shallow copy of a portion of an array into a new array object?
A) slice()
B) splice()
C) concat()
D) copyWithin()
Which method returns a new array consisting of the elements of the original array that meet a certain condition specified by a callback function?
A) map()
B) filter()
C) reduce()
D) forEach()
What does the arguments object represent in a JavaScript function?
A) It represents the parameters passed to the function.
B) It represents the function's return value.
C) It represents an array-like object containing the arguments passed to the function.
D) It represents the function's prototype.
Which method is used to remove the last element from an array and return that element?
A) pop()
B) shift()
C) unshift()
D) push()
What does the typeof operator return when used on an array?
A) "array"
B) "object"
C) "undefined"
D) "number"
Some of JavaScript assignment operator
*+
/=
%=
**=
Execute the function named myFunction
function myFunction() {
alert("Hello World!");
}
(a)
A function declaration consists of the following
The function keyword.
The name of the function, or its identifier, followed by parentheses.
A function body enclosed in the function’s curly brackets, { }.
a backtick inside console.log()
let superTired = true;
if (superTired) {
console.log("Go to sleep!");
} else {
console.log("Let's play some games!"
What will show up on the console?
Go to sleep
Let's play some games
Uncaught SyntaxError: Unexpected identifier
Uncaught SyntaxError: missing ) after argument list
Higher-order functions are functions that accept other functions as arguments and/or return functions as output.
True
False
The name:values pairs in JavaScript objects are called
properties
methods
value
variable
alert "Jae" by extracting information from the object
const person = {
firstName: "Jae",
lastName: "Won"
};
alert( (a) );
change this to an array
let animal1 = "dog";
let animal2 = "cat";
let animal3 = "bird";
(a)
if (time < 10) {
greeting = "Good morning";
} else if (time < 20) {
greeting = "Good day";
} else {
greeting = "Good evening";
}
What would be printed from this code?
Good day
Good morning
Good evening
Uncaught ReferenceError: time is not defined
What will this code log to the console?
let groceryItem = "papaya";
switch (groceryItem) {
case "tomato":
console.log("Tomatoes are $0.49");
break;
case "lime":
console.log("Limes are $1.49");
break;
case "papaya":
console.log("Papayas are $1.29");
break;
default:
console.log("Invalid item");
break;
Tomatoes are $0.49
Limes are $1.49
Papayas are $1.29
Invalid item
When you invoke a function, which of the following is true about generalization?
The function can accept multiple arguments and provide more generalized functionality.s
What does a JavaScript function typically return if there is no explicit return statement?
undefined
null
The value of the last executed statement inside the function.
An error.
Which of the following is an example of a function expression?
const myFunction = function() {}
What is an anonymous function in JavaScript?
What does an Immediately Invoked Function Expression (IIFE) do?
In JavaScript, what is the scope of a variable declared inside a function (function-level scope)?
Which of the following is a correct syntax for an arrow function in JavaScript?
const myFunction = () => {}
function aaa() {
return
{
test: 1
};
}
alert(typeof aaa());
What does the above alert?
User defined
Object
Number
function
Inside which HTML element do we put the JavaScript?
<script>
<javaScript>
<js>
<scripting>
Where is the correct place to insert a JavaScript?
The <head> section
The <body> section
both the <head> and <body> section are correct
Which is the correct way to write a JavaScript array?
var txt = new Array(1:"arr",2:"kim",3:"jim")
var txt = new Array:1=(" arr ")2=("kim")3=("jim")
var txt = new Array("arr ","kim","jim")
var txt = new Array=" arr ","kim","jim"
Which of the following is correct to write “Hello World” on the web page?
System.out.println(“Hello World”)
print(“Hello World”)
document.write(“Hello World”)
response.write(“Hello World”)
Which of the following is not a valid JavaScript variable name?
2java
_java_and_ java _names
javaandjava
None of the above
Why so Java and JavaScript have similar name?
Java Script is a stripped-down version of Java
The syntax of JavaScript is loosely based on Java syntax
They both support Object Oriented Programming
None of the above
How do you create a function in JavaScript?
function myFunction()
function:myFunction()
function=myFunction()
How do you call a function named "myFunction"?
call function myFunction()
call myFunction()
myFunction()
How can you add a comment in a JavaScript?
//This is comment
'This is comment
<!-- This is comment-->
How do you declare a JavaScript variable?
v carName
variable carName
var carName
Is JavaScript case-sensitive?
yes
No
