Discussion
If s and b are integers then purpose of the following program fragment is to
b = s + b ;
s = b - s ;
b = b - s ;
Answer: C
Correct answer: exchange (swap) the contents of s and b
Trace the three assignments with initial values \( s \) and \( b \):
\[ b \leftarrow s+b, \qquad s \leftarrow (s+b)-s = b, \qquad b \leftarrow (s+b)-b = s \]
so the two values are exchanged without a temporary. The technique is a curiosity rather than good practice: the intermediate sum can overflow, and a compiler produces better code from an ordinary swap using a temporary variable.
No comment is present. Be the first to comment.
Loading…
Post your comment
Please log in or register to post a comment.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development