Home / Past papers / MAD2 Quiz 2 / this paper
Consider the below javascript program. What will be the output of the program, if executed in a REPL environment?```javascript
var first = 30
const obj1 = {
first: 50,
second: () => {
console.log("Value:", this.first);
}
}
const obj2 = {
first: 80,
second: function () {
console.log("Value:", this.first);
this.second();
}
}
obj2.second.call(obj1);
```
Value: 80 Value: 50

Value: 80 Value: 30

Value: 50 Value: 50

Value: 50 Value: 30

Value: 30 Value: 30
Consider the below JavaScript code. What will be the output of the above program, assuming the value of the variable “some_value” is less than 18?```javascript
function isValidAge(age) {
return !(age > 18)
}
const validAgeParams = {
voter_id: 123,
constituency: 'New Delhi'
}
const teenageParams = {
age_criteria: '<18',
}
let some_value;
console.log(
isValidAge(some_value) ? {
'name': 'ABC',
...(!isValidAge(19) && validAgeParams)
} : {
'name': 'XYZ',
...(isValidAge(19) && teenageParams)
}
);
```
```
{
name: 'ABC'
}
```
```json
{
"name": "ABC",
"voter_id": 123,
"constituency": "New Delhi"
}
```
```
{
name: 'XYZ'
}
```
```
{
name: 'ABC',
age_criteria: '<18'
}
```
Consider the below JavaScript program. What will be the output of the above program, if executed?```javascript
const promise1 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('First promise resolved');
}, 1000);
});
promise1
.then((result) => {
console.log(result);
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('Second promise resolved');
}, 3000);
});
})
.then((result) => {
console.log(result);
return new Promise((resolve, reject) => {
setTimeout(() => {
reject('Third promise resolved');
}, 3000);
});
})
.then((result) => {
console.log(result);
})
.catch((error) => {
console.log("Promise chain interrupted. Reason:", error);
});
```
First promise resolved Second promise resolved Third promise Resolved

The program will crash without showing any output

First promise resolved Second promise resolved Promise chain interrupted. Reason: Third promise resolved

First promise resolved Second promise resolved Promise chain interrupted. Reason: Third promise resolved Third promise Resolved

First promise resolved Second promise resolved
Consider the following Script embedded in an HTML document. What will be the output on console, if the HTML document is rendered using a browser?```javascript
let Obj1 = {model: 'AIR101', brand: 'AIRBUS', type: 'Passenger'}
let Obj2 = {__proto__: Obj1, wheels: '36', engines: 4}
console.log(Obj2.model)
console.log(Object.keys(Obj2).length)
console.log(Obj1.engines)
```
``` AIR101 2 undefined ```

``` undefined 2 4 ```

``` AIR101 5 undefined ```

``` AIR101 undefined undefined ```

Fill in property_name & definition, which can be used in Vuex Store to update the "best_food" state variable with the objects of those food items which have their calorific value less than 300.
```javascript
const store = new Vuex.Store({
state: {
foods: [
{ "name": "Sandwich", "calories": 100 },
{ "name": "Pizza", "calories": 1250 },
{ "name": "Chips", "calories": 700 }
],
best_food: []
},
=property_name=: {
get_best_food: function(state) {
======== definition ========
}
}
})
```
property_name : actions,
definition:
```
state.foods.forEach(food => {
if (food.calories < 300){
context.best_food.push(food)
}
});
```
property_name : actions,
definition:
```javascript
state.foods.forEach(food => {
if (food.calories < 300){
state.best_food.push(food)
}
});
```
property_name : mutations,
definition:
```javascript
state.foods.forEach(food => {
if (food.calories < 300){
context.best_food.push(element)
}
});
```
property_name : mutations,
definition:
```javascript
state.foods.forEach(food => {
if (food.calories < 300){
state.best_food.push(element)
}
});
```
Consider the following JavaScript code snippet. What will be the values of 'storedUsername', 'removedUsername', and 'clearedStorage' after the execution of the above code snippets?```javascript
// Code Snippet 1
sessionStorage.setItem('username', 'course_user');
let storedUsername = sessionStorage.getItem('username');
// Code Snippet 2
sessionStorage.removeItem('username');
let removedUsername = sessionStorage.getItem('username');
// Code Snippet 3
sessionStorage.clear();
let clearedStorage = sessionStorage.username;
```
``` storedUsername: 'course_user', removedUsername: null, clearedStorage: null ```

``` storedUsername: 'course_user', removedUsername: null, clearedStorage: undefined ```

``` storedUsername: 'course_user', removedUsername: undefined, clearedStorage: null ```

