Home / Past papers / MAD2 Quiz 1 / this paper

Which of the following methods is not a valid DOM method in JavaScript?

``` getElementById() ```

``` getElementByPropertyName() ```

``` getElementsByTagName() ```

``` getElementByClassName() ```

What will be the output of the following code snippet?
```javascript
let result = "";
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 2; j++) {
result += "" + i + j + "";
}
}
console.log(result);
```
0 1 1 2 2 3

0 1 1 2 2 3 3 4

0 1 2 3 4 5

00 01 10 11 20 21

What will be the output of the following JavaScript code? ```javascript let x = undefined; let y = null; let z = NaN; console.log(x == y, x === y, y == z, y === z, isNaN(z)); ```

true false false true false

false false false false true

true false false false true

false true true false false

Consider the below JavaScript program.
```javascript
const a = 15;
const obj1 = {
a: 20,
b: function () {
console.log(this.a);
this.b();
}
}
const obj2 = {
a: 10,
b: () => {
console.log(this.a);
}
}
obj1.b.apply(obj2);
```
What will be the output of the above program, if executed?
20 20

20 undefined

undefined undefined

10 undefined

The program will crash due to maximum call stack size exceeded error.

Consider the below JavaScript program.
```javascript
let users = [
{ name: "Alice", age: 25 },
{ name: "Bob", age: 22 },
{ name: "Charlie", age: 30 },
];
users.sort((a, b) => a.age - b.age);
console.log(users.map((user) => user.name).join(", "));
```
What will be the output when the code is executed?
Alice, Bob, Charlie

Bob, Alice, Charlie

Charlie, Alice, Bob

Charlie, Bob, Alice

Consider the below javascript program. ```javascript const words = ['apple', 'banana', 'orange', 'grape', 'kiwi']; const result = words.map(word => word.length).filter(len => len > 5); console.log(result); ``` What will be the output of the above program, if executed?

``` ['apple', 'banana', 'orange', 'grape', 'kiwi'] ```

``` [5, 6, 6, 5, 4] ```

``` ['banana', 'orange'] ```

[6, 6]

[5, 5]

``` [true, true] ```

Consider the following JavaScript program.
```javascript
const obj1={
age: 29,
toy: 'kite'
}
const obj2={
__proto__: obj1,
animal: 'dogs',
members: 45
}
console.log(obj1.toy)
console.log(obj1.animal)
console.log(obj2.age)
console.log(obj2.members)
console.log(Object.keys(obj2))
```
What will be the output of the above program, if executed?
``` kite undefined 29 45 ['animal', 'members', 'age', 'toy'] ```

``` kite dogs undefined 45 ['age', 'toy', 'animal', 'members'] ```

``` kite dogs undefined 45 ['age', 'toy'] ```

``` kite undefined 29 45 ['animal', 'members'] ```

Consider the following html.
```html
<div id="app">
<p>{{ message }}</p>
<button @click="updateMessage">Click me</button>
</div>
<script>
new Vue({
el: '#app',
data: {
message: 'Hello!'
},
methods: {
updateMessage() {
this.message += '!';
}
}
});
</script>
```
Assuming vue cdn has been added to the html and the html format is correct. What would be the output on the screen after clicking the button twice?
Hello

Hello!

Hello!!

Hello!!!

```javascript let func = (num) => return num1*num1; ```

```javascript
let func = (num) => { num1*num1; }
```
```javascript let func = (num) => num1*num1; ```

```javascript
let func = (num) => { return num1*num1; }
```
Consider the following JavaScript code embedded in the body of the HTML document given below.
```html
<html>
<body>
<div id="mybox">Test JS</div>
<script>
let count = 0;
let change = setInterval(()=>{
let box = document.getElementById('mybox');
if(count % 2 == 1){
box.style.backgroundColor = 'red';
count++;
}
else if(count % 3 == 1){
box.style.backgroundColor = 'green';
count++;
}
else{
box.style.backgroundColor = 'yellow';
count++;
}
}, 1000)
</script>
</body>
</html>
```
If the document is rendered on the browser, what will be the sequence of background colors taken by the element with id="mybox" in the first 6 seconds of execution?
Red → Green → Yellow → Red → Green → Yellow

Red → Green → Red → Yellow → Red → Yellow

Yellow → Green → Yellow → Green → Yellow → Green

Yellow → Red → Yellow → Red → Green → Red

Consider the below JavaScript program.
```javascript
class Region{
constructor(region){
this.region = region;
}
get describe(){
return `${this.region} is one of the major geographical regions.`
}
}
class Country extends Region {
constructor(region, country, nation){
super(region)
this.country = country;
this.nation = nation;
}
get describe(){
return `${this.country} is a ${this.nation} nation in the region of ${this.region}.`
}
}
let Australia = new Region('Australia and NZ', 'Australia', 'developed')
let Germany = new Country('Europe', 'Germany', 'developed')
console.log(Australia.describe)
console.log(Germany.describe)
```
What will be the output of the above program, if executed?
``` Australia is a developed nation in the region of Australia and NZ. Germany is a developed nation in the region of Europe. ```

