ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

Select the output for the following set of code?

    static void Main(string[] args)

        {

            int i =;

            int j = %;

            if (Convert.ToBoolean(Convert.ToInt(i = j)))

            {

                Console.WriteLine("In if");

            }

            else

            {

                Console.WriteLine("In else");

            }

            Console.WriteLine("In main");

            Console.ReadLine();

        }

a) In ifb) In elsec) In ifIn maind) In elseIn main

user-image
تم إضافة السؤال من قبل Mohannad Bakbouk , Full Stack Web Developer , Almohtaseb
تاريخ النشر: 2016/10/19
Michael Magdy Wassili Wassili
من قبل Michael Magdy Wassili Wassili , Senior Developer , landmark group

the Correct answer

d) In elseIn main

Ahmed Elagamy
من قبل Ahmed Elagamy , Senior Sharepoint & Dot Net Developer , Zak Solutions for Computer Systems

correct answer (b) in esle

Awwab Ahmed
من قبل Awwab Ahmed , Web-Developer, Graphic-Designer. , Conobio

Answer: d

Explanation :Usage of ‘=’ operator instead of ‘==’ operator .hence,the condition is not true.

مستخدم محذوف‎
من قبل مستخدم محذوف‎

First of all the code written has some errors in it

 

Let's first clean the code.

  • "int i =; "  :  this will throw error because u need to assign integer to the variable like int i = 0 or int i=1; etc;
  • "int j=%;  "  : this will again throw error because u cannot assign percentage to integer. It has to be again something like this int j=1 or int j=2;
  • "if (Convert.ToBoolean(convert.Toint(i=j))) " :   here int has to be converted to int16 , int 32 or int 64. 
  • "if (Convert.ToBoolean(convert.Toint(i=j))) " : here i=j  condition willl not work because you will have to " ==" to compare two integers instead of "=".

 

Now suppose you assign :

  • int i = 0;
  • int j = 0;

the output result will be :

In If

In main

 

OR

 

if you assign :

  • int i = 0;
  • int j = 1;

the output result will be :

In else

In main

 

Note : Here, In main will render in all situation because it is written out of if else condition.

 

Waqas Ahmed
من قبل Waqas Ahmed , Tech. PM / Software Designer , Aga Khan University Hospital

The program is messed up:

int i =; is an invalid expression. A value must be assigned.

int j=%; is also an invalid expression. Again a value must be assigned.

toInt = Convert fn. does not contain definition of toInt.

Comparison operator equality is "==" instead of "="

Houssem Gharsallah
من قبل Houssem Gharsallah , Software Architect , NOMD Technologies

The values of i and j are invalid. they are integers. plus your equality condition is wrong (==).

However, if i == j then the answer would be b)

otherwise it's d)

Muhammad Umar Javed
من قبل Muhammad Umar Javed , Senior Software Engineer , Synergy-IT Pakistan

There two things with this program:

1. There are some errors for e.g int i=; int j=%; are wrong statements and compiler will generate compile time error. variables of data type int must be initialized properly by providing some value say int i=0, int j=2; etc.2. The calls to functions Convert.ToBoolean and Convert.ToInt are redundant. Convert.ToInt is normaly used to convert non-int types to int. similarly Convert.ToBoolean is used to convert non-boolean types to boolean. By analyzing the program these two calls are redundant as they main purpose is to check if i==j or not. so replace simply by if(i==j) that will do the same as both variables have same type.

Asad Mehmood Akram
من قبل Asad Mehmood Akram , Sr. Software Developer , Contour Software

For instance if we ignore compilation issues, following is the output

(d) In else

In main

Mohannad Bakbouk
من قبل Mohannad Bakbouk , Full Stack Web Developer , Almohtaseb

The  program is Right and Working 

Atif Jalal
من قبل Atif Jalal , Front-End Web Developer , Brain Plow

int i =; which is invalid expression

int j=%; invalid expression

toInt = convert does not contain defination of toInt.

comparison operator equality is == instead of =

المزيد من الأسئلة المماثلة