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

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

Multiple Choice Questions (MCQs)


Q.1. Which of the following statements is true about JavaScript functions in Java?
(a) JavaScript functions can be directly used in Java programs without any modification.
(b) Java functions can be used in JavaScript programs without any modification.
(c) JavaScript functions and Java functions are completely different and cannot be used interchangeably.
(d) JavaScript functions can only be used in Java with the help of third-party libraries.

Ans. (c)

Q.2. What is the correct syntax to define a JavaScript function in Java?
(a) function myFunction() { }
(b) def myFunction() { }
(c) void myFunction() { }
(d) function void myFunction() { }

Ans. (a)

Q.3. Which keyword is used to call a JavaScript function from Java?
(a) call
(b) execute
(c) invoke
(d) function

Ans. (c)

Q.4. Which of the following is an example of a JavaScript function that calculates the square of a number in Java?
(a) public int calculateSquare(int num) { return num * num; }
(b) function calculateSquare(num) { return num * num; }
(c) def calculateSquare(num): return num * num
(d) private double calculateSquare(double num) { return num * num; }

Ans. (b)

Q.5. What is the purpose of the JavaScript function toString() in Java?
(a) It converts a JavaScript function to a string representation.
(b) It converts a Java function to a JavaScript function.
(c) It converts a Java object to a JavaScript object.
(d) It converts a JavaScript object to a Java object.

Ans. (a)

High Order Thinking Questions (HOTS)


Q.1. Write a Java code snippet to call a JavaScript function named "calculateSum" with two arguments: num1 and num2. Assume the function returns the sum of the two numbers.

import javax.script.*;


public class JavaScriptFunctionExample {

    public static void main(String[] args) throws ScriptException {

        ScriptEngineManager manager = new ScriptEngineManager();

        ScriptEngine engine = manager.getEngineByName("JavaScript");

        engine.eval("function calculateSum(num1, num2) { return num1 + num2; }");

        Object result = engine.eval("calculateSum(5, 3);");

        System.out.println(result);

    }

}

Q.2. Explain the concept of function overloading in the context of using JavaScript functions in Java.

Function overloading in the context of using JavaScript functions in Java refers to defining multiple JavaScript functions with the same name but different parameters. The Java program can then invoke the appropriate JavaScript function based on the number and types of arguments passed. This allows for flexibility and code reusability when working with JavaScript functions in Java.

Q.3. Can you directly pass a Java object as an argument to a JavaScript function? Justify your answer.

No, you cannot directly pass a Java object as an argument to a JavaScript function. Java objects and JavaScript objects have different internal representations and data types. To pass a Java object to a JavaScript function, you would need to convert it to a JavaScript object using appropriate conversions or serialization techniques.

Q.4. Discuss the potential advantages and disadvantages of using JavaScript functions in Java programs.

Advantages of using JavaScript functions in Java programs:

  • Access to additional functionality and libraries provided by JavaScript.
  • Code reuse by leveraging existing JavaScript code.
  • Enhanced flexibility in handling dynamic behaviors.

Disadvantages of using JavaScript functions in Java programs:

  • Increased complexity due to the need for integrating JavaScript engines and handling data type conversions.
  • Potential performance impact when invoking JavaScript functions from Java.
  • Limited compatibility and potential versioning issues between different JavaScript engines and Java versions.

Q.5. Write a Java code snippet to define a JavaScript function named "greet" that takes a name as an argument and returns a greeting message. Invoke the function and print the result.

import javax.script.*;


public class JavaScriptFunctionExample {

    public static void main(String[] args) throws ScriptException {

        ScriptEngineManager manager = new ScriptEngineManager();

        ScriptEngine engine = manager.getEngineByName("JavaScript");

        engine.eval("function greet(name) { return 'Hello, ' + name + '!'; }");

        Object result = engine.eval("greet('John');");

        System.out.println(result);

    }

}

Fill in the Blanks


1. The __________ keyword is used to define a JavaScript function in Java.

Ans. function

2. The __________ keyword is used to call a JavaScript function from Java.

Ans. invoke

3. The ________ method is used to convert a JavaScript object to a Java object.

Ans. toJavaObject

4. The ________ method is used to convert a Java object to a JavaScript object.

