Jaegool_'s log

Coding Interview prep: Fundamental 본문

Job

Coding Interview prep: Fundamental

Jaegool 2023. 12. 30. 07:05

What is immutable & mutable?

An immutable object is an object whose state cannot be modified after it is created.

 

Java

Immutable Objects: String, Wrapper Classes(Integer, Double, Float, etc.)

Mutable Objects: StringBuilder and StringBuffer, Arrays, Custom Classes

 

C++

Immutable Objects: Const Objects

Mutable Objects: Standard Library Containers(std::vector, std::map, etc.), Strings, Custom Classes

 

Python

Immutable Objects: tuples, strings

Mutable Objects: Lists, dictionaries 

 

 

How does the memory allocation work?

Memory allocation for objects involves assigning a specific memory space to an object so that it can store its value or state. When an object is created, the programming language's runtime environment allocates a portion of memory for that object.

- For immutable objects, if you try to modify the object, a new object is often created in a different memory location with the new value, leaving the original object unchanged.

- For mutable objects, changes are made directly in the same memory space where the object resides.

 

Explain calling by reference & value.

In call by value, a copy of the actual argument's value is passed to the function. Therefore, changes made to the parameter inside the function have no effect on the original argument. This is common in languages like C where the function creates its own copy of the arguments.

 

In call by reference, a reference to the actual argument (rather than a copy) is passed to the function. This means that changes made to the parameter inside the function are reflected in the original argument. This is the default behavior in languages like Python and JavaScript, where objects are often passed by reference, meaning the function can modify the original object.