nerdexam
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:

  1. sampleCourse is initialized as { "name": "HTML", "grade": 90 }.
  2. sampleStudent is initialized as "HTML Student".
  3. The change function is called with sampleStudent (a string, passed by value) and sampleCourse (an object, passed by reference).
    • Inside change, student = "JavaScript Student"; reassigns the local student parameter. This does not affect the original sampleStudent variable outside the function because strings are primitive types and passed by value.
    • course.name = "JavaScript"; modifies the name property of the course object. Since course is a reference to sampleCourse, this changes sampleCourse.name to "JavaScript".
    • course.grade = 100; modifies the grade property of the course object. This changes sampleCourse.grade to 100.

Therefore, after the change function call:

  • sampleStudent remains "HTML Student".
  • sampleCourse.name becomes "JavaScript".
  • sampleCourse.grade becomes 100.

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. sampleStudent should 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, sampleStudent should remain "HTML Student". If the question intended student to be an object with a name property, the outcome would be different. Given "HTML Student" is a string primitive, it's passed by value.)
  • sampleCourse.name = JavaScript
  • sampleCourse.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.

Full 98-382 Practice