C Pointers Aptitude Questions


#include<stdio.h>
int main()
{
	int c = 130;
	char *ptr;
	ptr = (char *)&c;
	printf("%d ",*ptr);
	return 0;
}

Options:

  1. -126
  2. Runtime Error
  3. Garbage Value
  4. Compile Time Error
×

Answer

The answer id option A

Explanation:

The variable c holds the value 130 of integer datatype. Further the value 10 type casted to char datatype using pointer variable. Since the value 130 is exceeding the char range ( -128 to 127), thus it loops through its range.

For instance the ranges are;
128 = -128
129 = -127
130 = -126
140 = -125

See Answer

#include<stdio.h>
int main()
{
        int i = 5;
	int *j;
	int **k;
	j = &i;
	k = &j;
	k++;
	printf("%d ",**k);
	return 0;
}

Options:

  1. Compilation Error
  2. Runtime Error
  3. Garbage Value
  4. Linker Error
×

Answer

The answer is option B

Explanation:

The pointer variable k holds the address of pointer variable j and j holds the address of integer variable i. When the address of a pointer variable k is incremented by 1, then k hold some other garbage value which is not the address of any other variable. Hence runtime error occurs.

See Answer


#include<stdio.h>
int main()
{
	int i = 5;
	int *j;
	j = &i;
	j++;
	printf("%d ",*j);
	return 0;
}

Options:

  1. Compilation Error
  2. Runtime Error
  3. Garbage Value
  4. Linker Error
×

Answer

The answer is option C

Explanation:

The pointer variable j holds the address of an integer variable i. When j is incremented, the address stored in j is also incremented. Hence, some garbage value will be displayed as there is no initialization done to the address next to the address of a variable i. Hence garbage value will be displayed.

See Answer

#include<stdio.h>
#include<string.h>
int main(){
	char *ptr = "hello";
	char a[22];
	strcpy(a, "world");
	printf("\n%s %s",ptr, a);
	return 0;
}

Options:

  1. Runtime Error
  2. hello world
  3. Garbage Value
  4. Compilation Error
×

Answer

The answer is option B

Explanation:

The pointer variable ‘ptr’ is initialized with the char string “hello” and a normal variable ‘a’ is set to copy the string “world” using the inbuilt function strcpy. Hence, it displayed hello world.

See Answer

#include<stdio.h>
#include<string.h>
int main(){
	char *ptr = "hello";
	char a[22];
	*ptr = "world";
	printf("\n%s %s",ptr, a);
	return 0;
}

Options:

  1. Runtime Error
  2. Linker Error
  3. Garbage Value
  4. Compilation Error
×

Answer

The answer is option D

Explanation:

the variable ‘ptr’ is a pointer variable of character data type. The string “world” is should be assigned to the pointer variable ‘ptr’ only at the initializing time. Hence compilation error occurred.

See Answer

#include<stdio.h>
int main()
{
	char *ptr = "helloworld";
	printf(ptr + 6);
	return 0;
}

Options:

  1. orld
  2. world
  3. helloworld
  4. Compilation Error
×

Answer

The answer is option A

Explanation:

The pointer variable *ptr stores the characters in string “helloworld”. the code line ptr + 6 tells the compiler to start reading from 6th place of a string “Helloworld”.

See Answer

#include<stdio.h>
#include<string.h>
int main()
{
	register a = 10;
	int far *ptr;
	ptr = &a;
	printf("%u",ptr);
	return 0;
}

Options:

  1. Runtime error
  2. Address of a
  3. Garbage Value
  4. Compilation Error
×

Answer

The answer is option D

Explanation:

The variable ‘a’ is declared as a register variable that assigned a value 10. Since the value 10 is stored in registry, the pointer variable ‘ptr’ can not hold the address of ‘a’. Hence compile time error occured.

See Answer

#include<stdio.h>
#include<string.h>
int main(){
	char a = 30, b = 5;
	char *p = &a, *q = &b;
	printf("%d", p - q);
	return 0;
}

Options:

  1. Runtime error
  2. 1
  3. 25
  4. Compilation Error
×

Answer

The answer is option B

Explanation:

Difference between any two variables of same data type are always one.

See Answer

#include<stdio.h>
int main()
{
	int *ptr, b;
	b = sizeof(ptr);
	printf("%d" , b);
	return 0;
}

Options:

  1. Runtime error
  2. 2
  3. 4
  4. Compilation Error
×

Answer

The answer is option C

Explanation:

The pointer variable ptr is an integer data types. Hence the size is 4 bytes.

See Answer

#include<stdio.h>
struct shop
{
	int products[7];
};
int main()
{
	struct shop sp = {2, 3, 5, 7, 11, 13};
	int *ptr;
	ptr = (int *)&sp;
	printf("%d",*(ptr + 4));
	return 0;
}

Options:

  1. 5
  2. 11
  3. 13
  4. 7
×

Answer

The answer is option B

Explanation:

The pointer variable ptr stores the address of a first value (2) of an object ‘sp’. Further, the address of the pointer variable is incremented by 4 and then its value (11) is displayed.

See Answer
Share this:

Troubleshootyourself