0% found this document useful (0 votes)
71 views13 pages

Set-1 Automata Fix MCQs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views13 pages

Set-1 Automata Fix MCQs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Set-1

Automata Fix

Q1. Anita is given a task to find number of digits in a given array .For example
number of digits in 235 is three. She

writes a code in order to get desired result. Point out the Logical error in the
code and also suggest what should be in

place of MISSING LINES to get the desired output.


INCORRECT CODE:
int main (void)
{
int arr[5] ={120,76,85,45,56},i,a=0;
for(i=0;i<5;i++)
{
a=0;
while(arr[i]/10>=0)
{
a++;
MISSING LINE-1
}
printf(“%d”, MISSING LINE-2);
}
return 0;
}
}

A) a[i]/10; a++;
B) a[i]=a[i]/10; a++;
C) a[i] /= 10; ++a;
D) None of these

Q2. The following code is of Single Linked List Whose task is to detect the
loop. Lets list has 5 elements:
2->14->6->18->9, and a loop from 9 to 6.
Finally I have removed the loop and my list look like 2->14->6->18->9, but due
to some Logical error code is still

printing LOOP DETECTED.


In main we are calling detectLoop function as: detectLoop(head);
Your job is to rectify the code so that we get desired result.
INCORRECT CODE:
void detectLoop(struct Node* list)
{
1 struct Node *slow_p=list,*fast_p=list;
2 int f=0;
3 while(slow_p && fast_p &&fast_p->next)
4 {
5 slow_p=slow_p->next;
6 fast_p=fast_p->next;
7 if(slow_p==fast_p)
8 {
9 f=1;
10 missing code;
11 }
12 }
13 if(f==1)
14 printf(“Loop detected”);
15 else
16 printf(“No Loop”);
}

A) In line-5: slow_p=fast_p->next and missing code: break


B) In line-5: slow_p=fast_p->next and missing code: Not required
C) In line-6: fast_p=fast_p->next->next and missing code: break
D) None of these

Q3. Given code of bubble sort there are two Missing codes you need to fill these
Missing and correct the code.
INCORRECT CODE:
int main()
{
int arr[]={10,20,30,40,50},I,j, isSwap;
int n=sizeof(arr)/sizeof(*arr);
isSwap=1;
for(i=0;i<n-1 && Missing-1 ; i++)
{
Missing-2;
for(j=0;j<n-i-1;++j)
{
if(arr[j]>arr[j+1])
{
swap(&arr[j], &arr[j+1])
isSwap=1;
}
}
}

A) Missing-1: isSwap Missing-2: isSwap=0


B) Missing-1: isSwap>0 Missing-2: isSwap=1
c) Missing-1: isSwap==0 Missing-2: isSwap=1
D) None of these

Q4. Find the Errors in the given C function which tries to find out the common
elements in three sorted arrays A,B

and C have p,q, and r elements. Note that arrays are sorted in ascending
1.void common(int A[],int B[],int C[],int p,int q, int r){
2.int i,j,k;
3.for(i=0;i<p;i++){
4. for(j=0;j<q;j++){
5. for(k=0;k<r;k++){
6. if(A[i]==B[j] && B[j]==C[k]){
7. printf("%d",A[i]);
8. i++;
9. j++;
10. K++;}
11. else if(A[i]<B[j])
12. i++;
13. else if(B[j]<C[k])
14. j++;
15. else
16. k++;
17. }
18. }
19. }
20.}

A:Lines 3,4,5,7
B:Lines 4,11,15
C:Lines 4,5,6,7
D:Lines 5,11,13

Q5. Given a sorted array B of Size m having elements in descending order.It is


then rotated X number of times, This

C function finds the value of X .Identify the erroneous Line/Lines in the code.

1.void rotateCount(intB[],int m){


2. int fix=B[0],fix_index=1;
3. for(int i=0;i<m;i++){
4. if(fix>B[i]){
5. fix=B[i];
6. fix_index=i;
7. }
8. }
9. return fix-index ;
10.}

A: Line 1, Line 2
B: Line 3, Line 6
C: Line 9, Line 6
D: Line 1, Line 4

Q6. The following C Function Takes a array sorted in ascending order which
was rotated a few times and then find the

first element of the original order. You need to identify the line/lines having
error/errors in the code

1.int find(int arr[],int n)


2.{
3. for(int i=1;i<n-1;i++)
4. {
5. if(arr[i]>arr[i-1])
6. {
7. return arr[i];
8. }
9. }
10. return -99;
11.}
A: Line 5, Line 10,
B: Line 3,Line 5
C: Line 5
D: Line 3,Line 10

