If I assign 5 / 2 to a
double
, what value will be stored in the variable?Answer
2.0C-strings are arrays of characters, with one special difference. What is it?
Answer
C-strings are null-terminated, meaning they have a null character ('\0'
) at the end.When an array is passed to a function, what is actually passed?
Answer
A pointer to the first element of the array.Why might you pass something as a
const
reference instead of by value?Answer
To avoid making a copy of the object, which can be expensive for large objects.Draw a memory diagram for the following code:
int x = 5; int *p1; int *p2 = &x;
Answer
When extracting (using
>>
) an integer from an input stream, what happens to whitespace before and after the integer? Assume the integer is valid.Answer
Whitespace is consumed and skipped before the integer, but left behind in the buffer after.When does the
eof
flag get triggered?Answer
When an attempt is made to read past the end of the file.Name one advantage of separate compilation.
Answer
Possible answers:- Only compile the files that have changed (faster compilation)
- Re-use libraries of code
- Hide implementation details and keep the main logic clean
Trace the following code, assuming the user inputs
3 5 0 2
. What will be stored innums
at the end of this code snippet?int nums[8], n; cin >> n; for (int i = 0; i < n; i++) { cin >> nums[i]; }
Answer
[5, 0, 2, ?, ?, ?, ?, ?]
In the previous question, what would happen if you added
cout << nums[-1]
?Answer
Undefined behaviour - might crash, might print random garbage, might launch missiles.