wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

JAVASCRIPT-STRING,ARRAYS,FUNCTIONS

Total questions: 75

Worksheet time: 58mins

Name
Class
Date
1.

What does the following code snippet do? function add(x, y) { return x + y; }

console.log(add(5, 3));



(a)  

2.

NOT a valid way to pass arguments to a function?

(a)  

3.

What will the following code output? function foo() { var x = 10; if (true) { var x = 20; console.log(x); } console.log(x); } foo();

(a)  

4.

What is the result of the following code? function sayName(name) { console.log("Hello, " + name); } var greeting = sayName("John"); console.log(greeting);

(a)  

5.

What will be the output of the following code? let arr = [1, 2, 3]; console.log(arr[3]);

(a)  

6.

What will be the output of the following code? let arr = [1, 2, 3]; arr.length = 1; console.log(arr[2]);

(a)  

7.

What will be the output of the following code? let arr = [1, 2, 3]; arr[5] = 6; console.log(arr.length);

(a)  

8.

What will be the output of the following code? let arr = [1, 2, 3]; arr.length = 0; console.log(arr[0]);

(a)  

9.

What keyword is used to define a function in JavaScript?

a)

A) function

b)

B) define

c)

C) func

d)

D) def

10.

What is the scope of variables declared using the var keyword within a function?

a)

A) Global scope

b)

B) Local scope

c)

C) Function scope

d)

D) Block scope

11.

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)

A) indexOf()

b)

B) lastIndexOf()

c)

C) findIndex()

d)

D) includes()

12.

Which method is used to find the position of a specified value (substring) within a string?

a)

A) indexOf()

b)

B) search()

c)

C) positionOf()

d)

D) find()

13.

What does the bind() method do in JavaScript?

a)

A) Binds a function to an object.

b)

B) Binds a function to its parameters.

c)

C) Binds a function to a specific context, creating a new function.

d)

D) Binds a function to its return value.

14.

What does the typeof operator return when used on a function?

a)

A) "function"

b)

B) "object"

c)

C) "undefined"

d)

D) "number"

15.

What does the setTimeout() function do in JavaScript?

a)

Delays the execution of a function by a specified number of milliseconds.

b)

Calls a function repeatedly at a fixed interval.

c)

Halts the execution of a function for a specified number of milliseconds.

d)

Checks if a function is defined.

16.

Which of the following statements is true about JavaScript functions?

a)

A) JavaScript functions cannot be nested.

b)

B) JavaScript functions can return multiple values.

c)

C) JavaScript functions must have a return type specified.

d)

D) JavaScript functions are objects.

17.

What does the map() method do when called on an array in JavaScript?

a)

Modifies the original array.

b)

Creates a new array with the results of calling a provided function on every element in the calling array.

c)

Removes elements from the array.

d)

Sorts the array.

18.

What method reverses the order of the elements in an array in place?

a)

A) reverse()

b)

B) sort()

c)

C) flip()

d)

D) invert()

19.

What is a higher-order function?

a)

A) A function that is defined with the higher keyword.

b)

B) A function that operates on other functions, either by taking them as arguments or by returning them.

c)

C) A function that has a higher execution priority.

d)

D) A function that has a higher number of lines of code.

20.

Which method is used to extract a section of a string and returns a new string?

a)

A) extract()

b)

B) slice()

c)

C) split()

d)

D) cut()

21.

What is the difference between function declarations and function expressions in JavaScript?

a)

A) Function declarations can be hoisted, while function expressions cannot.

b)

B) Function expressions can be hoisted, while function declarations cannot.

c)

C) There is no difference.

d)

D) Function expressions are shorter in syntax compared to function declarations.

22.

The join() method joins all elements of an array into a string, using which separator by default?

a)

A) Comma (,)

b)

B) Pipe (|)

c)

C) Space ( )

d)

D) Underscore (_)

23.

Which method removes the last element from an array and returns that element?

a)

A) push()

b)

B) pop()

c)

C) shift()

d)

D) unshift()

24.

The Array.isArray() method returns true if the argument is an array, otherwise:

a)

A) It returns false

b)

B) It returns null

c)

C) It returns undefined

d)

D) It throws an error

25.

The toUpperCase() method returns the calling string value converted to uppercase. What happens if the string is already in uppercase?

