Test Yourself

Here are 20 multiple choice questions to test your knowledge of C programming and computer architecture. When you have completed the form, it will be marked automatically and a grade will be returned immediately, together with the numbers of any questions you got wrong.

The test is anonymous -- you cannot be identified. You may submit as many attempts as you like. (This means you could work out all the right answers by trial and error, but who are you fooling? Only yourself.)

Good luck in the test, and good luck in the exam!


Instructions

Attempt ALL 20 questions. All questions carry equal marks.

For each question, indicate your choice by selecting button (a) to (e), or [reset this question] if you wish to leave your answer blank.

Although marks will not be deducted for incorrect answers, randomly chosen answers are highly unlikely to gain a pass.

All questions refer to the ANSI C language, and all code is in C.


1. Which one of the following sentences is true?

(a)  In the binary number system, the only digits are 0, 1 and 2.
(b)  Computers actually store integers as floating point numbers.
(c)  All information handled by a computer must be in binary form.
(d)  The ASCII code is used to represent floating point numbers.
(e)  The decimal number 34 may be expressed in hexadecimal as 52.
        [reset this question  ]

2. A computer MUST contain:

(a)  An operating system and a disk drive
(b)  A central processing unit, buses and memory
(c)  A screen and a keyboard
(d)  A compiler and integrated circuits
(e)  At least 1 megabyte of RAM
        [reset this question  ]

3. The following are simple data types:

(a)  char[], long, short, double
(b)  *char, double, long, *float
(c)  float, double, struct, pointer
(d)  pointer, int, float, long
(e)  int, short, long, float
        [reset this question  ]

4. Assuming the #include <stdio.h> directive, which statement will print the following?

     5.0 to the power 3 is 125.00
(a)  printf("%f to the power %f is %f\n", 5.0, 3, 125.0);
(b)  printf("%3f to the power %1i is %6f\n", 5.0, 3, 125.0);
(c)  printf("%3.1f to the power %i is %6.2f\n", 5.0, 3, 125.0);
(d)  puts("5.0 to the power 3 is %f", 5*5*5);
(e)  printf("%i to the power %i is %i\n", 5, 3, 125);
        [reset this question  ]

5. How many of the following declarations are correct?

     int i = 7.0;
     double void = 0.000;
     short array[2] = {0, 1, 2};
     char c = "\n";
(a)  None
(b)  One is correct
(c)  Two are correct
(d)  Three are correct
(e)  All four are correct
        [reset this question  ]

6. What is the value of the following expression?

     (13 / 4 * 3) % 5 + 1
(a)  5.75
(b)  2.95
(c)  1.4875
(d)  5
(e)  The expression is not valid
        [reset this question  ]

7. For what values of integer i is the following expression non-zero?

     i > 5 && !(i > 17)
(a)  5, 6, 7, ... , 16, 17
(b)  6, 7, 8, ... , 16, 17
(c)  5, 6, 7, ... , 15, 16
(d)  6, 7, 8, ... , 15, 16
(e)  6, 7, 8, ... , 17, 18
        [reset this question  ]

8. Which one of the following will set the value of y to 5 if x has the value 3, but not otherwise?

(a)  if (x = 3) y = 5;
(b)  if x == 3 (y = 5);
(c)  if (x == 3); y = 5;
(d)  if (x = 3) y == 5;
(e)  if (x == 3) y = 5;
        [reset this question  ]

9. What are the values of p and q after the following statement has been executed?

     for (p = q = 0; p < 3; p++) q += p;
(a)  p is 2, q is 0
(b)  p is 0, q is 0
(c)  p is 2, q is 3
(d)  p is 3, q is 3
(e)  p is 2, q is 2
        [reset this question  ]

10. Which one of the following sentences is true?

(a)  The body of a while loop is executed at least once.
(b)  The body of a do...while loop is executed at least once.
(c)  The body of a do...while loop is executed zero or more times. 
(d)  A for loop can never be used in place of a while loop.  
(e)  A for loop can never be used in place of a do...while loop.
        [reset this question  ]

11. Consider the following program fragment:

     int block[] = {13, 2, 5, 8};
     block[4] = 15;

Which one of the following sentences is true?

