NPTEL Introduction to Programming in C - Important Questions with Explanations
🔹 Q1:
char abc[] = "C Programming";
printf("%s", abc + abc[3] - abc[4]);
Answer: B) rogramming
Explanation:
• abc[3] = 'r' = ASCII 114
• abc[4] = 'o' = ASCII 111
• Difference = 114 - 111 = 3
• So, abc + 3 = pointer to index 3
• abc[3] = 'r', abc[4] = 'o', abc[5] = 'g', etc.
• Hence, it prints from index 3 onwards: "rogramming"
🔹 Q2:
char s[] = "Hello";
printf("%c", *(s + 2) + 1);
Answer: m
Explanation:
• s[2] = 'l' = ASCII 108
• 108 + 1 = 109
• 109 = 'm' in ASCII
🔹 Q3:
int *a[10];
int b[10][10];
Which expressions are valid? Answer: All (a[2], *(a + 2), b[2][3])
1
Explanation:
• a[2] is valid: array of pointers
• *(a + 2) is same as a[2]
• b[2][3] is accessing 2D array: valid
🔹 Q4:
What is ASCII of 'A'? Answer: 65
Explanation: Standard ASCII value of capital A is 65
🔹 Q5:
char *x = "C Programming";
printf("%c", *x + 1);
Answer: D
Explanation:
• *x = 'C' = 67
• 67 + 1 = 68 = 'D'
🔹 Q6:
int a = 10, b = 20;
printf("%d", a+++b);
Answer: 31
Explanation:
• a+++b is interpreted as (a++) + b
• a = 10, a++ = 10, then a becomes 11
• So, 10 + 20 = 30
• But then a is 11, if reused later
2
🔹 Q7:
int a = 5;
printf("%d", ++a + a++);
Answer: 12
Explanation:
• ++a = 6
• a++ = 6 (then becomes 7)
• 6 + 6 = 12
🔹 Q8:
char s[] = "C";
printf("%d", *s);
Answer: 67
Explanation:
• *s = s[0] = 'C'
• 'C' = ASCII 67
🔹 Q9:
strlen("abc\0def") = ?
Answer: 3
Explanation:
• strlen counts characters up to the first null character '\0'
• So only "abc" is counted
3
🔹 Q10:
strcmp("apple", "apples")
Answer: Negative value
Explanation:
• "apple" is shorter and comes before "apples"
• At position 5: '\0' vs 's' → '\0' < 's'
• So, strcmp returns negative
🔹 Code-based Questions with Explanations
🔹 Code 1:
char s[] = "Coding";
printf("%s", s + 2);
Answer: "ding"
Explanation:
• s + 2 → pointer to index 2 → prints from 'd' onwards
🔹 Code 2:
char s[] = "NPTEL";
int count = 0;
for(int i=0; s[i]!='\0'; i++) {
if(s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'||s[i]=='U') count++;
}
printf("%d", count);
Answer: 1
Explanation: Only vowel is 'E'
4
🔹 Code 3:
int a[2][3] = {{1,2,3},{4,5,6}};
printf("%d", *(*(a+1)+2));
Answer: 6
Explanation:
• a+1 → second row
• *(a+1) → pointer to second row array
• *(a+1)+2 → pointer to third element in second row
• ((a+1)+2) = a[1][2] = 6
🔹 Code 4:
char c = 'A';
printf("%c", c + 5);
Answer: F
Explanation: 'A' = 65, 65 + 5 = 70 = 'F'
🔹 Code 5:
char *arr[] = {"NPTEL", "SWAYAM", "C"};
printf("%c", *arr[1] + 1);
Answer: 'T'
Explanation:
• arr[1] = "SWAYAM"
• *arr[1] = 'S' = 83
• 83 + 1 = 84 = 'T'
Tip: Understand pointer arithmetic, ASCII, and string functions to crack 90% of NPTEL C exam questions!