Pass by value means the function receives a copy of the argument's value. Changes inside the function do not affect the original variable.
Click to reveal answer
beginner
Explain pass by reference.
Pass by reference means the function receives a reference (or address) to the original variable. Changes inside the function affect the original variable.
Click to reveal answer
intermediate
What is the difference between pass by value and pass by reference?
Pass by value sends a copy of data, so changes don't affect the original. Pass by reference sends the actual location, so changes affect the original variable.
Click to reveal answer
intermediate
What does pass by value-result mean?
Pass by value-result copies the argument's value to the function (like pass by value) and then copies it back to the original variable after the function finishes.
Click to reveal answer
advanced
Describe pass by name parameter passing.
Pass by name delays evaluation of the argument until it is used inside the function, substituting the argument expression directly. It behaves like textual substitution.
Click to reveal answer
Which parameter passing mechanism allows the function to modify the caller's variable directly?
APass by value-result
BPass by reference
CPass by value
DPass by name
✗ Incorrect
Pass by reference passes the actual address, so changes inside the function affect the original variable.
In which parameter passing method is the argument evaluated only when used inside the function?
APass by value
BPass by value-result
CPass by reference
DPass by name
✗ Incorrect
Pass by name delays evaluation until the argument is used, substituting the expression directly.
What happens to the original variable in pass by value-result after the function finishes?
AIt is updated with the function's local copy
BIt is passed by reference
CIt is deleted
DIt remains unchanged
✗ Incorrect
Pass by value-result copies back the local changes to the original variable after the function ends.
Which parameter passing mechanism is safest to avoid unintended side effects?
APass by name
BPass by reference
CPass by value
DPass by value-result
✗ Incorrect
Pass by value uses copies, so the original data cannot be changed by the function.
Which mechanism can cause unexpected behavior due to multiple evaluations of the argument?
APass by name
BPass by reference
CPass by value
DPass by value-result
✗ Incorrect
Pass by name substitutes the argument expression directly, so if used multiple times, it evaluates multiple times.
Explain the main differences between pass by value and pass by reference with examples.
Think about whether the function can change the caller's variable.
You got /4 concepts.
Describe pass by value-result and pass by name, and discuss when each might be useful.
Consider copying values vs delaying evaluation.
You got /4 concepts.
Practice
(1/5)
1. Which parameter passing mechanism sends a copy of the argument to the function, so changes inside the function do not affect the original variable?
easy
A. Pass by name
B. Pass by reference
C. Pass by value
D. Pass by result
Solution
Step 1: Understand pass by value
Pass by value means the function receives a copy of the argument, so any changes inside the function do not affect the original variable.
Step 2: Compare with other mechanisms
Pass by reference sends the original variable, so changes affect it. Pass by name and result behave differently and are less common.
Final Answer:
Pass by value -> Option C
Quick Check:
Copy sent, original unchanged = Pass by value [OK]
Hint: Copy means pass by value, original means pass by reference [OK]
Common Mistakes:
Confusing pass by value with pass by reference
Thinking pass by name sends a copy
Assuming pass by result is the same as pass by value
2. Which of the following is the correct syntax to declare a function parameter passed by reference in C++?
easy
A. void func(int &x);
B. void func(int x);
C. void func(ref int x);
D. void func(*int x);
Solution
Step 1: Recall C++ reference syntax
In C++, passing by reference uses the ampersand (&) symbol after the type in the parameter list.
Step 2: Analyze options
void func(int &x); uses int &x which is correct. void func(int x); passes by value, C is invalid syntax, and D is pointer syntax, not reference.
Final Answer:
void func(int &x); -> Option A
Quick Check:
C++ reference uses & in parameter [OK]
Hint: Look for & symbol in parameter for C++ reference [OK]
Common Mistakes:
Confusing pointer (*) with reference (&)
Using invalid 'ref' keyword in C++
Assuming no symbol means pass by reference
3. Consider the following pseudocode using pass by reference:
function increment(x) {
x = x + 1
}
var a = 5
increment(a)
print(a)
What will be the output?
medium
A. 5
B. Undefined
C. Error
D. 6
Solution
Step 1: Understand pass by reference effect
Since x is passed by reference, the function changes the original variable a by adding 1.
Step 2: Trace the code execution
a starts at 5, increment adds 1 making it 6, then print outputs 6.
Final Answer:
6 -> Option D
Quick Check:
Pass by reference changes original = 6 [OK]
Hint: Pass by reference changes original variable [OK]
Common Mistakes:
Assuming pass by reference behaves like pass by value
Thinking the original variable remains unchanged
Ignoring the effect of increment inside the function
4. Identify the error in this code snippet using pass by value:
function swap(x, y) {
temp = x
x = y
y = temp
}
var a = 3
var b = 4
swap(a, b)
print(a, b)
medium
A. Print statement is incorrect
B. swap function does not swap original variables
C. Variables a and b are not declared
D. Syntax error in function definition
Solution
Step 1: Understand pass by value behavior in swap
Since parameters are passed by value, swap changes only local copies x and y, not original a and b.
Step 2: Check the output effect
After swap call, a and b remain 3 and 4, so swap does not work as intended.
Final Answer:
swap function does not swap original variables -> Option B
Quick Check:
Pass by value means original unchanged [OK]
Hint: Pass by value swaps local copies only [OK]
Common Mistakes:
Assuming swap changes original variables with pass by value
Looking for syntax errors where none exist
Ignoring parameter passing mechanism effect
5. You want to write a function that modifies a list of numbers by doubling each element. Which parameter passing mechanism is best to use to ensure the original list is changed efficiently?
hard
A. Pass by reference
B. Pass by value
C. Pass by name
D. Pass by result
Solution
Step 1: Consider efficiency and effect on original list
Pass by value copies the entire list, which is inefficient and changes won't affect original.
Step 2: Choose mechanism that modifies original efficiently
Pass by reference sends the original list, allowing direct modification without copying.
Final Answer:
Pass by reference -> Option A
Quick Check:
Modify original efficiently = Pass by reference [OK]
Hint: Modify original data efficiently with pass by reference [OK]
Common Mistakes:
Choosing pass by value and expecting original change
Confusing pass by name with pass by reference
Ignoring efficiency of copying large data
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.