Home / Past papers / MAD2 Quiz 1 / this paper
Suppose you are given the below HTML. Which of the following code(s) can be used to access the HTML paragraph element at “Statement 1” using JavaScript?```html
<body>
<p id = "id1" class = "class1">Content A</p> <!-- Statement 1 -->
<p id = "id2" class = "class1">Content B</p> <!-- Statement 2 -->
</body>
```
Which of the following statements is correct regarding "<noscript>" tag in HTML?
Consider the below JavaScript program, and predict the output, if executed. ```javascript
class Person {
constructor(name) {
this.name = name;
this.age = 23;
}
static fromJson(obj) {
let new_obj = new Person(obj.name);
new_obj.sound = obj.sound;
return new_obj;
}
}
result = new Person('Abhi');
console.log(Person.fromJson(JSON.parse(JSON.stringify(result))).constructor.name);
```
Consider the below JavaScript program, and predict the output, if executed. ```javascript
let x = 50;
const obj1 = {
x : 10,
func : function () {
console.log(this.x)
}
}
const obj2 = {
x : 20,
func : () => {
function abc () {
console.log(x, "and", this.x)
obj1.func.call(this)
}
abc()
}
}
obj2.func()
```
50 and 20 10

50 and 20 undefined

50 and undefined undefined

50 and undefined 10
Consider the below Vue application with markup file “index.html”, and JavaScript file “app.js”.
Consider the below Vue application with markup file “index.html”, and JavaScript file “app.js”. Suppose you open the “index.html” file in a browser, what will be rendered by the browser?index.html:
```html
<body>
<div id = "app">
<ol>
<li v-for="item in items" :key="item.id">{{ item.name }}</li>
</ol>
</div>
</body>
<script src = "app.js"> </script>
```
app.js:
```javascript
const a = new Vue({
el : '#app',
data : {
items: [
{ id: 1, name: 'App Dev I Theory' },
{ id: 2, name: 'App Dev I Project' },
{ id: 3, name: 'App Dev II Theory' },
{ id: 4, name: 'App Dev II Project' }
]
},
})
```
App Dev II Project App Dev II Theory App Dev I Project App Dev I Theory

App Dev I Theory App Dev I Project

App Dev II Theory App Dev II Project

App Dev I Theory App Dev I Project App Dev II Theory App Dev II Project
Consider the below Vue application with markup file “index.html”, and javaScript file “app.js”.
Consider the below Vue application with markup file “index.html”, and javaScript file “app.js”. Suppose you open the “index.html” file in a browser, what will be rendered by the browser?index.html:
```html
<body>
<div id = "app">
<ol>
<li v-for="item in items" :key="item.id">
<blog-post :post-title = "item.name" :post-content =
"item.message"></blog-post>
</li>
</ol>
</div>
</body>
<script src = "app.js"></script>
```
app.js:
```javascript
Vue.component('blog-post', {
props: {
'postTitle' : String,
'postContent' : String
},
template: `
<div>
<h3>{{ postTitle }}</h3>
<p>{{ postContent }}</p>
</div>
`
})
const a = new Vue({
el : '#app',
data : {
items: [
{ id: 1, name: 'IITM BS', message: "The World's first
4-year BS degree in DS and Applications"},
{ id: 2, name: 'IITM ES', message: "Learn Electronic
Systems from top-notch IIT Madras faculty"},
{ id: 3, name: 'NPTEL', message: "Online Learning
Initiatives by IITs and IISc"}
]
},
})
```
1. IITM BS 2. IITM ES 3. NPTEL

1. IITM BS The World's first 4-year BS degree in DS and Applications 2. IITM ES Learn Electronic Systems from top-notch IIT Madras faculty 3. NPTEL Online Learning Initiatives by IITs and IISc

1. The World's first 4-year BS degree in DS and Applications 2. Learn Electronic Systems from top-notch IIT Madras faculty 3. Online Learning Initiatives by IITs and IISc

