**KPIT Coding Questions and Answers 2024 | KPIT Coding Questions | KPIT Coding Questions For Freshers | KPIT Coding Questions and Answers For Freshers | KPIT Coding Questions For Experience**

**KPIT Coding Questions and Answers:** Aspirants who pass in the Online Test will qualify for the coding test process. The coding test will be conducted for the process to check the candidate’s capability. The coding questions will be asked in any language. In the below section, we have provided the Java, C, C++, and Python Coding Questions with Solutions. With the clear Explanation, we have provided all the details here. Solve the Frequently Asked coding test Questions to help with your study.

KPIT** **will conduct the Coding Test for each hiring Process. Every year, they hire many employees to fill many positions. As of now, KPIT **Coding Questions** & Answers before attending the process. Before entering the coding test, aspirants can start their preparation by downloading or viewing the practice questions. Here we have provided the study material for the coding test.

### KPIT Coding MCQ Questions With Solutions

**When her illness took a turn for the worse, the doctors took _____________ to ____________ the situation.**

- steps, ameliorate
- approaches, better
**modes, improve**- measures, enhance

**EXPLANATION: **‘Ameliorate’ – to make better, improve. Modes does not fit the first blank so option 3.

**We_____________to inform you that we cannot include your thesis in our library, on the __________________of not receiving permission from your supervisor.**

- saddened, reason
- lament, pretext
- repent, justification
**regret, grounds**

**EXPLANATION:**

‘regret’ is a feeling of disappointment.

repent is feeling remorse for one’s own wrong doings.

lament is to mourn.

Hence the 4th option is the best fit.

### To make sure the idea is understood correctly, choose the option that gives the sentence the best possible structure.

**According to the passage,**

- Helen Walne is a patient of cancer.
**Helen Walne is a megabyte megalomaniac.**- Helen Walne thinks that constant rival surfing is the best way to improve self worth.
- Helen Walne is a conscientious worker.

**EXPLANATION:** Megalomania is an obsession with doing extravagant or grand things. Helen Wayne is also obsessed with this. The other options are out of scope.

**According to the passage, all of the following are not true, except**

- Helen Walne received a prize wearing a turtle neck.
**Helen Walne’s husband feels she needs help to get out of her addiction.**- In reality there is very little difference in the number of internet users today and five years ago.
- Rival surfing gives a better job

**EXPLANATION: **Refer to 1st para line “My husband has gently…..addiction.”

**It can be inferred from the passage that**

- you have gone too far when you start googling your name multiple times daily.
- competitive rival surfing is the best way to increase your self esteem.
- being addicted to self googling is the way forward in the 21st century.
**the advent of the internet has helped in creating ‘a somebody’ out of ‘a nobody’.**

**EXPLANATION:** We can’t infer that somebody has gone far if one starts googling one’s name multiple times. Nothing can be inferred about increasing self esteem. Option C is out of scope.

**KPIT Coding Questions and Answers **For Freshers

**Questions:** While playing an RPG game, you were assigned to complete one of the hardest quests in this game. There are **n **monsters you’ll need to defeat in this quest. Each monster **i **is described with two integer numbers – **poweri **and **bonusi**. To defeat this monster, you’ll need at least **poweri **experience points. If you try fighting this monster without having enough experience points, you lose immediately. You will also gain **bonusi **experience points if you defeat this monster. You can defeat monsters in any order.

The quest turned out to be very hard – you try to defeat the monsters but keep losing repeatedly. Your friend told you that this quest is impossible to complete. Knowing that, you’re interested, what is the maximum possible number of monsters you can defeat?

**Input:**

- The first line contains an integer, n, denoting the number of monsters. The next line contains an integer, e, denoting your initial experience.
- Each line i of the n subsequent lines (where 0 ≤ i < n) contains an integer, poweri, which represents power of the corresponding monster.
- Each line i of the n subsequent lines (where 0 ≤ i < n) contains an integer, bonusi, which represents bonus for defeating the corresponding monster.

**Output**

- 2

**Example Programs:**

**JAVA PROGRAM**

import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); int exp = s.nextInt(); int monst[] = new int[n]; int bonus[] = new int[n]; for (int i = 0; i < n; i++) { monst[i] = s.nextInt(); } for (int i = 0; i < n; i++) { bonus[i] = s.nextInt(); } class Monster { private final int power, bonus; public Monster(int power, int bonus){ this.power = power; this.bonus = bonus; } } Monster[] monsters = new Monster[n]; for(int i = 0; i < n; i++) monsters[i] = new Monster(monst[i], bonus[i]); Arrays.sort(monsters, Comparator.comparingInt(m -> m.power)); int count = 0; for(Monster m: monsters){ if(exp < m.power) break; exp += m.bonus; ++count; } System.out.println(count); } }