Ans. toJavaScript

5. The ____________ function in JavaScript is used to convert a value to a string representation.

Ans. toString

True or False


1. JavaScript functions can be directly used in Java programs without any modification.

Ans. False

2. The syntax to define a JavaScript function in Java is the same as in JavaScript.

Ans. True

3. JavaScript functions in Java can only be invoked asynchronously.

Ans. False

4. The toString() method in Java is used to convert a JavaScript object to a string representation.

Ans. True

5. Function overloading is not supported when using JavaScript functions in Java.

Ans. False

Hands-On Questions


Q.1. Write a Java code snippet to define a JavaScript function named "calculateAverage" that takes an array of numbers as an argument and returns the average of the numbers. Invoke the function with an example array and print the result.

import javax.script.*;


public class JavaScriptFunctionExample {

    public static void main(String[] args) throws ScriptException {

        ScriptEngineManager manager = new ScriptEngineManager();

        ScriptEngine engine = manager.getEngineByName("JavaScript");

        engine.eval("function calculateAverage(numbers) { var sum = numbers.reduce((a, b) => a + b, 0); return sum / numbers.length; }");

        Object result = engine.eval("calculateAverage([2, 4, 6, 8, 10]);");

        System.out.println(result);

    }

}

Q.2. Implement a Java program that demonstrates the use of a JavaScript function named "getRandomNumber" that returns a random number between 1 and 10. Print five random numbers generated using this function.

import javax.script.*;


public class JavaScriptFunctionExample {

    public static void main(String[] args) throws ScriptException {

        ScriptEngineManager manager = new ScriptEngineManager();

        ScriptEngine engine = manager.getEngineByName("JavaScript");

        engine.eval("function getRandomNumber() { return Math.floor(Math.random() * 10) + 1; }");

        for (int i = 0; i < 5; i++) {

            Object result = engine.eval("getRandomNumber();");

            System.out.println(result);

        }

    }

}

Q.3. Write a Java code snippet to define a JavaScript function named "isEven" that takes a number as an argument and returns true if the number is even, and false otherwise. Invoke the function with an example number and print the result.

import javax.script.*;


public class JavaScriptFunctionExample {

    public static void main(String[] args) throws ScriptException {

        ScriptEngineManager manager = new ScriptEngineManager();

        ScriptEngine engine = manager.getEngineByName("JavaScript");

        engine.eval("function isEven(num) { return num % 2 === 0; }");

        Object result = engine.eval("isEven(6);");

        System.out.println(result);

    }

}

Q.4. Create a Java program that uses a JavaScript function named "reverseString" to reverse a given string. Prompt the user to enter a string and print the reversed string using the JavaScript function.

import javax.script.*;


public class JavaScriptFunctionExample {

    public static void main(String[] args) throws ScriptException {

        ScriptEngineManager manager = new ScriptEngineManager();

        ScriptEngine engine = manager.getEngineByName("JavaScript");

        engine.eval("function reverseString(str) { return str.split('').reverse().join(''); }");

        String userInput = "Hello World";

        Object result = engine.eval("reverseString('" + userInput + "');");

        System.out.println(result);

    }

}

Q.5. Write a Java code snippet to define a JavaScript function named "calculateFactorial" that takes a positive integer as an argument and returns its factorial. Invoke the function with an example number and print the result.

import javax.script.*;


public class JavaScriptFunctionExample {

    public static void main(String[] args) throws ScriptException {

        ScriptEngineManager manager = new ScriptEngineManager();

        ScriptEngine engine = manager.getEngineByName("JavaScript");

        engine.eval("function calculateFactorial(num) { if (num <= 1) return 1; return num * calculateFactorial(num - 1); }");

        Object result = engine.eval("calculateFactorial(5);");

        System.out.println(result);

    }

}

The document Assignment: JavaScript Functions | 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

Objective type Questions

,

Sample Paper

,

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

,

pdf

,

mock tests for examination

,

ppt

,

Summary

,

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

,

study material

,

MCQs

,

practice quizzes

,

Exam

,

Important questions

,

Viva Questions

,

Free

,

past year papers

,

video lectures

,

Previous Year Questions with Solutions

,

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

,

Semester Notes

,

Extra Questions

,

shortcuts and tricks

;