Reputation: 91
I have just completed a C# practice test and have got some answers wrong, but I cannot get hold of my lecturer.
One of the questions that I didn't understand was "Which of the following is a common exception to the principle of naming variables with whole words?" The suggested answers are:
f
i
l
b
Why is the answer i
?
Another question I don't understand is _"For which of the following lines of code will the following line be {
the least often?". I didn't really understand this question because of the way it was written. The suggested answers are:
for (int i =0; i < myArray.Length; i++)
do
if (foundMatch)
}
The answer was }
.
Upvotes: 1
Views: 114
Reputation: 129747
In C#, for
loops are very common. When writing a for
loop you need an index variable. By convention, the variable i
is often used rather than the full word index
. It is used so often, that most people understand that i
is the loop index variable without having to think about it. There is no such convention (that I'm aware of anyway) for the other suggested variables, f
, l
and b
. You would be better off spelling out what these variables represent in the code so that people understand it.
Therefore the answer is i
.
All of the statements for
, do
and if
require a code block to follow them. The code block could either be a bare, single statement (less common) or a group of one or more statements surrounded by braces {
and }
(very common).
For example:
for (int i = 0; i < myArray.Length; i++)
{
...
}
do
{
...
} while (!done);
if (foundMatch)
{
...
}
In contrast, the end of a code block }
is almost never followed immediately by the start of another code block {
. In other words, you won't see this in the code:
if (foundMatch)
{
...
}
{
...
}
Therefore, the answer is }
.
Upvotes: 2