Home / Past papers / MAD2 Quiz 2 / this paper

Consider the below JavaScript program.
```javascript
const first = "Hello 3";
const obj1 = {
first: "Hello 1",
secondFunc: function () {
console.log(this.first);
}
}
const obj2 = {
first: "Hello 2",
secondFunc: function () {
console.log(this.first);
this.secondFunc();
}
}
obj1.secondFunc.apply(obj2, [2, 5, 9]);
```
What will be the output of the above program, if executed?
Consider the following Vue application with markup "index.html" and JavaScript file "app.js".
index.html:
```html
<div id = "app">
<my-comp>
<template #header>
This is header content
</template>
<template #footer>
This is footer content
</template>
<p> This is some content </p>
</my-comp>
</div>
<script src = "app.js"> </script>
```
app.js:
```javascript
Vue.component("myComp", {
template : `<div>
<p>
<slot></slot>
</p>
<p>
<slot name="footer"></slot>
</p>
</div>`,
})
const app = new Vue({
el : "#app",
})
```
Suppose you open the "index.html" file in a browser. What will be rendered by the browser?
Consider the following application with markup index.html and script app.js.
index.html:
```html
<body>
<div id="app">
<router-view></router-view>
</div>
</body>
```
app.js:
```javascript
const Header = {
template: `<div>This is header <router-view /> </div>`,
};
const Error = {
template: `<div> 404 Page not found</div>`,
};
const Profile = {
template: `<div> Welcome to profile page</div>`,
};
const routes = [
{
path: '/',
component: Header,
children: [
{ path: 'profile', component: Profile },
{ path: '*', component: Error },
],
},
];
const router = new VueRouter({
routes,
});
new Vue({
el: '#app',
router,
});
```
Suppose the application is running on http://127.0.0.1:8080. What will be rendered in the browser?
Consider the below JavaScript program.
```javascript
new Promise(((error, pass) => {
if (5 === "5") error(5)
else pass(8)
})).
then(d => {
console.log("Checkpoint 4", d);
throw new Error(20);
return d * 5;
})
.then(d => {
console.log("Checkpoint 2", d);
return d;
}, d => {
console.log("Checkpoint 5", d.message);
return d.message * 2;
}).catch(e => {
console.log("Checkpoint 3", e.message);
return e.message * 2;
}).finally(d => {
console.log("Checkpoint 1", d);
return d * 5;
}).then(d => {
console.log("Checkpoint 6", d);
return d * 5;
})
```
What will be the output of the above program?
Consider the following Vue application with markup "index.html" and JavaScript file "app.js".
index.html:
```html
<div id = "app">
<input v-model = "phrase" @input = "compute_marks">
<p>{{marks}}</p>
</div>
<script scr = "app.js"></script>
```
app.js:
```javascript
new Vue({
el : "#app",
data : {
phrase : "AppDev",
marks : 50,
},
mounted() {
this.phrase = "Bravo";
this.marks = 50;
if (localStorage.marks) {
this.phrase += "2";
this.marks = parseInt(localStorage.marks) + 20;
}
else {
this.phrase += "1";
this.marks += 10;
}
},
methods : {
compute_marks() {
localStorage.setItem("phrase", this.phrase);
localStorage.setItem("marks", this.marks + 10);
}
}
})
```
Suppose you open "index.html" file in a browser, and type the text "Hurray" in the text box shown (after removing the previous text, if any), and hard refresh the page 7 times, without clicking anywhere. What will be the value shown in the text box, and the "marks" placeholder, respectively?
Tip: The "oninput" event occurs immediately after the value of an element has changed.
Consider the below JavaScript program, and predict the output, if executed.
```javascript
async function exam () {
let x = await new Promise((res, rej) => res(2 || 0));
console.log("Statement 1");
let y = await new Promise((res, rej) => res(0 && 2));
console.log("Statement 2", "gives result as", x + y);
}
exam();
console.log("Statement 3");
```
Consider the following Vue application with markup index.html and JavaScript file app.js.
File: index.html
```html
<div id="app"></div>
<script src="script.js"></script>
```
File: script.js
```javascript
new Vue({
el: '#app',
template: `<div>
Status: {{message}}<br>
ETA: {{ETA}}
</div>`,
data: {
message: "",
ETA: 10,
},
beforeCreate() {
this.message += "Creating,"
this.ETA -= 1
},
created() {
this.message += "Created,"
this.ETA -= 1
},
beforeMount() {
this.message += "Mounting,"
this.ETA -= 1
},
mounted() {
this.message += "Mounted"
this.ETA -= 1
}
})
```
Suppose the application is running on http://127.0.0.1:8080. What will be rendered by the browser?
Consider the following JavaScript code. What will be the output of the above code snippet on the console of the browser?```javascript
const library = {
name: "Central Library",
books: [
{ title: "Book A", author: "Author A" },
{ title: "Book B", author: "Author B" },
{ title: "Book C", author: "Author C" },
],
location: "IIT Madras",
};
let propertyList = "";
let bookDetails = "";
for (let key in library) {
if (key === "books") {
for (let book of library[key]) {
bookDetails += `${book.title} by ${book.author}, `;
}
} else {
propertyList += key + " ";
}
}
console.log(propertyList);
console.log(bookDetails);
```
``` name books location Book A by Author A, Book B by Author B, Book C by Author C, ```

