Home / Past papers / MAD2 Quiz 1 / this paper

Consider the following JavaScript code. What will be logged on to the console once the code is executed?
```javascript
function checkThis() {
return this.name
}
const Obj1 = {
name: 'Obj1',
checkThis: checkThis,
}
const Obj2 = {
name: 'Obj2',
checkThis: checkThis,
}
console.log(Obj2.checkThis(), Obj1.checkThis.call(Obj2))
```
Consider the following JavaScript code. What will be logged on to the console once the code is executed?
```javascript
const stateManager = function () {
let defaultState = false
function alterState() {
defaultState = !defaultState
}
return {
currentState() {
return defaultState
},
changeState() {
alterState()
},
}
}
const sm1 = stateManager()
sm1.changeState()
const sm2 = stateManager()
console.log(sm1.currentState(), sm2.currentState())
```
Consider the following JavaScript code. What will be logged on to the console once the code is executed?
```javascript
const player = {
name: 'Rohit',
state: 'Maharastra',
}
const batsman = Object.create(player)
console.log(batsman.name)
```
Rohit

undefined

null

None of these

Consider the following JavaScript code. What will be logged on to the console once the code is executed?
```javascript
const emails = [
'user1@study.iitm.ac.in',
'user2@gmail.com',
'user3@study.iitm.ac.in',
]
const studentsEmail = emails.filter((email) =>
email.endsWith('study.iitm.ac.in')
)
console.log(studentsEmail)
```
[ user1@study.iitm.ac.in, user2@gmail.com, user3@study.iitm.ac.in ]

[ user1@study.iitm.ac.in, user3@study.iitm.ac.in ]

[ user2@gmail.com, user3@study.iitm.ac.in ]

[user2@gmail.com]

Consider the following Vue application with markup index.html and JavaScript
app.js
index.html
```html
<body>
<div id="app"></div>
<script
src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script src="./app.js"></script>
</body>
```
app.js
```javascript
new Vue({
el: '#app',
template: `
<div>
{{text}}
<div>
<input type="text" v-model="userInput" />
<div id="match">
{{hasMatched?"Not Matched":"Matched"}}
</div>
</div>
</div>`,
data: {
text: 'This is text',
userInput: null,
},
computed: {
hasMatched() {
return this.text.startsWith(this.userInput)
},
},
})
```
Suppose the application is running on "http://localhost:8080/". If the User inputs "This" in the input box. What will be rendered inside the div with id "match"?
Not Matched

Matched

undefined

null

Consider the following Vue application with markup index.html and JavaScript app.js
index.html
```html
<body>
<div id="app"></div>
<script
src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script src="./app.js"></script>
</body>
```
app.js
```javascript
new Vue({
el: '#app',
template: `<div>
<div id="holiday">{{isHoliday?"Holiday":"Not Holiday"}}</div>
<button @click="increaseCount">Increase Count</button>
</div>`,
data: {
count: 1,
isHoliday: false,
},
watch: {
count() {
if (this.count % 5 == 0) {
this.isHoliday = true
}
},
},
methods: {
increaseCount() {
this.count += 6
},
},
})
```
Suppose the application is running on "http://localhost:8080" if the user clicks on button "Increase Count" 5 times. What will be rendered inside the div with id "holiday"?
Consider the following JavaScript code. What will be logged on to the console once the code is executed?
```javascript
class Album {
constructor(creator, albumName) {
this.creator = creator
this.albumName = albumName
this.songs = []
}
addSong(song) {
this.songs.push(song)
}
}
class Song {
constructor(name, duration) {
this.name = name
this.duration = duration
this.album = null
}
addAlbum(album) {
this.album = album
}
}
al = new Album('creator1', 'Album1')
s1 = new Song('Song1')
al.addSong(s1)
console.log(al.songs.length)
```
Consider the following JavaScript code. What will be logged on to the console once the code is executed?
```javascript
class Album {
constructor(creator, albumName) {
this.creator = creator
this.albumName = albumName
this.songs = []
}
addSong(song) {
this.songs.push(song)
}
}
class Song {
constructor(name, duration) {
this.name = name
this.duration = duration
this.album = null
}
addAlbum(album) {
this.album = album
}
}
al = new Album('creator1', 'Album1')
s1 = new Song('Song1')
s1.addAlbum(al)
console.log(al.songs.length)
```
Consider the below JavaScript program.
```javascript
const obj = [{
id: "1"
}, {
id: "2"
}, {
id: "3"
}, {
id: "4"
}, {
id: "5 "
}];
const finalObj = obj.map((element) => element.id).map((element) =>
element.trim()).map((element) => parseInt(element))).filter((element) =>
element % 2);
console.log(finalObj, finalObj.length);
```
What will be the output of the above program, if executed?
Consider the below JavaScript program.
```javascript
const property1 = 10;
const obj1 = {
property1: 20,
property2: function () {
console.log(property1, this.property1);
return this.property1;
}
}
const obj2 = {
property1: 30,
property2: () => {
console.log(property1, this.property1);
console.log(this.property2);
}
}
obj2.property2();
```
What will be the output of the above program, if executed?
Consider the below javascript program.
```javascript
const a = 2;
const b = "";
const obj1 = {
property1: 10,
property2: 20
}
const obj2 = {
a: 1,
b: 2,
...(a && !b && { obj1 })
};
console.log(obj2);
```
What will be the output of the above program, if executed?Auto-generated summary from the source site. Handy as a checklist; the lessons above explain each idea from scratch.
Variable Scoping and this Keyword
var, let, and const.this keyword in different contexts (e.g., functions, objects, arrow functions).Object-Oriented Programming in JavaScript
Object.create().Array Methods
filter, map, and reduce.Arrow Functions
Spread Operator
Vue Directives
v-model, v-if, v-show, etc.Vue Instance and Methods
Vue Lifecycle and State Management
State Management
Function Definitions
Since this summary is focused on JavaScript and Vue.js, there are no specific mathematical equations to highlight. However, understanding the syntax and behavior of various JavaScript methods and Vue directives is crucial.