Author Topic: Brainteasers  (Read 1480 times)

0 Members and 1 Guest are viewing this topic.

Jeff H

  • Needs a day job
  • Posts: 6150
Brainteasers
« on: August 15, 2011, 06:48:51 PM »
Nothing too hard or crazy but see if you can guess and I will post the link to answers later or you can run them to find out answers.
 
Will post C# & VB and they might or might not give same results. This was in C# but did it VB for VB guys.
 
See if you can guess output for each section of code.
 
1. Overloading
 
C#
Code: [Select]
using System;
namespace BrainTeasersCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
             Derived d = new Derived();
            int i = 10;
            d.Foo(i);
            Console.ReadKey();
        }
    }
    class Base
    {
        public virtual void Foo(int x)
        {
            Console.WriteLine("Base.Foo(int)");
        }
    }
    class Derived : Base
    {
        public override void Foo(int x)
        {
            Console.WriteLine("Derived.Foo(int)");
        }
        public void Foo(object o)
        {
            Console.WriteLine("Derived.Foo(object)");
        }
    }
}

VB
Code: [Select]
Module Module1
    Sub Main()
        Dim d As New Derived()
        Dim i As Integer = 10
        d.Foo(i)
        Console.ReadKey()
    End Sub
End Module
Class Base
    Public Overridable Sub Foo(x As Integer)
        Console.WriteLine("Base.Foo(int)")
    End Sub
End Class
Class Derived
    Inherits Base
    Public Overrides Sub Foo(x As Integer)
        Console.WriteLine("Derived.Foo(int)")
    End Sub
    Public Overloads Sub Foo(o As Object)
        Console.WriteLine("Derived.Foo(object)")
    End Sub
End Class


2. Order

C#
Code: [Select]
using System;
namespace BrainTeasersCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Foo f = new Foo();
            Bar b = new Bar();
            Console.ReadKey();
        }
    }
    class Foo
    {
        static Foo()
        {
            Console.WriteLine("Foo");
        }
    }
    class Bar
    {
        static int i = Init();
        static int Init()
        {
            Console.WriteLine("Bar");
            return 0;
        }
    }
}
 
VB
Code: [Select]
Module Module1
    Sub Main()
        Dim f As New Foo()
        Dim b As New Bar()
        Console.ReadKey()

    End Sub
End Module
 Class Foo
    Shared Sub New()
        Console.WriteLine("Foo")
    End Sub
End Class
Class Bar
    Shared i As Integer = Init()
    Private Shared Function Init() As Integer
        Console.WriteLine("Bar")
        Return 0
    End Function
End Class

3. Arithmetic
 
C#
Code: [Select]
using System;
namespace BrainTeasersCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            double d1 = 1.000001;
            double d2 = 0.000001;
            Console.WriteLine((d1 - d2) == 1.0);
            Console.ReadKey();
        }
    }
}
 
VB
Code: [Select]
Module Module1
    Sub Main()
        Dim d1 As Double = 1.000001
        Dim d2 As Double = 0.000001
        Console.WriteLine((d1 - d2) = 1.0)
        Console.ReadKey()
        Console.ReadKey()

    End Sub
End Module
 

4. Print....Print
 
Code: [Select]
using System;
using System.Collections.Generic;
namespace BrainTeasersCSharp
{
    class Program
    {
        delegate void Printer();
        static void Main(string[] args)
        {
            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();
            }
            Console.ReadKey();
        }
    }
}


Code: [Select]
Imports System.Collections.Generic

Module Module1
    Private Delegate Sub Printer()
    Sub Main()
        Dim printers As New List(Of Printer)
        For i As Integer = 0 To 9
            printers.Add(Sub() Console.WriteLine(i))
        Next
        For Each printer As Printer In printers
            printer()
        Next
        Console.ReadKey()

    End Sub
End Module


Will post more if wanted!!!!..............

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Brainteasers
« Reply #1 on: August 15, 2011, 06:59:25 PM »
Answers
http://www.yoda.arachsys.com/csharp/teasers-answers.html
 
Looks like the second one prints differently for 4.0
 
From the main page here is a subject to read to many get confrused about
 
http://www.yoda.arachsys.com/csharp/parameters.html