Frequently asked technical objective types multiple choice pointers questions and explanation for interview in c programming language
Note: Modern Linux GCC compilers and Visual C++ compilers do not support far and huge pointers.
1.
What will be output of following program?
#include <stdio.h>
int main(){
int a = 320;
char *ptr;
ptr =( char *)&a;
printf( "%d " ,*ptr);
return 0;
}
Explanation:
Turbo C++ 3.0: 64
Turbo C ++4.5: 64
Linux GCC: 64
Visual C++: 64
As we know int is two byte data byte while char is one byte data byte. char pointer can keep the address one byte at time.
Binary value of 320 is 00000001 01000000 (In 16 bit)
Memory representation of int a = 320 is:
So ptr is pointing only first 8 bit which color is green and Decimal value is 64.
2.
What will be output of following program?
#include <stdio.h>
#include <conio.h>
int main(){
void (*p)();
int (*q)();
int (*r)();
p = clrscr;
q = getch;
r = puts;
(*p)();
(*r)( "cquestionbank.blogspot.com" );
(*q)();
return 0;
}
Explanation:
Turbo C++ 3.0: cquestionbank.blogspot.com
Turbo C ++4.5: cquestionbank.blogspot.com
Linux GCC: Compilation error
Visual C++: Compilation error
p is pointer to function whose parameter is void and return type is also void. r and q is pointer to function whose parameter is void and return type is int . So they can hold the address of such function.
3.
What will be output of following program?
#include <stdio.h>
int main(){
int i = 3;
int *j;
int **k;
j=&i;
k=&j;
printf("%u %u %d ",k,*k,**k);
return 0;
}
Explanation:
Turbo C++ 3.0: Address, Address, 3
Turbo C ++4.5: Address, Address, Address
Linux GCC: Address, Address, 3
Visual C++: Address, Address, 3
Memory representation
Here 6024, 8085, 9091 is any arbitrary address, it may be different.
Value of k is content of k in memory which is 8085
Value of *k means content of memory location which address k keeps.
k keeps address 8085 .
Content of at memory location 8085 is 6024
In the same way **k will equal to 3.
Short cut way to calculate:
Rule: * and & always cancel to each other
i.e. *&a = a
So *k = *(&j) since k = &j
*&j = j = 6024
And
**k = **(&j) = *(*&j) = *j = *(&i) = *&i = i = 3
4.
What will be output of following program?
#include <stdio.h>
int main(){
char far *p =( char far *)0x55550005;
char far *q =( char far *)0x53332225;
*p = 80;
(*p)++;
printf( "%d" ,*q);
return 0;
}
Explanation:
Turbo C++ 3.0: 81
Turbo C ++4.5: Compilation error
Linux GCC: Compilation error
Visual C++: Compilation error
Far address of p and q are representing same physical address.
Physical address of 0x55550005 = (0x5555) * (0x10) + (0x0005) = 0x55555
Physical address of 0x53332225 = (0x5333 * 0x10) + (0x2225) = 0x55555
*p = 80, means content at memory location 0x55555 is assigning value 25
(*p)++ means increase the content by one at memory location 0x5555 so now content at memory location 0x55555 is 81
*q also means content at memory location 0x55555 which is 26
5.
What will be output of following program?
#include <stdio.h>
#include <string.h>
int main(){
char *ptr1 = NULL;
char *ptr2 = 0;
strcpy(ptr1, " c" );
strcpy(ptr2, "questions" );
printf( "\n%s %s" ,ptr1,ptr2);
return 0;
}
Explanation:
Turbo C++ 3.0: (null) (null)
Turbo C ++4.5: Run time error
Linux GCC: Run time error
Visual C++: Run time error
We cannot assign any string constant in null pointer by strcpy function.
6.
What will be output of following program?
#include <stdio.h>
int main(){
int huge *a =( int huge *)0x59990005;
int huge *b =( int huge *)0x59980015;
if (a == b)
printf( "power of pointer" );
else
printf( "power of c" );
return 0;
}
Explanation:
Turbo C++ 3.0: power of pointer
Turbo C ++4.5: power of c
Linux GCC: Compilation error
Visual C++: Compilation error
Here we are performing relational operation between two huge addresses. So first of all both a and b will normalize as:
a= (0x5999)* (0x10) + (0x0005) =0x9990+0x0005=0x9995
b= (0x5998)* (0x10) + (0x0015) =0x9980+0x0015=0x9995
Here both huge addresses are representing same physical address. So a==b is true.
7.
What will be output of following program?
#include <stdio.h>
#include <string.h>
int main(){
register a = 25;
int far *p;
p=&a;
printf( "%d " ,*p);
return 0;
}
Explanation:
Turbo C++ 3.0: Compilation error
Turbo C ++4.5: Compilation error
Linux GCC: Compilation error
Visual C++: Compilation error
Register data type stores in CPU. So it has not any memory address. Hence we cannot write &a.
8.
What will be output of following program?
#include <stdio.h>
#include <string.h>
int main(){
char far *p,*q;
printf( "%d %d" , sizeof (p), sizeof (q));
return 0;
}
Explanation:
Turbo C++ 3.0: 4 4
Turbo C ++4.5: 4 4
Linux GCC: Compilation error
Visual C++: Compilation error
p is far pointer which size is 4 byte.
By default q is near pointer which size is 2 byte.
9.
What will be output of following program?
#include <stdio.h>
int main(){
int a = 10;
void *p = &a;
int *ptr = p;
printf( "%u" ,*ptr);
return 0;
}
Explanation:
Turbo C++ 3.0: 10
Turbo C ++4.5: 10
Linux GCC: 10
Visual C++: 10
A void pointer, also known as a generic pointer, has the ability to store the memory address of any data type without requiring explicit type casting. Similarly, any specific pointer can accommodate a void pointer without the need for type casting.
10.
What will be output of following program?
#include <stdio.h>
#include <string.h>
int main(){
int register a;
scanf( "%d" ,&a);
printf( "%d" ,a);
return 0;
}
//if a=25
Explanation:
Turbo C++ 3.0: Compilation error
Turbo C ++4.5: Compilation error
Linux GCC: Compilation error
Visual C++: Compilation error
Register variables are stored directly in the CPU registers, and they don't have a memory address that you can directly access using the "&" operator. Therefore, attempting to write "&a" for a variable "a" declared as a register variable is invalid, as there's no memory address associated with it.
11.
What will be output of following program?
#include <stdio.h>
int main(){
char arr[10];
arr = "world" ;
printf( "%s" ,arr);
return 0;
}
Explanation:
Turbo C++ 3.0: Compilation error
Turbo C ++4.5: Compilation error
Linux GCC: Compilation error
Visual C++: Compilation error
Compilation error Lvalue required
Array name is constant pointer and we cannot assign any value in constant data type after declaration.
12.
What will be output of following program?
#include <stdio.h>
#include <string.h>
int main(){
int a,b,c,d;
char *p = ( char *)0;
int *q = ( int *q)0;
float *r = ( float *)0;
double *s = 0;
a = ( int )(p+1);
b = ( int )(q+1);
c = ( int )(r+1);
d = ( int )(s+1);
printf( "%d %d %d %d" ,a,b,c,d);
return 0;
}
Explanation:
Turbo C++ 3.0: 1 2 4 8
Turbo C ++4.5: Compilation error
Linux GCC: Compilation error
Visual C++: Compilation error
Address + 1 = next address
Since initial address of all data type is zero. So its
next address will be size of data type.
13.
What will be output of following program?
#include <stdio.h>
#include <string.h>
int main(){
int a = 5,b = 10,c;
int *p = &a,*q = &b;
c = p - q;
printf( "%d" , c);
return 0;
}
Explanation:
Turbo C++ 3.0: 1
Turbo C ++4.5: 1
Linux GCC: 1
Visual C++: 2
Difference of two same type of pointer is always one.
14.
What will be output of following program?
#include <stdio.h>
unsigned long int (* avg())[3]{
static unsigned long int arr[3] = {1,2,3};
return &arr;
}
int main(){
unsigned long int (*ptr)[3];
ptr = avg();
printf( "%d" , *(*ptr+2));
return 0;
}
Explanation:
Turbo C++ 3.0: 3
Turbo C ++4.5: 3
Linux GCC: 3
Visual C++: 3
15.
What will be output of following program?
#include <stdio.h>
int main(){
int * p , b;
b = sizeof (p);
printf("%d" , b);
return 0;
}
Explanation:
Turbo C++ 3.0: 2 or 4
Turbo C ++4.5: 2 or 4
Linux GCC: 4
Visual C++: 4
In the absence of information specifying the type of pointer 'p,' the resulting output is contingent upon the selected memory model. The default memory model is small, and variations in output may arise based on this implicit choice.
16.
What will be output of following program?
#include <stdio.h>
int main(){
int i = 5 , j;
int *p , *q;
p = &i;
q = &j;
j = 5;
printf( "%d %d" ,*p,*q);
return 0;
}
Explanation:
Turbo C++ 3.0: 5 5
Turbo C ++4.5: 5 5
Linux GCC: 5 5
Visual C++: 5 5
17.
What will be output of following program?
#include <stdio.h>
int main(){
int i = 5;
int *p;
p = &i;
printf( " %u %u" , *&p , &*p);
return 0;
}
Explanation:
Turbo C++ 3.0: Address Address
Turbo C ++4.5: Address Address
Linux GCC: Address Address
Visual C++: Address Address
Since * and & always cancel to each other.
i.e. *&a = a
so *&p = p which store address of integer i
&*p = &*(&i) //since p = &i
= &(*&i)
= &i
So second output is also address of i
18.
What will be output of following program?
#include <stdio.h>
int main(){
int i = 100;
printf( "value of i : %d addresss of i : %u" ,i,&i);
i++;
printf( "\nvalue of i : %d addresss of i : %u" ,i,&i);
return 0;
}
Explanation:
Turbo C++ 3.0:
value of i : 100 addresss of i : Address
value of i : 101 addresss of i : Address
Turbo C ++4.5:
value of i : 100 addresss of i : Address
value of i : 101 addresss of i : Address
Linux GCC:
value of i : 100 addresss of i : Address
value of i : 101 addresss of i : Address
Visual C++:
value of i : 100 addresss of i : Address
value of i : 101 addresss of i : Address
Within the scope of a variable, the value of the variable is subject to change through modifications, assignments, or other operations. However, the address of the variable, representing its location in memory, remains constant throughout its scope and does not alter with changes to the variable's value.
19.
What will be output of following program?
#include <stdio.h>
int main(){
char far *p =( char far *)0x55550005;
char far *q =( char far *)0x53332225;
*p = 25;
(*p)++;
printf( "%d" ,*q);
return 0;
}
Explanation:
Turbo C++ 3.0: 26
Turbo C ++4.5: Compilation error
Linux GCC: Compilation error
Visual C++: Compilation error
Far address of p and q are representing same physical address. Physical address of
0x55550005 = 0x5555 * ox10 + ox0005 = 0x55555
Physical address of
0x53332225 = 0x5333 * 0x10 + ox2225 = 0x55555
*p = 25, means content at memory location 0x55555 is assigning value 25
(*p)++ means to increase the content by one at memory the location 0x5555 so now content of memory location at 0x55555 is 26
*q also means content at memory location 0x55555 which is 26
20.
What will be output of following program?
#include <stdio.h>
int main(){
int i = 3;
int *j;
int **k;
j = &i;
k = &j;
printf("%u %u %u",i,j,k);
return 0;
}
Explanation:
Turbo C++ 3.0: 3 Address Address
Turbo C ++4.5: 3 Address Address
Linux GCC: 3 Address Address
Visual C++: 3 Address Address
Here 6024, 8085, 9091 is any arbitrary address, it may be different.
4 comments:
i like this questions......
wow........
Mokka
Mokka
Post a Comment