a)

A) It converts it to lowercase.

b)

B) It returns the string as it is.

c)

C) It throws an error.

d)

D) It returns null.

26.

How do you call a function named myFunction in JavaScript?

a)

A) call myFunction()

b)

B) myFunction.call()

c)

C) invoke myFunction()

d)

D) myFunction()

27.

In JavaScript, what is a callback function?

a)

A function that is called automatically when a certain event occurs.

b)

A function that is passed as an argument to another function and is executed after some operation has been completed.

c)

A function that is called inside a loop.

d)

A function that is executed before any other function in a program.

28.

What method returns the length of a string?

a)

A) size()

b)

B) length()

c)

C) count()

d)

D) getSize()

29.

What is the purpose of the apply() method in JavaScript?

a)

It applies a function to an object.

b)

It calls a function with a given this value and arguments provided as an array.

c)

It applies CSS styles to HTML elements.

d)

It creates a new function.

30.

What is the purpose of the return statement in a function?

a)

A) To stop the execution of the function.

b)

B) To define the function's parameters.

c)

C) To specify the function's name.

d)

D) To specify the value the function should return.

31.

What method combines the text of two strings and returns a new string?

a)

A) append()

b)

B) merge()

c)

C) concat()

d)

D) combine()

32.

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)

A) null

b)

B) undefined

c)

C) Empty string

d)

D) Error

33.

What method returns a new array containing all elements of the calling array for which the provided filtering function returns true?

a)

A) filter()

b)

B) map()

c)

C) reduce()

d)

D) forEach()

34.

In JavaScript, functions are first-class citizens. What does this mean?

a)

A) Functions are treated as objects.

b)

B) Functions can only be defined at the top of the code.

c)

C) Functions can only be used in specific contexts.

d)

D) Functions can't be passed as arguments to other functions.

35.

The trim() method removes whitespace from both ends of a string. Which of the following whitespace characters does it remove?

a)

A) Space

b)

B) Tab

c)

C) Newline

d)

D) All of the above

36.

Which of the following methods does not modify the original array?

a)

A) splice()

b)

B) slice()

c)

C) concat()

d)

D) map()

37.

Which of the following is true about arrow functions in JavaScript?

a)

Arrow functions do not have their own this context.

b)

Arrow functions cannot accept arguments.

c)

Arrow functions are always anonymous.

d)

Arrow functions cannot be used as callbacks.

38.

What method adds one or more elements to the end of an array and returns the new length of the array?

a)

A) push()

b)

B) pop()

c)

C) shift()

d)

D) unshift()

39.

Which of the following methods does not modify the original string but returns a new string?

a)

A) substr()

b)

B) substring()

c)

C) slice()

d)

D) splice()

40.

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)

A) Use a regular expression with the global flag (/g).

b)

B) Pass an additional parameter specifying 'all'.

c)

C) There's no way to replace all occurrences.

d)

D) Use the replaceAll() method.

41.

What method is used to split a string into an array of substrings, and returns the new array?

a)

A) split()

b)

B) divide()

c)

C) slice()

d)

D) break()

42.

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)

A) slice()

b)

B) splice()

c)

C) concat()

d)

D) copyWithin()

43.

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)

A) map()

b)

B) filter()

c)

C) reduce()

d)

D) forEach()

44.

What does the arguments object represent in a JavaScript function?

a)

A) It represents the parameters passed to the function.

b)

B) It represents the function's return value.

c)

C) It represents an array-like object containing the arguments passed to the function.

d)

D) It represents the function's prototype.

45.

Which method is used to remove the last element from an array and return that element?

a)

A) pop()

b)

B) shift()

c)

C) unshift()

d)

D) push()

46.

What does the typeof operator return when used on an array?

a)

A) "array"

b)

B) "object"

c)

C) "undefined"

d)

D) "number"

47.

Some of JavaScript assignment operator

a)

*+

b)

/=

c)

%=

d)

**=

48.

Execute the function named myFunction

function myFunction() {

alert("Hello World!");

}

(a)  

49.

A function declaration consists of the following

a)

The function keyword.

b)

The name of the function, or its identifier, followed by parentheses.

c)

A function body enclosed in the function’s curly brackets, { }.

d)

a backtick inside console.log()

50.

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?

a)

Go to sleep

