C
Before we start, here is a list of things to ask a new hire and current programmers should already know:
Forget the questions on classes or function overloading and get to the simple basics!
LIKE:
1) What is the difference between a global variable and a local variable and when should each be used?
Ans: A global variable is kept in memory and is available to all functions in the program. A local variable is kept on the stack along with the passed parameters and comes and goes with the function. Globals should not be passed on the stack unless you want a private copy.
2) what is the difference between a local variable, a "static" variable and a "const" and when should you use each one?
Ans: A local variable to a function was described in 1). A "static" variable is the same as a global except the compiler only lets the function see it (it sticks around between calls). a " const" tells the compiler that it should make sure the code does not try to modify the value assigned to it.
3) What is passed by address and what is passed as a variable on the stack?
Ans: Arrays are passed by address (strings are an array of char), structures are passed by data and non arrays are passed on the stack by value. You can make a non array be passed as a pointer (by address) by placing a '&' in front of it.
4) What is dereferencing, how do you use it and when do you need it?
Ans: dereferencing is going indirectly through an address to the variable. You dereference by placing a '*' in front of a variable. One use is to change the value of a variable that is placed on the stack as an address for the calling function.
5) What are the types of flow control?
Ans: if, else, else if,? (conditional expression), switch, for, while, do-while, goto, break, continue, and return.
6) what do they do and when do you use one over the others?
Ans:
(A) if, else, else if, and for statements every programmer should know so if you don't know, try your hand at something besides programming. For those of you that are using this to learn, if is followed by the condition followed by the code for the true part followed by the else followed by the false part. The for loop is doing something with an index running from one number to another number incremented by a third number. (note: you can look like a geek by using the for loop in place of a while with a unconventional structure but why bother. you can also do assignment inside of the for but again why bother)
(B) The '?' operator is usually frowned on by non C programmers. No reason for this as it has the same format as the : "if (condition) {true} else {false}"
if (c) {a=b/c} else {a=0} // stop divide by zero
a = (c) ? b/c : 0; // stop divide by zero
See - everyone knows that the true part is first in both cases. The "a=" has been moved to the front and does not have to be repeated twice.
A plus side of the ? operator is that nested if/ else statements can be indented wrong and look like the else is no on the previous if while the use of the ? cuts down the nesting.
(C) switch is similar to the multiple if-else if-else and is usually used when you have some numeric value (like 1 through 10 or char 'a' through 'z'). One advantage is that the break can be omitted between two or more cases to do multiple cases (this is also frowned on in the internal programming standards but I do not know why).
(D) while vs. do-while: do while does the loop as least one time before checking the condition and the while checks condition before executing the code.
(E) In C. structured means that you have a logical downward flow of processing. It does not mean that you have to exit at the bottom. This would negate the need for the return as it would only be available at the bottom and you could just write the variable. The C language was designed with three powerful processing controls. These are the "return, the "break" and the "continue". The goto is also available but is not normally needed. Using these will help reduce the amount of indenting that is necessary in the
if
if
if
if
type of structured thinking that is used in languages like FORTRAN.
(E1) return: If you find that you have no further need to be in a function, get out with the return. That is what it is for.
(E2) break: If you find that you have no further need to be in a loop, get out with the break. That is what it is for. It is also used in switch statements to not execute the next case statement.
(E3) continue: If you find that you have no further need to be processing the current index in a loop or want to start the loop over, go to the next index or the top of the loop with the continue. That is what it is for.
It should be noted that the lack of hardly any continue statements in the code indicates that maybe most programmers do not think in C or do not know C. If the code is sadly lacking break statements then it is usually due to not thinking in C.
7) what is a recursive function and what good is it?
Ans: Recursion is the programming method where a function calls itself. It is useful in solving thing like equations and doing statistics.
8) What does "extern" do?
Ans: "extern" tells the compiler not to allocate memory but to link to a variable defined elsewhere (usually in main). Struct's can be defined or defined and allocated. You should know the difference.
9) Are you logically oriented?
Ans: This is a trick question. No one can tell and it can not be determined until you have been programming for a long long time. If a "real" programmer can cut your code in half, you are probably not logically orientated. If you are not, you should probably think about taking up another profession.