Pre Quiz

Fall 2007 Name______

Question 1

Consider the following code segment:

main()

{

int i = 7;

int j = 20;

int k;

k = j / i * 7 % 4;

}

What is the final value of the variable k?

A. 5

B. 0

C. 2

D. 3.25

E. The program contains a compiler error

Type your answer below:


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 and III only

E. I, II and III

Type your answer below:


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.

Type your answer below:


Question 4

Which of the following code segments sets variable sum to be the sum of the even numbers between 1 and 99?

A.

int k, sum = 0;

for (k = 1; k <= 99; k++)

if (k % 2 == 0)

sum++;

B.

int k, sum = 0;

for (k = 1; k <= 99; k++)

if (k % 2 == 0)

sum += k;

C.

int k = 1, sum = 0;

while (k <= 99)) {

sum += k;

k += 2;

}

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;

}

Type your answer below:


Question 5

Consider the following code segment:

switch (x) {

case 10: y = 'a';

break;

case 20:

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)

y = 'a';

if (x == 20 || x == 30)

y = 'b';

y = 'c';

B. if (x == 10)

y = 'a';

if (x >= 20 & x <= 30)

y = 'b';

y = 'c';

C. if (x == 10)

y = 'a';

else if (x == 20 || x == 30)

y = 'b';

else

y = 'c';

D. if (x <= 10)

y = 'a';

else if (x >= 20 & x <= 30)

y = 'b';

else

y = 'c';

E. if (x == 10)

y = 'a';

else if (x >= 20 & x <= 30)

y = 'b';

else

y = 'c';

Type your answer below:

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] = 1;

A[N-k-1] = 2;

}

}

Which of the following shows the values of A after the program execution?

A. 1 1 2 2 2

B. 1 1 1 2 2

C. 2 2 0 2 2

D. 1 1 0 2 2

E. 1 1 2 2 0

Type your answer below:


Question 7

Consider the following code segment:

char* s1 = "Hello"

char* s2 = s1;

s2[2] = '!';

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 "Hello", and s2 contains "He!lo"

D. The code executes without error; both s1 and s2 contain "Hello"

E. The code executes without error; both s1 and s2 contain "He!lo"

Type your answer below:


Question 8

Consider the following program:

#include <stdio.h>

#define N 2

#define M 3

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. * * * * * *

* * * * * *

Type your answer below:


Question 9

Consider the following program:

int foo(int, int, int);

main ()

{

int a = 5;

int b = 3;

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. 10

B. 5

C. -10

D. -15

E. 2

Type your answer below:


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[2][1] + t[1][3];

}

What is the final value of the variable i?

A. The program generates a runtime error

B. 13

C. 9

D. 4

E. 7

Type your answer below: