What is the output of the following code snippet?
class Program
{
public string GetMarks(int Marks)
{
if ((Marks >= 100) && (Marks < 80))
return "Very good";
else if ((Marks >= 80 && Marks < 100))
return "good";
else if (Marks <= 60)
return "failure";
else if (Marks >= 60 && Marks < 90)
return "good";
else return "No records found";
}
static void Main(string[] args)
{
Program pObj = new Program();

Console.WriteLine(pObj.GetMarks(60));
}
}

 Posted by Nagasundar_Tn on 12/4/2012 | Category: C# Interview questions | Views: 2892 | Points: 40
Answer:

The output will be "Failure"

Here I used If-Else condition. As per if else logic if "if" part gets true then all other else parts are omitted eventhough the conditon might true.
Here the last condition "if (Marks >= 60 && Marks < 90)" satisfies condition if we give value 60 but third condition "if (Marks <= 60)" also gets true. Since
thrid condition itself gets true fourth condition will not be executed though it is true as well.


Asked In: Ramco Systems | Alert Moderator 

Comments or Responses

Login to post response