C Programming Theory Qs
C Programming Theory Qs
Questions
Instructions
• This exam consists of 17 questions totaling 100 points.
• Good luck!
1. Explain the structure of a basic C program. What are the different sections of a C program?
Illustrate with a simple example. [6 points]
2. Define variable in C. What are the rules for naming a variable? How is a keyword different
from a variable? List any 5 keywords in C and their purpose. [7 points]
3. Write a C program that takes an integer as input and checks whether the number is odd or
even. Print an appropriate message. [6 points]
4. The following C code is supposed to print numbers from 1 to 5, but there’s an error in it.
Identify the error and correct the code: [5 points]
1 # include < stdio .h >
2 int main () {
3 int i = 1;
4 while ( i <= 5) {
5 printf ( " % d \ n " , i ) ;
6 }
7 return 0;
8 }
5. The following C code has an error. Identify the mistake and correct it: [5 points]
1 # include < stdio .h >
2 int main () {
3 int a = 10;
4 if ( a = 20) {
5 printf ( " a is 20\ n " ) ;
6 }
7 else {
8 printf ( " a is not 20\ n " ) ;
9 }
10 return 0;
11 }
1
C Programming Theory Exam Page 2
6. The following code has an error. Identify the mistake and correct it: [5 points]
1 # include < stdio .h >
2 int main () {
3 int num = 2;
4 switch ( num ) {
5 case 1:
6 printf ( " One \ n " ) ;
7 case 2:
8 printf ( " Two \ n " ) ;
9 case 3:
10 printf ( " Three \ n " ) ;
11 default :
12 printf ( " Invalid \ n " ) ;
13 }
14 return 0;
15 }
7. What will be the output of the following C program? Explain how the program works. [6
points]
1 # include < stdio .h >
2 int main () {
3 int n = 10;
4 do {
5 printf ( " % d " , n ) ;
6 n - -;
7 } while ( n >= 5) ;
8 return 0;
9 }
8. What are the different types of operators in C? Explain with examples the following types:
[9 points]
• Arithmetic Operators
• Relational Operators
• Logical Operators
9. Describe the different basic data types in C. What are the sizes (in bytes) of the following
data types: [6 points]
• int
• float
• char
• double
10. Compare the if-else statement and the switch statement in C. In what situations would
you prefer to use a switch statement instead of if-else? [6 points]
11. What is a loop in C programming? Explain the differences between the following types of
loops: [7 points]
• for
• while
• do-while
C Programming Theory Exam Page 3
12. Explain the purpose of scanf() and printf() functions in C. What is the significance of
format specifiers, and why are they necessary? Provide examples for reading and printing:
[8 points]
• An integer
• A floating-point number
• A character
13. What is the difference between the prefix (++i) and postfix (i++) increment operators?
Illustrate with an example program how they behave differently in a loop. [6 points]
14. Explain the concept of scope of a variable in C. What is the difference between local variables
and global variables? Provide an example to illustrate both types. [6 points]
15. Explain the use of break and continue statements in loops. How do they alter the flow of
a loop? Provide examples to demonstrate each. [6 points]
16. In C programming, errors can be classified into different categories. Describe the following
types of errors and give an example for each: [6 points]
• Syntax Errors
• Logical Errors
• Runtime Errors
17. What are ”nested loops” in C programming? Provide an example of a nested loop that prints
a pattern of stars as follows: [6 points]
*
**
***
****
*****