Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

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
Question ajoutée par Mohannad Bakbouk , Full Stack Web Developer , Almohtaseb
Date de publication: 2016/10/19
Michael Magdy Wassili Wassili
par Michael Magdy Wassili Wassili , Senior Developer , landmark group

the Correct answer

d) In elseIn main

Ahmed Elagamy
par Ahmed Elagamy , Sharepoint Technical Consultant , Petrochemical Industries Company

correct answer (b) in esle

Awwab Ahmed
par Awwab Ahmed , Web-Developer, Graphic-Designer. , Conobio

Answer: d

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

Utilisateur supprimé
par Utilisateur supprimé

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
par 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
par 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
par 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
par 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
par Mohannad Bakbouk , Full Stack Web Developer , Almohtaseb

The  program is Right and Working 

Atif Jalal
par 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 =

More Questions Like This