Click here to Skip to main content
15,126,501 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm writing a c# exam.

I have the question as follows

C#
/// <summary>
///     This is a huge problem for any coder.  Can you correct the code and
///     describe the issue it causes?
/// </summary>
/// <param name="count"></param>
public void Recursion(int count)
{
   if (count < 20)
   {
       Recursion(count++);
   }
}


Is there any way I can write a test for a solution without the risk of crashing the exam?

What I have tried:

I tried a property, a class, but nothing I tried can have the same affect as getting the examinee to show that they know the difference between ++var and var ++
Posted
Updated 26-Oct-18 1:37am

If you look at it closely, it's pretty obvious.

Think about what the postfix operation actually does. This may help: Why does x = ++x + x++ give me the wrong answer?[^]
   
Comments
Andy Lanng 26-Oct-18 4:25am
   
I like that idea ^_^
jsc42 26-Oct-18 5:21am
   
I think that the OP knows what is the problem with the code. His / her qn was how solutions to the problem submitted by examinees can be tested safely.
OriginalGriff 26-Oct-18 5:25am
   
That's why I suggested the article! :laugh:
1. Set up a ManualResetEvent [^]

2. Create and start a new Thread Using threads and threading | Microsoft Docs[^] that:
a. Calls the method under test
b. Sets the ManualResetEvent you created in step 1

3. (In the original thread): Call ManualResetEventSlim.Wait Method (System.Threading) | Microsoft Docs[^] with a reasonable timeout (a second should be enough as long as you do not call with int.MinValue).

4. If Wait returned false, call Thread.Abort Method (System.Threading) | Microsoft Docs[^] and fail the test.

If it had to be really robust, I would consider spinning the test off to a different appdomain or process. Having a thread run out of stack space in your main process sounds somewhat risky to me, after all it does share address space with everything else. And remember while this test is specifically designed to initial run out of stack space, any answer to any test could do this by accident. And they could also call commands to format the drive "by accident", but I assume you are not running random peoples code on your own server. :)
   
v3
Comments
Dave Kreskowiak 26-Oct-18 10:36am
   
Yeah, that's WAY overboard for such a simple problem. Also, this is a simple question on a test.

There are other ways of testing knowledge of pre and post fix operators without going into recursion.
lmoelleb 29-Oct-18 5:45am
   
Sorry, it seems I made a mistake by answering the question asked, so I guess I deserve a low vote. :P
I would choose a different test as well, but if I wanted to run the test originally asked, this is a way. Or just use a test framework that allows the user to cancel the test if it is running in an infinite loop (which is pretty much all of them as far as I know).
I suggest you two little tests.
  • Add a line printing the count value at the very begin of the Recurse method.
Then
  • Modify the recursive call, using the pre-increment operator (++cout) instead of the post-increment one.
   

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