1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Studying Software Development Fundamentals 98-361

Discussion in 'Training & Development' started by lynnietjuh, Jun 24, 2012.

  1. lynnietjuh

    lynnietjuh New Member

    4
    0
    1
    Hi guys, I'm new to programming and studying Software Development Fundamentals. Do any of you know anything about C#?

    The assessment says to write the following code snippet:
    int n = 20;
    int d = n++ + 5;
    What will be the value of d after this code snippet is executed?
    a) 25
    b) 26
    c) 27
    d) 28

    How do i work this out?
     
  2. dmarsh

    dmarsh Terabyte Poster

    3,782
    302
    184
    Err, learn basic C# syntax ? :blink

    Learn about unary operators, binary operators, operator precedence, variable declaration and assignment ?

    Failing that code the program and debug it and see what actually happens and understand why, maybe even go and look at the CIL generated by the compiler.
     
    Last edited: Jun 24, 2012
    Certifications: CITP, BSc, HND, SCJP, SCJD, SCWCD, SCBCD, SCEA, N+, Sec+, Proj+, Server+, Linux+, MCTS, MCPD, MCSA, MCITP, CCDH
  3. lynnietjuh

    lynnietjuh New Member

    4
    0
    1
    I've learned all about unary operators, ternary operators, operator precedence, variable declaration and assignment in the latest chapter of my textbook. I still don't get how to work this out?
     
  4. dmarsh

    dmarsh Terabyte Poster

    3,782
    302
    184
    1. Why don't you get it ?

    2. What answers do you think are probable and why ?
     
    Certifications: CITP, BSc, HND, SCJP, SCJD, SCWCD, SCBCD, SCEA, N+, Sec+, Proj+, Server+, Linux+, MCTS, MCPD, MCSA, MCITP, CCDH
  5. lynnietjuh

    lynnietjuh New Member

    4
    0
    1
    I don't get it because i have absolutely no idea how to work this out. Do i need to apply this code snippet to a template in MS Visual C# in order to get the answer?
     
  6. dmarsh

    dmarsh Terabyte Poster

    3,782
    302
    184
    The questions are designed to ensure you understand C# syntax, you won't have a compiler in the exam and you won't know the questions until in the exam.

    If you're really stuck while studying you can create a console app in Visual Studio and debug it, but really you should be able to answer the question from first principles just like any math arithmetic problem. For example you do not need a calculator to work out 4+5=9 do you ?

    When doing certification exams always eliminate the bad answers one by one.

    Two answers are obviously wrong just from an understanding of basic arithmetic and assignment.

    The final answer elimination involves understanding how the post increment operator works in detail.
     
    Last edited: Jun 24, 2012
    Certifications: CITP, BSc, HND, SCJP, SCJD, SCWCD, SCBCD, SCEA, N+, Sec+, Proj+, Server+, Linux+, MCTS, MCPD, MCSA, MCITP, CCDH
  7. lynnietjuh

    lynnietjuh New Member

    4
    0
    1
    From what i have read in your response, i think i need to go back to studying C# syntax. I don't seem to be following too well. It's my first time studying Software Dev and I'm having quite a bit of trouble grasping C#.
     
  8. dmarsh

    dmarsh Terabyte Poster

    3,782
    302
    184
    int n = 20; // Let n = 20
    int d = n++ + 5; // Let d = n++ + 5

    Now from this I'd expect :-

    1. n to equal 21 at program end because of the assignment of 20 and post increment operator n++.
    2. d to equal either 25 or 26 due to it equalling n + 5.

    There is no possible way for answers c or d to be correct, if you do not understand this then yes I'd say you've learnt nothing.

    The only tricky part is determining which of answers a or b is correct and why, the answer has to do with the assignment order of the post increment operator and what an expression is and the order of expression evaluation. (MS calls expression evaluation an operation)

    Here is the CIL generated for the above program :

    Code:
    .method private hidebysig static void  Main(string[] args) cil managed
    {
      .entrypoint
      // Code size       13 (0xd)
      .maxstack  3
      .locals init ([0] int32 n,
               [1] int32 d)
      IL_0000:  nop
      IL_0001:  ldc.i4.s   20
      IL_0003:  stloc.0
      IL_0004:  ldloc.0
      IL_0005:  dup
      IL_0006:  ldc.i4.1
      IL_0007:  add
      IL_0008:  stloc.0
      IL_0009:  ldc.i4.5
      IL_000a:  add
      IL_000b:  stloc.1
      IL_000c:  ret
    } // end of method Program::Main
    
    See :-

    http://msdn.microsoft.com/en-us/library/36x43w8w.aspx
    http://msdn.microsoft.com/en-us/library/aa691363.aspx I agree this explanation seems a little obtuse.
     
    Last edited: Jun 24, 2012
    Certifications: CITP, BSc, HND, SCJP, SCJD, SCWCD, SCBCD, SCEA, N+, Sec+, Proj+, Server+, Linux+, MCTS, MCPD, MCSA, MCITP, CCDH
  9. Fergal1982

    Fergal1982 Petabyte Poster

    4,196
    171
    211
    I have to admit that this did not execute as I expected it myself to be honest. That said, the only time I've ever used ++ is during a for loop. Took me a few minutes of reading the msdn documentation before I realised that I was confusing it with += (no idea why).

    Think I need to brush up on IL to be honest, as that confused me a bit too (I've never had a need to delve into it beyond playing with ILDASM),
     
    Certifications: ITIL Foundation; MCTS: Visual Studio Team Foundation Server 2010, Administration
    WIP: None at present
  10. dmarsh

    dmarsh Terabyte Poster

    3,782
    302
    184
    Last edited: Jun 24, 2012
    Certifications: CITP, BSc, HND, SCJP, SCJD, SCWCD, SCBCD, SCEA, N+, Sec+, Proj+, Server+, Linux+, MCTS, MCPD, MCSA, MCITP, CCDH
  11. dmarsh

    dmarsh Terabyte Poster

    3,782
    302
    184
    Well a+=1 is similar to ++a.

    for(int i=1; i<10; ++i)

    probably makes more sense than

    for(int i=1; i<10; i++)

    but the second form looks more natural to us humans and optimising compilers generally sort out the details for us so they end up the same...
     
    Certifications: CITP, BSc, HND, SCJP, SCJD, SCWCD, SCBCD, SCEA, N+, Sec+, Proj+, Server+, Linux+, MCTS, MCPD, MCSA, MCITP, CCDH
  12. Fergal1982

    Fergal1982 Petabyte Poster

    4,196
    171
    211
    I tell a lie, I have used ++ before, but only as an incrementor on its own (ie "x++") never as part of a calculation. In that case it doesnt really matter if you prefix or suffix the operator. That's possibly what has thrown me about it. Some of the operators are brilliant. I encountered XOR (^ in C#) for the first time about 6 months back, and can immediately see where I could get benefit from it.

    It's one of the things I love about development, there is actually no real end-point. There isn't really any point you need to pass to be good at it. Obviously, more knowledge makes you better (and more elegant) at it, but you can still be effective and good at it without knowing everything there is to know about the language.
     
    Last edited: Jun 24, 2012
    Certifications: ITIL Foundation; MCTS: Visual Studio Team Foundation Server 2010, Administration
    WIP: None at present
  13. dmarsh

    dmarsh Terabyte Poster

    3,782
    302
    184
    Bitwise operators are a pretty fundamental part of computer science and computer programming, in fact computer chips could be built entirely from XOR and NAND gates if required. Generally bitwise operators are more useful when interfacing with hardware or working with binary protocols or file formats both of which many high level programmers rarely do directly these days, they use intermediate libraries or APIs.

    There is of course the famous swap trick with XOR also.

    Generally a lot of coding standard documents advise against using such 'side effects' and advise ensuring post increment is a stand alone expression. So as not to confuse novice coders.

    Well the thing with programming is often people think they are competent when they are not ;)

    I've witnessed senior programmers produce some deeply incorrect code and follow very bad practices and then instruct their juniors to follow the same practices.

    I think the most important thing is to know what everything you write actually does. Some places I've worked coders called functions just because they sounded right without ever reading the API. This resulted in some massive bugs, when asked why the coder just responded she 'didn't have time to know everything'. It would have been nice if she had had time to know the code she wrote at least. Such people should have to work with punched cards and formal methods until they learn some discipline.

    That is probably the point of the question, you should understand the basic syntax of the language enough to reliably predict what a piece of basic code will do. Unfortunately it is possible to find nasty parts of a language, especially in languages like C++ where entire books are written on nasty corner cases. Such code tests/puzzles can then become a diversion from producing something useful.

    Then there are areas like trying to write per-formant thread safe code where even the best programmers can come unstuck. However a lot of promising breakthroughs are being made in this area, with tools for both static and dynamic analysis.
     
    Last edited: Jun 25, 2012
    Certifications: CITP, BSc, HND, SCJP, SCJD, SCWCD, SCBCD, SCEA, N+, Sec+, Proj+, Server+, Linux+, MCTS, MCPD, MCSA, MCITP, CCDH

Share This Page

Loading...