C Family Exam Q's Flashcards

(30 cards)

1
Q

What does the statement #include stdlib.h do in a C or C++ program?

A

At compile time, the C preprocessor replaces this statement with the contents of the file called stdlib.h.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

define min(a, b) ( (a) < (b) ) ? (a) : (b)

Why are a and b enclosed in brackets in the macro construction:

A

To prevent unintended effects through strength of operator binding.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is an advantage of using the macro min(a, b)?

A

Faster as no function call is involved.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a disadvantage of using the macro min(a, b)?

A

Potential side effects, for example if an argument is incremented.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Write the equivalent C code for the following for loop:

int i;
for (i = 0; i < 10; i += 2)
printf(“i = %d\n”,i);

A

int i = 0;
while (i < 10)
{
printf(“i=%d\n”, i);
i += 2;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

In what sense is C considered weakly typed?

A

C permits implicit type conversions between variables of different types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a hazard of C’s weak typing characteristic?

A

Precision may be lost in implicit conversion.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What effect does a call to the fork function have?

A

It causes a child process to start in the operating system.

The child contains the same program code as the forking parent process.

The state of the child diverges from that of the parent once the fork has been executed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

State two methods by which one process can communicate with another.

A
  • By sending signals
  • By opening a pipe
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Which method of inter-process communication is preferred for large amounts of data?

A

Opening a pipe.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How should RGB be defined to represent color values ranging from 0 to 255?

A

RGB should be a typedef-ed as an unsigned char datatype. The
typedef makes the code more readable and enables the programmer to change the implementation more transparently later. The unsigned char datatype is the smallest amount of memory in which the integer range 0 to 255 can be stored.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How is the string “Kernighan & Ritchie” represented in C?

A

Represented by an array of char (bytes) with an additional null termination byte.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Fill in the blank: To allocate memory and copy a string, the code uses _______.

A

malloc

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Why should a programmer free memory that they have previously allocated?

A

If unused memory is not freed it remains on the heap and cannot be reused (memory leak); this leads to unnecessarily large programs, hindering performance and may exhaust the available memory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Why is it difficult to automatically free unused memory?

A

Pointers into memory make it hard to determine when the memory is no longer used (has no references to it).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Why is the goto statement considered dangerous in programming?

A

Considered dangerous because the non-local jumps make comprehension of the program potentially very difficult. Iteration, for example, can be introduced without it being clear where the iteration counter is or how it is terminated.

17
Q

Give an example where the use of the goto statement is considered acceptable.

A

To make non-local jumps to a common piece of code to tidy up and exit cleanly in case of an error.

18
Q

How do setjmp and longjmp provide similar functionality to goto?

A

Both mechanisms allow nonlocal jumps.

19
Q

What is a key difference between goto and setjmp/longjmp?

A

Goto only enables jumps within the current scope, while setjmp/longjmp allow arbitrary jumps.

20
Q

Describe the program state after setjmp has returned following a longjmp call.

A

The stack contains frames of main, funcA, and funcB with base and frame pointers pointing at the main frame.

21
Q

What is the key difference between subroutines and coroutines?

A
  • Subroutines begin execution at the entry point and finish executing their code before returning. Once they have returned, the program state in the subroutine is lost.
  • Coroutines can yield control to other routines during their execution. Execution at the yield point can subsequently be resumed, with the program state intact.
22
Q

Explain why the error on line 9 in the given C++ code occurs.

A

funcA is private in the base class and not accessible in the derived class.

23
Q

How can the error on line 9 in the C++ code be remedied?

A

By declaring funcA to be protected or public.

24
Q

Why can child call funcB on line 24 but not on line 25 in the C++ code?

A

funcB is public in Child but protected in GrandChild, restricting access from outside.

25
How can the call on line 25 be allowed in the C++ code?
By using a public access modifier for funcB in GrandChild or defining a wrapper member function.
26
What causes the error on line 32 of the C++ code?
It is ambiguous whether ta.age refers to the age attribute from Staff or Student.
27
What is one way to resolve the ambiguity error on line 32?
Using the scope resolution operator.
28
What will be the output if the scope resolution operator is used to refer to ta.age?
30 30
29
What is the second way to resolve the ambiguity error in the C++ code?
By declaring Student and Staff to be virtual member classes.
30
What will be the output when using virtual member classes in the C++ code?
Person::Person(30) Staff::Staff(30) Student::Student(30) TA::TA(30) 30