D
DJ
Here are my answers for quiz 2 - how well did I do?
1. Which of the following types cannot be used to subscript an array?
a. short
b. char
c. float
d. all of the above types may be used to subscript an array.
D
2. Given the following declaration:
int a[10];
which of the following is a pointer to the second element of a?
a. a+1
b. a[2]
c. a+2
d. &(a+1)
A
3. Given the following declarations:
int a[10], *p=a;
which of the following is equivalent to a[1]?
a. *(p+1)
b. p(1)
c. *(a+1)
d. Both a and c.
D
4. Given the following two-dimensional array declaration and initialization:
int a[ ][4] = {{1,2,3,4},{5,6,7,8}};
what is the value of the expression *(a[1]+2)?
a. 3
b. 6
c. 7
d. The subscript exceeds array bounds.
A
5. What is the output of the following code?
int a[5], i;
for (i=0; i<5; ++i)
a[(i+3) %5] = 2*i;
for (i=0; i<5; ++i)
printf("%d " , a);
a. 2 4 6 8 0
b. 4 6 8 0 2
c. 0 2 4 6 8
d. 4
6
8
0
2
C
6. Given the following two-dimensional array declaration:
int A[2][5];
what is the type of A[1]?
a. int
b. int *
c. int **
d. The expression has no type.
A
7. Which of the following string declarations contains an error?
a. char String1[3] = "xyz";
b. char String2[5] = "abcd";
c. char String3[7] = "hello";
d. char String4[ ] = "world";
A
8. Given the following declaration,
char S[ ]= "abcdefg";
What is the output of the following statement?
printf("%s", S+3);
a. abcdefg
b. abc
c. defg
d. cdefg
C
9. Given the following declaration,
char a[ ] = "cba", *p=a;
What is the difference between the values of the expression ++*p and *++p?
a. The first is equal to d, and the second one is equal to b.
b. The first is equal to c, and the second one is equal to a.
c. The first causes a compiler error, while the second does not.
d. The two expressions have the same value.
C
10. If S is a string, what can you say about the expression:
strlen(S+strlen(S))
a. It has the value 0.
b. It has a value of 1.
c. It will generate an error.
d. The value of the expression will depend on the contents of S.
A
11. Given the following declarations:
char a[ ] = "abc", *p = "def";
what is the effect of the following statement? p=a;
a. It copies the string "abc" into the string p
b. It changes p to point to string "abc"
c. It copies the first character of a to the first character of p
d. It generates an error.
B
12. How is the length of a string determined in C?
a. From the declared size of the array which contains it.
b. By a byte stored at the beginning of the string.
c. By a null byte stored at the end of the string.
d. By a byte stored at the end of the string.
C
13. What is the effect of the following recursive function?
void Mystery(int A[ ], int size)
{
if (size > 0) {A[0] = 0; Mystery (A+1, size -1);}
}
a. It sets the first element of the array A to zero.
b. It sets the last element of the array A to zero.
c. It sets all elements of the array A to zero.
d. It generates an infinite recursion loop.
C
14. What is the error in the following recursion function?
void Fill_Ary(int A[ ], int N)
{
if (N != 0)
{
--N;
A[N] = N;
Fill_Ary(A,N);
}
}
a. It will always result in an infinite recursion loop.
b. It will cause an infinite recursion loop for some values of N but
not for others.
c. There is a syntax error in the function.
d. There is no error in the function.
B
15. What is the effect of the following recursive function?
int Count (float A[ ], int Size, float X)
{
if( Size <=0)
{
return 0;
}
else
return (A[0] == X) + Count(A+1, Size-1, X);
}
a. It returns the number of elements in A.
b. It sets each element of A to X.
c. It returns the number of elements in A equal to X.
d. It will result in an error.
C
16. What is the output of the following code?
void Mystery(int n)
{
if (n >0) Mystery(n-1);
printf(" %d", n);
}
Mystery(9);
a. 0 1 2 3 4 5 6 7 8 9
b. 1 2 3 4 5 6 7 8 9
c. 9 8 7 6 5 4 3 2 1 0
d. 9 8 7 6 5 4 3 2 1
A
17. What is the effect of the following code?
int Mystery (int N)
{
if (N <= 0)
return 0;
else
return (1 + Mystery (N/10) );
}
a. It computes N/10
b. It computes the number of decimal digits in N
c. It computes N*2
d. It produces an error
D
18. What is the output of the following code?
void Mystery (char *Str)
{
if (*Str != '\0')
{
Mystery(Str + 1);
printf("%s\n", Str);
}
}
Mystery("abcd");
a. d
cd
bcd
abcd
b. "null string"
d
cd
bcd
abcd
c. abcd
bcd
cd
d
d. d cd bcd abcd
A
19. The main difference between a structure and an array is:
a. structures are fixed size, while arrays can vary in size.
b. structures are made up of dissimilar components, while arrays are made up
of identical components.
c. structures can contain pointers, while arrays cannot.
d. There is no real difference; whether a struct or an array is used is a
matter of programming style.
B
20. The -> operator has the effect of which other two operators?
a. * and ++ (dereference and then increment pointer)
b. - and > (decrement and then check for greater than)
c. [] and . (get array element and then get number)
d. * and . (dereference and then get member)
D
21. What is the effect of dereferencing a pointer variable, which has the
value NULL?
a. Zero is returned.
b. The operation is ignored.
c. The compiler detects the error and issues an error message.
d. A runtime error results.
D
22. What is the advantage of using a pointer to a structure as a parameter
to a function, instead of the structure itself?
a. The code is easier to read.
b. It is more efficient because the structure is not copied.
c. There is no difference; it is a matter of style which is used.
d. Passing a structure as a parameter is not allowed.
B
Questions 23 and 24 are based on the following program:
struct vector
{
float x;
float y;
float z;
};
void main(void)
{
struct vector a = {2.3, 4.2, 6.5};
struct vector b = {-1.2, 3.5, 5.1};
float scalar(vector *, vector *);
printf("The answer is %.2f", scalar(&a, &b));
}
float scalar(struct vector*po1, struct vector *po2)
{
float s;
s=po1->x*po2->x + po1->y*po2->y + po1->z*po2->z;
return(s);
}
23. After running this program, the screen will show:
a. 45.0900000
b. 45.09
c. the answer is 45.09
d. the answer is 45.090000
C
24. The scalar() function uses po1->x to access the first member of the a
structure. Which of the following constructs can be equally used within the
function to access the same member?
a. po1.x
b. *po1.x
c. (*po1).x
d. a.x
C
25. Given the structure and pointer declarations shown below, choose the
assignment statement which sets the Price member of the structure pointed to
by PC to 1000.
struct Computer
{
char Manufacturer[30];
float Price;
int Memory;
} *PC;
a. PC->Price = 1000.0;
b. PC.Price = 1000.0;
c. *PC.Price = 1000.0;
d. Computer.Price = 1000.0;
A
1. Which of the following types cannot be used to subscript an array?
a. short
b. char
c. float
d. all of the above types may be used to subscript an array.
D
2. Given the following declaration:
int a[10];
which of the following is a pointer to the second element of a?
a. a+1
b. a[2]
c. a+2
d. &(a+1)
A
3. Given the following declarations:
int a[10], *p=a;
which of the following is equivalent to a[1]?
a. *(p+1)
b. p(1)
c. *(a+1)
d. Both a and c.
D
4. Given the following two-dimensional array declaration and initialization:
int a[ ][4] = {{1,2,3,4},{5,6,7,8}};
what is the value of the expression *(a[1]+2)?
a. 3
b. 6
c. 7
d. The subscript exceeds array bounds.
A
5. What is the output of the following code?
int a[5], i;
for (i=0; i<5; ++i)
a[(i+3) %5] = 2*i;
for (i=0; i<5; ++i)
printf("%d " , a);
a. 2 4 6 8 0
b. 4 6 8 0 2
c. 0 2 4 6 8
d. 4
6
8
0
2
C
6. Given the following two-dimensional array declaration:
int A[2][5];
what is the type of A[1]?
a. int
b. int *
c. int **
d. The expression has no type.
A
7. Which of the following string declarations contains an error?
a. char String1[3] = "xyz";
b. char String2[5] = "abcd";
c. char String3[7] = "hello";
d. char String4[ ] = "world";
A
8. Given the following declaration,
char S[ ]= "abcdefg";
What is the output of the following statement?
printf("%s", S+3);
a. abcdefg
b. abc
c. defg
d. cdefg
C
9. Given the following declaration,
char a[ ] = "cba", *p=a;
What is the difference between the values of the expression ++*p and *++p?
a. The first is equal to d, and the second one is equal to b.
b. The first is equal to c, and the second one is equal to a.
c. The first causes a compiler error, while the second does not.
d. The two expressions have the same value.
C
10. If S is a string, what can you say about the expression:
strlen(S+strlen(S))
a. It has the value 0.
b. It has a value of 1.
c. It will generate an error.
d. The value of the expression will depend on the contents of S.
A
11. Given the following declarations:
char a[ ] = "abc", *p = "def";
what is the effect of the following statement? p=a;
a. It copies the string "abc" into the string p
b. It changes p to point to string "abc"
c. It copies the first character of a to the first character of p
d. It generates an error.
B
12. How is the length of a string determined in C?
a. From the declared size of the array which contains it.
b. By a byte stored at the beginning of the string.
c. By a null byte stored at the end of the string.
d. By a byte stored at the end of the string.
C
13. What is the effect of the following recursive function?
void Mystery(int A[ ], int size)
{
if (size > 0) {A[0] = 0; Mystery (A+1, size -1);}
}
a. It sets the first element of the array A to zero.
b. It sets the last element of the array A to zero.
c. It sets all elements of the array A to zero.
d. It generates an infinite recursion loop.
C
14. What is the error in the following recursion function?
void Fill_Ary(int A[ ], int N)
{
if (N != 0)
{
--N;
A[N] = N;
Fill_Ary(A,N);
}
}
a. It will always result in an infinite recursion loop.
b. It will cause an infinite recursion loop for some values of N but
not for others.
c. There is a syntax error in the function.
d. There is no error in the function.
B
15. What is the effect of the following recursive function?
int Count (float A[ ], int Size, float X)
{
if( Size <=0)
{
return 0;
}
else
return (A[0] == X) + Count(A+1, Size-1, X);
}
a. It returns the number of elements in A.
b. It sets each element of A to X.
c. It returns the number of elements in A equal to X.
d. It will result in an error.
C
16. What is the output of the following code?
void Mystery(int n)
{
if (n >0) Mystery(n-1);
printf(" %d", n);
}
Mystery(9);
a. 0 1 2 3 4 5 6 7 8 9
b. 1 2 3 4 5 6 7 8 9
c. 9 8 7 6 5 4 3 2 1 0
d. 9 8 7 6 5 4 3 2 1
A
17. What is the effect of the following code?
int Mystery (int N)
{
if (N <= 0)
return 0;
else
return (1 + Mystery (N/10) );
}
a. It computes N/10
b. It computes the number of decimal digits in N
c. It computes N*2
d. It produces an error
D
18. What is the output of the following code?
void Mystery (char *Str)
{
if (*Str != '\0')
{
Mystery(Str + 1);
printf("%s\n", Str);
}
}
Mystery("abcd");
a. d
cd
bcd
abcd
b. "null string"
d
cd
bcd
abcd
c. abcd
bcd
cd
d
d. d cd bcd abcd
A
19. The main difference between a structure and an array is:
a. structures are fixed size, while arrays can vary in size.
b. structures are made up of dissimilar components, while arrays are made up
of identical components.
c. structures can contain pointers, while arrays cannot.
d. There is no real difference; whether a struct or an array is used is a
matter of programming style.
B
20. The -> operator has the effect of which other two operators?
a. * and ++ (dereference and then increment pointer)
b. - and > (decrement and then check for greater than)
c. [] and . (get array element and then get number)
d. * and . (dereference and then get member)
D
21. What is the effect of dereferencing a pointer variable, which has the
value NULL?
a. Zero is returned.
b. The operation is ignored.
c. The compiler detects the error and issues an error message.
d. A runtime error results.
D
22. What is the advantage of using a pointer to a structure as a parameter
to a function, instead of the structure itself?
a. The code is easier to read.
b. It is more efficient because the structure is not copied.
c. There is no difference; it is a matter of style which is used.
d. Passing a structure as a parameter is not allowed.
B
Questions 23 and 24 are based on the following program:
struct vector
{
float x;
float y;
float z;
};
void main(void)
{
struct vector a = {2.3, 4.2, 6.5};
struct vector b = {-1.2, 3.5, 5.1};
float scalar(vector *, vector *);
printf("The answer is %.2f", scalar(&a, &b));
}
float scalar(struct vector*po1, struct vector *po2)
{
float s;
s=po1->x*po2->x + po1->y*po2->y + po1->z*po2->z;
return(s);
}
23. After running this program, the screen will show:
a. 45.0900000
b. 45.09
c. the answer is 45.09
d. the answer is 45.090000
C
24. The scalar() function uses po1->x to access the first member of the a
structure. Which of the following constructs can be equally used within the
function to access the same member?
a. po1.x
b. *po1.x
c. (*po1).x
d. a.x
C
25. Given the structure and pointer declarations shown below, choose the
assignment statement which sets the Price member of the structure pointed to
by PC to 1000.
struct Computer
{
char Manufacturer[30];
float Price;
int Memory;
} *PC;
a. PC->Price = 1000.0;
b. PC.Price = 1000.0;
c. *PC.Price = 1000.0;
d. Computer.Price = 1000.0;
A