DEV Community 👩‍💻👨‍💻

Cover image for 📝GPT-3 Takes on a C# Interview: Can AI Ace the Coding Questions?
ByteHide
ByteHide

Posted on • Updated on • Originally published at bytehide.com

📝GPT-3 Takes on a C# Interview: Can AI Ace the Coding Questions?

Can artificial intelligence pass a C# coding interview? That’s the question many are asking as GPT-3, the latest AI technology, is put to the test. GPT-3 is an advanced AI system that has been trained on millions of data points, and can generate human-like natural language responses. But can it answer complex coding questions?

To find out, in this article, both you and I will become recruiters and we will put GPT-3 through a C# coding interview to see if it could pass the test. From understanding object-oriented programming to knowing the basics of the .NET framework. So recruiter, do you thing that AI — our candidate — can pass the C# coding interview? Let’s try!


GPT-3 exam begins

Let’s test this AI with some C# interview questions from the GitHub list created by Kansiris that has the top 500 C# questions and answers. At first we will try with simpler and more generic questions and seeing how well they perform, we will increase the level of difficulty.

First let’s see if AI is ready for the test.

Image description

The interview starts with 3, 2, 1… Go!


What is an Array?

Yes, it is a simple question but let’s remember that we are asking an AI and we will gradually see what it is capable of.

Human: What is an Array in C#?

AI: An array in C# is a type of data structure that allows you to store multiple values of the same type in one single variable. It is normally used for storing and manipulating collections of data.

I don’t dislike it at all but I think it can be more highly responsive, let’s change some settings and try again:

Human: Detail the answer

AI: An array is a contiguous memory area used to store a collection of data elements of the same data type in a single variable. It allows fast access to the data and manipulation of the elements stored within it.

Arrays can be one-dimensional, two-dimensional, or multi-dimensional. A one-dimensional array holds a single row of elements while a two-dimensional array holds a set of rows and columns. More complex structures like multi-dimensional arrays allow for flexibility in data storage and manipulation.

In C#, an array can be declared using the “array” keyword followed by the data type of the elements and the number of elements that need to be stored in the array enclosed in square brackets.

An example of a one-dimensional array declaration would be: int[] myArray = new int [10];. This declares an array that can store 10 integers.

Well, any questions on your part? Not on my side, so let’s continue:


Which class is at the top of .NET class hierarchy?

