Testing Students' Knowledge of C Language Using ...

5 downloads 1912 Views 68KB Size Report
braces, and angle brackets [8]. The condensed fashion of coding in C is not always ..... ourse/index.html. [2]. Budny, D., Lund, L., Vipperman, J., & Patzer, J. L..
Testing Students' Knowledge of C Language Using Bonus Questions M. Korenblit Department of Computer Science, Holon Institute of Technology, Holon, Israel

Abstract - The paper deals with special questions which are asked students in exams in C programming language. A student receives a very short program in C and has to answer either a question on output of the program or on a function return. For some students, at first glance, the program seems tangled and strange, and the question looks unsolvable. In fact, however, the problem is immediately solved if a student understands some details of C. Keywords: C Programming Language, Bonus Question, ASCII Code, Character, Function, Operator

1

Introduction

C [3, 6] is a general-purpose high-level computer language. It is one of the most widely used programming languages and there are very few computer architectures for which a C compiler does not exist. C has greatly influenced many other popular programming languages, such as C++, C#, and others. C was designed to provide low-level access to memory with language constructs that map efficiently to machine instructions. Despite its low-level capabilities, the language encourages cross-platform programming. A standardscompliant and portably written C program can be compiled for a very wide variety of computer platforms and operating systems with few changes to its source code. The language is available on a very wide range of platforms, from embedded microcontrollers to supercomputers [11]. For this reason, C is widely used in academia, industry and commerce [4]. It is taught in many institutions of higher learning as the first (or basic) programming language for students of computer specialties and as an example of a programming language for students of non-computer specialties. For a computer specialty, a course in C is not only a prerequisite for other programming courses, it is required for all advanced studies in computer science. In addition, for students for whom computers are not a specialty, a course in C language can inspire the ability of logic thinking, provide advantaged computer method and application tools for later specialty theory studying [9]. At the same time, the experience with teaching C programming has shown that students have a problem understanding a number of its concepts and dealing with the syntax of the language [2]. C’s syntax, specifically, the syntax of input/output functions, is not elegant. The C programming

language can be mysterious and gives special meaning to many punctuation characters, such as asterisks, plus sign, braces, and angle brackets [8]. The condensed fashion of coding in C is not always clear for beginners. At the same time, in comparison to a MATLAB program, the C corresponding code is longer and carries a substantial overhead [4]. Because of these facts, many papers are devoted to teaching C programming. In some of these publications [2, 7, 9] the authors describe the specific courses in C language which are taught in their institutions. A number of articles [2, 4, 5, 7] compare and contrast C with other programming languages. Some of these concern the teaching of C (successively and concurrently) with other computer languages and platforms. Effective methods for teaching the C programming language are discussed in [8] and [10].

2

Exams in C and use of bonus questions

In this paper, we describe some special questions asked on exams in C programming at the Holon Institute of Technology. The titles of courses in which these exams are given are: “Programming in C” and “Advanced Programming Workshop”. The first course is intended for the first year students of electrical and electronics engineering. The second one is given to the first year students of the computer science department. The students take this second course after taking “Introduction to Compute Science.” In “Advanced Programming Workshop” students acquire skill in programming on the base of advanced tools in C. The following are examples of the kinds of questions asked on an exam in C programming: 1. Write a function that implements an algorithm to solve any problem. 2. Write a complete program that uses the above function and standard functions. 3. Fill in the blanks in the code of the following function (program). 4. Identify and correct the errors in the following code. 5. What does the following program (function) do? 6. What is the output of the following program? The questions concerning writing a code are the most important. They constitute a significant part of the exam and have the most weight on the final grade. A question like number 6 (shown above) is asked as a bonus question. A student receives a very short program and has to

show its output. At first glance, the program seems tangled and strange, and the question looks unsolvable. In fact, however, the problem is solved immediately if a student understands some details of C. A student gets additional points for the correct answer to this question. In addition, the question is very easy in checking exams. Although these questions are relatively short, by answering them, students demonstrate their understanding (or lack of understanding) of the material. The credit for developing this kind of question belongs to my colleague, Alexander Abramovich. He proposed the questions presented in Examples 1 and 2 (see below) which were included in the final exams of the course “Programming in C” in 2004 [1].

3

Examples of bonus questions Example 1 What is the output of the following program? #include void main() { printf("%d", '-'-'-'); }

This program prints the numerical value (specification %d indicates it) of the expression '-'-'-' which may seem strange. What is it? Let us analyze. Solution. The minus sign denotes the subtraction operator. However, the same sign in single quotes is a character. Each character has its numeric representation in the computer (ASCII code) and an arithmetic operation on characters is actually the operation on their codes. The first and the third minus signs (but not the second one) in our expression are in single quotes. Therefore, we have here the difference of codes of the same character, i.e., the difference of two equal numbers. This difference equals 0 and thus, the output of the program is 0. What must a student know to answer this question? First, he has to know the standard function printf and its specifications. Second, he must understand that it is possibly to perform arithmetic operations on characters as on numbers. Third, he has to possess the skills to read the sentence printf("%d", '-'-'-') and to distinguish its corresponding parts. At last, quick-wittedness is an important factor as well. Sometimes, a student asks during the exam: “What is the ASCII code of '-'?” That is, despite understanding the essence of the problem, he fails in the last step and does not see that the information on the ASCII code value is redundant.

Example 2 What is the output of the following program? #include void main() { printf("%d", '/'/'/'); } Solution. The idea is the same. The slash denotes the division operator. Therefore, we have the quotient of two equal numbers and thus, the output of the program is 1.

Example 3 What is the output of the following program? #include void main() { printf("%d", '%'%'%'); } Solution. The idea is the same. The percent sign denotes the remainder operator. The remainder after division of two equal numbers equals 0 and thus, the output of the program is 0.

Example 4 What is the output of the following program? #include void main() { printf("%d", '&'&&'&'); } Solution. The sign && denotes the logical operator AND. The ASCII code of each ordinary character (including '&') is a positive number. Any nonzero value in C is interpreted as TRUE. TRUE AND TRUE gives TRUE and the numerical value of TRUE is 1. Thus, the output of the program is 1. Here, in addition to demands of the previous examples, a student must understand that there is no essential difference between arithmetic and logical expressions in C. C evaluates all expressions that include relational, equality, and/or logical operations to 0 or 1 which represent FALSE or TRUE, respectively. Moreover, C accepts any nonzero value as TRUE and zero is treated as FALSE.

Example 5 What is the output of the following program?

When answering this question a student should remember that the unary operator ! has precedence compared to the binary operator !=.

#include

Example 8

void main() { printf("%d", '