DotNetCurry Logo

Test Your C# Basics

Posted by: Suprotim Agarwal , on 5/3/2016, in Category C#
Views: 23579
Abstract: Test your C# Basics around variables, types and operators. Post your answers in the comments section

Let’s have some fun while testing your C# basics around variables, types and operators. This quiz is aimed at developers who have some experience with C#. Note down the correct answers and post it in the comments section below. Some questions have more than one correct answers.

To make it more exciting, do not use a compiler or tool like Visual Studio to test these examples. Instead use your existing C# knowledge to choose the best option. Make sure you also post the reason why you feel a particular answer is correct.

The answers have been posted at the end of the quiz. Try to attempt the quiz without looking at the answers.

Let’s get started.

 

1. Which of the following variable names are allowed? (Choose all that apply)

a. 2name

b. _name2

c. @string

d. name-2

 

2. Value types can be assigned Null

a. True

b. False

 

3. Which of the following is true?

a. Garbage Collector cleans up old memory no longer being used on the stack

b. Garbage Collector cleans up old memory no longer being used on the heap

c. Both of the above

d. None of the above

 

4. A derived class can be a base class.

a. True

b. False

 

5. Which of the following is true?

a. If a base class has virtual methods, you can override them in derived classes

b. If a base class has abstract methods, you can override them in derived classes

c. Both of the above

d. None of the above

 

6. Which of these pieces of code will compile? (Choose all that apply)

a. var tmp = null;

b. string x = null;

     var tmp = x;

c. var tmp = (string)null;

d. var v = new object();

 

7. Which of the following statements is true? (Choose all that apply)

a. A Class can implement more than one interface

b. A Struct supports inheritance

c. A Class can implement more than one base class

d. A Class can be converted to Struct and vice-versa

 

8. Which of the following statements are true? (Choose all that apply)

a. out parameters must be initialized inside the called method

b. out parameters must be initialized inside the calling method

c. ref parameters must be initialized inside the called method

d. ref parameters must be initialized inside the calling method

 

9. Which of the following statements are true? (Choose all that apply)

a. All Operator overloads must be public and static

b. We can have multiple overloads of the same operator

c. You can overload the ‘&&’ and ‘||’ operators

d. Relational operators (eg: ==,!=) when overloaded, must return either a char, int or bool

 

10. What will the following piece of code return?

DateTime? dt = new DateTime();
dt = null;
Console.WriteLine(dt.ToString());

a. null

b. An empty string

c. The current date and time

d. The code will not compile

 

11. What is the value of ‘x’?

public static void Main()
{
    var x = 1;
    var y = 2;
    var z = 3;
    x = (y==z) ? 4 : 5;
    Console.WriteLine(x);
}

a. 2

b. 3

c. 4

d. 5

 

12. What is the value of ‘y’?

public static void Main()
{
    int? x = null; 
    int y = x ?? -100;
    Console.WriteLine(y);
}

a. null

b. 100

c. -100

d. 0 (zero)

 

13. The value of ‘y’ and ‘z’ in the following piece of code will be:

public static void Main()
{
    double x = 2.6;
    int y = Convert.ToInt32(x);
    int z = (int)x;
    
    Console.WriteLine(y);
    Console.WriteLine(z);
}

a. 2, 2

b. 3, 3

c. 2, 3

d. 3, 2

 

14. To read the value of a private field on a class, which BindingFlags will you use? (Choose all that apply)

a. Instance

b. Nonpublic

c. IgnoreReturn

d. Default

 

15. What is the output of the following statements?

public static void Main()
{
    Console.WriteLine(Math.Round(0.5));
    Console.WriteLine(Math.Round(1.5));
    Console.WriteLine(Math.Round(2.5));
}

a. 0, 2, 2

b. 0, 2, 3

c. 1, 2, 2

d. 1, 2 ,3


Some questions have more than one correct answer.

All those who complete the quiz first and with all correct answers will get a mention in our next magazine edition, with a special mention to the one who completes it first!! Have fun.

Update: Here are the answers:

1. b, c

2. b

3. b

4. a

5. c

6. b, c, d

7. a

8. a, d

9. a, b

10. b.

11. d

12. c

13. d

14. a, b

15. a

Huzefa Kagdi was the only person so far who got all answers correct. Well done Huzefa. You have got some Ninja C# skills!!

Was this article worth reading? Share it with fellow developers too. Thanks!
Share on Google+
Further Reading - Articles You May Like!
Author
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and a new one recently at The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for nine times in a row now. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that represents premium web sites and digital publications comprising of Professional web, windows, mobile and cloud developers, technical managers, and architects.

Get in touch with him on Twitter @suprotimagarwal, LinkedIn or befriend him on Facebook



Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!