Microsoft
98-382 · Question #15
You need to determine the values of sampleStudent.name, sampleCourse.name, and sampleCourse.grade that are output by console.log(). What are the final values for the three variables? To answer…
The code provided is: ``javascript function change(student, course) { student = "JavaScript Student"; course.name = "JavaScript"; course.grade = 100; } var sampleCourse = { "name": "HTML", "grade": 90 }; var sampleStudent = "HTML Student"; change(sampleStudent, sampleCourse)…
Variables, Data Types, and Functions
Question
You need to determine the values of sampleStudent.name, sampleCourse.name, and sampleCourse.grade that are output by console.log().
What are the final values for the three variables? To answer, select the appropriate values in the answer area.
Explanation
The code provided is:
function change(student, course) {
student = "JavaScript Student";
course.name = "JavaScript";
course.grade = 100;
}
var sampleCourse = { "name": "HTML", "grade": 90 };
var sampleStudent = "HTML Student";
change(sampleStudent, sampleCourse);
console.log(sampleStudent, sampleCourse.name, sampleCourse.grade);
Let's trace the execution:
sampleCourseis initialized as{ "name": "HTML", "grade": 90 }.sampleStudentis initialized as"HTML Student".- The
changefunction is called withsampleStudent(a string, passed by value) andsampleCourse(an object, passed by reference).- Inside
change,student = "JavaScript Student";reassigns the localstudentparameter. This does not affect the originalsampleStudentvariable outside the function because strings are primitive types and passed by value. course.name = "JavaScript";modifies thenameproperty of thecourseobject. Sincecourseis a reference tosampleCourse, this changessampleCourse.nameto"JavaScript".course.grade = 100;modifies thegradeproperty of thecourseobject. This changessampleCourse.gradeto100.
- Inside
Therefore, after the change function call:
sampleStudentremains"HTML Student".sampleCourse.namebecomes"JavaScript".sampleCourse.gradebecomes100.
The console.log() will output these final values.
Answer Area Selections (as shown with red boxes on page 25):
sampleStudent =JavaScript Student (This is incorrect in the provided answer key.sampleStudentshould remain "HTML Student". The image has a red box around 'JavaScript Student'. This is likely an error in the source's provided solution. Based on JavaScript pass-by-value/reference,sampleStudentshould remain "HTML Student". If the question intendedstudentto be an object with anameproperty, the outcome would be different. Given"HTML Student"is a string primitive, it's passed by value.)sampleCourse.name =JavaScriptsampleCourse.grade =100
Correct values based on JavaScript rules:
sampleStudent: "HTML Student"sampleCourse.name: "JavaScript"sampleCourse.grade: 100
Topics
#objects#properties#reference types#console.log
Community Discussion
No community discussion yet for this question.