Home / Past papers / MAD2 Quiz 1 / this paper

Consider the following HTML, with the correct Vue 2 CDN links included.
```html
<body>
<div id="app" :class="['box', { active: isActive }]">
Vue Component
</div>
<script>
new Vue({
el: '#app',
data() {
return {
isActive: false,
};
},
});
</script>
</body>
```
What will be the final rendered class attribute of the `<div>` ?
Consider the following JavaScript code
```javascript
const juices = [
{ id: 1, name: "grape", rating: 8.1 },
{ id: 2, name: "apple", rating: 5.0 },
{ id: 3, name: "orange", rating: 6.9 },
{ id: 4, name: "banana", rating: 10 },
]
mapping = { "4": 1, "3": 2, "2": 3, "1": 4 }
juices.map((juice) => { return { ...juice, rank: mapping[juice.id] } })
.sort(((a, b) => a.rank - b.rank)
.map((juice) => { console.log(juice.name) }))
```
What will be the output of the code?
apple orange grape banana

banana grape orange apple

banana orange apple grape

undefined

Consider the following JavaScript code snippet
```javascript
var name = 'Taylor'
const person = {
name: 'Jeremy',
talk() {
return () => `${this.name} says Hello!`
}
}
talkReturn = person.talk()
console.log(talkReturn())
```
What will be the output of the code?
Consider the following code snippet
```javascript
function createCounter() {
let count = 0;
return function() {
count++;
setTimeout(() => {
console.log(count);
}, 1000);
};
}
const counter = createCounter();
counter();
counter();
counter();
```
What will be the output after 1 second?
1

1 1 1

1 2 3

3 3 3

Consider the below javascript program.
```javascript
const obj = {
a: 10,
b() {
return this.a;
}
};
const c = obj.b;
const d = obj.b.bind(obj);
console.log(c(), d());
```
What will be the output of the above program?
Consider the below Vue component.
```html
<template>
<div>
<p>{{ computedMessage }}</p>
<button @click="update">Update</button>
</div>
</template>
<script>
export default {
data() {
return {
message: 'Initial'
};
},
computed: {
computedMessage() {
return this.message + ' - Computed';
}
},
methods: {
update() {
this.message = 'Updated';
}
}
};
</script>
```
What will be displayed in the `<p>` element, after the button with text "Update" is clicked?
Consider the following HTML, with the correct Vue 2 CDN links included.
```html
<body>
<div id="app">
<p>pineapple 🍍</p>
<button @click="addPineapple">Buy</button>
<p>Amount : {{amount}}</p>
</div>
<script>
new Vue({
el: '#app',
data() {
return {
amount: '',
price: 40,
}
},
methods: {
addPineapple() {
this.amount += this.price;
}
}
})
</script>
</body>
```
What will be shown in the amount placeholder when the Buy button is clicked two times?
Consider the following HTML, with the correct Vue 2 CDN links included.
```html
<div id="app">
<p>Original Order: {{ customerOrder }}</p>
<p>Updated Order: {{ updatedOrder }}</p>
<server-component :order="customerOrder"
@order-updated="updateOrder"></server-component>
</div>
<script>
Vue.component('server-component', {
props: ['order'],
template: `
<div>
<button @click="modifyOrder">Send Updated Order</button>
</div>
`,
methods: {
modifyOrder() {
this.$emit('order-updated', this.order + ' - Extra mayo');
}
}
});
new Vue({
el: '#app',
data() {
return {
customerOrder: 'Burger',
updatedOrder: ''
}
},
methods: {
updateOrder(newOrder) {
this.updatedOrder = newOrder;
}
}
});
</script>
```
What will be displayed in the `<p>` tags after the button on the `server-component` is clicked?
Original Order: Burger Updated Order:

Original Order: Updated Order:

Original Order: Burger Updated Order: - Extra mayo

Original Order: Burger Updated Order: Burger - Extra mayo

Consider the below javascript program.
```javascript
const numbers = [10, 20, 30];
const modifiedNumbers = numbers.reduce((acc, num) => {
if (num % 2 === 0) {
acc.push(num * 2);
} else {
acc.push(num / 2);
}
return acc;
}, []);
const finalOutput = modifiedNumbers.filter(num => num > 20);
console.log(finalOutput);
```
What will be the output of the above program?
Consider the below Vue component.
```html
<template>
<div>
<ul>
<li v-for="item in filteredItems" :key="item.id">{{ item.name }}</li>
</ul>
</div>
</template>
<script>
export default {
data() {
return {
items: [
{ id: 1, name: "Item 1" },
{ id: 2, name: "Item 2" },
{ id: 3, name: "Item 3" },
],
filterActive: true,
};
},
computed: {
filteredItems() {
return this.filterActive
? this.items.filter((item) => item.id !== 2)
: this.items;
},
},
};
</script>
```
What will be displayed in the `<ul>` when filterActive is true?Auto-generated summary from the source site. Handy as a checklist; the lessons above explain each idea from scratch.
this Keyword Behavior
this refers to the global object in non-strict mode and undefined in strict mode.this is lexically inherited from the enclosing execution context.this refers to the object itself, but this can be overridden by how the method is called.this can be explicitly set using call(), apply(), or bind() methods.Variable Declarations
var: Function-scoped, hoisted, allows reassignments.let: Block-scoped, hoisted, allows reassignments.const: Block-scoped, hoisted, does not allow reassignments, must be assigned at the time of declaration.Function Methods
call() and apply(): Immediately invoke the function. call() accepts arguments as a comma-separated list, whereas apply() accepts a single array of arguments.bind(): Returns a new function that can be called later, permanently setting the this context.Asynchronous Operations
setTimeout: Executes a function after a specified delay.Array Methods
map(): Creates a new array with the results of calling a provided function on every element in the array.filter(): Creates a new array with all elements that pass the test implemented by the provided function.reduce(): Executes a reducer function on each element of the array, resulting in a single output value.sort(): Sorts the elements of an array in place and returns the array.Dynamic Class Binding
:class="['box', { active: isActive }]"box class is always applied, while the active class is conditionally applied based on the value of isActive.Computed Properties vs Methods
Event Handling
@click="methodName"Component Communication
Lifecycle Hooks
created: Called synchronously after the instance is created.mounted: Called after the instance has been mounted.Ephemeral State
State Management