``` Central Library IIT Madras Book A by Author A, Book B by Author B, Book C by Author C, ```

``` name location Book A by Author A, Book B by Author B, Book C by Author C, ```

Central IIT Book A by Author A, Book B by Author B, Book C by Author C,

Consider the following HTML snippet that uses Vue 2 using CDN in the script.js
file
Filename: index.html
```html
<div id="app">
<p>Counter: {{ counter }}</p>
<p>Computed Value: {{ computedValue }}</p>
</div>
```
Filename: script.js
```javascript
new Vue({
el: "#app",
data: {
counter: 0,
},
computed: {
computedValue() {
return this.counter * 2;
},
},
watch: {
counter(newVal, oldVal) {
console.log(`Counter changed from ${oldVal} to ${newVal}`);
},
},
mounted() {
setInterval(() => {
this.counter++;
}, 1000);
},
});
```
What will be displayed in the console?
Suppose you are organizing a felicitation program for the students with exemplary performance in the BS program. You have the scorecard of all the students. The administration has decided to shortlist the students based on the following 2 criteria:
1. The student must have an "S" in their MAD II project
2. The overall CGPA of the student must be greater than 8
Fill in the `code1` & `code2`, which can be used in Vuex Store to update the "awardees" state variable with the objects of those students who satisfy the above-mentioned criteria. The objects to be inserted in the "awardees" state variable must only have the student ID and a random token generated in the "send_task" action function.
```javascript
const store = new Vuex.Store({
state: {
bs_program: [{
student_id: 'stu1001',
mad2_grade: 'S',
cgpa: 8.35
}, {
student_id: 'stu1002',
mad2_grade: 'A',
cgpa: 8.78
}, {
student_id: 'stu1003',
mad2_grade: 'S',
cgpa: 9.1
}, {
student_id: 'stu1004',
mad2_grade: 'S',
cgpa: 7.44
}, ],
awardees: []
},
mutations: {
update_final(state, payload) {
for (student of state.bs_program)
code2
}
},
actions: {
send_task: function(context) {
const token = get_token(); // generates a random token
code1
}
}
})
```
```javascript
code1: this.$store.commit("update_final", {"min" : 8, "token" : token});
code2: if (student.mad2_grade != "S" && student.cgpa > 8) {
const obj = {};
obj.student_id = student.student_id;
obj.token = payload.token
state.awardees.push(student)
}
```
```
code1: context.commit("update_final", 8, token);
code2: if (mad2_grade != "S" && cgpa > 8){
awardees.push(company)
}
```
```javascript
code1: this.$store.commit("update_final", 8, token);
code2: if (mad2_grade != "S" && cgpa > 8){
awardees.push(company)
}
```
```
code1: context.commit("update_final", {"min" : 8, "token" : token});
code2: if (student.mad2_grade != "S" && student.cgpa > 8) {
const obj = {};
obj.student_id = student.student_id;
obj.token = payload.token
state.awardees.push(student)
}
```
Consider the below Vue class binding and select the most appropriate option(s). ```html <div v-bind:class="[isclassA ? classA : '', classB]"></div> ```