Second easy question (I have defined the difficulty level based on the fact that it’s an artificial intelligence so maybe this C# interview is easy for it).

Human: Which class is at the top of .NET class hierarchy?

AI: The System.Object class is at the top of the .NET class hierarchy. The System.Objectclass defines several methods and properties that can be used by any other class, so it is the ancestor of all other classes in .NET.

Okay, the response is correct. Let’s continue.


What’s the C# syntax to catch any possible exception?

Human: What’s the C# syntax to catch any possible exception?

AI: The C# syntax to catch any possible exception is as follows:

try
  {
    // Statements that might throw an exception
  }
catch (Exception e)
  {
    // Statements to handle the exception
  }
Enter fullscreen mode Exit fullscreen mode

Wow, it’s giving curious results so far. I knew I had the ability to write code but not to what extent.

What do you think recruiter? Is AI qualified to move on to the next round of questions? Let’s get to it!


Can “this” be used within a static method?

Human: Can this be used within a static method in C#?

AI: No, the this keyword cannot be used within a static method in C# as it is a non-static context. The “this” keyword can only be used inside an instance method to reference the current instance of the class.

Correct answer, although I think AI got shy. I think we need to ask him for a more detailed explanation, see what happens.

Human: Detail me the reason

AI: The reason why the this keyword cannot be used within a static method in C# is because it references the current instance of the class.

Since static methods are not associated with an instance of a class, they cannot use the this keyword. Static methods are used only to access static members of a class and do not have access to any instance members.

So far, the results we are getting are very positive.

As a reminder, we must not forget that this is not the latest version and therefore there may be current data that we are not yet taking into account.


What are partial classes?

Human: What are partial classes in C#?

AI: Partial classes are classes that are divided into multiple files in C#. They are useful when a large class needs to be split into smaller parts to make it easier to understand and maintain. Partial classes can also be used to add functionality to an existing class without having to modify the code in the existing file.

Well, let’s move on to more difficult questions to see how much our candidate is capable.


Difference between Equality Operator (==) and Equals() Method in C#

Will AI be able to compare and differentiate? Who knows, let’s try.

Human: Difference between Equality Operator (==) and Equals() Method in C#

AI: The Equality Operator (_==_) is used to compare two objects or primitive type values and determine if they are equal. It can be used with any type of data, including reference types, value types, and user-defined types.

On the other hand, the _Equals()_ Method is used to compare two objects for equality and returns a boolean value. This method is usually overridden by the classes so that their specific implementation of the _Equals()_ function can be called. Additionally, the _Equals()_ Method is not limited to primitive data types and can also be used to compare reference types.

Well, it’s a correct answer but a lot of theory.

Recruiter, shall we ask AI to explain it in a more practical way? Let’s get to it:

Human: Write a code explaining the difference between Equality Operator (==) and Equals() Method in C#

AI:

// Equality Operator (==) example
int x = 10;
int y = 20;
if (x == y)
  {
    Console.WriteLine("x and y are equal");
  }
else
  {    
    Console.WriteLine("x and y are not equal");
  }

//Equals() Method example
string str1 = "Hello";
string str2 = "World";
if (str1.Equals(str2))
  {
    Console.WriteLine("str1 and str2 are equal");
  }
else
  {
    Console.WriteLine("str1 and str2 are not equal");
  }
Enter fullscreen mode Exit fullscreen mode

AI may be the best candidate I have ever interviewed.


What is IEnumerable<> in C#?

Human: What is IEnumerable<> in C#?

AI: _IEnumerable<>_ is an interface in C# that provides a way to access a collection of objects one item at a time. It allows you to iterate over the items in a collection without needing to know the underlying implementation of the collection.

It provides methods to get or set the elements of a collection, as well as methods to check whether a particular element exists in the collection. _IEnumerable<>_ can be used with any type of collection, including lists, dictionaries, and sets.

Good, let’s ask for an example again

Human: Write an example using IEnumerable<> in C#

AI:

// Create a list of integers
List<int> myList = new List<int> { 1, 2, 3, 4, 5 };

// Get the IEnumerable from the list
IEnumerable<int> myEnumerable = myList;

// Iterate over the IEnumerable
foreach (int item in myEnumerable)
  {
    Console.WriteLine(item);
  }
Enter fullscreen mode Exit fullscreen mode

What is Virtual Method in C#?

Human: What is Virtual Method in C#?

AI: A virtual method in C# is a method that can be overridden by derived classes. The ability to override a virtual method allows subclasses to customize their behavior while still taking advantage of the base class’s implementation. Virtual methods are declared with the virtual keyword and overridden with the override keyword.


What is LINQ in C#?

Human: What is LINQ in C#?

AI: LINQ (Language Integrated Query) is a collection of technologies that provide a unified programming model for querying and manipulating data regardless of the type of data source. It uses a syntax similar to SQL to enable developers to write queries that can retrieve, filter, and group data. LINQ can be used with a variety of data sources, such as collections, databases, and XML documents.


Differentiate between Boxing and Unboxing

Human: Differentiate between Boxing and Unboxing in C#

AI: Boxing is the process of converting a value type to a reference type, while unboxing is the process of converting a reference type back to a value type.

Boxing is done implicitly when a value type needs to be converted to an object type, and unboxing is done explicitly using a cast.

Boxing is usually slower than unboxing since boxing involves creating a new object on the heap, while unboxing involves retrieving an existing value from the heap.

Good again, let’s ask for an example to understand the difference better.

Human: Write me an example of both

AI:

// Boxing example
int x = 10;
object obj = x; // Implicit boxing of the value type

// Unboxing example
object obj = 10;
int x = (int) obj; // Explicit unboxing of the value type
Enter fullscreen mode Exit fullscreen mode

Results

The GPT-3 model achieved excellent results in passing all the C# interview questions, demonstrating its capability to understand and answer questions related to the programming language.

It showed its proficiency in understanding the syntax and semantics of C#, and adeptly answered questions about various topics such as arrays, classes, methods, and LINQ. Overall, GPT-3’s performance on the C# interview was highly impressive and provides further evidence of its potential to revolutionize the AI industry.

Top comments (2)

Collapse
 
joanrosell profile image
Joan

Looks promising, but the AI made some mistakes:

  • When asked about arrays: "In C#, an array can be declared using the “array” keyword". Wrong, there is no array keyword in C#.
  • When asked about IEnumerable: "It provides methods to get or set the elements of a collection". Wrong, IEnumerable does not expose any methods to mutate the underlying collection. The interface that exposes mutation methods is ICollection.

So, yeah, it looks cool and sounds convincing but it makes some things up, which is worrying given the amount of hype around it.

If we are not careful when using this tools, we could help spread misinformation and cool-sounding lies.

Collapse
 
jwrightlgs profile image
Justin

And this is why Chat GPT is banned from Stack Overflow. Admittedly I was kind of skimming the post but I write C# code daily and did not even notice something as blatant as talking about an array keyword in C#. Very easy to see some decent answers on other stuff or be partly through a long post and just assume it's good even when it makes such blatant errors.

It's still really cool but people need to avoid using it's output without doing any verification.

Regex for lazy developers

regex for lazy devs

You know who you are. Sorry for the callout 😆