Cognizant Automata Fix Questions and Answers

CTS Automata Fix Questions and Answers

Cognizant has updated its pattern recently (August 2019) . This article provides more details about Automata Fix pattern, syllabus and sample questions, practise questions can found below –

Cognizant Automata Fix Questions and Answers

What is Cognizant Automata Fix Section?

  • Automata Fix is the new assessment that cognizant has included as part of its online test. It checks the candidate’s computer programmer’s ability to debug an erroneous code and fix it.
  • The test provides a set of programs to the candidate, , where they have to either debug them or write 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.
Type of questionDescription
LogicalThe candidate is required to fix all logical errors in the given code.
CompilationCandidate needs to correct the syntax of the given code without changing its logic.
Code ReuseCandidates need to compare the given code by reusing existing functions.

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.

Some more details – 

Automata Fix is an assessment solution to check for a computer programmer’s ability to debug an erroneous code and fix it. The test provides a set of programs to the candidate, where they have to either debug 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.

Question : 1

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 : 2

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 : 3

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 : 4

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 : 5

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 : 6

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 : 7

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 : 8

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 debug 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 : 9

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;
}
Summary
Review Date
Reviewed Item
CTS Automata Fix
Author Rating
51star1star1star1star1star

Please Login/Signup to comment

One comment on “Cognizant Automata Fix Questions and Answers”