0% found this document useful (0 votes)
14 views

Advanced Constructs Advanced Function Concepts

The document contains assignment questions and solutions related to advanced function concepts, including problems on subarrays and majority elements in arrays. It also includes multiple-choice questions (MCQs) about closures and immediately invoked function expressions (IIFE). Solutions for the assignment problems are provided, demonstrating the implementation of the required functions.

Uploaded by

jj491bendreacc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Advanced Constructs Advanced Function Concepts

The document contains assignment questions and solutions related to advanced function concepts, including problems on subarrays and majority elements in arrays. It also includes multiple-choice questions (MCQs) about closures and immediately invoked function expressions (IIFE). Solutions for the assignment problems are provided, demonstrating the implementation of the required functions.

Uploaded by

jj491bendreacc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Advanced Constructs: Advanced

Function Concepts

Assignment Questions
Assignment Questions

Basic Constructs: Intro Functions and Scopes

Assignment Questions

Problem 1:

A subarray of an array is defined as the contiguous cross section of the array. Example: [1,2,3] has the

following subarrays: [1],[2],[3],[1,2],[2,3].[1,2,3] Given an array print all the subarrays of the given array

Example-1

Input: [1,2,3]

Output: [1],[2],[3],[1,2],[2,3].[1,2,3]

Problem 2:

You have an array of n elements. Your job is to find the element that is in majority.

Any element whose count is greater than n/2 will be considered as a majority element.

Example-1:

Input: [3,1,3,3,2]

Output: 3

MCQ (Day 6)

1. How does any function can create closure?

A. Value changes whenever the document reloads

B. A reference is returned to a parent scopes

C. Doesn't return anything

D. None of the above


2. Which concept does this code illustrate?

function makeAdder(x) {

return function (y) {

return x + y;

};

var addFive = makeAdder(5);

console.log(addFive(3));

A. overloading

B. closure

C. currying

D. overriding

Assignment Questions

3. Which statement represents the starting code converted to an IIFE?

A. function() { console.log('lorem ipsum'); }()();

B. function() { console.log('lorem ipsum'); }();

C. (function() { console.log('lorem ipsum'); })();

D. None of these

4. What will this code print?

var x = 1

var x1 = function () {

console.log(x);

};

var y2 = function () {

var x = 2;

x1();

};

y2();

A. 1

B. 2

C. error

D. undefined

5. What will be the output?

var x = [ 1, 2, 3, 4, 5, 6, 7 ]

const f = (arr) => {

return arr.map((x) => x + 3).filter((x) => x < 7);

};

console.log(f(x));

A. [4,5,6,7,8,9,10]

B. [4,5,6,7]

C. [1,2,3,4,5,6]

D. [4,5,6]

Advanced Constructs: Advanced

Function Concepts

Assignment Solutions
Assignment Solutions

Basic Constructs: Intro Functions and Scopes

Assignment Solutions
Solution 1:

function subArray(n) {

for (let i = 0; i < n; i++) {

for (let j = i; j < n; j++) {

for (let k = i; k <= j; k++)

document.write(arr[k] + " ");


document.write("</br>");

let arr = [1, 2, 3];

subArray(arr.length);

Solution 2:

function findMajority(arr, n)

let maxCount = 0;

let index = -1;

for(let i = 0; i < n; i++)

let count = 0;

for(let j = 0; j < n; j++)

if (arr[i] == arr[j])

count++;

if (count > maxCount)

maxCount = count;

index = i;

if (maxCount > n / 2)

document.write(arr[index]);

else

document.write("No Majority Element");

let arr = [3,1,3,3,2];

let n = arr.length;

findMajority(arr, n);

Assignment Solutions

MCQ Answers

You might also like