CS3251 - Programming in C - Assessment Test I Answers
PART-A: Answer ALL Questions (12 × 2 = 24 Marks)
1. Use of Preprocessor Directives:
- Provide instructions to compiler before compilation.
- Example: #include, #define
2. External Storage Class:
- Keyword: extern
- Used to declare a global variable defined elsewhere.
3. Keywords in C Language:
- Reserved words like int, return, for, while, if.
4. C Program to Compare Two Strings:
#include <stdio.h>
#include <string.h>
int main() {
char str1[50], str2[50];
gets(str1); gets(str2);
if (strcmp(str1, str2) == 0)
printf("Equal");
else
printf("Not Equal");
return 0;
}
5. Advantages of Recursion:
- Simplifies code, reduces size.
6. Purpose and Prototype of strcpy:
- Copies string. Prototype: char *strcpy(char *dest, const char *src);
7. Switch Statements:
switch(expression) {
case 1: break;
default: break;
}
8. Input/Output Statements:
- Input: scanf(), gets()
- Output: printf(), puts()
9. Define String with Example:
- char name[] = "John";
10. String Functions:
- strlen(), strcpy(), strcmp(), strcat()
11. Sorting:
- Rearranging elements. Ex: Bubble Sort, etc.
12. C Program to Add Two Numbers:
int a, b, sum;
scanf("%d%d", &a, &b);
sum = a + b;
PART-B: Answer Any FOUR Questions (4 × 12 = 36 Marks)
11. a) Decision and Looping Statement:
if (a > b) { printf("a is greater"); }
int i=1; while(i<=5){ printf("%d ", i); i++; }
11. b) Operators in C:
- Arithmetic: +, -, *, /, %
- Relational: ==, !=, >, <
- Logical: &&, ||, !
- Assignment: =, +=, -=
- Bitwise: &, |, ^, <<, >>
12. a) Structure of C Program:
- Preprocessor, main(), declarations, I/O, processing, return
12. b) Array and Types:
- One-dim: int a[10];
- Two-dim: int b[3][3];
13. a) Binary and Linear Search:
- Linear: for loop check each
- Binary: mid check
13. b) Compare Strings Without strcmp():
while(str1[i]!='\0'||str2[i]!='\0'){
if(str1[i]!=str2[i]){ flag=1; break; }
i++;
}
if(flag==0) printf("Equal"); else printf("Not Equal");