You are on page 1of 13

Ques 1.

Yugam just learnt about modular exponentiation and wanted to implement it on his own.
He wrote the following code which unfortunately gives wrong answer for many cases. You,
being his best friend, are asked to help him. Debug the code for him.

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int t,a,b,c,i,x=1;
scanf("%lld",&t);
for(i=0;i<t;i++)
{
scanf("%d%d%d",&a,&b,&c);
while(b>1)
{
if(e&1==1)
{
x=(x*a)%m;
a=(a*a)%m;
}
b/=2;
}
printf("%lld. %lld\n",i,x);
}
return 0;
}

Sample Input:
3
10 5 100
2 10 1023
6 6 1000007
Sample Output:
1. 0
2. 1
3. 46656
Ques 2:
John is really excited about his next lecture on data structures. As soon as the lecture starts,
the teacher asks them to write a program to implement queue as a linked list. Being a novice
in programming, John writes some code which is not working properly. Please help him to
debug the code.
#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
struct node
{
int info;
struct node* next;
}
typedef struct node* NODEPTR;
NODEPTR getnode()
{
NODEPTR p;
p=malloc(sizeof(node));
return p;
}

void freenode(NODEPTR p)
{
free(p);
}

struct queue
{
NODEPTR front,rear;
}
int Empty(struct queue *pq)
{
return((&pq->front==NULL)?TRUE:FALSE);
}
void Insert(struct queue *pq,int x)
{
NODEPTR p;
p=getnode();
p->info=x;
p->next=NULL;
(pq->rear)->next=p;
pq->rear=p;
}

int Remove(struct queue *pq)
{
NODEPTR p;
int x;
p=&pq->front;
x=p->info;
&pq->front=p->next;
if(pq->front==NULL)
pq->rear=NULL;
freenode(p);
return x;
}


int main()
{
struct queue q;
q.front=NULL;
q.rear=NULL;
int c,data;
printf("Enter 1 for insertion and 2 for deletion\n");
while(1)
{
printf("\nEnter your choice:");
scanf("%d",&c);
if(c==1)
{
printf("Enter element for inserting:");
scanf("%d",&data);
Insert(&q,data);
printf("element %d has been inserted\n",data);
}
else if(c==2)
{
data=Remove(&q);
printf("element %d has been removed from the queue\n",data);
}
else
printf("enter valid choice\n");
}
return 0;
}

Ques 3:
Ian is fascinated about bits. His friend Nina just gave him an interesting task. She asked him
to write a program which counted the number of set bits in a number. Ian has some
experience in programming and so he wrote this piece of code. As expected, the program
has some bugs which need to be removed. Help him do this task.
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,i;
scanf("%d",&n);
int c=0;
for(i=n;i>0;i--)
{
while(i)
{
c+=i&1;
i>>=1;
}
}
printf("%d\n",c);
}
return 0;
}
Sample Input:
4
1
5
100
23
Sample Output:
1
7
319
52
Ques 4:
Paul has just learnt the concept of classes in C++. He wrote the following code but to his
great surprise, it gave an error. Please help him debug the code.
#include<iostream>
using namespace std;
class Base
{
protected:
int x;
public:
Base (int i)
{
x=i;
}
};

class Derived : public Base
{
public:
Derived (int i):x(i){ }
void print() { cout<<x; }
};
int main()
{
Derived d(10);
d.print();
}
Input: None
Output: 10

Ques 5:
Divyanshi loves to give challenges to her friends. One day, she asked her friend Sam to write
a program in C++ to overload main function in C++. Daring as he is, Sam wrote the
following code but unfortunately it produces compilation errors. Help him debug the code.
#include<iostream>
using namespace std;
int main(int a)
{
cout<<a<<"\n";
return 0;
}
int main(char *a)
{
cout<<a<<endl;
return 0;
}
int main(int a, int b)
{
cout<<a<<" "<<b;
return 0;
}
int main()
{
main(3);
main("Sam");
main(9,6);
return 0;
}

Input: None
Output:
3
Sam
9 6
Ques 6:
You have to write a program to reverse a string. Debug the program to produce the required
output.
# include <stdio.h>
void reverse(char str)
{
reverse(str);
printf("%c", str);
}
int main()
char s[100];
{
Printf(Enter string to reverse:);
Scanf(%s,&s);
reverse(s);
return 0;
}
Sample Input:
NAME
Sample Output:
EMAN

Ques 7:
Saumya was asked to check if one string was rotation of the other. She wrote the following
program which needs to be debugged. Help her.
# include <stdio.h>
# include <stdlib.h>
int isRotation(char *str1, char *str2)
{
int size1 = strlen(str1);
int size2 = strlen(str2);
char *temp;
void *ptr;
if (size1 != size2)
return 0;
temp = malloc(sizeof(char)*(size1*2));
strcat(temp, str1);
strcat(temp, str1);
ptr = strstr(temp, str2);

if (ptr != NULL)
return 1;
else
return 0;
}
int main()
{
Char str1[100],str2[100];
Printf(Enter 2 strings);
Scanf(%s%s,&str1,&str2);
if (isRotation(str1, str2))
printf("Strings are rotations of each other\n");
else
printf("Strings are not rotations of each other\n");
return 0;
}
Sample Input:
AMSTERDAM DAMAMSTER
Sample Output:
Strings are rotations of each other

Ques 8:
Ankita wants to implement her own atoi() function, so she wrote the following code. But it
does not handle all the cases. Debug the program so that it handles those cases where the
number is negative or the string contains non-integer values.
#include <stdio.h>
int myAtoi(char *str)
{
int res = 0;
for (int i = 0; str[i] != '\0'; ++i)
res = res*10 + str[i] - '0';
return res;
}
int main()
{
char str[] = "89789";
int val = myAtoi(str);
printf ("%d ", val);
return 0;
}
Sample Input:
-12345 // This is a string
Sample Output:
-12345 // This is an integer

Ques 9:
Sanskriti has to write a program which does the following work. Given a number n, it
returns the smallest value of x-y where x>y and x.y=n. She wrote the following program but
it does not give the correct output. Debug the program for her.
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
long long int n,i;
scanf("%lld",&n);
for(i=1;i<=n/2;i++)
{
if(n%i==0)
{
printf("%d\n",i-n/i);
break;
}
}
}
return 0;
}

Sample Input:
4
5
99
125
759
Sample Output:
4
2
20
10

You might also like