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:
var: Global or function scope.let and const: Block scope.let x = 10;this Keyword:
this.name in an object method.Closures:
Example:
function outer() {
let x = 10;
function inner() {
console.log(x);
}
return inner;
}
const innerFunc = outer();
innerFunc(); // Output: 10Inheritance:
Object.create() to create a new object with the specified prototype object.Example:
const parent = { name: 'Parent' };
const child = Object.create(parent);
console.log(child.name); // Output: 'Parent'Array Methods:
filter(): Creates a new array with all elements that pass the test implemented by the provided function.Example:
const numbers = [1, 2, 3, 4, 5];
const evenNumbers = numbers.filter(num => num % 2 === 0);
console.log(evenNumbers); // Output: [2, 4]Classes and Objects:
Example:
class Person {
constructor(name) {
this.name = name;
}
}
const person = new Person('Alice');
console.log(person.name); // Output: 'Alice'Arrow Functions:
Example:
const add = (a, b) => a + b;
console.log(add(2, 3)); // Output: 5Spread Operator:
Example:
const arr1 = [1, 2, 3];
const arr2 = [...arr1, 4, 5];
console.log(arr2); // Output: [1, 2, 3, 4, 5]Template Literals:
Example:
const name = 'Alice';
console.log(`Hello, ${name}!`); // Output: 'Hello, Alice!'Vue Instance:
Example:
new Vue({
el: '#app',
data: {
message: 'Hello Vue!'
}
});Data Binding:
{{ }} for text interpolation.Example:
<div id="app">
{{ message }}
</div>Directives:
v- prefix.v-model, v-if, v-show.Computed Properties:
Example:
computed: {
reversedMessage() {
return this.message.split('').reverse().join('');
}
}Watchers:
Example:
watch: {
message(newVal, oldVal) {
console.log(`Message changed from ${oldVal} to ${newVal}`);
}
}Methods:
Example:
methods: {
greet() {
return 'Hello!';
}
}Lifecycle Hooks:
created, mounted, updated, destroyed.