A. NPTEL Online Learning Initiatives by IITs and IISc B. IITM ES Learn Electronic Systems from top-notch IIT Madras faculty C. IITM BS The World's first 4-year BS degree in DS and Applications
Consider the following JavaScript program. What will be logged on to console?```javascript
function doSomething(n, ...params) {
return params
.map((x) => {
return x ** n
})
.sort()
}
console.log(doSomething(2, 5, 10))
```
Consider the following JavaScript program. What will be logged on to console?```javascript
function coolerPlace({ ...obj }) {
let tmp = 0
let cty = null
Object.entries(obj).forEach((([city, temp]) => {
if (temp > tmp) {
cty = city
}
}))
return cty
}
console.log(coolerPlace({ UP: 37, Delhi: 40 }))
```
Consider the following JavaScript program. What will be logged on to console?```javascript
const address1 = {
state: 'Tamil Nadu',
city: 'Chennai',
place: 'IIT Madras',
getAddress() {
return `State: ${this.state}, city: ${this.city}, place:${this.place}`
},
}
const address3 = {
place: 'Taramani',
__proto__: address1,
}
console.log(address3.getAddress())
```
Consider the following JavaScript program. What will be logged on to console?```javascript
let x = 1
let y = 2
{
let x = 4
let y = 5
}
function outer() {
function inner() {
return x + y
}
return inner
}
console.log(outer() ())
```
Consider the below JavaScript program, and predict the output. Also, predict the number of times the while loop will run. ```javascript
function do_something(arr, val){
var i1 = 0;
var i2 = arr.length;
while(i2 > i1){
var i3 = Math.floor((i1 + i2)/2);
if(val > arr[i3]){
i1 = i3 + 1;
}
else if(val < arr[i3]){
i2 = i3;
}
else{
return true;
}
}
return false;
}
result = do_something([3, 6, 34, 39, 48, 56, 78, 87], 40)
console.log(result)
```
true The while loop will run 3 times

false The while loop will run 4 times

false The while loop will run 3 times

true The while loop will run 4 times
Consider the below Vue application with markup file “index.html”, and JavaScript file “app.js”.
Consider the below Vue application with markup file “index.html”, and JavaScript file “app.js”. ```javascript
app.js
new Vue({
el: '#app',
template: `<div> The nearest city to you is: {{minDistance}}
</div>`,
data: {
userCoordinate: [1, 2],
allCooridates: [
{ name: 'City1', coodinate: [1, 7] },
{ name: 'City2', coodinate: [2, 4] },
{ name: 'City3', coodinate: [3, 5] },
{ name: 'City4', coodinate: [1, 4] },
],
},
methods: {
calculateDistance(x, y) {
return Math.abs(x[0] - y[0]) + Math.abs(x[1] - y[1])
},
},
computed: {
minDistance() {
let minDist = Infinity
let city = null
this.allCooridates.forEach((cord) => {
let d = this.calculateDistance(cord.coodinate,
this.userCoordinate)
if (d < minDist) {
minDist = d
city = cord.name
}
})
return city
},
},
})
```
index.html
```html
<div id="app"></div>
<script
src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script src="./app.js"></script>
```
Suppose the application is running on http://localhost:8080.
What will be rendered by the browser?
Consider the below Vue application with markup file “index.html”, and JavaScript file “app.js”.
Consider the below Vue application with markup file “index.html”, and JavaScript file “app.js”. ```javascript
app.js
new Vue({
el: '#app',
template: `<div>
<input type='text' v-model='num'></input>
<div id='lon'>Lucky or not? {{lucky?'Lucky':'Not Lucky'}}</div>
</div>`,
data: {
num: 1,
prev: 0,
current: 1,
lucky: false,
},
watch: {
num(num1, num2) {
if (num1 == '') {
return
}
this.prev = this.current
this.current = Number(num1)
this.current > this.prev ? (this.lucky = true) : (this.lucky =
false)
},
},
})
```
index.html
```html
<div id="app"></div>
<script
src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script src="./app.js"></script>
```
Suppose the application is running on 'http://localhost:8080'. Suppose the user inputs 7 and then 5. What will be rendered inside the div with id 'lon'?
Consider the below Vue application with markup file “index.html”, and JavaScript file “app.js”.
Consider the below Vue application with markup file “index.html”, and JavaScript file “app.js”. ```javascript
app.js
const Sender = {
template: '<div> Message: <input type=\'text\'
v-model=\'message\'></input> </div>',
data() {
return {
message: null,
}
},
watch: {
message(msg) {
this.$emit('newmsg', this.message)
},
},
}
const Receiver = {
template: '<div id="msg"> {{msg}} </div>',
props: ['msg'],
}
new Vue({
el: '#app',
template: '<div>
<Sender @newmsg = \'updateMessage\'/>
<Receiver :msg=\'message\' />
</div>',
data: {
message: null,
},
methods: {
updateMessage(msg) {
this.message = msg.split('').reverse().join('').toUpperCase()
},
},
components: {
Sender,
Receiver,
},
})
```
index.html
```html
<div id="app"></div>
<script
src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script src="./app.js"></script>
```
Suppose the application is running on 'http://localhost:8080'. If the user inputs 'hello' inside the input box, what will be rendered inside the div with ID 'msg'?
hello

