SPC CAT-1: Mock 2 (Operators & Standard Libraries)
Pattern: 5 coding questions (2 Easy + 2 Medium + 1 Hard) covering bitwise XOR, strlen, pow, increment tracing, and billing logic.
← Back to Mocks Overview
Easy 1 — Swap Two Integers Using XOR
Given:
a b
swap them without a temporary variable, using XOR.
Input
5 9
Output
9 5
Another Input
0 0
Output
0 0
Another Input
-5 10
Output
10 -5
Another Input
100 -200
Output
-200 100
Also test:
7 7
which must remain:
7 7
Use:
a = a ^ b;
b = a ^ b;
a = a ^ b;
View Solution
#include <stdio.h>
int main()
{
int a, b;
scanf("%d %d", &a, &b);
a = a ^ b;
b = a ^ b;
a = a ^ b;
printf("%d %d\n", a, b);
return 0;
}
Easy 2 — String Length
Read a string and print its length using strlen().
The input may contain spaces.
Input
Hello World
Output
11
Another Input
A
Output
1
Another Input
Z
Output
1
Another Input
Hello C
Output
7
Use:
#include <string.h>
and a character array such as:
char str[100];
View Solution
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
scanf(" %[^\n]", str);
printf("%d\n", (int)strlen(str));
return 0;
}
Medium 1 — Compound Interest Using pow()
Given:
principal
rate
time
n
where n is the number of times interest is compounded per year.
Calculate:
Amount = P × (1 + R/(n×100))^(n×T)
Compound Interest = Amount - P
Another Input
0 10 2 1
Output
0.00 0.00
Another Input
1000 0 2 1
Output
1000.00 0.00
Another Input
5000 5.5 3 4
Output
5890.34 890.34
Use:
pow()
and define:
#define HUNDRED 100.0
Input
1000 10 2 1
Output
1210.00 210.00
View Solution
#include <stdio.h>
#include <math.h>
#define HUNDRED 100.0
int main()
{
double principal, rate, time, n;
double amount, compound_interest;
scanf("%lf %lf %lf %lf",
&principal,
&rate,
&time,
&n);
amount =
principal *
pow(1.0 + rate / (n * HUNDRED),
n * time);
compound_interest = amount - principal;
printf("%.2f %.2f\n",
amount,
compound_interest);
return 0;
}
Medium 2 — Increment / Decrement Trace Program
Read x, then execute these exact statements:
a = x++;
b = ++x;
c = x--;
d = --x;
Print:
x a b c d
Input
5
Output
5 5 7 7 5
Another Input
0
Output
0 0 2 2 0
Another Input
-10
Output
-10 -10 -8 -8 -10
Another Input
100
Output
100 100 102 102 100
You need to understand:
x++
++x
x--
--x
View Solution
#include <stdio.h>
int main()
{
int x;
int a, b, c, d;
scanf("%d", &x);
a = x++;
b = ++x;
c = x--;
d = --x;
printf("%d %d %d %d %d\n",
x, a, b, c, d);
return 0;
}
Hard 1 — Shopping Discount Engine
Read:
amount
premium
coupon
where premium and coupon are either 0 or 1.
Base discount:
amount >= 10000 → 20%
amount >= 5000 → 15%
amount >= 2000 → 10%
otherwise → 5%
Then:
premium == 1 → add another 5% discount
maximum total discount → 25%
coupon == 1 → subtract ₹500 after percentage discount
Print the final payable amount.
Input
10000 1 1
Output
7000.00
Use nested conditional operators rather than if.
Another Input
1500 0 0
Output
1425.00
Another Input
2000 1 1
Output
1200.00
Another Input
5000 0 1
Output
3750.00
Another Input
10000 0 0
Output
8000.00
Another Input
15000 1 1
Output
10750.00
View Solution
#include <stdio.h>
int main()
{
double amount;
int premium, coupon;
double discount_rate;
double final_rate;
double total_amount;
scanf("%lf %d %d",
&amount,
&premium,
&coupon);
discount_rate =
amount >= 10000 ? 0.20 :
amount >= 5000 ? 0.15 :
amount >= 2000 ? 0.10 :
0.05;
discount_rate =
premium == 1
? discount_rate + 0.05
: discount_rate;
final_rate =
discount_rate > 0.25
? 0.25
: discount_rate;
total_amount =
amount - amount * final_rate;
total_amount =
coupon == 1
? total_amount - 500.0
: total_amount;
printf("%.2f\n", total_amount);
return 0;
}