Questions and exercises for Review
Note.
Questions
preceded
by green numbers apply to the midterm
1. What does the copyright law protect?
2.
What is a software trademark?
3. What is a bit, a byte, a word?
4. How many bytes are in 12kB, 3GB, 128MB?
5.
What is included in an Interactive Development Environment (IDE)?
6.
Describe the stages needed to produce an executable program from
sources.
7.
Describe the stages of the compilation process.
8. Draw
a diagram showing how one cand get from
source code
to executable code.
9.
Explain the terms: program, programming
10. What
are the basic requirements for a well-posed problem?
11. What are the results of the analysis stage?
12.
What is program design?
13.
Explain the benefits of modularity.
14.
Briefly describe the top-down programming technique for program
design
15. Briefly describe the bottom-up programming technique for program design
16. What are the benefits of top-down program-design?
17. Explain the stages of program implementation.
18. What is the purpose and the contents of program documentation?
19.
What must be done during program maintenance?
20. What is an algorithm and what properties should it have?
21. Explain the general requirements for an algorithm.
22. Explain the term flowchart.
23. Explain the term pseudocode.
24. Explain the term hierarchy
chart.
25. What are the types of control structures used in describing algorithms?
26. Describe the selection control structure.
26. Describe the while loop control structure.
27. Describe the repeat-until
loop control structure.
28. Describe the for
loop
control structure.
29. Explain the in,
inout, out formal parameters to
procedures.
30. What is
programming style and
what does it characterize?
31. Provide a set
of recommendations
for good programming style.
32. Describe the
basic/typical form
of a C program.
33. List the primitive integral types and their sizes in bits for PCs.
34. What are the primitive types in C?
35. Describe the while
loop
control structure.
36. Provide examples
of decimal,
octal, and hexadecimal integer constants in C.
37. Write in C the
equivalent of the
following string (text starts at My and ends one
space after the second dot): My
name
is
"Sun". I'm very shiny.
38. List and
explain the
common C escape sequences.
39. How can one
achieve conditional
compilation in C?
40. What can one do using the #define directive in C?
41. Explain the differences between functions and macrodefinitions.
42. Show how can you
use the #ifndef
directive
to avoid multiple inclusions of header files in C.
43. What is a condition?
44. Why may an algorithm be finite and yet impractical to use?
45. Describe
the three "files" automatically attached to a standard terminal at
runtime in a C program.
46. Provide
examples of declarations
of constants of various types in C.
47. When the program is running it produces a number that is too large to fit into the space allocated for it in memory. What type of error is this?
48. What would be output by the following segment of C?
int fact,
i;
fact = 1;
i
= 2;
while (i <= 6)
fact *= i;
i++;
printf(%d\n, fact);
If the purpose of this segment of C was to output the value of 1*2*3*4*5*6 how should it be changed?
49. Consider the following piece of C code
int i;
while (i < 10)
{
printf("%d\n", i);
i++;
}
To what should i be initialized so that the loop would be traversed 10 times? In this case what would be printed out? How would you change the body of the loop so that with the same initialization the numbers 1 to 10 would be printed? If the body of the loop was kept as it is above how should the initialization and the condition be changed so that the numbers 1 to 10 are printed out?
50. What would be output by the following section of C?
int A[5]
= {1 , 2, 3, 4};
int i;
for (i=0;
i<5; i++)
{
A[i] = 2*A[i];
printf("%d ", A[i]);
}
51. What is wrong with the following section of program?
int
A[10], i;
for (i=1;
i<=10; i++)
scanf("%d",
&A[i]);
52. What
is
the
purpose
of gets
and why it is
dangerous to use it?
53. What can be an operand to an expression?
54. What is taken
into account when
evaluating an expression?
55. What are the relational operators?
56. Write a relational expression which would evaluate to true if the sum of variables x and y was equal to the value of a variable z.
57. Bracket the following logical expressions to show the order of evaluation of the operators. Hence if a is 5, b is 10, c is 15 and d is 0 what are the truth values of the expressions?
c == a+b
a != 7
b <= a
a > 5
a+d
>= c-b
d/a <
c*b
58. Bracket the following logical expressions to show the order of evaluation of the operators. Hence if a is 5, b is 10, c is 15 and d is 0 what are the truth values of the expressions?
c == a+b || c == d
a != 7
&& c >= 6 || a+c <= 20
!(b <=
12) && a % 2 == 0
!(a
>5) || c < a+b
59. Write a logical expression which returns true if a float variable x lies between -10.0 and 10.0.
60. To what do the following expressions evaluate?
17/3 17%3
1/2 1/2*(x+y)
61. Given the declarations:
float x;
int k, i = 5, j = 2;
To what would the variables x and k be set as a result of the assignments
k = i/j;
x = i/j;
k = i%j;
x =
5.0/j;
62. If x has the value 3.5 when the following statement is executed what value would be assigned to y?
if (x + 1
<= 3.6)
y = 1.0;
else
y = 2.0;
63. In words what do you think the effect of the following statement is intended to be? Why is the statement syntactically incorrect? How could it be changed to be syntactically correct? Has the change that you have made ensured that the statement actually carries out the logical effect you stated at the beginning?
if (x
>= y)
sum += x;
printf("x is bigger\n");
else
sum += y;
printf("y is bigger\n");
64. Write an if-else statement which would add a variable x to a variable possum if x is positive and would add x to negsum if the variable x was negative.
65. Expand the solution to the previous question so that if x is positive then a variable poscount is incremented and if x is negative a variable negcount is incremented. If this was part of a program what would be sensible initializations to carry out?
66. It is decided to base the fine for speeding in a built up area as follows - 50 pounds if speed is between 31 and 40 mph, 75 pounds if the speed is between 41 and 50 mph and 100 pounds if he speed is above 50 mph. A programmer writing a program to automate the assessment of fines produces the following statement:
if (speed
> 30)
fine = 50;
else if
(speed > 40)
fine = 75;
else if
(speed > 50)
fine = 100;
Is this correct? What fine would it assign to a speed of 60 mph? If incorrect how should it be written?
67. Write a nested if-else statement that will assign a character grade to a percentage mark as follows - 70 or over: A, 60-69: B, 50-59: C, 40-49: D, 30-39: E, less than 30 :F.
68. What is the major difference between a while statement and a do-while statement?
69. What would be output by the following segment of C?
int i;
i
= -12;
do
{
printf("%d\n", i);
i = i - 1;
}
while (i > 0)
70. What would be output by the following segment of C?
int i;
for ( i = 1; i <= 12; i *= 2 )
printf("%d\n", i);
71. What is printed by the following segment of C?
int i;
for
(i = 1; i <
20; i = i + 3)
printf("%d\n", i);
72. What would happen if the i+3 in the update expression were changed to i-3?
73. Write a segment of C using a for statement which accepts n real values entered by a user and outputs their average value. Assume n will be greater than zero.
74. What type(s) is(are)
allowed for the expression of a switch
statement?
75. What is the #define
directive used
for?
76. Given the
following code snippet
#define plus(a, b) a + b
int a = 3;
int b = 5;
int z = -1;
z *=
plus(a, b)
What is the value of z
after the execution of the above sequence?
77. How can one
guard against
multiple inclusions of header file content?
78. What is the #define
directive used
for?
79.
Write a
statement using scanf
which reads an octal integer, followed by a number of spaces,
then by a
hexadecimal number, a comma, then a double, and assigns the values to
variables
octal,
hex, and dbl.
Include proper declarations for those variables.
80. If a
= 1, b = -2, c = 0 and d = 7
, determine the value
assigned to y
.
y =((c=(a<0)? a: a),
(d=(b<0)? -b: b),
(c>d)? c: d);
81.
If x = 1, y = -2, z = 11
and w = 7
, determine
the value assigned to alpha
.
alpha = ((a1=x+y+z),
(a2=x+y+w), (a1*a2+10));
82. What is a
comma expression
typically used for?
83.
Write a set of
statements to read from the standard input three variables a, b
and c
, where the variables
have the following
types:
a
doubleb
unsigned long integerc
string of at most 33
charactersThe input values are separated by one to three spaces. The second variable is given as a hexadecimal number.
The admissible values for a
and b
are
as follows:
-10-5 <=
a < 1040
314 < b < 974927
Suitable error messages should be given if the values are not within the allowed range or in the wrong format.
Sample input:
-.000001
0xfff Is this a good one?
-1.e-3
0xEE04F Maybe this one...
213e+36
0xEE04E Or this one. Could this string be too long to
put
into allocated area?
84. Write a set of
statements to read
from the standard input three variables a, b
and c
,
where the variables have the following types:
a
unsigned short integerb
floatc
string of at most 31
charactersThe input values are separated by one comma. The first variable is given as an octal number.
The admissible values for a
and b
are as follows:
-4331 < a <=
28731
-1035 <
b < 9749273
Suitable error messages should be given if the values are not within the allowed range or in the wrong format.
Sample input:
12745,-1.e-3,Is
this
a
good
one?
0xEE04F,21e75,Maybe
this
one...
70073,21e+6,Or
this
one.
Could
this string be too long to put into allocated area?
85. Write a
set of statements
to read from the standard input three variables a,
b
and c
, where the variables
have the following
types:
a
long integerb
string of at most 15
characters, not containing any semicolonc
doubleThe input values are separated by one semicolon. The first variable is given as an hexadecimal number.
The admissible values for a
and c
are as follows:
-43310000 < a <=
2888731
-1035 <
b < 9749273
Suitable error messages should be given if the values are not within the allowed range or in the wrong format.
Sample input:
43310000
; Is
this a good one;-1.e-3
0xEE04F,
Maybe
this
one...;21e75
700x73;Or
this
one.
Could
this string be too long to put into allocated area?;21e+6
86.
Write a set of
statements to write to the standard output a sequence of variables, a, b, c
and d, of
the
following types:
a
long integerb
string of at most 15
charactersc
doubled
characterThe output fields have the following specifications:
a
width: 20 characters,
left justifiedb
width: 14 characters,
right justified, padded left with spacesc
width: 20 characters,
10 positions for the fraction partd
characterAll fields are separated by a vertical bar (i.e. '|').
87. Write a set of statements to read from the standard input the output produced by exercise 86.
88.
Write a set of
statements to write to the standard output a sequence of variables, a, b, c
and d, of
the
following types:
a
floatb
doublec
unsigned long integerd
pointerThe output fields have the following specifications:
a
use shortest
representationb
width: 20 characters,
8 positions for the fraction part c
hexadecimal number d
pointer representationAll fields are separated by a semicolon (i.e. ';').
89. Write a set of statements to read from the standard input the output produced by exercise 88.
90. What C statements cause a program to become unstructured?
91. For what purpose can one use the function exit?
92. How can one achieve forced program termination?
93. Write a sequence of C code to calculate the geometric mean of n variables stored in array a, where 1 <=n <= 200.
The elements of the array are real numbers in the range -1055..10+55.
94. Write a sequence of C code to draw diamonds of vowels or consonants, according to the model shown in examples below:
Input
data |
Output
|
Input
data |
Output |
v,4 |
|
V,6 |
|
c,4 |
|
C,7 |
|
That is, the first letter of input specifies the filler for the diamond: v for vowel, c for consonant.
The second element is the number of stars ('*') on each side of the diamond.
Questions/Exercises
for
the
final exam
void example(int n)
{
int i;
for (i=0; i<n;
i++)
printf("%c",
'*');
printf("\n");
}
*
**
***
****
a)
void
change(void)
{
int
x;
x
=
1;
}
void
main()
{
int
x;
x
=
0;
change();
printf("%d\n",
x);
}
b)
void change(int x)
{
x
= 1;
}
void main()
{
int x;
x
= 0;
change(x);
printf("%d\n", x);
}
void
change(int& y)
{
y
=
1;
}
void
main()
{
int x;
x
=
0;
change(x);
printf("%d\n",
x);
}
int day[] = {mon, tue,
wed, thu, fri};
How many
elements has the array day? If the declaration is changed to
int day[7] = {mon, tue,
wed, thu, fri};
how many
elements does day have?
int (*pfi)(int, int);
float a[10], *p;
p=a;
what is the difference between p and
a?
char
*(*myfunc1)(char *, int) and
char
*myfunc2(char
*,
int)?
typedef
struct cell{
int
a[5];
char
*name[41];
struct
cell*
next;
}
union
marks // A
{
float
perc;
//
B
char
grade;
//
C
}
struct
address // A
{
char state[30];
char street[30];
char city[30];
}
struct student // B
{
char name[30];
float marks;
struct address adr; // C
}
#include <stdio.h>
void f(int d);
int a = 1, b =
2, c = 3, d = 4;
int main(){
int a = 5, c
= 6;
f(a);
f(b);
f(c);
printf("%d
%d %d %d\n",a,b,c,d);
return 0;
}
void f(int d){
static int a
= 0;
a = a + 7;
b = a + d;
c++;
d--;
printf("%d
%d %d %d\n",a,b,c,d);
}
int q_dequeue(q_queue_t * from_queue){
q_node_t
*node;
node = q_remove_front(from_queue);
if (node == NULL) {
return 0;
} else {
return node->data;
}
}
char
a[6] = "abcde";
char
b[6] = "fgh";
strcpy(
a,
b
);
float x = 3.67;
printf("%d\n",x);
1074617385
#define N 4
int a[N];
int &pa =
a[0];
int i;
for( i = 0; i
< N; i++ ){
pa++ == 0;
}
#define N 4
int a[N];
int *start = a;
int *end = a +
N;
int *ptr;
for( ptr =
start; ptr != end; *ptr++
=
0
);
int f( int
( * g )( int ), int
h );
s1( char dest[],
char src[] ){
int i =
0;
while( dest[i++]
=
src[i++]
) ;
}
s2( char * dest,
char * src ){
while( *dest++
= *src++ ) ;
}
#include <stdio.h>
int main(){
char line[256];
int i =
0;
while( gets( line ) != EOF ){
printf(
"line %d is \"%s\"\n", i, line );
i++;
}
return 0;
}
#include <stdio.h>
int main(){
FILE *fp1;
char line[256];
if( (fp1 = fopen(
"my_data", "r" )) == NULL ){
perror(
"my_data" );
exit( -1 );
}
fseek( fp1,
(long) 256, SEEK_SET );
fgets( line,
256, fp1 );
fprintf( stdout,
"line
is
%s\n", line );
fclose( fp1 );
}