NEW
Font size
S
M
L
XL
Worksheets20 dec 2023 SRMIST TRP CPS AN
Total questions: 15
Worksheet time: 15mins
Name
Class
Date
1.
Time complexity of the code?
int fun(int n, int m, int o)
{
if (n <= 0)
printf("%d, %d\n",m, o);
else
{
fun(n-1, m+1, o);
fun(n-1, m, o+1);
}
a)
Quadratic
b)
O (log n)
c)
O (n log m)
d)
Exponential
2.
What is the time complexity of the following code?
int fun(int x)
{
if (x <= 0)
return 1;
return 1 + fun(x-1);
}
a)
O(n)
b)
O (log n)
c)
O (n log n)
d)
O (log log n)
3.
What is the time complexity of the code?
int fun(int x)
{
if (x <= 0)
return 1;
return 1 + fun(x/5);
}
a)
O(n)
b)
O (log n)
c)
O (n log n)
d)
O (log log n)
4.
int sumDigits(int n) {
if (n == 0)
return 0;
else
return n % 10 + sumDigits(n / 10);
}
int main() {
printf("Sum of Digits: %d\n", sumDigits(123));
return 0;
}
a)
6
b)
5
c)
9
d)
12
5.
void show(int,int,int);
int main()
{
int a = 1;
show(++a, a++, a);
return 0;
}
void show(int i, int j, int k)
{
printf("%d %d %d,\n", i, j, k);
}
a)
1 1 3
b)
3 1 3
c)
3 1 1
d)
3 3 3
6.
#include <stdio.h>
int factorial(const int n) {
if (n <= 1)
return 1;
else
return n * factorial(n - 1);
}
int main() {
printf("Factorial: %d\n", factorial(5));
return 0;
}
a)
200
b)
100
c)
120
d)
error
7.
int mysteryFunction(int a, int b) {
if (b == 0)
return 1;
else
return a * mysteryFunction(a, b - 1);
}
int main() {
printf("Mystery Function: %d\n", mysteryFunction(2, 3));
return 0;
}
a)
8
b)
6
c)
16
d)
2
8.
What does the following recursive function calculate? int gcd(int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
int main() {
printf("GCD: %d\n", gcd(24, 36));
return 0;
}
a)
Greatest common divisor of a and b
b)
Least common multiple of a and b
c)
Exponential power of a with base b
d)
Factorial of a
9.
int countEven(int n) {
if (n == 0)
return 0;
else
return 1 + countEven(n - 2);
}
int main() {
printf("Count of Even Numbers: %d\n", countEven(10));
return 0;
}
a)
3
b)
4
c)
5
d)
6
10.
static int count = 0;
void recursiveFunction() {
if (count < 5) {
printf("%d ", count);
count++;
recursiveFunction();
}
}
int main() {
printf("Recursive Function: ");
recursiveFunction();
printf("\n");
return 0;
}
a)
0 1 2 3 4
b)
1 2 3 4 5
c)
0 2 4 6 8
d)
2 4 6 8 10
11.
void printPattern(int n) {
if (n > 0) {
printf("%d ", n);
printPattern(n - 1);
printf("%d ", n);
}
}
int main() {
printf("Print Pattern: ");
printPattern(3);
printf("\n");
return 0;
}
a)
3 2 1 1 2 3
b)
1 2 3 3 2 1
c)
3 3 2 1 1 2
d)
1 1 2 3 3 2
12.
void fibonacciSeries(int n, int a, int b) {
if (n > 0) {
printf("%d ", a);
fibonacciSeries(n - 1, b, a + b);
}
}
int main() {
printf("Fibonacci Series: ");
fibonacciSeries(5, 0, 1);
printf("\n");
return 0;
}
a)
0 1 1 2 3
b)
1 2 3 5 8
c)
0 1 2 3 5
d)
1 1 2 3 5
13.
int powerOfTwo(int n) {
if (n == 0)
return 1;
else
return 2 * powerOfTwo(n - 1);
}
int main() {
printf("Power of Two: %d\n", powerOfTwo(3));
return 0;
}
a)
4
b)
8
c)
16
d)
2
14.
int sumSeries(int n) {
if (n == 0)
return 0;
else
return n + sumSeries(n - 1);
}
int main() {
printf("Sum of Series: %d\n", sumSeries(4));
return 0;
}
a)
6
b)
10
c)
15
d)
20
15.
int productOfDigits(int n) {
if (n == 0)
return 1;
else
return (n % 10) * productOfDigits(n / 10);
}
int main() {
printf("Product of Digits: %d\n", productOfDigits(356));
return 0;
}
a)
120
b)
45
c)
90
d)
75
Reset
