Cognizant GenC Automata Fix Questions and Answers

CTS GenC Automata Fix Questions and Answers

Cognizant GenC Autotmata Fix Questions and Answers has updated its pattern recently (March 2023) . This article provides more details about Cognizant GenC Automata Fix Questions pattern, syllabus and sample questions can found below –

Cognizant Automata Fix Questions and Answers

What is Cognizant GenC Automata Fix Section?

  • Automata fix is the new section being added by Cognizant GenC in its recruitment drive pattern.
  • In this section, there will be 7 code snippets, in which there may be some error, or you may have to code a specific part of the code.
  • You can code in any of your desired language like C, C++ or Java. There is no restriction in coding language which you want to choose.
  • There is even a possibility that you can code first question in some specific language, and then you are coding the next question in some other specific language.
  • This test is basically for judging your interpreting level of code. This section tests, that how good you are in fixing a code written by someone else, or if you can add a small function of your own, in someone’s other code.

Questions of Automata Fix may come from the below concepts. Also, the difficulty level of sections is given below.

  1. Logical questions:  This checks the various logical concepts like Conditions, looping etc.
  2. Compilation questions:  This checks the candidate basic knowledge of syntax and language-specific concepts.
  3. Code Reuse questions:  This is a bit tougher than the logical and compilation based questions. The candidate is required to complete the code using the predefined structure or functions.

Cognizant Automata Fix Test Pattern | CTS GenC Code Debugging Round

Type of QuestionsDescriptions
LogicalThere may be some logical errors in the code snippet, which the candidate must
be able to find and resolve
CompilationThere may be some compilation error in the code,
the candidate must be able to find those syntactical errors and resolve them
Code ReuseThe candidate must try to reduce the length of the code,
without changing the functionality of the code.

Some more details –

Automata Fix is an assessment solution to check for a computer programmer’s ability to correct an erroneous code and fix it. The test provides a set of programs to the candidate, where they have to either correct them or right new functionality based on the given functions and code. It tests the candidate’s skill of being able to understand code written by someone else, be able to find bugs and also use the code to implement new functionality. This assessment is available across multiple programming languages.

Use Coupon Code “CT10” and get flat 10% OFF on PrepInsta Prime, additionally get:

  • 1 months extra on 3 & 6 months plan
  • 2 months extra on 12 & 24 months plans
  • 3 months extra on 36 & 48 months plan

(Use Coupon Code “CT10” and get 10% off and additional months)

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Cognizant GenC Automata Fix Questions and Answers

Question : 1

Check for syntax error/ logical error and correct the error to get the desired output.

The function sortString  modifies the input list by sorting its elements depending upon the length of the array, i.e; if the length of the array is even, then the elements are arranged in the ascending order, and if the length of the array is odd, then the elements are arranged in the descending order 

The function sortString accepts two arguments – len representing the length of the string, and arr a list of characters, representing the input list respectively.

The function sortString compiles successfully but fails to get the desired results for some test cases due to logical errors. Your task is to fix the code, so that it passess all the test cases

 

Incorrect Code

void sortArray(int len, int *arr)
{
    int i, max, location, temp, j,k;
    if(len/2 == 0)//error in this line
    {
    for(i=0;i<len;i++)
    {
        max=arr[i];
        location = i;
        for(j=i;j<len;j++)
        if(max<arr[j])//error in this line
        {
            max=arr[j];
            location = j;
            
        }
        temp=arr[i];
        arr[i]=arr[location];
        arr[location]=temp;
    }
    }
    else
    {
        for(i=0;i<len;i++)
    {
        max=arr[i];
        location = i;
        for(j=i;j<len;j++) if(max>arr[j])//error in this line
        {
            max=arr[j];
            location = j;
            
        }
        temp=arr[i];
        arr[i]=arr[location];
        arr[location]=temp;
    }
    }
}
Correct Code
void sortArray(int len, int *arr)
{
int i, max, location, temp, j,k;
if(len%2 == 0)
{
for(i=0;i<len;i++)
{
max=arr[i];
location = i;
for(j=i;j<len;j++)
if(max>arr[j])
{
max=arr[j];
location = j;

}
temp=arr[i];
arr[i]=arr[location];
arr[location]=temp;
}
}
else
{
for(i=0;i<len;i++)
{
max=arr[i];
location = i;
for(j=i;j<len;j++)
if(max<arr[j])
{
max=arr[j];
location = j;

}
temp=arr[i];
arr[i]=arr[location];
arr[location]=temp;
}
}
}

Question : 2

Check for syntax error/ logical error and correct the error to get the desired output.

The function maxReplace print space separated integers representing the input list after replacing all the elements of the input list with the sum of all the element of the input list.

The function maxReplace accepts two arguments – size  an integer representing the size of the input list and inputList, a list of integers representing the input list respectively.

The function maxReplace compiles unsuccessfully due to compilation errors. Your task is to fix the code so that it passes all the test cases.

 

