0
0
Typescriptprogramming~20 mins

Fresh object literals vs variable assignment behavior in Typescript - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Object Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of modifying fresh object literals
What is the output of the following TypeScript code?
Typescript
function update(obj: {count: number}) {
  obj.count += 1;
}

const a = {count: 0};
update(a);
update({count: 0});
console.log(a.count);
A0
B2
C1
DTypeError
Attempts:
2 left
💡 Hint
Consider which object is actually modified and which is a fresh literal.
Predict Output
intermediate
2:00remaining
Effect of variable assignment on object references
What will be logged by the following TypeScript code?
Typescript
const obj1 = {value: 10};
const obj2 = obj1;
obj2.value = 20;
console.log(obj1.value);
AReferenceError
B10
Cundefined
D20
Attempts:
2 left
💡 Hint
Think about whether obj1 and obj2 point to the same object or different objects.
🔧 Debug
advanced
2:30remaining
Why does this object mutation not persist?
Consider this TypeScript code snippet. Why does the mutation not affect the original object?
Typescript
function mutate(obj: {x: number}) {
  obj = {x: obj.x + 1};
}

const original = {x: 5};
mutate(original);
console.log(original.x);
ABecause the original object is frozen and cannot be changed.
BBecause the function reassigns the parameter to a new object, not mutating the original.
CBecause the function parameter is passed by value, so changes don't affect the original.
DBecause the console.log is called before the mutation happens.
Attempts:
2 left
💡 Hint
Look at what happens to the parameter inside the function.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in object literal assignment
Which option contains a syntax error when trying to create a fresh object literal with a conditional property?
Aconst obj = {a: 1, b: 2 if true};
Bconst obj = {a: 1, b: true ? 2 : 3};
Cconst obj = {a: 1, ...(true && {b: 2})};
Dconst obj = {a: 1, ...(false ? {b: 2} : {})};
Attempts:
2 left
💡 Hint
Check the syntax for conditional properties inside object literals.
🚀 Application
expert
3:00remaining
Predict the final object after multiple assignments and mutations
Given the following TypeScript code, what is the final value of result?
Typescript
let base = {count: 1};
let copy = base;
copy.count = 2;
copy = {count: 3};
base.count = 4;
const result = copy.count + base.count;
A7
B3
C5
D6
Attempts:
2 left
💡 Hint
Track the references and mutations carefully step-by-step.