Australia and NZ is one of the major geographical regions. Germany is one of the major geographical regions.

``` Australia is a developed nation in the region of Australia and NZ. Germany is one of the major geographical regions. ```

Australia and NZ is one of the major geographical regions. Germany is a developed nation in the region of Europe.

Consider the following Vue application with markup index.html and JavaScript file app.js.
File: index.html
```html
<div id = "app">
<div>Welcome to Frontend Development</div>
<new>Vue is a Frontend Framework</new>
</div>
<script src="script.js"></script>
```
File: script.js
```javascript
Vue.component('new', {
template : `<div>
<slot>Vue is JS Framework</slot>
<div>Learn Vue 2 and Vue 3</div>
</div>`
});
new Vue({
el : "#app",
})
```
What will be rendered on the browser screen?
Welcome to Frontend Development Vue is a JS Framework Vue is a Frontend Framework Learn Vue 2 and Vue 3

Welcome to Frontend Development Vue is a Frontend Framework Vue is a JS Framework Learn Vue 2 and Vue 3

Welcome to Frontend Development Vue is a Frontend Framework Learn Vue 2 and Vue 3

Welcome to Frontend Development Vue is a JS Framework Learn Vue 2 and Vue 3

Consider the following html.
```html
<div id="app">
<parent-component :title="parentTitle">
<template slot="header">
<h2>{{ headerTitle }}</h2>
</template>
</parent-component>
</div>
<script>
Vue.component('parent-component', {
props: ['title'],
template: `
<div>
<h1>{{ title }}</h1>
<slot name="header"></slot>
<p>Main Content</p>
</div>
`
});
new Vue({
el: '#app',
data: {
parentTitle: 'Parent Component Title',
headerTitle: 'Header Content'
}
});
</script>
```
Assuming Vue CDN has been added to the HTML and the HTML format is correct.
What would be the output on the screen?
Parent Component Title Main Content

Parent Component Title Header Content Main Content

Parent Component Title
```html
<h2>{{ headerTitle }}</h2>
```
Main Content
Header Content Main Content
Auto-generated summary from the source site. Handy as a checklist; the lessons above explain each idea from scratch.
DOM Methods:
getElementById()getElementsByTagName()getElementsByClassName()getElementByPropertyName() is not a valid DOM method.Loops and Concatenation:
Example:
let result = "";
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 2; j++) {
result += " " + i + j + " ";
}
}
console.log(result); // Output: " 00 01 10 11 20 21 "Variable Comparisons:
undefined, null, and NaN.Example:
let x = undefined;
let y = null;
let z = NaN;
console.log(x === y, x === y, y === z, y === z, isNaN(z)); // Output: false, false, false, false, trueObject Prototypes and Inheritance:
__proto__ and property inheritance.Example:
const obj1 = { age: 29, toy: 'kite' };
const obj2 = { __proto__: obj1, animal: 'dogs', members: 45 };
console.log(obj1.toy); // Output: 'kite'
console.log(obj1.animal); // Output: undefined
console.log(obj2.age); // Output: 29
console.log(obj2.members); // Output: 45
console.log(Object.keys(obj2)); // Output: ['animal', 'members']Arrow Functions:
Example:
let func = (num) => num * num;Intervals and Conditional Logic:
setInterval and conditional logic to change styles.Example:
let count = 0;
let change = setInterval(() => {
let box = document.getElementById('mybox');
if (count % 2 == 1) {
box.style.backgroundColor = 'red';
count++;
} else if (count % 3 == 1) {
box.style.backgroundColor = 'green';
count++;
} else {
box.style.backgroundColor = 'yellow';
count++;
}
}, 1000);State Management:
Directives:
v-bind: Used for one-way data binding.v-model: Used for two-way data binding.Ephemeral State:
Components and Slots:
Example:
<div id="app">
<parent-component :title="parentTitle">
<template slot="header">
<h2>{{ headerTitle }}</h2>
</template>
</parent-component>
</div>
<script>
Vue.component('parent-component', {
props: ['title'],
template: `
<div>
<h1>{{ title }}</h1>
<slot name="header"></slot>
<p>Main Content</p>
</div>
`
});
new Vue({
el: '#app',
data: {
parentTitle: 'Parent Component Title',
headerTitle: 'Header Content'
}
});
</script>Vue Instance and Data Binding:
Example:
<div id="app">
<p>{{ message }}</p>
<button @click="updateMessage">Click me</button>
</div>
<script>
new Vue({
el: '#app',
data: {
message: 'Hello!'
},
methods: {
updateMessage() {
this.message += '!';
}
}
});
</script>Array Methods:
map, filter, and sort methods.Example:
const words = ['apple', 'banana', 'orange', 'grape', 'kiwi'];
const result = words.map(word => word.length).filter(len => len > 5);
console.log(result); // Output: [6, 6]Object Methods:
Object.keys() method.Example:
const obj = { a: 1, b: 2, c: 3 };
console.log(Object.keys(obj)); // Output: ['a', 'b', 'c']