11.6. Exercises
The following code fragment generates a compile error. What's the problem with the code?
private int TestCode() { int j; int x; j = 4; if (j == 4) x = 5; return x; }The present value of a future asset is determined by the number of years into the future at which the asset will be received and the current market rate of interest. For example, if I promise to pay you $100 a year from now and the current interest rate is 5%, the present value of the asset is $95.23. Stated differently, placing $95.23 in the bank today at 5% interest would yield you $100 a year from now. The equation is:
where p is the present value, a is the future amount, i is the interest rate, and y is the number of years into the future the asset is deferred. How would you code this formula and why?
C# enables you to use code like the following, in which several variables are defined with a single statement:
long number, val, len;
What are the pros and cons of this type of variable definition statement?
You've written a very long and complex program that compiles and executes, but produces an incorrect solution. You've tried to locate the error, but so far without success. What would you do to fix the program bug(s)?
Most of my students are loath to place try-catch blocks in their code on their own. That is, unless I remind them to do so, I rarely see try-catch blocks in their assignments. If you were ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access