HELLO

OLLEH

olleh
Auto-generated summary from the source site. Handy as a checklist; the lessons above explain each idea from scratch.
Accessing HTML Elements with JavaScript
document.getElementById("id")document.getElementsByClassName("className")JavaScript Classes and Static Methods
``javascript class Person { constructor(name) { this.name = name; this.age = 23; } static fromJson(obj) { let new_obj = new Person(obj.name); new_obj.sound = obj.sound; return new_obj; } } ``
JavaScript Scope and this Keyword
``javascript let x = 50; const obj1 = { x: 10, func: function() { console.log(this.x) } } const obj2 = { x: 20, func: () => { function abc() { console.log(x, "and", this.x) obj1.func.call(this) } abc() } } obj2.func() ``
JavaScript Functions and Parameters
``javascript function doSomething(n, ...params) { return params .map((x) => { return x ** n }) .sort(); } ``
JavaScript Objects and Prototypes
``javascript const address1 = { state: 'Tamil Nadu', city: 'Chennai', place: 'IIT Madras', getAddress() { return State: {this.city}, place: ${this.place} } } const address3 = { place: 'Taramani', __proto__: address1, } ``
JavaScript Binary Search
``javascript function do_something(arr, val) { var i1 = 0; var i2 = arr.length; while (i2 > i1) { var i3 = Math.floor((i1 + i2) / 2); if (val > arr[i3]) { i1 = i3 + 1; } else if (val < arr[i3]) { i2 = i3; } else { return true; } } return false; } ``
Vue.js Directives and Components
v-for directive for looping``html <div id="app"> <ol> <li v-for="item in items" :key="item.id">{{ item.name }}</li> </ol> </div> ` `javascript const a = new Vue({ el: '#app', data: { items: [ { id: 1, name: 'App Dev I Theory' }, { id: 2, name: 'App Dev I Project' }, { id: 3, name: 'App Dev II Theory' }, { id: 4, name: 'App Dev II Project' } ] } }); ``
Vue.js Watchers and Data Binding
v-model for two-way data binding``javascript new Vue({ el: '#app', template: <div> <input type='text' v-model='num'></input> <div id='ion'>Lucky or not? {{lucky ? 'Lucky' : 'Not Lucky'}}</div> </div>, data: { num: 1, prev: 0, current: 1, lucky: false, }, watch: { num(num1, num2) { if (num1 === '') { return } this.prev = this.current this.current = Number(num1) this.current > this.prev ? (this.lucky = true) : (this.lucky = false) } }, }) ``
Vue.js Component Communication
``javascript const Sender = { template: '<div> Message: <input type="text" v-model="message"></input> </div>', data() { return { message: null, } }, watch: { message(msg) { this.$emit('newmsg', this.message) }, }, } const Receiver = { template: '<div id="msg"> {{msg}} </div>', props: ['msg'], } new Vue({ el: '#app', template: '<div><Sender @newmsg="updateMessage"/><Receiver :msg="message" /></div>', data: { message: null, }, methods: { updateMessage(msg) { this.message = msg.split('').reverse().join('').toUpperCase() }, }, components: { Sender, Receiver, }, }) ``