(Full Stack JavaScript) Object & Object Obstructors Knowledge Assessment Q & S 2024, Exams of Programming Languages

(Full Stack JavaScript) Object & Object Obstructors Knowledge Assessment Q & S 2024(Full Stack JavaScript) Object & Object Obstructors Knowledge Assessment Q & S 2024(Full Stack JavaScript) Object & Object Obstructors Knowledge Assessment Q & S 2024(Full Stack JavaScript) Object & Object Obstructors Knowledge Assessment Q & S 2024

Typology: Exams

2022/2023

Available from 07/05/2024

emilio-clemente-2
emilio-clemente-2 🇺🇸

2

(2)

356 documents

1 / 24

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Full Stack JavaScript
Object & Object
Obstructors
Knowledge Assessment
Q & S
2024
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18

Partial preview of the text

Download (Full Stack JavaScript) Object & Object Obstructors Knowledge Assessment Q & S 2024 and more Exams Programming Languages in PDF only on Docsity!

Full Stack JavaScript

Object & Object

Obstructors

Knowledge Assessment

Q & S

  1. Which of the following keywords is used to create a new object in JavaScript? a. class b. instance c. new d. object

Answer: c. new

Rationale: The new keyword is used to create a new instance of an object using a constructor function.

  1. What is the output of the following code snippet?
    function Person(name) { this.name = name; } const person1 = new Person("Alice"); console.log(person1 instanceof Person); 
    a. true b. false

Answer: c. An object from which other objects inherit properties

Rationale: In JavaScript, prototypes allow objects to inherit properties and methods from other objects.

  1. What will be the output of the following code?
    function Car(make, model) { this.make = make; this.model = model; } Car.prototype.getDetails = function() { return this.make + " " + this.model; } const myCar = new Car("Toyota", "Corolla"); console.log(myCar.getDetails()); 
    a. undefined undefined b. Toyota Corolla c. Toyota undefined d. Error

Answer: b. Toyota Corolla

Rationale: The method getDetails in the prototype correctly returns the make and model of the car.

Fill-in-the-Blank Questions

  1. In JavaScript, the __________ property of a function allows you to define properties and methods that can be shared among all instances.

Answer: prototype

Rationale: The prototype property lets you add methods and properties to all instances of a function or object.

  1. The process of creating a new instance of an object using a constructor function is called __________.

Answer: instantiation

Rationale: Instantiation is the process of creating an instance of an object from a class or constructor function.

  1. In JavaScript, every function (constructor) has a built-in object called __________, which is linked to all instances created by that constructor.

Rationale: Methods on a prototype are shared among all instances created from the constructor.

  1. True or False: In JavaScript, you can only create objects using constructor functions.

Answer: False

Rationale: Objects can also be created using object literals, Object.create(), and class syntax.

  1. True or False: JavaScript supports multiple inheritance.

Answer: False

Rationale: JavaScript only supports single inheritance through prototypes. However, mixins can be used to achieve multiple inheritance- like behavior.

  1. True or False: The Object.create() method allows you to specify the prototype of a new object.

Answer: True

Rationale: Object.create() creates a new object with the specified prototype object.

  1. True or False: A constructor function in JavaScript can return an object that overrides the default instance.

Answer: True

Rationale: If a constructor function explicitly returns an object, this object will be the result of the new expression, overriding the default object.

Advanced Multiple Choice Questions

  1. How can you implement private properties in JavaScript for an object? a. Using closures and local variables inside a constructor function b. By prefixing properties with an underscore c. Using the private keyword d. Both a and b

Answer: d. Both a and b

Rationale: Private properties can be simulated using closures (a), and by convention, an underscore (b) indicates private properties (though this is not enforced by the language).

  1. Which of the following correctly defines a simple class in ES6 syntax with a method?
     

constructor(height, width){ } calcArea() { return this.height this.width; } }

 ## Answer: a. Rationale: The ES6 class syntax is correctly used in option (a), where the constructor is defined properly, and the method `calcArea` is outside the constructor. 18. What will be the output of the following code? ```javascript class Animal { constructor(name) { this.name = name; } speak() { console.log(`${this.name} makes a noise`); } } class Dog extends Animal { speak() { console.log(`${this.name} barks`); } } const d = new Dog('Mitzie'); d.speak(); 

a. Mitzie makes a noise b. Mitzie barks c. Error d. undefined barks

Answer: b. Mitzie barks