b)

Let's play some games

c)

Uncaught SyntaxError: Unexpected identifier

d)

Uncaught SyntaxError: missing ) after argument list

51.

Higher-order functions are functions that accept other functions as arguments and/or return functions as output.

a)

True

b)

False

52.

The name:values pairs in JavaScript objects are called

a)

properties

b)

methods

c)

value

d)

variable

53.

alert "Jae" by extracting information from the object

const person = {

firstName: "Jae",

lastName: "Won"

};


alert( (a)   );

54.

change this to an array

let animal1 = "dog";

let animal2 = "cat";

let animal3 = "bird";

(a)  

55.

if (time < 10) {

greeting = "Good morning";

} else if (time < 20) {

greeting = "Good day";

} else {

greeting = "Good evening";

}

What would be printed from this code?

a)

Good day

b)

Good morning

c)

Good evening

d)

Uncaught ReferenceError: time is not defined

56.

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;

a)

Tomatoes are $0.49

b)

Limes are $1.49

c)

Papayas are $1.29

d)

Invalid item

57.

When you invoke a function, which of the following is true about generalization?

a)
Generalization is the process of invoking a function
b)
Generalization is the process of making something more general
c)
Generalization is the process of defining a function
d)

The function can accept multiple arguments and provide more generalized functionality.s

58.

What does a JavaScript function typically return if there is no explicit return statement?

a)

undefined

b)

null

c)

The value of the last executed statement inside the function.

d)

An error.

59.

Which of the following is an example of a function expression?

a)
function myFunction() {}
b)
myFunction() {}
c)

const myFunction = function() {}

d)
myFunction: function() {}
60.

What is an anonymous function in JavaScript?

a)
A function with no name
b)
A function that is not declared
c)
A function that returns null
d)
A function that cannot be called
61.

What does an Immediately Invoked Function Expression (IIFE) do?

a)
It allows to run a function as soon as it is defined
b)
It allows to delay the execution of a function
c)
It allows to repeat the execution of a function
d)
It allows to stop the execution of a function
62.

In JavaScript, what is the scope of a variable declared inside a function (function-level scope)?

a)
Global scope
b)
Block scope
c)
Function-level scope
d)
None of the above
63.

Which of the following is a correct syntax for an arrow function in JavaScript?

a)
function() => {}
b)
() -> {}
c)
function => {}
d)

const myFunction = () => {}

64.

function aaa() {

return

{

test: 1

};

}

alert(typeof aaa());


What does the above alert?

a)

User defined

b)

Object

c)

Number

d)

function

65.

Inside which HTML element do we put the JavaScript?

a)

<script>

b)

<javaScript>

c)

<js>

d)

<scripting>

66.

Where is the correct place to insert a JavaScript?

a)

The <head> section

b)

The <body> section

c)

both the <head> and <body> section are correct

67.

Which is the correct way to write a JavaScript array?

a)

var txt = new Array(1:"arr",2:"kim",3:"jim")

b)

var txt = new Array:1=(" arr ")2=("kim")3=("jim")

c)

var txt = new Array("arr ","kim","jim")

d)

var txt = new Array=" arr ","kim","jim"

68.

Which of the following is correct to write “Hello World” on the web page?

a)

System.out.println(“Hello World”)

b)

print(“Hello World”)

c)

document.write(“Hello World”)

d)

response.write(“Hello World”)

69.

Which of the following is not a valid JavaScript variable name?

a)

2java

b)

_java_and_ java _names

c)

javaandjava

d)

None of the above

70.

Why so Java and JavaScript have similar name?

a)

Java Script is a stripped-down version of Java

b)

The syntax of JavaScript is loosely based on Java syntax

c)

They both support Object Oriented Programming

d)

None of the above

71.

How do you create a function in JavaScript?

a)

function myFunction()

b)

function:myFunction()

c)

function=myFunction()

72.

How do you call a function named "myFunction"?

a)

call function myFunction()

b)

call myFunction()

c)

myFunction()

73.

How can you add a comment in a JavaScript?

a)

//This is comment

b)

'This is comment

c)

<!-- This is comment-->

74.

How do you declare a JavaScript variable?

a)

v carName

b)

variable carName

c)

var carName

75.

Is JavaScript case-sensitive?

a)

yes

b)

No