Click here to Skip to main content
15,917,641 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            A a = new A();
            bool ret = a.returnValue(true, true);
            Console.WriteLine(ret);
            Console.ReadLine();
        }
    }

    class A
    {
        public bool returnValue(bool a, bool b)
        {
            if (a && b)
                return false;

            if (a)
                if (b)
                    return true;

            return false;
        }
    }
}


What I have tried:

I have tried putting all the possible values for both a and b
Posted
Updated 12-Feb-17 23:09pm

Your function will never return true. For "return true" to be hit both a and b must be true. However if a and b are true then the first "if (a && b)" will be executed so it will return false from that.
 
Share this answer
 
Comments
Ankur_Garg 13-Feb-17 5:11am    
This question was asked in an interview.
The problem is that your two conditions are the same:
if a and b are both true, your first condition returns false.
the second pair of conditions is the same thing: you only get to test b if a is true, and you only return true if b is also true. So to turn true, a and b must both be true - which they can't be as if they were, you have already exited from the method with a result of false!

I think you need to think a bit more about exactly what conditions you want to return true under - I can't suggest any as I don't know what you are trying to do with the method - it's name doesn't give me any clues about its logical purpose!
 
Share this answer
 
Comments
Ankur_Garg 13-Feb-17 5:10am    
This question was asked in an interview.
OriginalGriff 13-Feb-17 5:31am    
Then you should be able to answer it the same way I did! :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900