Cognizant Code Debugging Questions and Answers 2021-20

Cognizant Code Debugging Questions and Solutions PDf 2020

Cognizant has introduced some changes in its hiring process for 2021 pass-outs. They have introduced some new rounds, and 2 different hiring positions. Code Debugging is one of the most important round in Cognizant Selection Process. Here we have given some Sample Practice Questions for Cognizant Code Debugging Section, prepare well for this section.

Cognizant Code Debugging Questions and Answers

Important Information about Code Debugging in Cognizant

This round will test your code analyzing and problem solving techniques. The questions in this section will mostly be related to number and string operations, data structures, and basic searching and sorting algorithms, you need to analyze that what part of the code is incorrect and then correct that faulty part.

Code Debugging SectionDetails
Number of Questions7
Time Limit20 mins
Difficulty LevelHigh
Questions based on
  • Operations on numbers and strings
  • Data Structures
  • Searching Algorithms
  • Sorting Algorithms

Below we have provided some sample practice questions, based on the analysis of Code Debugging Section of CTS, prepare well for this round

Sample Code Debugging Questions for Cognizant(CTS)

Question 1

Problem Statement – :

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

The following code snippet finds out the last digit of a number entered by the user.

  • For example– If the user enters a number 56, than the output will be 6

The code snippets accepts a single argument – num representing the number entered by the user.

The code snippet 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

#include <stdio.h>
int main()
{
    int num, LD;
    printf(" Enter a number"4589);
    scanf("%d", &num);
   LD = num / 10; 
    printf(" \n The Last Digit of a Given Number %d =  %d", num, LD);
   return 0;
}

Correct Code

#include <stdio.h>
int main()
{
  	int num, LD;
    printf(" Enter a number");
   	scanf("%d", &num);
  	LD = num % 10;  //this is the corrected statement
  	printf(" \n The Last Digit of a Given Number %d =  %d", num, LD);
  	return 0;
}

Question 2

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

The function palindrome(int number)  checks whether the number entered by the user is palindrome or not.A palindrome number is a number which remains the same when its digits are reversed

The function palindrome(int number) accepts a single argument, and print whether the number is palindrome or not.

The function palindrome(int number) 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

void palindrome(int number)
{
    int rev = 0,store, n1,left;
    n1=number;
    store= number;
    while (number > 0)
      {
        left= number/10;
        rev = rev + 10 * left;
        number=number%10;
       }
    if(n1==rev)
        printf("Number %d is Palindrome number",n1);
    else
        printf("it is not a Palindrome number");
}      
Corrected Code
void palindrome(int number)
{
    int rev = 0,store, n1,left;
    n1=number;
    store= number;
    while (number > 0)
      {
        left= number%10;   //these are the correct lines
        rev = rev * 10 + left; //these are the correct lines
        number=number/10;  //these are the correct lines
       }
    if(n1==rev)
        printf("Number %d is Palindrome number",n1);
    else
        printf("it is not a Palindrome number");
}

Question 3

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

The function sumOfValue(int len, int *arr, int value) adds up all the numbers of the entered array, which are completely divisible by the entered value,

  • For example – If the entered array is arr{1,2,3,4,5}, and the entered value is 2, then the output will be 2+4 = 6.

The function sumOfValue(int len, int *arr, int value) accepts three inputs, that are – len which represents the length of the array, arr[] which represents the array of numbers and value which represents the value that will be entered by the user.

The function sumOfValue(int len, int *arr, int value) 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 passes all the test cases

int sumOfValue(int len, int* arr, int value)
{
    int sum = 0;
    for(int i =0 ; i < len; i++   )
    {
        if(arr[i]%value == 0) 
            sum =+ arr[i];
    }
    return sum;
}
Corrected Code
int sumOfValue(int len, int* arr, int value)
{
    int sum = 0;
    for(int i =0 ; i < len; i++   )
    {
        if(arr[i]%value == 0) 
            sum += arr[i];   //these are the correct lines

    }
    return sum;
}

Question 4

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

The function search(int a[ ], int start, int last, int item) searches a certain value entered by the user in the entered array.

  • For example – If the entered array is arr{1,2,3,4,5}, and the entered value is 2, then the output will be, item found at location 2. And if the entered value is 6, then the output will be item not found.

The function search(int a[ ], int start, int last, int item) accepts 4 inputs, that are – int a [ ] which represents the array, start which represents the start of the array, last which represents the last element of the list, and item that represents the .

The function search(int a[ ], int start, int last, int item) 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 passes all the test cases

int Search(int a[], int start, int last, int item)
{
  int mid;
  if(last >= start)
  {
    mid = (start + last)/2;
    if(a[mid] == item){
       return mid+1;
    }
    else if(a[mid] < item){
       return Search(a,start,mid+1,item);
    }
    else{
       return Search(a,mid-1,list,item);
    }
  }
  return -1;
}
Corrected Code
int Search(int a[], int start, int last, int item)
{
  int mid;
  if(last >= start)
  {
    mid = (start + last)/2;
    if(a[mid] == item){
       return mid+1;
    }
    else if(a[mid] < item){
       return Search(a,mid+1,last,item);  //these are the correct lines
    }
    else{
       return Search(a,start,mid-1,item);  //these are the correct lines
    }
  }
  return -1;
}

Question 5

Problem Statement – :

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

The following code snippet converts a binary decimal into a decimal number.

  • For example– If the user enters a number 1001, than the output will be 9

The code snippets accepts a single argument – num representing the number entered by the user.

The code snippet 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

#include <stdio.h>
int main()
{
      int  num, binary_val, decimal_val = 0, base = 1, rem;
      printf("Insert a binary num (1s and 0s) \n");
      scanf("%d", &num); 
      binary_val = num;
      while (num > 0)
      {
          rem = num % 10;
          decimal_val = decimal_val + rem * base;
          num = num / 2 ;
          base = base * 10;
      }
      printf("%d \n", binary_val);
      printf("%d \n", decimal_val);
   return 0;
}
#include <stdio.h>
int main()
{
      int  num, binary_val, decimal_val = 0, base = 1, rem;
      printf("Insert a binary num (1s and 0s) \n");
      scanf("%d", &num); 
      binary_val = num;
      while (num > 0)
      {
          rem = num % 10;
          decimal_val = decimal_val + rem * base;
          num = num / 10 ;  //these are the correct lines
          base = base * 2;  //these are the correct lines
      }
      printf("%d \n", binary_val);
      printf("%d \n", decimal_val);
   return 0;
}