Question
Predict the output for the following set of code :
static void Main(string[] args)Â {Â Â Â int a = 3, b = 5, c = 1;Â Â Â int z = ++b;Â Â Â int y = ++c;Â Â Â b = Convert.ToInt32((Convert.ToBoolean(z)) && (Convert.ToBoolean(y)) || Convert.ToBoolean(Convert.ToInt32(!(++a == b))));Â Â Â a = Convert.ToInt32(Convert.ToBoolean(c) || Convert.ToBoolean(a--));Â Â Â Console.WriteLine(++a);Â Â Â Console.WriteLine(++b);Â Â Â Console.WriteLine(c);Â }
static void Main(string[] args)Â {Â Â Â int a = 3, b = 5, c = 1;Â Â Â int z = ++b;Â Â Â int y = ++c;Â Â Â b = Convert.ToInt32((Convert.ToBoolean(z)) && (Convert.ToBoolean(y)) || Convert.ToBoolean(Convert.ToInt32(!(++a == b))));Â Â Â a = Convert.ToInt32(Convert.ToBoolean(c) || Convert.ToBoolean(a--));Â Â Â Console.WriteLine(++a);Â Â Â Console.WriteLine(++b);Â Â Â Console.WriteLine(c);Â }
Answer: Option C
Was this answer helpful ?
z = 6 as ++b.
y = 2 as ++c.
6 && 2 = 1
(++a == b ) which is false as 4!=6. Now, !(false) = true i.e 1.
So, 1 || 1 = 1. So, b = 1.
Similarly, c = 2 and a = 4.Now, 2 || 4 = 1.
So, a = 1.
Hence ++a = 2,++b = 2, c = 2.
Output : 2, 2, 2
Was this answer helpful ?
Submit Solution