1] Given the C code segment below, answer question i -v.
for (I = 1; I <= 10; ++ I)
{
for (J = 1; J<=I; ++ J)
printf("%i", I * J);
printf("\n");
}
i) How many times does the first printf statement execute?
ii) How many times does the second printf statement execute?
iii) What is the final value in I?
iv) What is the final value in J?
v) What is the last value displayed by first printf statement?
Answer :
i) 9
ii) 9
iii) 10
iv) 100
v) 100
2] State the value in x for the following arithmetic expressions.
i) x = 17 % 5
ii) x = 1 % 5
iii) x = 6 + 4/2 * 4/4 + 3 * 2/2 - 1
iv) x = 25 % 5 + 12.5 * 2/5
v) x = 7/2
Answer:
i) 2
ii)
iii) 7
iv) 5
v) 3.5
3] Given the following C statement, state the output produce. Assume count was declared earlier.
for (count = 1; count <=10; ++(++count))
printf("%2i", count *2);
Answer : Error
4] Answer questions (i) through (v) regarding an array called fractions.
i) Define a symbolic constant SIZE to be replaced with the replacement text 10.
ii) Declare an array fractions with SIZE elements of type float and initialise the elements to 0.
iii) Name the fourth element from the beginning of the array.
iv) Name the last element from the beginning of the array.
v) Name the first element from the beginning of the array.
5] Rewrite the following erroneous C statements.
i) #define COUNT = 20
ii) #include <string>
iii) if (a=b) printf("a equal b\n");
iv) char name[20] = Peterpan;
v) int age[5] = {"0"};
Answer :
i) int COUNT = 20;
ii)
iii) if (a=b)
printf("a equal b\n");
iv) int name[20] = Peterpan;
v) int age[5] = {0};
6] Given the declaration below, state the output produce by the following printf statements. Indicate a space with a "."
char item[20] = "Singapore";
i) printf("%s"\n", item);
ii) printf("12s\n", item);
iii) printf("%-12s\n", item)
iv)printf("%5s\n", item);
v) printf("%5.5s\n", item);
vi) printf("%6.5s\n", item);
Answer :
i) Singapore
ii) Singapore............
iii) ...Singapore
iv) Singapore.....
v)
vi)
7] Show the proper mode for opening a file in each case.
i) Read from an existing test file but ensure that the program can make no changes.
ii) Create a new binary file for both read and write access.
iii) Read and write any part of an existing text file.
iv) If a file doesn't exist, create it; otherwise allow reads and writes anywhere beyond the end of the original binary file.
Answer :
i)
ii) Book=fopen("File.txt", "rw");
iii)
iv) ((Book=fopen("File.txt", "rw")) !=NULL)
8] Given the following array's declaration statements, indicate whether valid or invalid.
i) cha a[10] = "Alice";
ii) char a[] = "Alice";
iii) char a[10] = 'A', '1', 'i', 'c', 'e';
iv) char a[10] = {0};
v) char a[5] = "Alice";
Answer :
i) Valid
ii) Invalid
iii) Invalid
iv) Valid
v) Valid
9] Given the following C program, indicate the output produce.
void main (void)
{ int i = 0, x = 0;
for (i = 1; i<20; i* = 2)
{
x++;
printf("%i", x);
}
printf("\nx = %i", x);
}
Answer :
12345
x = 5
10] Write a for loop that will calculate the sum of every third integer, begining with i = 2 (i.e. calculate the sum 2+5+8+11 ...) for all values of i that are less than 100.