MSBTE JavaScript Questions and Answers
Attempt any FIVE of the following: (Only 10 marks)
a) List any four Bitwise operators with its meaning.
| Operator | Meaning |
|----------|------------------------|
|& | Bitwise AND |
|| | Bitwise OR |
|^ | Bitwise XOR |
|~ | Bitwise NOT |
b) Give the Syntax of do-while statement.
do {
// Code to be executed
} while (condition);
c) Develop a program for embedding external JavaScript to the HTML file.
HTML File (index.html):
<!DOCTYPE html>
<html>
<head>
<title>External JS Example</title>
<script src="script.js"></script>
</head>
<body>
<h1>Hello from HTML</h1>
</body>
</html>
JavaScript File (script.js):
alert("Hello from External JavaScript!");
d) Develop a JavaScript program to add any two elements at the end.
let arr = [1, 2, 3];
arr.push(4, 5);
console.log(arr); // [1, 2, 3, 4, 5]
e) Compare join() and concat() methods of an array object.
| Feature | join() | concat() |
|--------------|-----------------------------|-------------------------------|
| Purpose | Joins array into string | Combines arrays |
| Return type | Returns a string | Returns a new array |
f) List any four attributes of <form> tag with their significance.
| Attribute | Significance |
|----------|----------------------------------|
| action | URL to send form data |
| method | HTTP method (GET or POST) |
| name | Name of the form |
| target | Where to display response |
g) Give the syntax of any two timer functions.
setTimeout(function, milliseconds);
setInterval(function, milliseconds);
Attempt any THREE of the following: (Only for 12 marks)
a) Explain "switch-case" statement with syntax and example.
switch(expression) {
case value1:
// code
break;
case value2:
// code
break;
default:
// code
Example:
let day = 3;
switch(day) {
case 1: console.log("Monday"); break;
case 2: console.log("Tuesday"); break;
case 3: console.log("Wednesday"); break;
default: console.log("Invalid day");
b) Describe getter and setter with example.
let person = {
firstName: "John",
lastName: "Doe",
get fullName() { return this.firstName + " " + this.lastName; },
set fullName(name) {
let parts = name.split(" ");
this.firstName = parts[0];
this.lastName = parts[1];
};
console.log(person.fullName);
person.fullName = "Jane Smith";
console.log(person.fullName);
c) JavaScript program to check if a number is prime.
let num = 7;
let isPrime = true;
if (num <= 1) isPrime = false;
else {
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) { isPrime = false; break; }
console.log(isPrime ? "Prime" : "Not Prime");
d) Methods to convert string to number.
Number("123"); // 123
parseInt("123.45"); // 123
parseFloat("123.45"); // 123.45
+"456"; // 456
Attempt any THREE of the following: (Only for 12 marks)
a) Use of charCodeAt() and fromCharCode() with example.
"A".charCodeAt(0); // 65
String.fromCharCode(65); // "A"
b) Defining and calling a function with arguments.
function add(a, b) { return a + b; }
let result = add(5, 10);
console.log(result); // 15
c) Sort an array of 5 integers in descending order.
let numbers = [10, 5, 25, 1, 15];
numbers.sort(function(a, b) { return b - a; });
console.log(numbers); // [25, 15, 10, 5, 1]
d) Features of JavaScript:
1. Lightweight
2. Interpreted
3. Object-Based
4. Event-Driven
5. Platform Independent
6. Dynamic Typing
7. Functional Support
8. Browser Control
Attempt any THREE of the following: (Only for 12 marks)
a) Non-explicit and explicit quantifiers in regex.
Non-Explicit: + (one or more), * (zero or more), ? (zero or one)
Explicit: {n}, {n,}, {n,m}
Example:
/a+/ matches "aaa"
/a{3}/ matches "aaa"
b) Four JavaScript frameworks:
1. React.js ? UI library
2. Angular ? Full MVC framework
3. Vue.js ? Lightweight and reactive
4. Node.js ? Server-side JS runtime
c) Disable right-click on webpage.
document.addEventListener("contextmenu", function(e){
e.preventDefault();
alert("Right-click disabled!");
});
d) Call a function in child window.
Parent (index.html):
let child = window.open("child.html");
setTimeout(function() { child.childFunction(); }, 1000);
Child (child.html):
function childFunction() {
alert("Hello from child");
e) Techniques to protect webpage:
- Disable right-click
- Email obfuscation
- Use CAPTCHA
- JavaScript obfuscation
Example (conceal email):
<script>
let user = "info", domain = "example.com";
document.write("<a href='mailto:" + user + "@" + domain + "'>" + user + "@" + domain + "</a>");
</script>