Consider the following HTML, with Vue 2 CDN attached to it.
Filename: index.html
```html
<div id="app">
<p>{{ message }}</p>
<button @click="changeMessage">Change Message</button>
<button @click="changeCount">Change Count</button>
</div>
```
Filename: script.js
```javascript
<script>
new Vue({
el: "#app",
data: {
message: "Hello, Vue!",
count: 0,
},
methods: {
changeMessage() {
this.message = "Hello, Vue.js!";
},
changeCount() {
this.count++;
},
},
beforeUpdate() {
console.log("beforeUpdate called");
},
});
</script>
```
What will be logged to the console when the Change Count button is clicked?
Analyze the given HTML with embedded script.
```html
<body>
<div id="status"></div>
<div id="result"></div>
<script>
function updateStatus(message) {
document.getElementById("status").textContent = message;
}
function updateResult(message) {
document.getElementById("result").textContent = message;
}
let promise1 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Data Loaded");
}, 1000);
});
let promise2 = Promise.reject("Network Error");
promise1
.then((result) => {
updateStatus(result);
})
.catch((error) => {
updateResult(error);
});
promise2
.catch((error) => {
updateStatus(error);
return Promise.resolve("Retry Successful");
})
.then((result) => {
updateResult(result);
});
</script>
</body>
```
What will be the final state of HTML elements, after all the `setTimeout()` have finished executing.
#status will be "Data Loaded", and #result will be "Data Save Failed".

``` #status will be "Network Error", and #result will be "Retry Successful". ```

``` #status will be "Data Loaded", and #result will be "Retry Successful". ```

``` #status will be "Network Error", and #result will be "Data Save Failed". ```

Consider the following HTML document embedded with Vue CDN and the script file shown below.
Filename: index.html
```html
<div id="app">
<main-comp>
<nest-comp></nest-comp>
</main-comp>
</div>
```
Filename: script.js
```javascript
let main_comp = {
template: `<div style="border: 1px solid black; padding: 5px">This is main Component</div>`
}
let nest_comp = {
template: `<div style="border: 1px solid black; padding: 5px">This is nested Component</div>`
}
const app = new Vue({
components: {
"main-comp": main_comp,
"nest-comp": nest_comp,
}
}).$mount('#app')
```
What will be rendered on the browser?
This is a UI screenshot. It shows two text labels, one nested inside another. The outer label says: "This is nested Component" The inner label says: "This is main Component"

This is main Component This is nested Component

This is main Component

This is nested Component

Consider the below Vue component template and script definitions that use Vue router.
Vue component template
```html
<template>
<div>
<h1>{{ pageAction }}</h1>
<router-link to="/login" v-if="showLogin">Login</router-link>
<router-link to="/register" v-if="showRegister">Register</router-link>
<router-view></router-view>
</div>
</template>
```
Filename: script.js
```javascript
<script>
export default {
name: 'App',
data() {
return {
pageAction: 'Login or Signup',
showLogin: true,
showRegister: false,
};
},
watch: {
'$route.path'() {
if (this.$route.path === '/login') {
this.showRegister = true;
this.showLogin = false;
this.pageAction = 'Login Page';
} else if (this.$route.path === '/register') {
this.showRegister = false;
this.showLogin = true;
this.pageAction = 'Register Page';
} else {
this.pageAction = 'Login or Signup';
}
},
},
};
</script>
```
Assuming that the corresponding routes are properly configured, what does this component structure accomplish?