(a)  The code will cause compile-time errors.
(b)  The code will compile but not run.
(c)  The code will compile and run but may produce 
        incorrect results.
(d)  The code will compile and will always run properly.
(e)  The code will compile and run, but may damage the 
        computer's memory. 
        [reset this question  ]

12. Consider the following program fragment:

 int table[2][4], i, j;
 for (i = 0; i <2; i++) for (j=0; j < 4; j++) table[i][j]=i + j;

Which one of the following sentences is true?

(a)  The code will cause compile-time errors.
(b)  The code will compile but cause run-time errors.
(c)  The value of table[1][3] will be set to 4.
(d)  The value of table[3][1] will be set to 4.
(e)  The value of table[0][0] will be set to 2.
        [reset this question  ]

13. What are the values of trouble and *pt after the following program fragment has been executed?

     double trouble = 13.13;
     double *pt;
     pt = &trouble;
     *pt = 9.9;
(a)  trouble is 13.13, *pt is 9.9
(b)  trouble is 9.9, *pt is 13.13
(c)  trouble is undefined, *pt is 9.9
(d)  trouble is 13.13, *pt is undefined
(e)  trouble is 9.9, *pt is 9.9
        [reset this question  ]

14. Consider the following declaration:

     short sheep[4][32];

Which one of the following sentences is true?

(a)  sheep[1][1] is a pointer-to-short.
(b)  sheep[1] is a short.
(c)  sheep[0] is a pointer-to-pointer-to-short.
(d)  sheep is a pointer-to-pointer-to-short.
(e)  sheep is a short.
        [reset this question  ]

15. How many of the following string declarations are correct?

     char string1 = "Hello";
     char string2[] = "Hello";
     char string3[5] = "Hello";
     char string4[6] = {'H','e','l','l','o','\0'};
(a)  None
(b)  One is correct
(c)  Two are correct
(d)  Three are correct
(e)  All four are correct
        [reset this question  ]

16. Consider the following program fragment, assuming the #include <stdio.h> directive:

     char saying[] = "Too many cooks spoil the broth.";
     char *p1, *p2;
     p1 = saying;
     p2 = saying + 8;
     *p2 = '\0';
     printf("%s\n", saying);

What string will be printed?

(a)  "Too many cooks spoil the broth."
(b)  "cooks spoil the broth."
(c)  "Too many"
(d)  "Too manycooks spoil the broth."
(e)  "saying"
        [reset this question  ]

17. Which one of the following sentences is true?

(a)  Function calls always use the call-by-value method.
(b)  All functions must have prototypes. 
(c)  All the arguments of a function must be of the same data type.
(d)  Passing an array to a function involves a pointer.
(e)  Every function must return a value.
        [reset this question  ]

18. Consider the following program fragment:

     int b = 1;

     int fiddle(int c)
     {  c *= 2; b = 0;
        return 3;
     }

     main()
     { int a = 5, b = 7, c = 2;
     c = fiddle(a);
     /* HERE */
     }

What are the values of a, b and c when execution reaches /* HERE */?

(a)  a is  5, b is 1, c is 4
(b)  a is 10, b is 7, c is 2
(c)  a is 10, b is 1, c is 3
(d)  a is  5, b is 0, c is 2
(e)  a is  5, b is 7, c is 3
        [reset this question  ]

19. Consider the following program fragment, assuming the #include <stdio.h> directive:

     FILE *stream;
     textfile = "file.txt";

How could the file file.txt (present in the current directory) be opened for reading?

(a)  stream = fopen(textfile, "r");
(b)  textfile = fopen(stream, "r");
(c)  fopen(stream.textfile, r);
(d)  *stream = fopen(textfile, stream);
(e)  file.txt = fopen(*stream, "r");
        [reset this question  ]

20. A structured data type and variable is declared as follows:

     struct person { char name[40];
                     int age;
                     char gender;
                   } someone = {"Francis Doolittle", 20, 'm'};

How many of the following statements are valid?

     person another = someone;
     someone.name = "Joseph Q. Chang";
     (person.age)++;
     gender = (char) age;
(a)  None
(b)  One statement is valid
(c)  Two statements are valid
(d)  Three statements are valid
(e)  All four statements are valid
        [reset this question  ]

Answers here