**Questions: **One of the first lessons IT students learn is the representation of natural numbers in the binary number system (base 2) This system uses only two digits, 0 and 1. In everyday life we use for convenience the decimal system (base 10) which uses ten digits, from 0 to 9. In general, we could use any numbering system.

Computer scientists often use systems based on 8 or 16. The numbering system based on K uses K digits with a value from 0 to K-1. Suppose a natural number M is given, written in the decimal system To convert it to the corresponding writing in the system based on K, we successively divide M by K until we reach a quotient that is less than K

The representation of M in the system based on K is formed by the final quotient (as first digit) and is followed by the remainder of the previous divisions**For example :**

- If M=122 and K=8, 122 in base 10= 172 in base 8 This means that the number
- In decimal system = 172 in octal system.
- 172 in base 8 = 1*8^2 + 7*8 + 2 = 122

You made the following observation in applying the above rule of converting natural numbers to another numbering system

- In some cases in the new representation all the digits of the number are the same. For example 63 in base 10= 333 in base 4

Given a number M in its decimal representation, your task is find the minimum base B such that in the representation of M at base B all digits are the same.

**Input Format**

- The first line contains an integer, M, denoting the number given

**Constraints**

- 1 <= M = 10^12

**Sample Input 1 :**

41

**Sample Output 1 :**

40

**Explanation :**

Here 41 in base 40. will be 11 so it has all digits the same, and there is no smaller base satisfying the requirements

**Sample Input 2 :**

34430

**Sample Output 2 :**

312

**Explanation :**

Here 34430 in base 312 will have all digits the same and there is no smaller base satisfying the requirements

**Example Programs:**

**JAVA PROGRAM**

import java.util.*; class Main { public static boolean convertBase (int m, int base) { int rem = m % base; m = m / base; while (m >= base && (m % base == rem)) m = m / base; if (m == rem) return true; return false; } public static void main (String[]args) { Scanner sc = new Scanner (System.in); int m = sc.nextInt (); int base = 2; while (convertBase (m, base) != true) base++; System.out.println (base); } }

**Q) **You have been given a string S of length N. The given string is a binary string that consists of only 0’s and ‘1’s. Ugliness of a string is defined as the decimal number that this binary string represents.

** Example:**

- “101” represents 5.
- “0000” represents 0.
- “01010” represents 10.

There are two types of operations that can be performed on the given string.

- Swap any two characters by paying a cost of A coins.
- Flip any character by paying a cost of B coins
- flipping a character means converting a ‘1’to a ‘0’or converting a ‘0’ to a ‘1’.

Initially, you have been given coins equal to the value defined in CASH. Your task is to minimize the ugliness of the string by performing the above mentioned operations on it. Since the answer can be very large, return the answer modulo 10^9+7.

**Note:**

- You can perform an operation only if you have enough number of coins to perform it.
- After every operation the number of coins get deducted by the cost for that operation.

**Input Format**

- The first line contains an integer, N, denoting the number of character in the string
- The next line contains a string, S, denoting the the binary string
- The next line contains an integer, CASH, denoting the total number of coins present initially
- Next will contains an integer, A, denoting the cost to swap two characters.
- Then the next line contains an integer, B, denoting the cost to flip a character.

**Constraints**

- 1 <= N <= 10^5
- 1< len(S)<= 10^5
- 1<=CASH <=10^5
- 1<=A<=10^5
- 1<=B<=10^5

**Sample Input 1 :**

4

1111

7

1

2

** Sample Output 1 :**

1

** Explanation:**

3 flips can be used to create “0001” which represents 1.

** Sample Input 2:**

6

111011

7

1

3

** Sample Output 2:**

7

** Explanation:**

First swap 0 with the most significant 1, then use flip twice first on index one and then on index two “111011”=>”0111111″=>”001111″=>”000111″ the value represented is 7.

** Sample Input 3:**

6

111011

7

3

2

** Sample Output 3:**

3

** Explanation:**

Flip the 3 most significant characters to get “000011” : the value represented by this string is 3.N

**EXAMPLE PROGRAMS**

**JAVA PROGRAM**