void maxReplace(int size, int *inputList)
{
    int i,sum=0;
    for(i=0;i<size;i++)
    {
         sum += inputList[i];
    }
        for(i=0;i<size;i++)
        {
            sum = inputList[i];//error in this line
        }
    for(i=0;i<size;i++)
    {
        printf("%d ",inputList[i]);
    }
}
Corrected Code
void maxReplace(int size, int *inputList)
{
int i,sum=0;
for(i=0;i<size;i++)
{
sum += inputList[i];
}
for(i=0;i<size;i++)
{
inputList[i]=sum;
}
for(i=0;i<size;i++)
{
printf("%d ",inputList[i]);
}
}

Question : 3

Check for syntax error/ logical error and correct the error to get the desired output.

The function replaceElements is modifying the input list in such a way – if the sum of all the elements of the input list is odd, then all the elements of the input list are supposed to be replaced by 1s, and in case if the sum of all the elements of the input list  is even, then the elements should be replaced by 0s.

For example, given the input list [1,2,3,4,5], the function will modify the input list like [1, 1, 1, 1, 1] 

The function replaceElements  accepts two arguments – size an integer representing the size of the given input list and arr, a list of integers representing the input list.

The function replaceElements compiles successfully but fails to get the desired result for some test cases due to incorrect implementation of the function. Your task is to fix the code so that it passes all the test cases

 