Q7. In the given pseudocode of sorting an array ,there are two blank spaces at
Line 6 and 15 , according to you which

option will make this code complete and produce the resultant array in sorted
form . a[]={77,22,44,11,88,33,66,55}
1.sort()
2. Integer a [],i,j=1,k,min,temp,flag;
3. Repeat while(j<=length a[])
4. min =a[j];
5. Repeat for i=j+1 to length a[]
6. if(min__ a[i])
7. min=a[i];
8. k=i;
9. flag=1;
10. End of if loop
11. End of for Loop
12. if(flag==1)
13. swap(a[j] and a[k])
14. j++;
15. -----------
16. end of while loop
17.end of sort()

A: < and flag=0


B: > and flag=0
C: < and flag=1
D: > and flag=1

Q8. In an array, the majority element refers to an element that appears more
than half of the array's size. In other
words, if an array has N elements, the majority element occurs more than N/2
times. A majority element may not

always exist in an array in that case return -1.


The majorityElement( ) takes two input, first one is the array and second one is
the number of elements in the array.

You need to identify the line/lines having syntactical or logical error/errors in


the code .

1.int majorityElement(int a[], int size)


2.{
3. map<int,int> mp;
4. for(int i=0;i<size;i++)
5. mp[a[i]++];
6. for(int i=0;i<size;i++)
7. {
8. if(mp[a[i]]> size/2)
9. return a[i];
10. }
11. return -1;
12.}

A. Line1, Line4
B. Line4, Line5
C. Line5
D. Line8.

Q9. Given an array A of n positive numbers. The task is to find the first
Equilibrium Point in an array.
An equilibrium point in an array is a position at which the sum of elements on
the left side of the point is equal to
the sum of elements on the right side. In other words, it is a position where the
array can be split into two parts,
and the sum of elements on both sides is the same.
Ex: n = 5
A[] = {1,3,5,2,2}
A[0]+A[1] = A[3]+A[4] = 4 so element at index 2 is equilibrium point and
Ans is 2.
1.int equilibriumPoint(long long a[], int n) {
2. int s=0,ls=0;
3. for(int i=0;i<n;i++)
4. s+=a[i];
5. for(int i=0;i<n;i++){
6. s-=a[i];
7. if(ls==s){
8. return i+1;
9. ls+=a[i];
10. }//end if
11. }//end for
12. return -1;
13. }

A. Line8, Line9
B. Line4, Line5
C. Line5, line8
D. Line8

Q 10. Given a function binarySearch() where few lines are not correct and
produce incorrect results, you need to

identify those lines from the given options.

1. int binarySearch(int arr[], int L,int R, int item)


2.{
3. if(R>=L)
4. {
5. int mid=L+(R-L)/2
6. if(arr[mid]==item);
7. return mid;
8. if(arr[mid]>item)
9. return binarySearch(arr,L,mid+1,item);
10. return binarySearch(arr,mid-1,R,item);
11. }
12. return -1;
13. }

A: Line 9 and 10
B: Line 5
C: Line 3
D: Line 12

Q11.Raj wants to write a clever program which takes a string input from the
user prints both the input string and its

length.

1.#include<stdio.h>
2.int main(){
3. int n;
4. char string[20];
5. scanf("%s", string);
6. printf("%c",printf("%s",string));
7. return 0;
8 }

Choose all Logical Errors from list below that may apply to program
A: The format specifier used is wrong
B: The string does not accept space characters
C: Extra variable n is used
D: May exceed storage allocated

Q12. The Program below tries to find the second most frequent character
.Identify the errors in program below.

1.#include<stdio.h>
2. int main(){
3. chr str[100]=="okay fine Hello";
4. int i, word;
5. i=0;
6. word=1;
7. while(str[i]!='\0'){
8. if(str[i]==''){
9. word++;
10. i++;
11. }
12. }
13. printf("words: %d\n",word);
14.}
Note: Choose All the Logical Errors from list below that may apply to program

A. The Format specifiers used is wrong


B. The algorithm does not consider other characters such as '\t'
C. Misplaces brackets
D. Multiple spaces would lead to wrong results

Q13. Below C program tries to converts a decimal number to its binary


representation using bitwise operators.
Find out the line/ lines of code which is/are logically incorrect.

1.void decimalToBinary(int decimalNum) {


2. if (decimalNum == 0) {
3. printf("Binary: 0\n");
4. return;
5. }
6. int binary[32]; // Assuming integers are 32 bits in size
7. int i = 0;
8. while (decimalNum > 0) {
9. binary[i] = decimalNum | 1;
10. decimalNum >>= 1;
11. i++;
12. }
13. printf("Binary: ");
14. for (int j = i - 1; j >= 0; j--)
15. printf("%d", binary[j]);
16. printf("\n");
17. }

A: Line 8
B: Line 9
C: Line 10
D: No error

Q14. A person wants to Print the individual character of the given input string
separated by space .Identify the

Problem in the Given code.

1.#include<stdio.h>
2. int main(){
3. char str[100];
4. int len=0;
5. printf("Input the string :");
6. scanf("%c",str);
7. for(int i=0;str[i]!='\0';i++)
8. len++;
9. printf("String characters in reverse order:");
10. for(str[len]='\0'; len<=0; len++)
11. printf("%c",str[len]);
12. return 0;
13.}

A. Line 7, Line 10
B. Line 6 ,Line 7
C. Line 6, Line 10
D. Line 6, Line 7, Line 10

Q15. Below is the code to check whether the input strings are equal or not . Find
the Problem in the Given Code.

1. int main(){
2. char s1[100],s2[100],*str1,*str2;
3. int flag=0,i=0;
4. printf("Input two Strings :");
5. scanf("%[^\n]*%c %[^\n]%c*c",s1,s2);// Enter the String which may have
space
6. str1 = s1; str2=s2;
7. while(*str1==*str2);{
8. if(*str1=='\0' || *str2=='\0')
9. break;
10. str1++,str2++;}
11. if(*str1=='\0' || *str2=='\0')
12. printf("\n Both Strings are equal");
13. else
14. printf("\nBoth Strings are Not Equal");
15. return 0;

A. Line 7 ,Line 10
B. Line 11, Line 7
C. Line 11, Line 10
D. No Error

Q16. Below C program tries to converts a binary number to its decimal


equvalent using bitwise operators. Find out

the line/ lines of code which is/are logically incorrect.

1.int binaryToDecimal(int binaryNum) {


2. int decimalNum = 0;
3. int base = 1;
4. while (binaryNum > 0) {
5. int lastDigit = binaryNum & 1;
6. decimalNum += lastDigit * base;
7. base *= 2;
8. binaryNum >>= 1;
9. }
10. return decimalNum;
11. }

A: Line 5
B: Line 6
C: Line 7
D: Line 8

Q17. The following c functions arranges n number of integer data elements


from the smallest to largest in an array.

Identify the erroneous line /Lines oin the code.

1.void arrange(int a[],int n){


2. int i,j,min,t;
3. for(i=0;i<=n;i++){
4. min=a[i];
5. for(j=i+1;j<n;j++)
6. if(a[j]>a[min])
7. min=j;
8. t=a[min];
9. a[min]=a[i];
10. a[i]=t;
11. }
12.}

A. Line 3, Line 4
B. Line 3, Line 6
C. Line 4, Line 6
D. Line 3, Line 5, Line 6

Q18. The given function wants to print the sum of all the elements of the array
but there are some logical errors in the

given code. Find the Line(s) in which error is Present

1. int sumOfValues(int len, int *a)


2.{
3. int sum=0;
4. for(int i=0;i<len-1;i++)
5. sum+=i;
6. return sum;
7.}

A. Only in Line number 4


B. In Line 4 and 5
C. Only in Line 5
D. In Line 3 and 5

Q19. A peak element is an element that is strictly greater than its neighbors.
on a given 0-indexed integer array, the following code tries to find a peak
element, and return its index.
The peakElement() takes array and number of elements in the array as input
parameter and returns the index
of peak element.
Find out the erroneous lines of code.

1.int peakElement(int arr[], int n)


2. {
3. int f=0,l=n-1;
4. while(f<=l){
5. int m=f+(l-f)/2;
6. if((m==0 || arr[m-1]<=arr[m]) && (m==n-1 || arr[m+1]<=arr[m]))
7. return m;
8. else if(arr[m-1]>=arr[m])
9. l=m-1;
10. else f=m+1;
11. }
12. }

A: Line 4
B: Line 6
C: Line 8
D: no error

Q20. Anita is given a task to find the number of digits in a given array . For
example, the number of digits in 235 is

three. She writes a code in order to get desired result.point out the logical error
in the code and also suggest what

should be in place of Missing Line to get desired Output.

1. Int main(void)
2. int arr[5]={120,76,85,45,56},i,a=0;
3. for(i=0;i<5;i++){
4. a=0;
5. while(arr[i]/10>=0)
6. a++;
7. MISSING CODE
8. printf("%d",++a);
9. return 0;}

A. 5, arr[i]=arr[i]/10;
B. 8, arr[i]=arr[i]/10;
C. 5, arr[i]=arr[i]%10;
D. 8, arr[i]=arr[i]%5;

You might also like