Awesome questions, I'll have a shot at them, let's see how I fare. I'm not too sure on question 1 and 2, particularly with the behavior with double. I think the problem might be integer division, but I'm not sure if we deal with that in ITSD or if there's something else I'm overlooking. I've listed my ideas for those questions anyway.
1) What is the output of the following code fragment?
double number = (1/3) * 3;
Print number;
The answer should be one.
I'm curious as to why double was used, as opposed to float, since it'd be unnecessary usage of memory considering that we're only dealing with numbers like 25.20.
What I know about double:
Double is a data type, where double is shorthand for double precision, as opposed to single precision (precision is the number of digits in the number)? If I'm not mistaken, single precision is equivalent to floating point numbers.
I can't remember the exact numbers that it covers: I know they're huge numbers and that it can deal with numbers
something like
(1, followed by 300 zeroes) and
(not sure if that's correct)
I also know that division is a bit weird at times, depending on what data types you're using. Division involving integers returns integers, division involving at least one floating point returns a floating point.
We have 1/3, division involving two integers, so that would evaluate as zero, and hence the final output would be zero.
If that is the problem to fix that we could go (basically making 1 and 3 not integers) :
float number = (1.0/3.0) * 3;
print number;
which would output: 1
2) Given the following code fragment that converts from degrees Celsius to degrees Fahrenheit, answer the following questions:
double celsius = 20;
double fahrenheit;
fahrenheit = (9/5) * celsius + 32.0;
a) What is the value assigned to fahrenheit?
b) Explain what is actually happening, and what the programmer likely wanted.
c) Rewrite the code as the programmer intended.
Again, the only problem I can think of is integer division.
a)
b) I don't think there would be an error with BODMAS here.
The programmer would have wanted
The decimal point would just depend on how you format the output.
There could also be an integer division error, with the 9/5 (two integers), getting 1 instead of 1.8 like we'd want, so the program would output 52 instead.
The other problem would be is that it's unnecessary to use double for the numbers we're dealing with here?
c)
float celsius = 20;
float fahrenheit;
fahrenheit = (9.0/5.0) * celsius + 32.0;
3)
a) How do you write a single line comment?
b) What about a multi-line (block) comment?
Depends on the language I guess, but single line comments are usually along the lines of this:
# Comment
or
// Comment
and block comments are usually
/* rooooooooooooooooo
oooooooooooooooooooo
ooooooooooooooooooar */
or you can just repeat single-line comments
#A
#A
#A
4)
Trace the following code fragment, that is, step through the boolean logic to evaluate whether the Boolean statement is true or false.
count = 8
NOT ( (count < 3) OR (count > 7) )
NOT ( (count < 3) OR (count > 7) )
This is basically saying that count will not be true if it's less than 3 or if it's greater than 7.
This means that if count is between 3 and 7, it's true.
If it's equal to 3 or 7, it'll also evaluate true.
Since count is 8, it's false.
Bonus question:
Consider the following code fragment. Assume that all variables are integers.
if ( (kids != 0) && ( (pieces / kids) >=2) )
Print("Each child may have two pieces of yummy delicious chicken.")
Explain specifically how short circuit evaluation prevents a run time error in the case that a specific numeric value is assigned to the 'kids' variable.
The condition for the if statement is:
If kids is not equal to zero AND if pieces/kids is greater than equal to 2.
Looking at the code, it seems to be saying that if we do have kids around, and we have enough pieces of chicken to pass around so that each kid will at least get 2 pieces, then we give out the chicken.
Since we're dealing with integers, we can have numbers such as -5, -3, 0, 1, 2, 3. That's assuming that it's signed integers. If it's unsigned integers, then the following sentence wouldn't matter since we only have positive numbers and zero (e.g. 0, 1, 2, 3).
If kids was a negative value, the if statement wouldn't run, since pieces/kids would be a negative number and hence less than 2. If pieces and kids were a negative value, it's possible you could get something like -6/-2 = 3 and the code would run.
I'm not entirely sure what short-circuit evaluation is, but I'll go out on a limb and guess that it's referring to the kids != 0 bit.
If kids equaled zero, then we would get a division by zero error with the pieces/kids condition. Since the condition requires BOTH to be true, if kids = 0, then the division by zero error shouldn't matter.
I do have a question though, does the order you list them in matter?
e.g. will if ( (kids != 0) && ( (pieces / kids) >=2) ) and if ( ( (pieces / kids) >=2) && (kids != 0)) produce different results?
How is boolean logic actually evaluated by the computer?
My guess at the answer is that it won't produce different results. If pieces/kids did evaluate to zero, I assume that it would move onto evaluating the other condition straight away, rather than giving a run-time error immediately there. (I'll probably google some stuff now)