Rationale: The Dog class overrides the speak method of the Animal class, so barks is printed instead of makes a noise.

  1. Which method would you use to check if an object has a particular property as its own (not inherited)? a. in b. hasOwnProperty

d) To execute a function

Correct Answer: c) To create multiple objects of the same type

Rationale: Constructor functions are a fundamental concept in JavaScript used to create multiple instances of an object, each with their own unique properties.

  1. True/False: The 'this' keyword within a constructor function always refers to the global object.

Answer: False

Rationale: In a constructor function, the 'this' keyword refers to the instance of the object that is being created, not the global object.

  1. Fill-in-the-Blank: An object's prototype can be accessed using the __________ property.

Correct Answer: proto

Rationale: The proto property is a reference to the prototype of the object from which properties and methods are inherited.

  1. Multiple Choice: Which of the following is not a valid way to create an object in JavaScript? a) Using the Object() constructor b) Using the {} object literal notation c) Using the create() method of the Object prototype d) Using the new keyword before a string

Correct Answer: d) Using the new keyword before a string

Rationale: The new keyword is used before a constructor function, not a string, to create an instance of an object.

  1. True/False: Object constructors can contain local variables that are not accessible outside the constructor function.

Answer: True

Rationale: Local variables within a constructor function are private to that function and cannot be accessed outside of it.

  1. Fill-in-the-Blank: To add a method to all instances of an object created by a constructor function, you should add it to the constructor's _________.

Correct Answer: prototype

Rationale: Adding a method to the constructor's prototype property ensures that all instances of the object inherit the method.

  1. Multiple Choice: What does the 'new' keyword do in JavaScript? a) Instantiates a new object b) Declares a new variable c) Creates a new function d) All of the above

Correct Answer: a) Instantiates a new object

Rationale: The 'new' keyword is used in JavaScript to create an instance of an object from a constructor function.

Rationale: Properties can be added to JavaScript objects at any time, even after they have been created.

  1. Fill-in-the-Blank: A constructor function in JavaScript is a special type of function designed to _________ objects.

Correct Answer: initialize

Rationale: Constructor functions are used to initialize new objects with specific properties and methods.

  1. Multiple Choice: Which keyword is used to define a constructor function in JavaScript? a) constructor b) create c) function d) class

Correct Answer: c) function

Rationale: In JavaScript, a constructor function is defined using the 'function' keyword.

  1. True/False: Every JavaScript object has a constructor property that references the function that created it.

Answer: True

Rationale: The constructor property of an object points to the function that created that particular object.

  1. Fill-in-the-Blank: The _________ pattern in JavaScript is used to simulate the concept of classes before ES6.

Correct Answer: constructor/prototype

Rationale: The constructor/prototype pattern was commonly used to simulate class-like behavior in JavaScript before the introduction of the class syntax in ES6. a. Which of the following statements about object-oriented programming is true? i. Objects are instances of classes. ii. Objects do not have properties or methods. iii. Objects cannot be passed as arguments to functions. iv. Objects cannot be modified once created.

Correct Answer: i. Objects are instances of classes.

Rationale: Understanding the relationship between objects and classes is fundamental to object-oriented programming. Objects are instances of classes, inheriting their properties and methods. Fill-in-the-Blank Classes are templates for creating objects. They define the initial state and behavior that objects created from them will have.

Correct Answer: objects

Rationale: The 'new' keyword is used to create a new instance of a constructor function, allowing for the creation of multiple objects with shared properties and methods. Fill-in-the-Blank An object constructor in JavaScript is a function that is used to create multiple instances of an object with the same structure.

Correct Answer: object constructor

Rationale: Object constructors provide a convenient way to create multiple objects with consistent properties and methods, promoting code reusability. True/False In JavaScript, every object has a prototype property that allows for inheritance of properties and methods.

Correct Answer: True

Rationale: The prototype property in JavaScript enables objects to inherit properties and methods from other objects, facilitating the concept of inheritance in object-oriented programming. Multiple Choice

c. Which of the following methods is used to create a new object with a specified prototype object in JavaScript? i. Object.create() ii. Object.defineProperties() iii. Object.freeze() iv. Object.keys()

Correct Answer: i. Object.create()

Rationale: The Object.create() method in JavaScript is used to create a new object with a specified prototype object, allowing for prototype-based inheritance. Fill-in-the-Blank The prototype property of an object in JavaScript is used to enable object inheritance.

Correct Answer: prototype, inheritance

Rationale: The prototype property establishes a chain of inheritance in JavaScript, allowing objects to inherit properties and methods from prototype objects. True/False