``` storedUsername: 'course_user', removedUsername: undefined, clearedStorage: undefined ```
Consider the below JavaScript program. Considering the asynchronous nature of promises in the above JavaScript program, what is the minimum time (in seconds) it will take for the entire execution to complete? ```javascript
const promise1 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('First promise resolved');
}, 1000);
});
promise1
.then((result) => {
console.log(result);
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('Second promise resolved');
}, 3000);
});
})
.then((result) => {
console.log(result);
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('Third promise resolved');
}, 3000);
});
})
.then((result) => {
console.log(result);
})
.catch((error) => {
console.log("Promise chain interrupted. Reason:", error);
});
```
Consider the following Vue application with markup “index.html” and JavaScript file “app.js”. Suppose you open “index.html” file in a browser, and type the text “App Dev” in the text box shown (after removing the previous text, if any), and hard refresh the page twice, without clicking anywhere. What will be the value shown in the text box, and the “age” placeholder, respectively?index.html:
```html
<div id = "app">
<input v-model = "course" @input = "do_something">
<p> {{role}} </p>
</div>
```
app.js:
```javascript
new Vue({
el : "#app",
data : {
course : "#app",
role : "user",
},
mounted () {
try {
this.course = localStorage.getItem("course").split(" ")[0];
this.role = localStorage.getItem("course").split(" ")[1];
localStorage.setItem("course",
localStorage.getItem("course").split(" ")[0] + " " +
this.course);
}
catch {
this.course = "MAD_II";
this.role = "admin";
}
},
methods : {
do_something() {
localStorage.setItem("course", this.course);
localStorage.setItem("role", this.role);
}
}
})
```
```javascript
async actionFunction ( context ) {}
```
```javascript
async actionFunction ( { state, commit }, payload ) { }
```
```javascript
async actionFunction ( context, payload1, payload2 ) { }
```
```javascript
async actionFunction ( {context, commit} ) {}
```
Consider the below Vue class binding. Which of the following statements is/are true, assuming both code 1 and code 2 are part of app object? Script.js:
```javascript
var app = new Vue({
el: '#app',
data: {
classObj: {
is_active: true,
completed: false
},
myClass: 'is_active',
status: true
}
})
```
Code 1:
```html
<div :class="classObj"></div>
```
Code 2:
```html
<div :class="[status ? myClass : '']"></div>
```
Consider the below Vue component template and script definitions that use Vue router. Assuming that the corresponding routes are properly configured, what does this component structure accomplish?Template:
```html
<template>
<div>
<h1>{{ pageTitle }}</h1>
<router-link to="/home" v-if="showHomeLink">Home</router-link>
<router-link to="/about" v-if="showAboutLink">About</router-link>
<router-view></router-view>
</div>
</template>
```
Script:
```html
<script>
export default {
name: 'App',
data() {
return {
pageTitle: 'Vue Router Demo',
showHomeLink: true,
showAboutLink: false,
};
},
watch: {
'$route.path'() {
if (this.$route.path === '/home') {
this.showAboutLink = true;
this.showHomeLink = false;
this.pageTitle = 'Home Page';
} else if (this.$route.path === '/about') {
this.showAboutLink = false;
this.showHomeLink = true;
this.pageTitle = 'About Page';
} else {
this.pageTitle = 'Vue Router Demo';
}
},
},
};
</script>
```
It displays a web page with a heading title "Vue Router Demo" and two navigation links to "Home" and "About."

It dynamically updates the heading title based on the route and conditionally shows navigation links to "Home" and "About."

It has a bug and will throw a runtime error.

The heading title is populated with the value "Vue Router Demo" when the user opens the app for the first time.
Consider the below application with markup “index.html” and JavaScript file “app.js”. Suppose the application is running on “http://127.0.0.1:8080”, select the correct option(s)?Filename: index.html
```html
<body>
<div id="app"></div>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router@3.0.0/dist/vue-router.js"></script>
<script src="app.js"></script>
</body>
```
Filename: app.js
```javascript
const records = {
1: {albums: "Heartrisers", songs: 20, genre: 4},
2: {albums: "Pianobind", songs: 13, genre: 5}
}
const notFound = { template: `<h1>Unknown record!</h1>` }
const myRecords = {
template: `<h1>{{record.albums}} has {{record.songs}} songs across
{{record.genre}} genres.</h1>`,
data() {
return {record: records[this.$route.params.id]}
},
}
const router = new VueRouter({
routes: [
{ path: '/records/:id', component: myRecords },
{ path: '*', component: notFound },
],
})
new Vue({
el: '#app',
template: '<div><router-view /></router-view></div>',
router,
})
```
For the endpoint, http://127.0.0.1:8080/records/1, The browser will render: **Heartrisers has 20 songs across 4 genres.**

For the endpoint, http://127.0.0.1:8080/#/records/2, The browser will render: **Pianobind has 13 songs across 5 genres.**

For the endpoint, http://127.0.0.1:8080/#/records, The browser will render: **Unknown record!**

For the endpoint, http://127.0.0.1:8080/#/records/3, The browser will render: **Unknown record!**