You are on page 1of 17

# 1.

## 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. c) i = 5, k = 6. 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 b) j = 6, k = 10. d) j = 6, k = 8.

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 c) 15 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. b) 6 d) 0

## 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 c) 10 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. b) Compilation Error d) None of the above

6. What will be the output of the following program? main() { printf(3+Gyantonic+4); } a. Compilation Error c. tonic Answer : d Explanation: Gyantonic is a constant string and statement b. ntonic d ic

## 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 c. t Answer: c Explanation: Gyantonic is a constant string and character at index 4 will get printed. d n b. G

8. What will be the output of the following program? main() { printf(Gyantonic \t .com); } a. Gyantonic .com b.Gyantonic.com d. None of these

c. Gyantonic\t.com Answer: a

Explanation: printf() print Gyantonic then a tab and then print .com. It is same as writing it as printf(Gyantonic\t.com);
1.

## What value of c will get printed

main() { int a, b, c; a = 10; b = 20; c = printf(%d,a) + ++b; printf (%d,c); } a. 23 c. 30 Answer: a Explanation: printf() will return no. of bytes it printed. Expression becomes c = 2 + ++b; then value of b is incremented before addition. 2. How many times the below loop will get executed? main() { b. 22 d. Compilation Error

int i; for (i=9 ; i ; i=i-2) { printf(\n%d,i); } } a. 5 c. Compilation Error Answer: d Explanation : Above loop will iterate till i have non zero value. Initial value of i is 9 and it get decremented by 2 in every iteration ( 9, 7, 5, 3, 1, -1, -3 .). Value of i will never become 0 and loop will run infinitely. b. 6 d. Infinite

3. What will be the output main() { int i; i = 10; printf(%d\t,5,6); printf(%d, i , i++); } a. 5 11 b. 6 10

c. 5

10

d. 6

11

Answer: a Explanation: Value in a function get passed from right to left. First i++ get passed and it make i = 11.

4. What the below statement will print if a=10 and b = 20? printf( %d,a==b); a. 20 c. 10 Answer: b Explanation: a==b is a expression and will return 1 (true) or 0 (False) depending on the values of a and b. Here a and b are not equal so 0 is printed. b. 0 c. 1

5. What the below statement will print if a=10? printf( %d %d,a, !a++); a. 11 c. 10 0 0 b. 10 d. 0 10 10

Answer: a Explanation: Values in the function get passed from right to left. First !a++ get processed which pass zero as argument and make a equal to 11.

## 6. What will be the output

main() { int i; i =10; if (i ==20 || 30) { printf(True); } else { printf(False); } } a. True c. Syntax error Answer : a Explanation: i==20 is a expression which will return TRUE or FALSE depending on the value of i. In this program it will return 0 so the statement become If ( 0 || 30) 30 is a nonzero value which means TRUE (1) in C when ORed with 0 will result TRUE. b. False d. Run time Error

7. What will be the output of the following program? main() { printf(%c,4[Gyantonic]); } a. Compilation Error c. t Answer: c Explanation: Gyantonic is a constant string and character at index 4 will get printed. d n b. G

## 8. What will be the output main() { if (1,0) { printf(True); } else { printf(False);

} } a. True c. Compilation error Answer : b Explanation : comma(,) operator returns the value which at the right hand side of , . if statement become if(0)
1.

## What will be the output

main() { int i , j ,*ptr, *ptr1; i =10; j = 10; ptr = &i; ptr1 = &j;

## if (ptr == ptr1) { printf(True); } else

{ printf(False); } } a. True c. Syntax error Answer : b Explanation: In this program we are comparing the addresses contained by ptr & ptr1 not the value at those addresses and pointers ptr and ptr1 have the addresses of different variables so above condition is false. b. False d. Run time Error

2. How many times the below loop will get executed? main() { int i; for (i=20, i =10 ; i<=20 ; i++) { printf(\n%d,i); } } a. 1 c. 11 b. Run time Error d. Compilation Error

## Answer : c Explanation: i will start from 10.

3. How many times the below loop will get executed? main() { int i,j; i = 10; for (j=i==10 ; j<=10 ; j++) { printf(\n%d,j); } } a. 1 c. 11 Answer : b Explanation: Expression i ==10 return 1 and j get initialized to 1. b. 10 d. Compilation Error

4. How many times the while loop will get executed? main ( ) {

int a = 1 ; while ( a <= 100) ; { printf ( %d, a++ ) ; } } a. 100 c. 0 Answer : d Explanation: Loop will execute infinite no of times because of the ; at the end while loop. b. 1 d. Infinite

5. How many times main() will get called? main( ) { printf ( \nMain called again ) ; main( ) ; } a. 1 b. 100 d. Infinite

## c. main cannot be called recursively Answer : d

Explanation: There is no condition in the main() to stop the recursive calling of the main() hence it will be called infinite no of times.

6. What will be the output of the following program if the base address of array is 100. main( ) { int gyan[] = { 1, 2, 3, 4, 5 }; int i, *ptr ; ptr = gyan ; for ( i = 0 ; i <4 ; i++ ) { printf ( \n%d, *ptr++ ) ; } } a. 1 2 3 4 c. 100 101 102 103 Answer: a Explanation: ptr contains the base address of the array and printf() is printing the value at the current address contained by ptr and then incrementing the pointer to point to the next array element address. b. 2 3 4 5 d. 101 102 103 104

7. What will be the output of the following program main( ) { int gyan[] = { 10, 20, 30, 40, 50 }; int i, *ptr ; ptr = gyan; for ( i = 0 ; i <4 ; i++ ) { fun(ptr++); printf ( \n%d, *ptr ) ; } } void fun(int *i) { *i = *i + 1; } a. 11 21 31 41 c. 21 31 41 51 Answer: b Explanation: When we call the function fun() the current address contained by it will get passed and then it get incremented. For ex. If the base address is of the array is 100 then successive elements are stored at 102, 104, 106 and 108. b. 20 30 40 50 d. 10 20 30 40

In the first call 100 get passed to fun() and ptr becomes 102. Now the called function fun() manipulates the value stored at 100 and in the main() function values at the address contained by ptr are getting printed.

8. What will be the output main() { char *ptr = Gyantonic.com; char a = printf(%c, ++*ptr++); } a. Compilation Error c. G Answer: b Explanation: ++*ptr++ will retrieve the value currently pointed by ptr i.e G and then increment the value and will print H. b. H d. a