0% found this document useful (0 votes)
2 views

Week 8 Assignment

The document contains a series of programming questions related to C# code snippets, asking for explanations of outputs and behaviors of the code. Each question focuses on different aspects of C# such as variable manipulation, method overriding, equality checks, and date handling. The answers require understanding of C# syntax and behavior to explain the results of the provided code examples.

Uploaded by

ndt280504
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Week 8 Assignment

The document contains a series of programming questions related to C# code snippets, asking for explanations of outputs and behaviors of the code. Each question focuses on different aspects of C# such as variable manipulation, method overriding, equality checks, and date handling. The answers require understanding of C# syntax and behavior to explain the results of the provided code examples.

Uploaded by

ndt280504
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CSharp

Answer all questions in just one file named StudentID.txt and submit your assignment on Moodle.
Question 1: What is displayed, and why?
using System;

class Test
{
static void Main()
{
int x = 5;
int y = ++x + x++;

Console.WriteLine(y);
}
}

Question 2: What is displayed, and why?


using System;

class Father
{
public virtual void doIt(int x)
{
Console.WriteLine("Father.doIt(int)");
}
}

class Son : Father


{
public override void doIt(int x)
{
Console.WriteLine("Son.doIt(int)");
}

public void doIt(object o)


{
Console.WriteLine("Son.doIt(object)");
}
}

class Test
{
static void Main()
{
Son d = new Son();
int i = 10;
d.doIt(i);
}
}
Question 3: Why does this print "False"?
using System;

public class Test


{
public static void Main()
{
double d1 = 1.000001;
double d2 = 0.000001;
Console.WriteLine((d1 - d2) == 1.0);
}
}

Question 4: What is displayed, and why?


using System;

class Test
{
static void Main()
{
int[] array1 = { 1, 2, 3 };
int[] array2 = { 1, 2, 3 };

bool areEqual = array1 == array2;

Console.WriteLine(areEqual);
}
}

Question 5: The bellow code uses the anonymous method. What does it do?
using System;
using System.Collections.Generic;

class Test
{
delegate void Printer();

static void Main()


{
List<Printer> printers = new List<Printer>();
for (int i = 0; i < 10; i++)
{
printers.Add(delegate { Console.WriteLine(i); });
}

foreach (Printer printer in printers)


{
printer();
}
}
}
Question 6: Should this code compile? Does it? What does it mean?
using System;

class Test
{
enum Ooooop { Dog, Cat};
static void Main()
{
Ooooop t = 0.0;
Console.WriteLine(t);
}
}

Question 7: What is displayed, and why?


using System;

class Test
{
static void Main()
{
Foo("Hello");
}

static void Foo(object x)


{
Console.WriteLine("object");
}

static void Foo<T>(params T[] x)


{
Console.WriteLine("params T[]");
}
}

Question 8: Explain the result of the code below


using System;

class Test
{
static void Main(string[] args)
{
DateTime dateTime = new DateTime(2024, 3, 1);
Console.WriteLine(dateTime); // Outputs: 3/1/2024 12:00:00 AM

dateTime.AddHours(24);
Console.WriteLine(dateTime); // Still outputs: 3/1/2024 12:00:00 AM
}
}

You might also like