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() (Not a valid DOM method)JavaScript Loops and String Manipulation:
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 "JavaScript Data Types and Comparisons:
undefined, null, NaN===, isNaN()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 trueJavaScript Functions and Context:
apply() method and this context.Example:
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); // Output: 10 undefinedJavaScript Array Methods:
sort(), map(), filter(), join()Example:
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(", ")); // Output: "Bob, Alice, Charlie"JavaScript Object Prototypes and Inheritance:
__proto__ and object inheritance.Object.keys()Example:
const obj1 = {
age: 29,
toy: 'kite'
};
const obj2 = {
__proto__: obj1,
animal: 'dogs',
members: 45
};
console.log(Object.keys(obj2)); // Output: ['animal', 'members']Vue.js Directives:
v-bind for one-way data binding.v-model for two-way 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>Vue.js 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.js State Management:
Example:
new Vue({
el: '#app',
data: {
message: 'Hello!'
},
methods: {
updateMessage() {
this.message += '!';
}
}
});HTML Structure and Rendering:
Example:
<div id="app">
<div>Welcome to Frontend Development</div>
<div>Vue is a Frontend Framework</div>
</div>CSS Styling:
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);Classes and Inheritance:
Example:
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);