Post Quiz
04/ /06 Name______
Question 1
Consider the following code segment:
main()
{
int i = 14;
int j = 20;
int k;
k = j / i * 7 % 4;
}
What is the final value of the variable k?
A. 3
B. 0
C. 2
D. 2.5
E. The program contains a compiler error
Question 2
Consider the following two code segments:
Segment 1:
scanf("%d", &k);
while (k 0) {
printf("%d", k);
k++;
}
Segment 2:
scanf("%d", &k);
while (k 0) {
printf("%d", k);
k++;
}
while (k 0) {
printf("%d", k);
k++;
}
Under which of the following conditions will the two code segments produce identical output?
I. The value read into variable k is greater than zero.
II. The value read into variable k is zero.
III. The value read into variable k is less than zero.
A. I only
B. II only
C. III only
D. I, II and III
E. I and III only
Question 3
Consider the following program:
void main() {
int x = 0;
int y = 1;
int z = 0;
if (x <= 0) {
if (y = 0)
z += 2;
}
else
z += 4;
}
What is the final value of the variable z?
A. 0
B. 2
C. 4
D. 6
E. None of the above.
Question 4
Which of the following code segments sets variable sum to be the sum of the odd numbers between 1 and 99?
A.
int k, sum = 0;
for (k = 1; k <= 99; k++)
if (k % 2 != 0)
sum++;
B.
int k = 1, sum = 0;
while (k <= 99))
sum += 2;
C.
int k, sum = 0;
for (k = 1; k <= 99; k++)
if (k % 2)
sum += k;
D.
int k = 2, sum = 0;
while (k <= 99)) {
sum ++;
k += 2;
}
E.
int k = 2, sum = 0;
while (k <= 99)) {
k += 2;
sum += k;
}
Question 5
Consider the following code segment:
switch (x) {
case 10:
case 20: y = 'a';
break;
case 30: y = 'b';
break;
default: y = 'c';
}
Which of the following code segments is equivalent to the one shown above?
A. if (x == 10 || x == 20)
y = 'a';
if (x == 30)
y = 'b';
y = 'c';
B. if (x == 10 || x == 20)
y = 'a';
else if (x == 30)
y = 'b';
else
y = 'c';
C. if (x = 10 & x <= 20)
y = 'a';
else if (x <= 30)
y = 'b';
else
y = 'c';
D. if (x >= 10 & x <= 20)
y = 'a';
if (x == 30)
y = 'b';
y = 'c';
E. if (x >= 10 & x <= 20)
y = 'a';
else if (x == 30)
y = 'b';
else
y = 'c';
Question 6
Consider the following program:
#define N = 5;
void main() {
int k;
int array A = {0, 0, 0, 0, 0};
for(k = 0; k < 2; k++) {
A[k] = 2;
A[N-k-1] = 1;
}
}
Which of the following shows the values of A after the program execution?
A. 2 2 1 1 1
B. 2 2 2 1 1
C. 1 1 0 1 1
D. 2 2 1 1 0
E. 2 2 0 1 1
Question 7
Consider the following code segment:
char* s1 = "SIS"
char* s2 = s1;
s2[1] = '*';
Which of the following best describes what happens when this code is executed?
A. A runtime error occurs because of an out-of-bounds string index.
B. A runtime error occurs because of an attempt to change a character in a string.
C. The code executes without error; the string s1 contains "SIS", and s2 contains "S*S"
D. The code executes without error; both s1 and s2 contain "S*S"
E. The code executes without error; both s1 and s2 contain "SIS"
Question 8
Consider the following program:
#include <stdio.h>
#define N 3
#define M 2
void main() {
int j, k;
for (j = 0; j < N; j++) {
for (k = 0; k < M; k++)
printf("* ");
printf("\n");
}
}
What is the output of the program?
A. * * * * * *
B. * * *
* * *
C. * *
* *
* *
D. *
*
E. * * * * * *
* * * * * *
Question 9
Consider the following program:
int foo(int, int, int);
main ()
{
int a = 3;
int b = 5;
int c = foo(a, b, a);
}
int foo (int x, int y, int z)
{
return x * (z - y);
}
What is the final value of the variable c?
A. 6
B. -6
C. 0
D. 10
E. -10
Question 10
Consider the following program:
main()
{
int i;
int t[3][5] = { {1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{2, 4, 3, 2, 1} };
i = t[1][4] + t[2][2];
}
What is the final value of the variable i?
A. The program generates a runtime error
B. 11
C. 8
D. 12
E. 13