Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. brainbench test with no answer?

brainbench test with no answer?

Scheduled Pinned Locked Moved C#
questionhelp
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    brsecu
    wrote on last edited by
    #1

    I took the Brain Bench test and was asked this question. I thin the answer should be 0,30,30 but it wasn't listed. What am I doing wrong? public static void Main() { Coordinates c1 = new Coordinates(); Coordinates c2 = new Coordinates(); int x = 30; c1.X = 30; c2.X = 30; Test(ref c1, c2, x); Console.WriteLine("C1.X=" + c1.X.ToString() + ", C2.X=" + c2.X.ToString() + ", X=" + x.ToString()); Console.Read(); } public static void Test(ref Coordinates Coord1, Coordinates Coord2, int X) { Coord1 = new Coordinates(); Coord2 = new Coordinates(); Coord1.X = 0; Coord2.X = 0; X = 0; } What is the console output for the above sample code? Choice 1 C1.X=30, C2.X=30, X=30 Choice 2 C1.X=30, C2.X=0, X=00 Choice 3 C1.X=0, C2.X=0, X=30 Choice 4 C1.X=0, C2.X=0, X=0 Thanks in advance for you help

    L V 2 Replies Last reply
    0
    • B brsecu

      I took the Brain Bench test and was asked this question. I thin the answer should be 0,30,30 but it wasn't listed. What am I doing wrong? public static void Main() { Coordinates c1 = new Coordinates(); Coordinates c2 = new Coordinates(); int x = 30; c1.X = 30; c2.X = 30; Test(ref c1, c2, x); Console.WriteLine("C1.X=" + c1.X.ToString() + ", C2.X=" + c2.X.ToString() + ", X=" + x.ToString()); Console.Read(); } public static void Test(ref Coordinates Coord1, Coordinates Coord2, int X) { Coord1 = new Coordinates(); Coord2 = new Coordinates(); Coord1.X = 0; Coord2.X = 0; X = 0; } What is the console output for the above sample code? Choice 1 C1.X=30, C2.X=30, X=30 Choice 2 C1.X=30, C2.X=0, X=00 Choice 3 C1.X=0, C2.X=0, X=30 Choice 4 C1.X=0, C2.X=0, X=0 Thanks in advance for you help

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      brsecu wrote:

      What am I doing wrong?

      Nothing. You are right, whether Coordinates is a class or a struct, it is 0,30,30 :)

      Luc Pattyn [My Articles] [Forum Guidelines]

      1 Reply Last reply
      0
      • B brsecu

        I took the Brain Bench test and was asked this question. I thin the answer should be 0,30,30 but it wasn't listed. What am I doing wrong? public static void Main() { Coordinates c1 = new Coordinates(); Coordinates c2 = new Coordinates(); int x = 30; c1.X = 30; c2.X = 30; Test(ref c1, c2, x); Console.WriteLine("C1.X=" + c1.X.ToString() + ", C2.X=" + c2.X.ToString() + ", X=" + x.ToString()); Console.Read(); } public static void Test(ref Coordinates Coord1, Coordinates Coord2, int X) { Coord1 = new Coordinates(); Coord2 = new Coordinates(); Coord1.X = 0; Coord2.X = 0; X = 0; } What is the console output for the above sample code? Choice 1 C1.X=30, C2.X=30, X=30 Choice 2 C1.X=30, C2.X=0, X=00 Choice 3 C1.X=0, C2.X=0, X=30 Choice 4 C1.X=0, C2.X=0, X=0 Thanks in advance for you help

        V Offline
        V Offline
        Vrungar
        wrote on last edited by
        #3

        The answer is Choice 3 (0,0,30) if Coordinates is a class, or your answer if Coordinates is a Struct ALL classes in C# are reference types so regardless if you use the ref keyword or not, they are passed by reference. If Coordinates is a struct instead of a Class then the first parameter is passed by reference and the other 2 are passed by value.

        L 1 Reply Last reply
        0
        • V Vrungar

          The answer is Choice 3 (0,0,30) if Coordinates is a class, or your answer if Coordinates is a Struct ALL classes in C# are reference types so regardless if you use the ref keyword or not, they are passed by reference. If Coordinates is a struct instead of a Class then the first parameter is passed by reference and the other 2 are passed by value.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Nope. Just run the code, and you will see the result is 0,30,30 whether Coordinates is a struct or a class. The Test() method replaces both Coordinates by new ones, however only the first one makes it back to the caller, thanks to the ref keyword; the second Coordinates is just local to Test(). :)

          Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

          Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups