OLT-1 Automata Fix
OLT-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
ANS: C
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”);
}
ANS: C
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;
}
}
}
Ans: A
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
Ans: A
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.
A: Line 1, Line 2
B: Line 3, Line 6
C: Line 9, Line 6
D: Line 1, Line 4
Ans: D
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
Ans: B
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()
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 .
A. Line1, Line4
B. Line4, Line5
C. Line5
D. Line8.
Ans. C
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.
A. Line8, Line9
B. Line4, Line5
C. Line5, line8
D. Line8
Ans A.
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.
A: Line 9 and 10
B: Line 5
C: Line 3
D: Line 12
Ans: A
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
Ans: A
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
Ans: C
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.
A: Line 8
B: Line 9
C: Line 10
D: No error
Ans: B
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
Ans: C
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
Ans: B
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.
A: Line 5
B: Line 6
C: Line 7
D: Line 8
Ans : D
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.
A. Line 3, Line 4
B. Line 3, Line 6
C. Line 4, Line 6
D. Line 3, Line 5, Line 6
Ans: C
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
Ans: B
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.
A: Line 4
B: Line 6
C: Line 8
D: no error
Ans : D
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;
Ans: A
char c = 'a';
while( c++ <= 'z')
putchar(Exp);
If the required output is abcdefghijklmnopqrstuvwxyz, then Exp should be:
A) C
B) C++
C) C-1
D) --C
Answer C
Integer a, b, c
Set a = 8, b = 51, c = 2
c = (a ^ c)^ (a)
b = b mod 4
Print a + b + c
A. 13
B. 17
C. 26
D. 16
Ans: A
Q23. Which of the following series will be printed by the given C code?
main(){
int i, j, k, n;
j=1; k=1;i=5;
while(i)
{
printf ("%d ",k);
j=j+1;
k=k+j;
i-- ;
}
}
A) 1 3 6 10 15
B) 1 2 3 4 5
C) 2 4 6 8 10
D) 1 1 2 3 5
Ans:A
main(){
int value, n;
value = 1; n = 45;
while(value <= n)
value = value << 1;
printf("%d", value);
}
A) 64
B) 32
C) 45
D) None of the above
Ans: A
int main()
{
int k=8;
switch(k)
{
case 1==8:
printf("ROSE ");
break;
default:
printf("FLOWER ");
case 1 && 2:
printf("JASMINE ");
break;
}
printf("GARDEN");
}
A) FLOWER GARGEN
B) FLOWER JASMINE GARDEN
C) FLOWER GARDEN
D) Compiler error
Answer C