- 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 b. False
c. Syntax error d. Run time 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.
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 b. Run time Error
c. 11 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 b. 10
c. 11 d. Compilation Error
Answer : b
Explanation: Expression i ==10 return 1 and j get initialized to 1.
4. How many times the while loop will get executed?
main ( )
{
int a = 1 ;
while ( a <= 100) ;
{
printf ( “%d“, a++ ) ;
}
}
a. 100 b. 1
c. 0 d. Infinite
Answer : d
Explanation: Loop will execute infinite no of times because of the ; at the end while loop.
5. How many times main() will get called?
main( )
{
printf ( “\nMain called again” ) ;
main( ) ;
}
a. 1 b. 100
c. main cannot be called recursively d. Infinite
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 b. 2 3 4 5
c. 100 101 102 103 d. 101 102 103 104
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.
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 b. 20 30 40 50
c. 21 31 41 51 d. 10 20 30 40
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.
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 b. H
c. G d. a
Answer: b
Explanation: ++*ptr++ will retrieve the value currently pointed by ptr i.e G and then increment the value and will print H.