IIT PG Entrance Test
7 May 2023
MCQ/Fill in the Blanks
Q1) What is the output of the following program? (5 marks)
Write -1 if there is a compilation error, -2 if it goes into an infinite loop, and 0 if it does not print
anything; else, write the actual output. Do not enter any space in the textbox
#include <iostream>
using namespace std;
int i = 0;
void hanoi(int diskSize, char source, char dest, char spare) {
if (diskSize == 0) {
i++;
if (i == 6)
cout << source;
}
else {
hanoi(diskSize - 1, source, spare, dest);
i++;
if (i == 6)
cout << source;
hanoi(diskSize - 1, spare, dest, source);
}
}
int main() {
hanoi(2, 'A', 'B', 'C');
return 0;
}
Q2) What is the output of the following program? (5 marks)
Write -1 if there is a compilation error, -2 if it goes into an infinite loop, and 0 if it does not print
anything; else, write the actual output. Do not enter any space in the textbox
#include<iostream>
using namespace std;
int displayStr(char *printStr, int i) {
if (i == 1)
return 0;
else {
i--;
if (i == 4) {
for (int j = 0; j < i; j++)
cout << printStr[j];
cout << endl;
}
displayStr(printStr, i);
return 1;
}
}
int main() {
int i = 11;
char printStr[]={'P', 'G', 'E', 'n', 't', 'r', 'a', 'n', 'c',
'e'};
displayStr(printStr, i);
return 0;
}
Q3) What will be printed by the program given below? (5 marks)
Choose the correct option.
#include <iostream>
#include <string>
using namespace std;
struct footballer {
string name;
};
struct footballer player;
struct footballer copy_struct() {
player.name = "Mbappe";
return player;
}
int main() {
struct footballer sportsman = copy_struct();
sportsman.name = "Ronaldo";
cout << sportsman.name << endl; // Output 1
cout << player.name << endl; // Output 2
return 0;
}
Option 1:
Output 1 will print Ronaldo
Output 2 will print Ronaldo
Option 2:
Output 1 will print Ronaldo
Output 2 will print Mbappe
Option 3: Output 1 will print Ronaldo
Output 2 will print garbage values
Option 4: Output 1 will print Mbappe
Output 2 will print Mbappe
Q4) Go through the program given below and (5 marks)
identify what the function, ‘trick’ does.
#include <iostream>
using namespace std;
int trick(int *arr, int N) {
int A = (N * (N + 1)) / 2;
int B = 0;
for (int i = 0; i < N + 1; i++)
B += arr[i];
return (B - A);
}
int main() {
int arr[100], N;
cin >> N;
for (int i = 0; i <= N; i++) {
cin >> arr[i];
}
cout << trick(arr, N);
return 0;
}
Option 1: The function adds all the elements of the array and returns the difference between the
computed sum and the average of all the elements of the array.
Option 2: Assuming that the array, 'arr' contains the first N numbers and one duplicate entry, the
function detects the duplicate element in the array and returns it. The function does as specified
only if the array is sorted.
Option 3: The function adds all the elements of the array and returns the difference between the
computed sum and the median.
Option 4: Assuming that the array, 'arr' contains the first N numbers and one duplicate entry, the
function detects the duplicate element in the array and returns it. The function does as specified,
irrespective if the array is sorted or not.
Q5) The following two functions have been defined to check whether the number is odd or not. As per
the specification, if the number is odd, the below functions should return true(1) or false(0) otherwise.
(5 marks)
bool is_odd1(int n) {
return n % 2 == 1;
}
bool is_odd2(int n) {
return n % 2 != 0;
}
Go through the statements given below and select the most appropriate choice
H1. Function 'odd1' returns true(1) for all values of odd integers.
H2. Function 'odd2' returns true(1) for all values of odd integers.
H3. Function 'odd1' returns true(1) for only positive values of odd integers.
H4. Function 'odd2' returns true(1) for only positive values of odd integers.
H5. Both functions do not return true(1) for all values of odd integers.
Based on the above statements, which of the following options is correct?
Option 1: Both H1 and H3 are correct
Option 2: Both H2 and H3 are correct
Option 3: Both H3 and H4 are correct
Option 4: H5 only
Option 5: None of these
Q6) Read the program given below and select the correct option. (5 marks)
#include<iostream>
using namespace std;
int main() {
unsigned int i = 1;
unsigned int j;
while(i>0) {
j = i;
i++;
}
}
Go through the statements given below with respect to the execution of the above program.
H1. The variable 'j' will contain the value 0
H2. The variable 'j' will contain the largest value that can be represented in a 32-bit unsigned
integer.
H3. The variable 'i' will contain the largest value that can be represented in a 32-bit unsigned
integer.
H4. The variable 'i' will contain the value 0
H5. The program does not terminate
Based on the above statements, which of the following options is correct?
Option 1: Both H1 and H3 are correct
Option 2: Only H1 is correct
Option 3: Both H2 and H4 are correct
Option 4: Only H2 is correct
Option 5: Only H5 is correct
Option 6: None of these
Q7) Consider the following function and answer the question that follows. (5 marks)
void recursion(int n) {
if(n < 0)
cout << n;
else {
recursion(n/2);
cout << n%2;
}
}
Which one of the following will happen when the function recursion is called with any positive
integer n as an argument?
Option 1: It will print the binary representation of n and terminate
Option 2: It will print the binary representation of n in the reverse order and terminate
Option 3: It will print the binary representation of n but will not terminate
Option 4: It will not print anything and will give a segmentation fault
Q8) What is the output printed when the following program is executed? Enter 0 for a
compilation error, enter -1 for a value that may change every time the same program is run, or
else enter the output produced. (5 marks)
Note: Here min(a,b) returns the minimum of two integers, a and b.
#include<iostream>
using namespace std;
int main() {
const int N = 5;
int matrix[N][N] = {
{ 1, 2, 3, 4, 5},
{16, 17, 18, 19, 6},
{15, 24, 25, 20, 7},
{14, 23, 22, 21, 8},
{13, 12, 11, 10, 9}
};
int Unrolled[N*N];
int idx=0;
for(int s=0; s<2*N-1; s++) {
for(int i=min(s,N-1); i>=0 && s-i<N; i--){
Unrolled[idx] = matrix[i][s-i];
idx++;
}
}
cout << Unrolled[15];
}
Programming Assignments
Q1) For this problem, you will be given n numbers a1, a2, a3, .., an. Write a C++
program to print the sum of the product of all pairs a_i, a_j where i < j (8 Marks)
Input format:
A single number n (1 <= n <= 10^6)
Followed by n numbers a1, a2, …, an
Output format: A single number.
The number equals the sum of the product of all possible pairs a_i and a_j such that i<j.
a1*a2 + a1*a3 + ... + a1*an + a2*a3 + a2*a4 + ... + a(n-1)*an
Sample input:
5
12345
Sample Output:
85
Explanation:
1*2 + 1*3 + 1*4 + 1*5 + 2*3 + 2*4 + 2*5 + 3*4 + 3*5 + 4*5
Visible / Practice Test Cases
Input Output
4 19964
56 78 99 10
11 574
5 1 9 6 0 -4 3 15 -6 10 2
15 -2737131
-550 552 -981 267 -18 808 -109
513 -782 556 947 -168 -562 143
-895
Q2) Input a positive decimal number n. Find the maximum run length of a number when
represented in its binary form. A maximum run length in a binary number is defined as maximal
consecutive alternating ones and zeroes. (8 Marks)
For example, 100110101 has runs {10, 01, 10101} counting from left to right.
Input format: Input a single number n (<=10^8) in decimal format.
Output format: Output the length of the longest run in the binary representation of n.
Sample Input 1: 309
Sample Output 1: 5
Explanation: The binary representation is: 100110101. The longest consecutive alternating
sequence of 1’s and 0’s is 10101.
Sample Input 2: 554
Sample Output 2: 7
Explanation: The binary representation is: 1000101010. The longest consecutive alternating
sequence of 1’s and 0’s is 0101010.
Practice/Visible test cases
Input Output
963 2
465 4
661 6
Q3) A new phone is priced at some price, P. Every year, the phone undergoes a sudden price
drop given by the formula: priceDrop = (current_price - 12000)2 / 20000.
Write a recursive function price() to calculate the price for every year. Accept the number of
years, n, and price, P as input in the main, and pass them to the function. Calculate the price after
n years, and return the result back to the main. (8 Marks)
Sample input: 1 20000
Sample output: 16800
Sample input: 3 20000
Sample output: 14892.6
Template code, in red, should be made visible to the students.
#include <iostream>
using namespace std;
int price(int years, int current_price) {
//Write your code below this line
return price(years, current_price);
}
int main() {
int years, current_price;
cin >> years >> current_price;
cout << price(years, current_price) << endl;
return 0;
}
Practice/Visible test cases
Input Output
3 13873
14750
6 13493
15000
9 13503
22500
Q4) Consider a 2D-array with dimensions m*n. We will use the notation arr[i][j] to refer to
the element at the ith row and jth column of the array arr. For a positive input number k,
perform the following simulation k times: (12 Marks)
Simulation: For each element at i and j, replace arr[i][j] by sum of its neighbors divided
by the number of neighbors (integer division). Two elements arr[i][j] and arr[i’][j’]
are neighbours if |i - i’| + |j - j’| = 1.
Note that the corner element will have only two neighbors and the non-corner elements on the
border will have only 3 neighbors.
Input format: The first line consists of 3 integers m, n, k. The next lines contain m x n
integers in row-major order.
The output should be just one line containing m x n elements also in row-major order.
Hint: You will have to use two 2D arrays.
E.g.
3 3 2 (3x3 is m and n, and k=2)
1 2 3
4 5 6
7 8 9
After the first simulation:
3 3 4
4 5 5
6 7 7
After the second iteration:
3 4 4
4 4 5
5 6 6
So, output is: 3 4 4 4 4 5 5 6 6
Sample Input:
3 3 2
1 2 3
4 5 6
7 8 9
Sample Output
3 4 4 4 4 5 5 6 6
Practice/Visible test cases
Input Output
4 4 1 8 5 5 4 6 8 4 6 8 4 8 5 4 8 4 9
3 9 3 3
8 9 5 5
8 10 1 10
6 1 9 10
3 4 2 7 7 6 5 7 7 5 6 7 5 7 5
8 10 8 1
10 7 4 4
7 2 10 10
3 5 2 4 4 6 5 7 5 5 5 6 6 5 6 6 6 6
2 4 7 10 10
5 8 1 4 3
3 10 9 10 7
Q5) Sudoku is a logic-based combinatorial number-placement puzzle. In this problem, you are
given a 9x9 array filled with digits from 1 to 9. You have to check whether the given array is
valid sudoku or not, i.e, all rows and columns contain all the digits from 1 to 9, and each of the
nine 3×3 subgrids that compose the grid, contains all the digits from 1 to 9. (12 Marks)
The program should first count the number of rows, number of columns, and number of subgrids
that satisfy this condition i.e. each of these should contain all the digits from 1 to 9. Thereafter,
the count should be added and printed. i.e.
Number of valid rows + number of valid columns + number of valid subgrids
Input format:
A 9x9 array with each row written in a new line. Elements in each row are separated by spaces.
Output Format:
A single number indicating the no. of rows + no. columns + no. of subgrids that satisfy the
condition of containing all digits from 1 to 9.
Sample Input:
1 5 2 4 8 9 3 7 6
7 3 9 2 5 6 8 4 1
4 6 8 3 7 1 2 9 5
5 8 7 1 2 4 6 5 9
3 9 1 7 6 3 4 2 8
2 4 6 8 9 5 7 1 3
9 1 4 6 3 7 5 8 2
6 2 5 9 4 8 1 3 7
8 7 3 5 1 2 9 6 9
Sample Output: 22
Explanation:
The fourth row is invalid as the number 5 appears twice; the fifth row is invalid as the number 3
appears twice. However, independently the fourth, fifth, and sixth sub-grid are valid as they
contain numbers from 1 to 9. Finally, the 9th row, 9th column, and 9 sub-grid are not valid as the
number 4 is not present, and the number 9 is repeated twice.
So, 6 rows + 8 columns + 8 sub grids = 22
Practice test case
Input Output
7 8 9 6 3 4 2 1 5 17
2 3 5 1 8 2 4 9 7
1 4 2 5 7 9 3 8 6
8 2 1 3 4 6 7 5 9
9 5 3 7 1 9 6 2 4
4 6 7 2 9 5 1 3 8
3 7 6 9 5 1 8 4 2
2 3 4 8 6 3 5 7 1
5 7 8 4 2 7 9 6 3
4 6 5 9 2 8 3 7 1 18
7 3 9 6 1 4 2 8 5
8 2 1 3 0 5 9 6 4
9 1 3 5 8 7 6 4 2
6 8 4 2 9 1 5 3 6
5 7 2 4 3 6 8 1 9
3 5 6 1 4 2 7 9 8
2 4 8 7 6 9 1 5 3
1 9 7 1 5 3 4 2 6
7 8 3 1 4 7 9 3 6 23
9 6 1 3 2 8 5 4 7
2 4 5 9 6 5 1 2 8
1 3 6 5 8 2 7 9 4
7 9 8 4 1 6 2 5 3
2 5 4 7 9 3 8 6 1
6 2 5 8 3 1 4 7 9
4 1 3 2 7 9 6 8 5
8 7 9 6 5 4 3 1 2
Q6) For a given set of numbers and an integer S for each set, we need to identify whether there
exists a subset of numbers in the given set whose sum is equal to S. (12 Marks)
Write a program that accepts n sets and a value S for each set. Create a result array to store the
output for each set S as described below. For each set, store 1 in the result array, if there exists a
subset of numbers whose sum is equal to the corresponding value S; otherwise, store 0. Assume
that sets contain non-negative integers only and consider -1 as the termination for each set.
Finally, print the result array.
Hint: Recursion is your friend.
Input format:
<number of sets>
<S1> a1 a2 a3 …… an -1
<S2> a1 a2 a3 …… am -1
. . .
<Sn> a1 a2 a3 …… ak -1
Sample input:
3 // 3 input sets
11 1 2 3 4 5 6 -1 // set1, S=11, 11=1+2+3+5.
// So, result[0]=1
// other solutions are also possible.
25 7 8 11 1 4 2 6 21 -1 // set2, S=25, 25=4+21. So, result[1]=1
10 4 5 7 8 -1 // set3, S=10, No subset of this set
// sums to 10, Hence, result[2]=0
Sample output:
1
1
0
Input Output
4 1
20 1 2 3 4 5 6 -1 1
35 10 7 5 18 12 20 15 -1 0
9 3 5 7 11 13 17 19 23 29 31 -1 1
22 3 5 7 11 13 17 19 23 29 31 -1
5 1
17 3 5 9 17 20 25 30 35 -1 1
17 3 5 9 20 25 30 35 -1 0
17 5 9 20 25 30 35 -1 1
50 5 9 20 25 30 35 -1 0
15 5 9 20 25 30 35 -1
5 0
8998530 816327 290386 934149 129492 670427 0
655400 970376 475906 374491 784143 357319 86306 1
-1 1
11293964 886187 274910 369738 406412 806769 0
962949 514039 66152 267774 790347 693857 930161
610133 -1
1297772 506558 408069 176713 398038 665948
487659 493179 402044 -1
2129701 400160 718325 620186 371410 382601
972170 743625 989453 439206 -1
17611398 701682 154428 87888 743975 73484 812711
207061 -1