Q1: Consider the following C program:
Which one of the following statements is CORRECT? (2024 SET-1)
(a) The program prints 9 as output
(b) The program prints 10 as output
(c) The program gets stuck in an infinite loop
(d) The program prints 6 as output
Ans: (c)
Sol: The initial value is a = 6, b = 0. When the first time while loop is runs the conditions
become true as (6 < 10). Now control enters inside the for loops and executes the following lines:
a = a/12 + 1 = (6/12) + 1 = 0 + 1 = 1: it returns a = 1
a = a + b ⇒ 1 + 0 = 1: it returns again a = 1
Now in the second iteration again while conditions become true as (1 < 10) and again control enters inside the loops and executes the inside statements:
a = a/12 + 1: it returns a = 1
a = a + b: it returns again a = 1
If we execute again and again every time conditions become true. So no output is printed and programs go into infinite loops.
Q2: What is printed by the following ANSI C program?
ASCII encoding for relevant characters is given below (2022)
(a) z K S
(b) 122 75 83
(c) * - +
(d) P x +
Ans: (a)
Sol: char a = 'P'
char b = 'x'
As per precedence of operators, () evaluated prior to +/-
Note that &,^ and | are bit wise operators and Addition/Subtraction of characters applied on their ASCII values.
a = 'P' ===> a = Ascii value of ('P') = 65+15 = 80 ==> a = (0101 0000)2
b = 'x' ===> b = Ascii value of ('x') = 97+23 = 120 ==> b = (0111 1000)2
a & b = (0101 0000)2 [apply & logic on corresponding bits ] = (80)10
'*' = 42 = (0010 1010)2
a & b + '*' (0111 1010)2 = 64+32 + 16 + 8 + 2 = 122 [add corresponding bits ], [ simply 80 + 42 = 122]
print character (122) = small 'z'
a | b = (0111 1000)2 [apply | logic on corresponding bits ] = (120)10
'-' = 45
a | b - '-' = 120 - 45 = 75 = 65 + 10
print character (75) = Capital 'K'
ab = (0010 1000)2 [apply logic on corresponding bits] = (40)10
'+' = 43
ab + '+' = 40 + 43 = 83 = 65 + 18
print character (83) = Capital 'S'
Q3: Consider the following C program:
The number of times variable sum will be printed When the above program is executed is _________ . (2019)
(a) 0
(b) 3
(c) 6
(d) 5
Ans: (d)
Sol: i = 2.0, j = 1.0
Total 5 times sum will be printed.
Q4: Consider the following C program: (2018)
(a) 0-9 lines of output
(b) 10-19 lines out output
(c) 20-29 lines of output
(d) More than 29 lines of output
Ans: (d)
Sol: Here while loop runs till (i/j) becomes equal to 0.001.
Now, 0.001=(1/1000)
So, the while loop runs till (i/j) = (1/1000)
=> i*1000=j
Now i =2 and j=1
So, the while loop runs till j becomes equal to 2000.
So, the output will have more or less 2000 lines.
Hence option D is correct.
Q5: Consider the following C code segment:
For the program fragment above, which of the following statements about the variables i and j must be true after execution of this program? [ !(exclamation) sign denotes factorial in the answer] (2018)
(a) (j = (x - 1)!) ∧ (i ≥ x)
(b) (j = 9!) ∧ (j = 10)
(c) ((j = 10!) ∧ (i = 10)) ∨ ((j = (x - 1)!) (i = x))
(d) (j = 9!) ∧ (i ≥ 10)) ∨ ((j = (x - 1)!) ∧ (i = x))
Ans: (d)
Sol: when value of x > = 10 then value of j will be 9! because condition of while loop is (i < 10).
so i will run till 9 and į value will be computed as 9!
and if x < 10 then loop will terminate when i will be incremented up to x. So that time į value will be computed as (x - 1)!
Q6: What does the following program do when the input is unsigned 16 bit integer? (2017)
(a) It prints all even bits from num
(b) It prints all odd bits from num
(c) It prints binary equivalent of num
(d) None of above
Ans: (c)
Sol:
Therefore: The program prints binary equivalent of num.
Above task can also be performed using the following program:
Q7: Consider the following C program.
The output of the program is ______________. (2017 SET-2)
(a) 10
(b) 11
(c) 1
(d) 0
Ans: (d)
Sol:
So,
printf("%d", n); // prints 0.
Hence, answer is 0.
Q8: The attributes of three arithmetic operators in some programming language are given below.
The value of the expression 2 - 5 + 1 - 7 x 3 in this language is_______ . (2016 SET-1)
(a) -23
(b) 9
(c) -27
(d) 23
Ans: (b)
Sol: 2 - 5 - 1 - 7 x 3 will be evaluated according to the precedence and associativity as given in the question as follows:
((2 - ((5 + 1) - 7)) x 3) ⇒ ((2 - ( - 1)) x 3) ⇒ 9
Q9: Consider the following statements
#define hypotenuse (a, b) sqrt (a*a+b*b);
The macro call hypotenuse(a + 2, b + 3); (2015)
(a) Finds the hypotenuse of a triangle with sides a+2 and b+3
(b) Finds the square root of (a + 2)2 and (b + 3)2
(c) Is invalid
(d) Find the square root of 3 x a + 4 x b + 5
Ans: (d)
Sol: #define hypotenuse (a, b) sqrt (a x a + b x b);
hypotenuse(a + 2, b + 3)
after macro expansion it will become sqrt(a + 2 x a + 2 + b + 3 x b + 3)
Now after evaluating you will get D.
Q10: The for loop (2015)
prints
(a) 0101010101
(b) 0111111111
(c) 0000000000
(d)1111111111
Ans: (a)
Sol:
Because It is Bitwise And Operation.
i & 1
0000 & 0001=0
0001 & 0001=1
0010 & 0001=0
0011 & 0001=1
Here the result is 1 if LSB of i is 1(Odd no.) & it occur at alternative position
So Ans is 0101010101
Q11: Consider the following program fragment
On termination j will have the value (2015)
(a) 4
(b) 8
(c) 9
(d) 6720
Ans: (c)
Sol:
i = 6720, j = 4
while ((6720) % 4 = 0)
i = 6720/4 = 1680
j = 4 + 1 = 5
while ((1680) % 5 = 0)
i = 1680/5 = 336
j = 5 + 1 = 6
while ((336) %6 = 0)
i = 336/6 = 56
j = 6 + 1 = 7
while ((56 %7) = 0)
i = 56/7 = 8
j = 7 + 1= 8
while((8%8 = 0)
i = 8/8 = 1
j = 8 + 1 = 9
while (1 % 9 = 0) // false
Ans - j = 9
Q12: If n has 3, then the statement a[++n]=n++; (2015)
(a) assigns 3 to a[5]
(b) assigns 4 to a[5]
(c) assigns 4 to a[4]
(d) what is assigned is compiler dependent
Ans: (d)
Sol: Correct Answer - Option 4: what is assigned is compiler dependent
This question can be solved in two different ways by different compilers:
1. First perform n++ then calculate ++n for index value
2. Calculate ++n for index value initially and then find n++. Therefore different compilers can solve this code in one of the above ways. Hence, the correct answer is "option 4".
Increment operation is of two types:
1. Pre increment (++n): Increase the value first & then substitute the value in variable n.
2. Post increment (n++): Initially substitute the value in n & then increase the value of n.
Q13: The following three 'C' language statements is equivalent to which single statement? (2014)
y=y+1;
z=x+y;
x=x+1
(a) z = x + y + 2;
(b) z = (x++) + (++y);
(c) z = (x++) + (y++);
(d) z = (x++) + (++y) + 1;
Ans: (b)
Sol: Just take x,y,z =1 and apply to all the options.
B and C will give correct answer
But as Y is incremented before X, and we are only using updated value of Y to find value of Z, so we will only choose B)
Answer is B).
Q14: How many lines of output does the following C code produce? (2014)
(a) 8
(b) 9
(c) 10
(d) 11
Ans: (d)
Sol: initial condition j = 1 and i =2
i/j > 0.001 j should be > 2000
now j increases as 2n
So 2n > 2000 i.e. 2048
therefor n = 11
for n = 12.... i/j < 0.001 so condition false and exits loop
Q15: What is the output of the following C program? (2014)
(a) The value of shifty is 15c0
(b) The value of shifty is 4300
(c) The value of shifty is 5700
(d) The value of shifty is 2700
Ans: (d)
Sol: If an integer constant starts with 0, it is considered an octal constant.
So 0570 = 101 111 000 in binary as each octal digit can be directly converted to 3 bits.
Now, >>4 (right shift 4) gives 010 111 and <<6 (left shift 6) gives 010 111 000 000 = 2700 in octal.
If we use "%d" in print of, this would be 1024 + 64 x 7 = 1472.
Q16: What is the output of the following C program? (2014)
(a) 14
(b) 36
(c) 18
(d) 20
Ans: (a)
Sol: The macro function SQR (x) (x*x) calculate the square of the given number 'x'. (Eg: 102)
Step 1: int a, b=4; Here the variable a, b are declared as an integer type and the variable b is initialized to 4.
Step 2: a = SQR (b+2); becomes,
=> a = b+2 * b+2; Here SQR (x) is replaced by macro to x*x.
=> a = 4+2 * 4+2;
=> a = 4 + 8 + 2;
=> a = 14
Q17: Suppose n and p are unsigned int variables in a C program. We wish to set p to nC3. If n is large, which one of the following statements is most likely to set p correctly? (2014 SET-2)
(a) p = n * (n - 1)* (n-2) / 6;
(b) p = n * (n - 1)/2 * (n-2) / 3;
(c) p = n * (n - 1)/3 * (n-2) / 2;
(d) p = n * (n - 1)/2 * (n-2) / 6.0;
Ans: (b)
Sol: In c, * and / have the same precedence and are left associative.
Evaluation of n * (n - 1) * (n - 2) might exceed the unsigned int range.
So, (A) and (D) are eliminated.
n * (n - 1) is always divisible by 2. (Gives an integer value). Where as it is not always divisible by 3.
(You don't always get an integer.. truncation possible, less accuracy)
(C) eliminated.
In option (B)
n* (n - 1)/2 gives an integer value.
This integer value multiplied by (n - 2) again gives an integer value.
Which when divided by 3 gives an integer value(Sets p correctly).
Reason: n * (n - 1) * (n - 2) is the multiplication of 3 consecutive numbers. which is divisible by 2 as well as 3.
Hence, (n * (n - 1)/2 * (n - 2)) is divisible by 3.
Q18: What is the output of the following program? (2013)
(a) 8 2
(b) 8 5
(c) 8 3
(d) 5 3
Ans: (a)
Sol: Before solving the questions, if we think why they asked this particular question then probability of getting correct answer increased. For example this question is an example of Short-Circuit Evaluation.
Here is the Answer:
z = 0 : x = 1 and y = 1, Because x++; will not be executed because condition is false
z = 1 : x = 2 and y = 2, Because x++; will not be executed because condition is false
z = 2 : x = 4 and y = 2, Here x++; will be executed, but + + y will not be executed due to short circuit evaluation
z = 3 : x = 6 and y = 2, Here x++; will be executed, but + + y will not be executed due to short circuit evaluation
z = 4 : x = 8 and y = 2, Here x ++; will be executed, but + + y will not be executed due to short circuit evaluation
Hence at the end, It will print 8 2.
Q19: Consider the following C code.
What would the program print? (2013)
(a) 000
(b) 010
(c) 101
(D) 111
Ans: (c)
Sol: Initially I thought there is a typo in this line
if (a = cos(pi * i/2))
Then I checked the ISRO paper and found that it is correct i.e. @makhdoom has not done any mistake.
According to that,
i = 0 : Cos(0), i.e. a = 1 i.e. condition will be True, hence it will print 1.
i = 1 : Cos(Pi/2) i.e. a = 0 i.e. Condition will be False, Hence It will print 0.
i = 2 : Cos(Pi) i.e. a = -1, i.e. Condition will be True, Hence It will print 1.
Hence answer is C. 101
Q20: Find the output of the following Java code line (2011)
System.out.printIn(math.floor(-7.4))
(a) -7
(b) -8
(c) -7.4
(d) -7
Ans: (b)
Sol: floor is lower limit
Q21: In Java, after executing the following code what are the values of x, y and z? (2011)
int x, y = 10; z=12;
x = y++ + z++;
(a) x=22, y=10, z=12
(b) x=24, y=10, z=12
(c) x=24, y=11, z=13
(d) x=22, y=11, z=13
Ans: (d)
Sol: x = y++ + z++;
As in post increment operator, first the value is assigned and then it is incremented, this statement can be re-written as:
x = y + z;
y = y++;
z = z++;
So, the value of x = 10 + 12 = 22, y = 10 + 1 = 11 and z = 12 + 1 = 13.
Option (D) is correct.
Q22: Which combination of the integer variables x, y and z makes the variable a get the value 4 in the following expression? (2008)
a = (x > y) ? ((x > z) ? x : z) : ((y > z) ? y : z)
(a) x = 3, y = 4, z = 2
(b) x = 6, y = 5, z = 3
(c) x = 6, y = 3, z = 5
(d) x = 5, y = 4, z = 5
Ans: (a)
Sol: Using option (A): x = 3, y = 4, z = 2
a = (3 > 4)? No
therefore don't evaluate the first part and check second part ((y > z)? y : z)
(4 > 2)? Yes
a = value of y = 4
Answer is (A) x = 3, y = 4, z = 2
Q23: What does the following code do? (1993)
(a) exchanges a and b
(b) doubles a and stores in b
(c) doubles b and stores in a
(d) leaves a and b unchanged
(e) none of the above
Ans: (a)
Sol:
1. a = a + b
2. b = a - b = (a + b) - b = a (from 1)
3. a = a - b = (a + b) - (a) = b (from 1 and 2)
Hence the values are getting exchanged
Answer(A).