void replaceElements(int size, int *arr)
{
    int i,j;
    int sum=0;
    for (i=0;i<size;i++)
    {
        sum+=arr[i];
    }
    if(size % 2 == 0)//error in this line
    {
        i=0;
        while(i<size)
        {
            arr[i] = 0;
            i += 1;
            
        }
    }
    else
    {
        j=1;
        while(j<size)
        {
            arr[j]=1;
            j+=1;
        }
    }

Corrected Code

void replaceElements(int size, int *arr)
{
int i,j;
int sum=0;
for (i=0;i<size;i++)
{
sum+=arr[i];
}
if(sum % 2 == 0)
{
i=0;
while(i<size)
{
arr[i] = 0;
i += 1;

}
}
else
{
j=1;
while(j<size)
{
arr[j]=1;
j+=1;
}
}

Question : 4

Check for syntax error/ logical error and correct the error to get the desired output.

Given n, print 0 to n by identifying whether the n is even or odd.

Test Case : 1

n : 10

Output

0  2  4  6  8  10

Test Case : 2

n : 9

Output

1  3  5  7  9

Incorrect Code 

#include <stdio.h>

int main()
{
int n, i;
printf("n : ");
scanf("%d",*n);
if(n%2=0)
{
for(i=0;i<n:i=i+2)
{
printf("%d ",i);
}
}
else
{
for(i=1;i<n;i=i+2)
{
printf("%d ",i);
}
}
return 0;
}

Corrected Code

#include <stdio.h>

int main()
{
int n, i;
printf("n : ");
scanf("%d",&n);
if(n%2==0)
{
for(i=0;i<=n;i=i+2)
{
printf("%d ",i);
}
}
else
{
for(i=1;i<=n;i=i+2)
{
printf("%d ",i);
}
}
return 0;
}

Question : 5

Check whether the below program print the below pattern

Input 

enter number of rows : 4

Output 

1

22

333

4444

4444

333

22

1

Incorrect Code

#include <stdio.h>

int main()
{
int i,j,n;
printf("Enter the number of rows: ");
scanf("%d",&n);
for(i=1;i<=n;i=i+1)
{
for(j=1;j<=i;j=j+1)
{
printf("%d",i);
}
printf("\n");
}
for(i=n;i>=1;i=i-1)
{
for(j=1;j<=i;j=j-1)
{
printf("%d",i);
}
}

return 0;
}

Corrected Code

#include <stdio.h>

int main()
{
int i,j,n;
printf("Enter the number of rows: ");
scanf("%d",&n);
for(i=1;i<=n;i=i+1)
{
for(j=1;j<=i;j=j+1)
{
printf("%d",i);
}
printf("\n");
}
for(i=n;i>=1;i=i-1)
{
for(j=1;j<=i;j=j+1)
{
printf("%d",i);
}
printf("\n");
}

return 0;
}

Question : 6

Check for syntax/logical error and correct the code for desired output.

In the code you need to find the greatest among three numbers.

Incorrect Code

#include <stdio.h>

int main()
{
int a, b, c, max_num;
printf("Enter the three numbers\n");
printf("First: ");
scanf("%d",&a);
printf("Second: ");
scanf("%d",&b);
printf("Third: ");
scanf("%d",&c);
max_num = (a > b) ? (a > c ? a : c) ? (b > c ? b : c);

printf("Largest number among %d, %d and %d is %d.", a, b, c, max_num);

return 0;
}

Corrected Code

#include <stdio.h> 

int main()
{
int a, b, c, max_num;
printf("Enter the three numbers\n");
printf("First: ");
scanf("%d",&a);
printf("Second: ");
scanf("%d",&b);
printf("Third: ");
scanf("%d",&c);
max_num = (a > b) ? (a > c ? a : c) : (b > c ? b : c);


printf("Largest number among %d, %d and %d is %d.", a, b, c, max_num);

return 0;
}

Question : 7

Fix the error, recompile and match against the output provided.

Output : Welcome to “prepinsta”

Incorrect Code

#include<stdio.h>
int main(void)
{
printf("Welcome to \"prepinsta");
return 0;
}

Corrected Code

#include <stdio.h>
int main(void)
{
printf("Welcome to \"prepinsta\"");
return 0;
}

Question : 8

Print the prime numbers from an array up to given value n by using existing function.

int isprime(int num)
{
// type your code here 
}
int main()
{
int n, m, arr[100], size=0, i;
scanf(“%d”, &n);
for(m = 2; m <= n; m++)
{
if(isprime(m))
{
    arr[size++]= m;
}
}
for(i = 0; i < size; i++)
{
printf(“%d\n”, arr[i]);
}
return 0;
}

Corrected Code

int isprime(int num)

  int i;
  int isprime = 1;
  for(i = 2;  i <= num / 2; i++) 
  {      
       if(num % i == 0)      
       {            
        isprime = 0;     
          break;    
       }
   }   
   return isprime;
 }

Question : 9

Instructions: You are required to write the code. The code should be logically/syntactically correct.

 Problem: Write a program in C to display the table of a number and print the sum of all the multiples in it.

Test Cases:

 TestCase 1:

Input:

5

Expected Result Value:

5, 10, 15, 20, 25, 30, 35, 40, 45, 50 

275

TestCase 2:

Input:

12

Expected Result Value:

12, 24, 36, 48, 60, 72, 84, 96, 108, 120 

 660

#include 
int main()
{
     // write your code here
}

Corrected Code 

#include <stdio.h>
int main()
{
int n, i, value=0, sum=0;

printf("Enter number : ",n);
scanf("%d",&n);

for(i=1; i<=10; ++i)
{
value = n * i;
printf("%d \t",value);
sum=sum+value;
}

printf("\nsum : %d",sum);

return 0;
}

Question : 10

Question

You have to find the security key from the data transmitted.

Input

The input consists of an integer data, representing the data to be transmitted.

Output

Print an integer representing the security key for the given data.

Example 

Input 

578378923

Output

783

Explanation 

The repeated digits in the data are 7, 8 and 3. So, the security key is 783

#include <stdio.h> 
#include <string.h>
int main()
{
char a[50];
int i, j, len, count=0;

    scanf("%s",a);
strlen(a);

for(i=0;i<len;i++)
{
for(j=i+1;j<len;j++)
{
if(a[i]=a[j])
{
printf("%c",a[i]);
break;
}
}
}
return 0;
}

Corrected Code 

#include <stdio.h>
#include <string.h>
int main()
{
char a[50];
int i, j, len, count=0;

scanf("%s",a);
len = strlen(a);

for(i=0;i<len;i++)
{
for(j=i+1;j<len;j++)
{
if(a[i]==a[j])
{
printf("%c",a[i]);
break;
}
}
}
return 0;
}

Question : 11

Question : 

Function/method numberCount accepts three arguments-len, an integer representing the length of input list. arr, a list of integers. and value, an integer value. It returns an integer representing the sum of all the elements of arr that are equal to the given integer value for example 

len = 9, arr = [1,2,4,3,2,5,4,1,2], value = 2

function /method will return 6

function/method compiles successfully but fails to return the desired result for some/all cases due to incorrect implementation. Your task is to correct the code so that it passes all test cases.

int numberCount(int len, int* arr, int value)
{
int count = 0;
for(int i =0 ; i < len -1 ; )
{
if(arr[i]==value)
count++;
}
return sum;
}

Corrected Code

int numberCount(int len, int* arr, int value)
{
int count = 0;
for(int i =0 ; i < len ; i++ )
{
if(arr[i]==value)
count++;
}
return count;
}

Question : 12

Question

Print the sum of fibonacci series nth term.  

Series : 0, 1, 1, 2, 3, 5, 8, 13 

Check syntax/logical error for the code 

Example 

Input 

8

Output 

33

Explanation:

The sequence generated by the system will be 0, 1, 1, 2, 3, 5, 8, 13 

The sum till 8th term will be 33.

Incorrect Code

#include <stdio.h>
int main()
{
int a=0,b=1,c,n,i=2,sum=0;
printf("n : ");
scanf("%d",&n);
while(i<=n)
{
c=a+b;
a=b;
b=c;
sum=sum+c;
i++;
}
printf("%d",sum);
return 0;
}

Corrected Code

#include <stdio.h>
int main()
{
int a=0,b=1,c,n,i=2,sum=1;
printf("n : ");
scanf("%d",&n);
while(i<n)
{
c=a+b;
a=b;
b=c;
sum=sum+c;
i++;
}
printf("%d",sum);
return 0;
}

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

10 comments on “Cognizant GenC Automata Fix Questions and Answers”