Tricky Question 1 | C++ Tricky Questions

 


  • Guys now a days many of us after familiar with c++  do mistakes ,Here we come up with solution.
  • Daily we post tricky question here ,which will increase your way of thinking and reduce your simple mistakes
  • Question : Solve the Question and post the first answer you got, in comment box .
    1. #include <bits/stdc++.h>
    2. using namespace std;
    3. int main()
    4. {
    5. int a = 6,b = 3;
    6. if(b = 5)
    7. b--;
    8. a++;
    9. cout<<a<<" "<<b;
    10. return 0;
    11. }
  • Many of you thinks the output of this question = 7 3
  • But this is totally wrong 👀💣 . You are mistaken and done wrong .
  • Go through our solution and rectify you mistakes .
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  Solution : 
  • Many of us thinks in code in line 6 b is not equals to 5 ,so if statement won't execute. Hence answer will be 7 3.
  • But this is wrong ,if we observe clearly in line 6 if statement contains assignment operator ("=") not relational operator("==").
  • As we can observe the code in line number 6 it is given if(b = 5) ,  it means it will assign 5 to b and returns true in if statement .
  • So  now b becomes  5 and inside if statement it will be like if( true ) .
  • Hence line 7 will executes and b will be equals to 4 .
  • As usual a will be come 7 in line 8 .
  • Hence , the answer is 7 4

Guys if you have any doubts, please let me know

Post a Comment (0)
Previous Post Next Post

How to start Coding | coding for beginners | Lecture 1