Home / Past papers / MAD2 Quiz 1 / this paper
Which of the following statements are correct?```javascript console.log(typeof setTimeout) console.log(typeof document) ```
."function"
."undefined"
will work in Browser by default because V8 provides the DOM.```javascript document.querySelector() ```

```javascript items.forEach(item => console.log(item)) ```

```javascript setTimeout(showMessage, 1000) ```

```javascript const result = showMessage() ```

```javascript
button.addEventListener("click", handleClick)
```
```javascript
console.log("1")
setTimeout(() => {
console.log("2")
setTimeout(() => console.log("3"), 0)
}, 0)
console.log("4")
```
. The parent first updates a prop passed to the child, and then sets
. After the child has already mounted once, which sequence can occur in the child after initial render?``` v-if="show" ``` ``` show = false ```
->
->
-> 
beforeUpdate updated beforeDestroy destroyed
->
->
-> 
beforeDestroy destroyed beforeUpdate updated
->
->
-> 
created mounted beforeUpdate destroyed
->
->
-> 
updated beforeCreate destroyed mounted
What will be the output of the above code on the browsers’ console?```javascript
let var1 = 20;
var var2 = 30;
const val = 10;
{
let var1 = 40;
console.log(var1 + val);
}
console.log(var1 + val);
{
var var2 = 50;
console.log(var2 + val);
}
console.log(var2 + val);
```
What will be the output of the above code on the browsers’ console?```javascript
const obj1 = {
prop1: "Germany",
prop2: "Spain",
prop3: 50,
method1: function(){
console.log(`The European countries are ${this.prop2} and ${this.prop1}.`)
},
method2: function(param){
let value = param + 2*this.prop3;
console.log(`The final numerical value is: ${value}.`)
}
}
const obj2 = {
prop1: "England",
prop2: "Italy",
prop3: "25"
}
const func1 = obj1.method1;
const func2 = obj1.method2;
func1.apply(obj1);
func2.bind(obj2)(15);
```
What will be the output of the above code on the browsers’ console?```javascript
const obj1 = {
prop1: "Aluminium",
prop2: "Gold",
prop3: "Silver"
}
const obj2 = {
propA: "Argon",
propB: "Neon"
}
const {prop2: element1, propA: element2} = {...obj2, ...obj1};
console.log(`The selected elements are: ${element1} and ${element2}`);
```
and inside the child:
Which statements are correct?```html
<child-box :message="parentMsg" @reply="handleReply"></child-box>
```
```javascript
this.$emit('reply', 'ok')
```
flows from parent to child through props.message
is a custom event sent from child to parent.reply
to update the parent source of truth.message
changes in the parent, the child can receive the updated prop value.parentMsg
Why does the
value not update correctly when the button is clicked?```html
<div id="app">
<p>{{ message }}</p>
<button @click="changeMessage">Click</button>
</div>
<script>
new Vue({
el: '#app',
data: {
message: "Hello"
},
methods: {
changeMessage: () => {
this.message = "Updated"
}
}
})
</script>
```
message
should be used instead of 
v-on ``` @click ```

