- Given the following program fragment
main ()
{
int i, j, k;
i = 3;
j =2*(i++);
k =2*(++i);
}
which one of the given option is correct?
a) i = 4, j = 6. b) j = 6, k = 10.
c) i = 5, k = 6. d) j = 6, k = 8.
Answer: b
Explanation : In the expression j = 2 * (i++) the value of i is used before incrementing and in expression k =2*(++i); will get incremented first and then used in the expression
2. How many times the below loop will run
main()
{
int i;
i = 0;
do
{
--i;
printf (“%d”,i);
i++;
} while (i >= 0)
}
a) 1
b) Infinite
c) 0
d) Compilation Error
Answer: b
Explanation: In every iteration value of i is decremented and then incremented so remains 0 and hence a Infinite Loop.
3. switch (option) {
case ‘H’ : printf(“Hello”);
case ‘W’ : printf(“Welcome”);
case ‘B’ : printf (“Bye”);
break;
}
What would be the output if option = ‘H’?
a) Hello
b) Hello Welcome
c) Hello Welcome Bye
d) None of the Above
Answer: c
Explanation : If option = ‘H’ then the first case is true so “Hello” gets printed but there is no break statement after this case to come out of the switch statement so the program execute all other case statements also and Hello Welcome Bye get printed.
4. Suppose a,b,c are integer variables with values 5,6,7 respectively. What is the value of the expression:
!((b + c) > (a + 10))
a) 1 b) 6
c) 15 d) 0
Answer : a
Explanation:
1. !((b + c) > (a + 10))
2. !((6 + 7) > (5+10))
3. !(13 > 15) 13 is less than 15 so it will return False (0 ).
4. !(0). Not of 0 is 1.
5. Consider the following program,
main ()
{
int i, j;
for (i=0, j=5; j >0, i < 10; i ++, j--)
printf(“\nGyantonic.com”);
}
How many times “Gyantonic.com” will get printed
a) 5 b) Compilation Error
c) 10 d) None of the above
Answer: c
Explanation : Condition part of for loop ( j>0, i<10 ) is separated by commas which means that compiler will use the value which is at right hand side of comma i.e of i<10 so the loop will execute till the value of i is less than 10.
6. What will be the output of the following program?
main()
{
printf(3+”Gyantonic”+4);
}
a. Compilation Error b. ntonic
c. tonic d ic
Answer : d
Explanation: Gyantonic is a constant string and statement
printf(3+”Gyantonic”+4); will skip seven(3 + 4) characters of the string before printing.
7. What will be the output of the following program?
main()
{
printf(“%c”,”Gyantonic”[4]);
}
a. Compilation Error b. G
c. t d n
Answer: c
Explanation: Gyantonic is a constant string and character at index 4 will get printed.
8. What will be the output of the following program?
main()
{
printf(“Gyantonic” ”\t” ”.com”);
}
a. Gyantonic .com b.Gyantonic.com
c. Gyantonic\t.com d. None of these
Answer: a
Explanation: printf() print Gyantonic then a tab and then print .com. It is same as writing it as printf(“Gyantonic\t.com”);