Joseph Haugh
University of New Mexico
Question 1: Syntax
Which of these statements would result in an error?
int a = 40 * 2*(1 + 3);
int b = (10 * 10 * 10) + 2
int c = (2 + 3) * (2 + 3);
int d = 1/2 + 1/3 + 1/4 + 1/5 + 1/6;
int e = 1/2 - 1/4 + 1/8 - 1/16;
Question 1: Syntax
Which of these statements would result in an error?
int b = (10 * 10 * 10) + 2
Question 2: =
symbol
In the C programming language, the =
symbol is most accurately read:
Question 2: =
symbol
In the C programming language, the =
symbol is most accurately read:
Question 3: Alphabet char char c = getchar();
Which is true if and only if c
is a letter in the standard English alphabet?
Question 3: Alphabet char char c = getchar();
Which is true if and only if c
is a letter in the standard English alphabet?
Question 4: Call by value
In the C Programming Language, call by value means:
Question 4: Call by value
In the C Programming Language, call by value means:
Question 5: Automatic variable
In the C Programming Language, an automatic variable:
Question 5: Automatic variable
In the C Programming Language, an automatic variable is:
Question 6: if, else if, else
int main(void)
{
int x = 4;
if (x == 1) {
printf("x is 1\n");
}
else if (x == 2) {
printf("x is 2\n");
}
else x = 3; {
printf("x is %d\n", x);
}
}
What is the output of this code?
Question 6: if, else if, else
int main(void) {
int x = 4;
if (x == 1) {
printf("x is 1\n");
}
else if (x == 2) {
printf("x is 2\n");
}
else x = 3; {
printf("x is %d\n", x);
}
}
What is the output of this code?
Question 7: Flag
int main(void) {
int i, n;
int flag;
for (n = 10; n > 1; n--) {
flag = 0;
for (i = 2; i < n; i++) {
if(n % i == 0) flag = 1;
}
if (flag == 0)
printf("%d ", n);
}
printf("\n");
}
What is the output of this code?
Question 7: Flag
int main(void) {
int i, n;
int flag;
for (n = 10; n > 1; n--) {
flag = 0;
for (i = 2; i < n; i++) {
if(n % i == 0) flag = 1;
}
if (flag == 0)
printf("%d ", n);
}
printf("\n");
}
What is the output of this code?
Aside: Flag
Aside: More efficient printer of primes
int main(void) {
int i, n;
int flag;
for (n = 10; n > 1; n--) {
flag = 0;
for (i = 2; i < n; i++) {
if(n % i == 0) {
flag = 1;
break;
}
}
if (flag == 0)
printf("%d ", n);
}
printf("\n");
}
Once a factor of n
is found, n
cannot be prime, so break out of the inner loop.
Question 8: Functions
This code will not compile because:
int foo(float x);
void main(void) {
int n=5;
printf("%d\n", foo(n));
}
int foo(int n) {
return 2*n;
}
Question 8: Functions
This code will not compile because:
int foo(float x);
void main(void) {
int n=5;
printf("%d\n", foo(n));
}
int foo(int n) {
return 2*n;
}