Bird
Raised Fist0
Typescriptprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. In TypeScript, what happens when you assign an object variable to another variable using let b = a;?
easy
A. The original object is deleted.
B. A new copy of the object is created.
C. The object is converted to a string.
D. Both variables point to the same object in memory.

Solution

  1. Step 1: Understand variable assignment with objects

    Assigning one object variable to another copies the reference, not the object itself.
  2. Step 2: Effect of reference copying

    Both variables now point to the same object in memory, so changes via one affect the other.
  3. Final Answer:

    Both variables point to the same object in memory. -> Option D
  4. Quick Check:

    Object assignment copies reference = A [OK]
Hint: Assigning objects copies references, not objects themselves [OK]
Common Mistakes:
  • Thinking a new object is created on assignment
  • Assuming variables hold independent copies
  • Confusing object assignment with primitive assignment
2. Which of the following is the correct way to create a fresh object literal in TypeScript?
easy
A. let obj = obj2;
B. let obj = Object.assign({});
C. let obj = { name: 'Alice' };
D. let obj = new Object(obj2);

Solution

  1. Step 1: Identify fresh object literal syntax

    Using curly braces {} creates a new, fresh object literal.
  2. Step 2: Check other options

    let obj = Object.assign({}); is incorrect because Object.assign({}) needs a source object to copy from; let obj = obj2; copies reference; let obj = new Object(obj2); creates an object but with a reference or unexpected behavior.
  3. Final Answer:

    let obj = { name: 'Alice' }; -> Option C
  4. Quick Check:

    Fresh object literal uses {} = A [OK]
Hint: Use curly braces {} to create fresh objects [OK]
Common Mistakes:
  • Using assignment to copy objects instead of fresh literals
  • Misusing Object.assign without source
  • Confusing new Object() with fresh literal
3. What will be the output of this TypeScript code?
let a = { x: 1 };
let b = a;
b.x = 5;
console.log(a.x);
medium
A. 1
B. 5
C. undefined
D. Error

Solution

  1. Step 1: Understand reference assignment

    Variable b points to the same object as a.
  2. Step 2: Modify property via b

    Changing b.x to 5 changes the shared object property.
  3. Step 3: Log a.x

    Since a and b reference the same object, a.x is now 5.
  4. Final Answer:

    5 -> Option B
  5. Quick Check:

    Shared reference means change visible = B [OK]
Hint: Changing one reference changes all pointing to same object [OK]
Common Mistakes:
  • Expecting original object to remain unchanged
  • Confusing object copying with reference assignment
  • Assuming primitive copy behavior for objects
4. Identify the bug in this TypeScript code snippet:
let obj1 = { val: 10 };
let obj2 = obj1;
obj2 = { val: 20 };
console.log(obj1.val);
medium
A. obj2 assignment creates a new object, obj1 remains unchanged.
B. Both obj1 and obj2 point to the new object.
C. Syntax error due to reassignment.
D. obj1.val will be 20, which is unexpected.

Solution

  1. Step 1: Analyze initial assignment

    obj2 initially points to the same object as obj1.
  2. Step 2: Reassign obj2 to a new object

    obj2 now points to a fresh object; obj1 still points to the original.
  3. Step 3: Check console output

    obj1.val remains 10 because obj1's reference did not change.
  4. Final Answer:

    obj2 assignment creates a new object, obj1 remains unchanged. -> Option A
  5. Quick Check:

    Reassignment changes reference, original unaffected = D [OK]
Hint: Reassigning variable changes reference, original object stays same [OK]
Common Mistakes:
  • Thinking obj1 changes when obj2 is reassigned
  • Confusing property change with variable reassignment
  • Expecting both variables to point to new object
5. You want to create two independent objects in TypeScript so that changing one does not affect the other. Which code snippet achieves this correctly?
const obj1 = { a: 1 };
const obj2 = ???
obj2.a = 5;
console.log(obj1.a);
hard
A. obj2 = { ...obj1 };
B. obj2 = Object.assign(obj1);
C. obj2 = obj1;
D. obj2 = obj1 as any;

Solution

  1. Step 1: Understand shallow copy with spread operator

    Using { ...obj1 } creates a fresh object with copied properties.
  2. Step 2: Check other options

    obj2 = obj1; copies reference; obj2 = Object.assign(obj1); is incorrect usage of Object.assign; obj2 = obj1 as any; is just a type assertion, no copy.
  3. Step 3: Confirm independence

    Changing obj2.a does not affect obj1.a.
  4. Final Answer:

    obj2 = { ...obj1 }; -> Option A
  5. Quick Check:

    Spread operator creates fresh object copy = C [OK]
Hint: Use spread {...obj} to copy objects independently [OK]
Common Mistakes:
  • Assigning reference instead of copying
  • Misusing Object.assign without target
  • Confusing type assertion with copying
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard