SPC CAT-1: Mock 3 (Functions & Pointers)
Pattern: 5 coding questions (2 Easy + 2 Medium + 1 Hard) covering user-defined functions, pointer swapping, abs/labs, difftime, and result calculations.
← Back to Mocks Overview
Easy 1 — Maximum Using a Function
Write:
int max2(int a, int b)
and use it to print the larger number.
Input
17 9
Output
17
Another Input
0 0
Output
0
Another Input
-5 -10
Output
-5
Another Input
10 -10
Output
10
Another Input
100 100
Output
100
This covers:
- function declaration
- parameters
- return value
- function call
- modular programming
View Solution
#include <stdio.h>
int max2(int a, int b)
{
return a > b ? a : b;
}
int main()
{
int a, b;
scanf("%d %d", &a, &b);
printf("%d\n", max2(a, b));
return 0;
}
Easy 2 — Absolute Difference Using stdlib.h
Read two integers and print:
|a - b|
using:
abs()
Input
3 10
Output
7
Include:
#include <stdlib.h>
Another Input
0 0
Output
0
Another Input
-50 50
Output
100
Another Input
100 -50
Output
150
Another Input
10 10
Output
0
View Solution
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a, b;
scanf("%d %d", &a, &b);
printf("%d\n", abs(a - b));
return 0;
}
Medium 1 — Pointer Function Swap
Write:
void swap(int *a, int *b)
Swap two integers using pointers.
Input
12 35
Output
35 12
Another Input
0 0
Output
0 0
Another Input
-10 20
Output
20 -10
Another Input
100 -100
Output
-100 100
This covers:
int *p
&x
*p
plus functions.
View Solution
#include <stdio.h>
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int a, b;
scanf("%d %d", &a, &b);
swap(&a, &b);
printf("%d %d\n", a, b);
return 0;
}
Medium 2 — Time Difference
Given two timestamps in seconds:
t1 t2
store them as time_t, use:
difftime()
and convert the elapsed time into:
hours minutes seconds
Input
0 3723
Output
1 2 3
Include:
#include <time.h>
Another Input
0 0 0 0 0 0
Output
0 0 0
Another Input
23 59 59 23 59 59
Output
0 0 36
Another Input
10 15 30 14 20 45
Output
0 0 5
View Solution
#include <stdio.h>
#include <time.h>
int main()
{
long long input1, input2;
time_t t1, t2;
int total_seconds;
int hours, minutes, seconds;
scanf("%lld %lld", &input1, &input2);
t1 = (time_t)input1;
t2 = (time_t)input2;
total_seconds = (int)difftime(t2, t1);
hours = total_seconds / 3600;
total_seconds %= 3600;
minutes = total_seconds / 60;
seconds = total_seconds % 60;
printf("%d %d %d\n",
hours,
minutes,
seconds);
return 0;
}
Hard 1 — Complete Student Result System
Input:
student_id
marks_obtained
total_marks
attendance
assignments
Calculate percentage accurately.
Grade:
>= 90 → A
>= 75 → B
>= 60 → C
>= 40 → D
else → F
Eligibility:
percentage >= 60
attendance >= 75
assignments >= 5
Print:
ID: ...
Percentage: ...
Grade: ...
Eligibility: ...
Input
101 47 60 82 6
Output
ID: 101
Percentage: 78.33
Grade: B
Eligibility: Eligible
Another Input
1 2 3 80 5
Output
ID: 1
Percentage: 66.67
Grade: C
Eligibility: Eligible
Another Input
40 40 40 90 6
Output
ID: 40
Percentage: 100.00
Grade: A
Eligibility: Eligible
Another Input
39 50 60 85 8
Output
ID: 39
Percentage: 83.33
Grade: B
Eligibility: Eligible
Another Input
50 60 70 80 7
Output
ID: 50
Percentage: 85.71
Grade: B
Eligibility: Eligible
Another Input
80 80 80 95 10
Output
ID: 80
Percentage: 100.00
Grade: A
Eligibility: Eligible
Another Input
45 45 45 78 5
Output
ID: 45
Percentage: 100.00
Grade: A
Eligibility: Eligible
Another Input
100 100 100 100 10
Output
ID: 100
Percentage: 100.00
Grade: A
Eligibility: Eligible
Use:
- correct
scanf - type casting
- floating-point calculation
- relational operators
&&- nested ternary
- formatted output
View Solution
#include <stdio.h>
int main()
{
int student_id;
int marks_obtained;
int total_marks;
int attendance;
int assignments;
float percentage;
char grade;
const char *eligibility;
scanf("%d %d %d %d %d",
&student_id,
&marks_obtained,
&total_marks,
&attendance,
&assignments);
percentage =
(float)marks_obtained /
total_marks *
100.0;
grade =
percentage >= 90.0 ? 'A' :
percentage >= 75.0 ? 'B' :
percentage >= 60.0 ? 'C' :
percentage >= 40.0 ? 'D' :
'F';
eligibility =
(percentage >= 60.0 &&
attendance >= 75 &&
assignments >= 5)
? "Eligible"
: "Not Eligible";
printf("ID: %d\n", student_id);
printf("Percentage: %.2f\n", percentage);
printf("Grade: %c\n", grade);
printf("Eligibility: %s\n", eligibility);
return 0;
}