Cognizant GenC Code Debugging Questions and Answers 2023

Cognizant GenC Code Debugging Questions and Solutions PDf 2023

Cognizant GenC has introduced some changes in its hiring process for 2023 pass-outs. They have introduced some new rounds, and 2 different hiring positions. Code Debugging was one of the most important round in Cognizant GenC Selection Process.Go through Cognizant Code Debugging Questions.

Here we have given some Sample Practice Questions for Cognizant GenC Code Debugging Section, prepare well for this section.
Although this section is not asked any more in the exam.

Cognizant Code Debugging Questions and Answers

Important Information about Code Debugging in Cognizant GenC

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 Series07
Time Limits20 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 GenC, prepare well for this round

(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

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

Sample Cognizant GenC Code Debugging Questions (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");
}

Use Coupon Code “PREPINSTA” 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

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,last,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);  // This line is correct.
    }
    else{
       return Search(a,start,mid-1,item); // This line is correct.
    }
  }
  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;
}

Corrected Code

#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 ;  // This line is correct.
          base = base * 2;  // This line is correct.
      }
      printf("%d \n", binary_val);
      printf("%d \n", decimal_val);
   return 0;
}

Question 6

Problem Statement – :

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

The following code snippet to Print the Fibonacci series up to the nth term.

  • For example– If the user enters the number 15, then the output will be 15 Fibonacci numbers.

The code snippets accept a single argument – n 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 passes all the test cases

#include<stdio.h>
int main()
{
    int n;
    printf("Enter the value of n: ");
    scanf("%d", &n);

    int a = 0, b = 1;
    
    // printing the 0th and 1st term
    printf("%d, %d", a, b);
    
    int nextTerm;
    
    // printing the rest of the terms here
    for(int i = 2; i < n; i++){
        nextTerm = a + b;
        a = nextTerm;
        b = a;
        
        printf(", %d", nextTerm);
    }

    return 0;
}

Corrected Code

#include<stdio.h>
int main()
{
    int n;
    printf("Enter the value of n: ");
    scanf("%d", &n);

    int a = 0, b = 1;
    
    // printing the 0th and 1st term
    printf("%d, %d", a, b);
    
    int nextTerm;
    
    // printing the rest of the terms here
    for(int i = 2; i < n; i++){
        nextTerm = a + b;
        a = b;         // This line is correct.
        b = nextTerm;  // This line is correct.
        
        printf(", %d", nextTerm);
    }

    return 0;
}

Use Coupon Code “CT10” and get flat 10% OFF on your Prime Subscription plus:
1 months extra subscription on 3 & 6 months plans
2 months extra subscription on 12 & 24 months plans
3 months extra subscription on 36 & 48 months plan

Question 7

Problem Statement – :

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

The following code snippet to Check whether a given year is leap year or not.

For example – 

  • If the user enters the year 2020 , then the output will be ” 2020 is a Leap Year “.
  • If the user enters the year 2023 , then the output will be ” 2023 is not a Leap Year “.

The code snippets accept a single argument – year 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 passes all the test cases

#include<stdio.h>
int main ()
{
int year;
scanf("%d",&year);

if(year % 200 == 0)
printf("%d is a Leap Year",year);

else if(year % 4 == 0 && year % 200 != 0)
printf("%d is a Leap Year",year);

else
printf("%d is not a Leap Year",year);

return 0;
}
Corrected Code
#include<stdio.h>
int main ()
{
int year;
scanf("%d",&year);

if(year % 400 == 0)                       //  This line is Correct.
printf("%d is a Leap Year",year);

else if(year % 4 == 0 && year % 100 != 0) // This line is correct.
printf("%d is a Leap Year",year);

else
printf("%d is not a Leap Year",year);

return 0;
}

Question 8

Problem Statement – :

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

The following code snippet to Check whether a character is a vowel or consonant.

For example – 

  • If the user enters the letter E , then the output will be ” E is a vowel “.
  • If the user enters the letter F , then the output will be ” F is a consonant “.

The code snippets accept a single argument – c(character) representing the Alphabet 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 passes all the test cases

#include<stdio.h>
int main()
{
char c;
int isLowerVowel, isUpperVowel;
printf("Enter an alphabet: ");
scanf("%c",&c);

isLowerVowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');

isUpperVowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');

if (isLowerVowel && isUpperVowel)
printf("%c is a vowel", c);

else if((c >= 'a' || c== 'A')&& c <= 'Z')
printf("\n not a alphabet\n");

else
printf("%c is a consonant", c);

return 0;
}
Corrected Code
#include<stdio.h>
int main()
{
char c;
int isLowerVowel, isUpperVowel;
printf("Enter an alphabet: ");
scanf("%c",&c);

isLowerVowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');

isUpperVowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');

if (isLowerVowel || isUpperVowel)            This line is correct.
printf("%c is a vowel", c);

else if((c >= 'a' && c== 'A')&& c <= 'Z')    This line is correct.              
printf("\n not a alphabet\n");

else
printf("%c is a consonant", c);

return 0;
}

Question 9

Problem Statement – :

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

The following code snippet To Reverse the element of the array.

For example – 

  • If the user enters the
    No. of elements – 5 , then
    Enter 5 elements:
    1 2 3 4 5
  • Output:
    Array in Reverse:
    5 4 3 2 1

The code snippets accept  2 arguments :

  • Number of elements
  • All 5 elements of array to be reversed

The code snippet fails to give 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

#include<stdio.h>

void printReverse(int arr[], int len){
    for(int i = len + 1; i <= 0; i++)
        printf("%d ", arr[i]);
}

int main()
{
    int len;
    printf("Enter the length of the array: ");
    scanf("%d", &len);

    int arr[len];
    
    printf("Enter %d elements:\n", len);
    for (int i = 0; i < len; i++) {
        scanf("%d", &arr[i]);
    }
    
    printf("Array in Reverse:\n");
    printReverse(arr, len);

    return 0;
}
Corrected Code
#include<stdio.h>

void printReverse(int arr[], int len){
    for(int i = len - 1; i >= 0; i--)     // This line is correct.
        printf("%d ", arr[i]);
}

int main()
{
    int len;
    printf("Enter the length of the array: ");
    scanf("%d", &len);

    int arr[len];
    
    printf("Enter %d elements:\n", len);
    for (int i = 0; i < len; i++) {
        scanf("%d", &arr[i]);
    }
    
    printf("Array in Reverse:\n");
    printReverse(arr, len);

    return 0;
}

Question 10

Problem Statement – :

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

The following code snippet To Find out the Sum of Digits of a Number.

For example – 

  • Enter a number:
    223153
  • Output:
    Sum: 16

The code snippets accepts 1 argument num : Number

The code snippet fails to give 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

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

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

while(num!=0){
sum =+ num % 10;
num = num % 10;
}

//output
printf("Sum: %d",sum);

return 0;

}
Corrected Code
#include<stdio.h>

int main ()
{
int num, sum = 0;

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

while(num!=0){
sum += num % 10;  This line of code is correct.
num = num / 10;   This line of code is correct.  
}
printf("Sum: %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