this
should be a function in all Vue instancesdata
Which of the following statements is/are correct?```javascript
class MathUtils {
static add(a, b) {
return a + b;
}
multiply(a, b) {
return a * b;
}
}
const utils = new MathUtils();
```
will work correctly and return 8``` MathUtils.add(5, 3) ```
will work correctly and return 8``` utils.add(5, 3) ```
will work correctly and return 15``` utils.multiply(5, 3) ```
will work correctly and return 15``` MathUtils.multiply(5, 3) ```
What will be the output of the above code on the browsers’ console?```javascript
var var1 = 25;
var var2 = 35;
const obj = {
var1: 45,
var2: 55,
func1: function(){
console.log(`You are accessing: ${var1}`)
},
func2: () => {
console.log(`You are accessing: ${this.var2}`)
},
}
obj.func1()
obj.func2()
```
What will be the output of the above code on the browsers’ console in correct order?```javascript
console.log(z);
var a = "alpha";
console.log(a);
let b = "beta"
console.log(b);
d();
console.log(c);
const c = "charlie"
function d(){
console.log("delta")
}
var z = function(){
console.log("zeta")
}
```
What will be the output of the above code on the browsers’ console?```javascript
let myArr = ["January", "March", "April", "August", "September", "October"]
let modArr = myArr.map((m) => m.slice(2)).
filter((m) => m.length < 5).
sort();
console.log(modArr[1]);
```
What will be the output of the above code on the browsers’ console?```javascript
function first() {
console.log("First start");
setTimeout(function () {
console.log("First timeout");
}, 0);
console.log("First end");
}
function second() {
console.log("Second start");
setTimeout(function () {
console.log("Second timeout");
}, 0);
}
console.log("Program start");
first();
second();
console.log("Program end");
```Auto-generated summary from the source site. Handy as a checklist; the lessons above explain each idea from scratch.
The questions provided cover fundamental and advanced concepts in JavaScript and frontend development with Vue.js. No specific mathematical equations were required for solving these questions, but a strong foundation in logical reasoning and problem-solving, which are core to mathematics, is essential for software engineering.
Variables and Scope (var, let, const):
var: Function-scoped (or global), hoisted and initialized with undefined. Accessing it before its declaration within its scope will result in undefined.let and const: Block-scoped, not hoisted in the same way as var. They exist in a Temporal Dead Zone (TDZ) from the start of their block until their declaration. Accessing them before declaration/initialization results in a ReferenceError. const additionally requires immediate initialization and its value cannot be reassigned.Functions and this Context:
=>): Do not bind their own this, arguments, super, or new.target. They inherit this from their lexical (enclosing) scope. This is a common pitfall in object methods or Vue.js methods if not used carefully.Regular Functions (function): The this keyword's value is determined by how the function is called.
obj.method(), this refers to obj.func(), this refers to the global object (e.g., window in browsers) or undefined in strict mode.Explicit this Binding (call, apply, bind):
func.apply(thisArg, [argsArray]): Calls the function with a specified this context and arguments provided as an array.func.bind(thisArg, arg1, ...): Returns a new function with this permanently bound to thisArg and initial arguments. It does not call the function immediately.Objects and Destructuring:
{ propName: newVarName })....) for Objects: Merging objects. When multiple objects are spread, properties with the same key in later objects overwrite those in earlier ones.Arrays and Higher-Order Methods:
array.map(callbackFunction): Creates a new array populated with the results of calling a provided callbackFunction on every element in the calling array.array.filter(callbackFunction): Creates a new array with all elements that pass the test implemented by the provided callbackFunction.array.sort(compareFunction): Sorts the elements of an array in place and returns the sorted array. For strings, it sorts lexicographically by default. For numbers, a compareFunction is crucial.string.slice(startIndex, endIndex): Extracts a section of a string and returns it as a new string, without modifying the original.Asynchronous JavaScript and Event Loop:
setTimeout, DOM events) that handle asynchronous tasks.setTimeout callbacks) are placed after their asynchronous operation completes.setTimeout(callback, delay): Schedules a callback to be executed after at least delay milliseconds. Even with delay = 0, the callback is placed in the Callback Queue and executes only after the current Call Stack is empty.Classes and Object-Oriented Programming (OOP):
class Syntax: Blueprint for creating objects.static Methods: Belong to the class itself, not to instances of the class. They are called directly on the class (ClassName.staticMethod()).instance.method()).JavaScript Runtimes:
document is browser-specific, setTimeout is globally available in both but managed by different underlying mechanisms).Component-Based Architecture:
Vue Directives:
v-show: Toggles the CSS display property of an element. The element is always rendered to the DOM.v-for: Used for iterating over a list (e.g., an array or object) to render multiple elements or components.v-bind (: shorthand): One-way binding for HTML attributes or component props. It binds an expression to an attribute, keeping it in sync with the component's data.v-model: Primarily used for two-way data binding on form input elements. It's a syntactic sugar for v-bind:value and v-on:input (or v-on:change etc.).Component Communication:
:message="parentMsg"). Props are one-way data flow; child components should not directly modify a prop received from the parent.this.$emit): The mechanism for child-to-parent communication. A child component emits a named event, which the parent component can listen to (@reply="handleReply") and react to.mounted for when a component is attached to the DOM, updated for when data changes and the component re-renders).this Context in Vue Methods: Within a Vue instance's methods object, this typically refers to the Vue instance itself. Using arrow functions as methods directly in a Vue instance's methods property can lead to incorrect this binding because arrow functions lexically bind this to the context in which they are defined (which would be the global window object or undefined in strict mode, not the Vue instance).No specific mathematical equations were required for these questions. The relevance of mathematics lies in the underlying logical structures of programming and problem-solving.