Module 3 Important Questions
CSE1001 | Programming for Problem Solving
Prof. Jhalak Dutta Department of CSE [Link]@[Link]
Module 3 Important Questions
1. Write the output of the following program with justification.
2. Explain the difference between the following declarations:
(i) const int *p;
(ii) int *const p;
3. Write a program to accept a single line of text as a command line argument and print the number of
vowels, consonants and spaces present in the line.
4. Write the output of the following C program with justification.
5. What is the use of the static storage class in C? Illustrate with an example.
6. Write a program to delete all duplicate elements from a dynamically-allocated integer array.
7. Write the output of the following C program with justification.
8. Explain the difference between formal and actual parameters in C. When is the return statement
mandatory in a function? Explain with an example.
9. Explain the difference between function prototype, function definition and function call in C with
examples.
10. Can two functions in the same program have same name, given that their return types are different
and that they accept different number and type of arguments? Justify your answer.
Prof. Jhalak Dutta | Assistant Professor | Department of CSE |[Link]@[Link] 1
Module 3 Important Questions
11. Write a program to accept the name and marks of n number of students in 3 subjects where n is
taken as input from user. Print in descending order the rank list based on the average of the 3
subjects. Also print the name of the first ranker along with the corresponding percentage.
12. Write a function to calculate the sum of each row of a two-dimensional array. Also write the main()
function to show the function call.
13. What is the output of the following C program?
14. Write a C function with the prototype given below:
char* fun(char *);
It will take one word as input and encrypt it by shifting each alphabet to two alphabets
ahead. Assume that all alphabets are in uppercase and that the letter A follows Z in a cyclic
manner. Numbers and special characters remain unchanged. For example:
Input: @FUZZY^THEORY123
Output: @HWBBA^VJGQTA123
15. Write a C program to insert a target element (given by the user) into an array at a particular valid
position (also given by the user) assuming that the array is not full. Insertion will take place in a
user-defined function, which will be called by your main() function by passing the array, the target
element and the position as parameters. The array elements must be accessed by using pointers
only.
16. The C header file string.h contains a library function strcat (string1, string2) that appends a
character array string2 at the end of the character array string1. Write a user-defined C function
called mycat( ) that will behave exactly the same way strcat behaves. Elements of the character
arrays must be accessed through pointers.
17. Given an 2D integer array A[5][10] in C, explain the difference between the following notations:
(i) *(A + i)
(ii) *(*(A + i) + j)
Assume that i and j are both integers and that the values of i and j lie within the ranges
(0,4) and (0,9) respectively.
18. You are working for a stock trading company. You have to create a program that takes the closing
stock prices of a company for n days using dynamic memory allocation (using pointers). Write a
function findMaxProfit( ) that calculates the maximum profit that can be earned by buying and
selling the stock on specific days. The function should return the maximum profit and the days
when the stock should be bought and sold. If no profit is possible, display a message saying "No
Profit Possible". Access all the elements of the array using pointers only. Also, handle the case
where the stock prices are the same or decreasing throughout the period.
Prof. Jhalak Dutta | Assistant Professor | Department of CSE |[Link]@[Link] 2
Module 3 Important Questions
Sample Input 1:
Number of days: 7
Closing prices: 100 180 260 310 40 535 695
Sample Output 1:
Maximum profit: 655
Buy on day: 5
Sell on day: 7
Sample Input 2:
Number of days: 5
Closing prices: 200 180 160 150 100
Sample Output 2:
No Profit Possible
19. You are working for a company that collects customer feedback about its services. The feedback
score is between 1 and 5 where 1 = Very Bad, 2 = Bad, 3 = Neutral, 4 = Good, and 5 = Excellent.
Write a C program that takes feedback scores from n customers using an array and pointers. Then,
create a function calculateStats( ) that counts how many times each score appears, calculates and
displays the total number of feedback responses, computes the average feedback score, and shows
the percentage of positive feedback (score 4 or 5). Display the feedback frequency using a simple
list format. Handle invalid scores (greater than 5 or less than 1) by skipping them and displaying an
appropriate message. Access all array elements using pointer notation only.
Sample Input:
Number of customers: 8
Feedback scores: 1 5 3 2 4 5 6 2
Sample Output:
Invalid score: 6 - Skipped
Feedback Frequency:
1 -> 1
2 -> 2
3 -> 1
4 -> 1
5 -> 2
Total Feedbacks: 7
Average Score: 3.1
Positive Feedback Percentage: 42.86%
20. Write a C function which takes a single line of text as a parameter, extracts words from the text,
stores these words into an array within its local scope and returns that array to the calling function.
All arrays must be dynamically allocated. Here is a sample function prototype:
char** extract(char *line);
21. Write a C function to sort a list of integers in ascending order. The array containing the list of
integers must be passed to the function as parameter. The function should return the total number
of swaps done by your sorting algorithm. Can you describe a scenario where the value returned by
your function will be 0?
Prof. Jhalak Dutta | Assistant Professor | Department of CSE |[Link]@[Link] 3
Module 3 Important Questions
22. Write the output of the following C program with proper justification (pointer arithmetic involved):
int arr[] = {10, 20, 30, 40};
int *p = arr;
printf("%d %d", *(p + 2), p[3]); Explain how array indexing and pointer arithmetic
are related.
23. Write a C function with the prototype:
void swap(int *a, int *b);
The function should swap two integers using pointers. Also explain why call by reference is required
here instead of call by value.
24. Explain the difference between:
char str[] = "HELLO";
char *str = "HELLO";
What happens if you try to modify str[0] in each case? Justify your answer.
25. Write a program to dynamically allocate memory for a 2D matrix of size m × n. Then write a
function to compute the transpose of that matrix. All elements must be accessed using pointer
notation only.
26. What will be the output of the following program? Justify your answer.
int x = 10;
void fun() {
static int x = 5;
x++;
printf("%d ", x);
}
int main() {
fun();
fun();
fun();
return 0;
}
Explain why the output behaves this way.
27. Write a C program to merge two sorted arrays into a single sorted array. The merging operation
must be done inside a user-defined function. Return the total number of comparisons performed
during merging.
28. Explain the difference between:
• malloc()
• calloc()
• realloc()
• free() Write a short program demonstrating how realloc() can increase and decrease array size
dynamically.
Prof. Jhalak Dutta | Assistant Professor | Department of CSE |[Link]@[Link] 4
Module 3 Important Questions
29. Write a C program to find the second largest element in an integer array using a function. Handle
the case where all elements are equal. Access array elements using pointers only.
30. Consider the following program. What is the output? Justify your answer.
int a = 5;
int *p = &a;
*p += 3;
printf("%d %d", a, *p); Now modify the program so that changing *p does not affect a. Explain how
this is possible.
Prof. Jhalak Dutta | Assistant Professor | Department of CSE |[Link]@[Link] 5