import java.util.*; class Main { static String str; static int cash, n, a, b; static void swapf () { char s[] = str.toCharArray (); int i = 0; for (int a = 0; a < s.length; a++) if (s[a] == '1') { i = a; break; } int j = s.length - 1; while (j > i) { if (cash < a) break; if (s[j] == '0') { if (s[i] == '0') i++; else { char temp = s[i]; s[i] = s[j]; s[j] = temp; cash -= a; j--; } } else j--; } str = new String (s); } static void flipf () { char s[] = str.toCharArray (); int i = 0; for (int a = 0; a < s.length; a++) if (s[a] == '1') { i = a; break; } while (cash >= b) { if (i == s.length) break; if (s[i] == '1') { s[i] = '0'; i++; cash -= b; } } str = new String (s); } public static void main (String[]args) { Scanner sc = new Scanner (System.in); n = sc.nextInt (); str = sc.next (); cash = sc.nextInt (); a = sc.nextInt (); b = sc.nextInt (); if (a < b) { swapf (); flipf (); } else { flipf (); swapf (); } System.out.println (Integer.parseInt (str, 2)); } }

**Cognizant GenC Elevate Coding Questions With Solutions 2024**

**Write a program to check whether the given number is even or odd**

```
import java.util.*;
class ninjaSolution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please Enter a number!!");
int num = sc.nextInt();
if(num%2==0)
System.out.println(num+" is even");
else
System.out.println(num+" is odd");
}
}
```

**Write a program to print the Fibonacci series.**

```
import java.util.*;
class ninjaSolution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the limit!!");
int limit = sc.nextInt();
int n1 = 0, n2=1, n3;
System.out.println("Fibonacci series upto "+limit+" numnber");
System.out.print("\n\n"+n1+" "+n2+" ");
for(int i=2;i<limit;i++){
n3 = n1+n2;
n1 = n2;
n2 = n3;
System.out.print(" "+n3+" ");
}
}
}
```

**Print the length of a string without using the string functions.**

```
import java.util.*;
class ninjaSolution {
public static void main(String[] args) {
String str = "Hey Ninja!!";
int len=0;
char[] strCharArray=str.toCharArray();
for(char c:strCharArray){
len++;
}
System.out.println("Lenght of string "+str+" is: "+len);
}
}
```

**Write a code to add two numbers without using arithmetic operations.**

```
class ninjaSolution {
public static void main(String[] args) {
int num1 = 10, num2 = 50;
while (num2 != 0){
int carry = (num1 & num2) ; //CARRY is AND of two bits
num1 = num1^num2; //SUM of two bits is A XOR B
num2 = carry << 1; //shifts carry to 1 bit to calculate sum
}
System.out.println("Sum of 10 and 50 is: "+num1);
}
}
```

**Write a code to print numbers from 1 to 10 without using a loop or recursion in C++.**

```
#include <iostream>
using namespace std;
template<int n>
class PrintZeroToN
{
public:
static void display()
{
PrintZeroToN<n-1>::display();
cout << n << endl;
}
};
template<>
class PrintZeroToN<0>
{
public:
static void display()
{
cout << 0 << endl;
}
};
int main()
{
const int n = 10;
PrintZeroToN<n>::display();
return 0;
}
```

**Print the address of a variable without using a pointer.**

We can directly print the address of a variable using the ‘&’ symbol in c,

```
#include <stdio.h>
int main()
{
int x;
printf("Address of x: %p\n", &x);
return 0;
}
```

**Write a code to check whether a number is prime or not.**

```
import java.util.*;
class ninjaSolution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a number");
int num = sc.nextInt();
int count=0;
for(int i=2;i<num/2;i++){
if(num%i==0)
count++;
}
if(count>0)
System.out.println("Number is not prime");
else
System.out.println("Number is prime");
}
}
```

Follow regularly our **Dailyrecruitment.in** site to get upcoming all up-to-date information.

JOB ALERT ON INSTAGRAM | FOLLOW NOW>> |

JOB ALERT ON YOUR EMAIL DAILY | SUBSCRIBE NOW>> |

**Govt Jobs by Qualifications**

Education & Vacancies | Salary | Apply Link |
---|---|---|

12th Pass Govt Jobs - 18,000+ Vacancies | Rs. 5,200 - 92,300 | Apply Now |

ITI Pass Jobs - 3,500 Vacancies | Rs. 5,200 - 35,000 | Apply Now |

Any Graduate Jobs - 19,100 Vacancies | Rs. 5,200 - 92,300 | Apply Now |

Central Govt Jobs | Rs. 5,200 - 17,000 | Apply Now |

Bank Jobs - 1,000 Vacancies | Rs. 5,200 - 29,200 | Apply Now |

Diploma Jobs - 9,300 Vacancies | Rs. 5,200 - 35,000 | Apply Now |

BTech/BE Jobs - 18,000 Vacancies | Rs. 15,000 - 1,00,000 | Apply Now |

Data Entry Jobs - 1,300 Vacancies | Rs. 5,200 - 29,200 | Apply Now |

Private Jobs | Rs. 10,000 - 67,700 | Apply Now |