C Programming

General Questions

C Programming Exercise Mode

General Questions

Practice and master this topic with our carefully crafted questions.

10 Questions
15 Minutes
0% Completed
QUEST ? !
Question 1
A pointer is

A
A keyword used to create variables
B
A variable that stores address of an instruction
C
A variable that stores address of other variable
D
All of the above
Correct Answer: Option C

Normal variable means it stores the values directly. But if you want to change those values without touching those variables we are using pointers.

Pointer is a one type of variable it holds the address of the variable.

int a=5; // suppose a variable address is 100
int *p;

p=&a;// here p(pointer variable)holdes the address of the a variable.

The answer can be summarized as follows...

[A] A keyword used to create variables -> Wrong. A pointer is NOT a keyword.

[B]. A variable that stores address of an instruction -> Wrong. Though a pointer may hold the address of an instruction (i.e. function pointer), it is no required to

[C]. A variable that stores address of other variable -> Correct. A pointer is a variable that stores the address of any other variable be it a value or another address.

[D]. All of the above -> Wrong as A and B are incorrect.

Question 2
The operator used to get value at address stored in a pointer variable is

A
*
B
&
C
&&
D
||
Correct Answer: Option A

For example;
int *p; // creates a pointer to integer
int a=10//variable a has value as 10 and address as 2000
p=&a//here address of a(2000) is stored in p ie p is pointing to a
int b;//a variable
b=*p;//the pointer penetrates into a to fetch the value and stores it in b;
printf("%d",b);

output:10

Because you can get value stored at any address only through *.& is used to retrieve the address value.!

Question 3
If a variable is a pointer to a structure, then which of the following operator is used to access data members of the structure through the pointer variable?

A
.
B
&
C
*
D
->
Correct Answer: Option D

"." is used to access elements of a class via an object of class

eg-

class cls
{
public int a;
//some other work in this class
}
void main()
{
cls cls1=new cls();
cls1.a=20;//using "." operator here
}

&(ampersand) = points the address of the variable declared

*(as-trick) = points the value of the variable declared.

->(spangles) = accesses the pointer variable that contains the address of another variable.

Note:

". " : used to access structure members with normal structure variable.

"->" : used to access structure members with pointer to structure.

"*" : used to dereference a pointer.

"&" : used to obtain address of variable.

Question 4
What is (void*)0?

A
Representation of NULL pointer
B
Representation of void pointer
C
Error
D
None of above
Correct Answer: Option A

It is Null Pointer because, Pointer stores the addresses of another variable but when variable type is not known then it will be (void) and to store address it is consist(*) also,so (void *) and initially it points to 0th address, Means Null.

The main difference between NULL pointer and Void pointer is, Null pointer doesn't represent any memory location.Its value is 0. But void pointer can represents to memory location of its own type.

In the example (Void*)0(zero). it means a pointer type void represents to the location of zero.so its NULL(as mentioned 0 in the example) so syntax for the NULL pointer can be used as (Void*)0

This is the definition for VOID pointer. we can assign this to any data type.

Example:

#include
int main()
{
int a;
a=(void*)0;
printf("%d",a);
return 0;
}

Question 5

Can you combine the following two statements into one?

char *p;
p = (char*) malloc(100);


A
char p = *malloc(100);
B
char *p = (char) malloc(100);
C
char *p = (char*)malloc(100);
D
char *p = (char*)malloc(100);
Correct Answer: Option C

Prototype of malloc is

ptr = (data type *)malloc(size);
- where ptr is pointer of type datatype.

So in the above example as we need to allocate memory for char it can be done with the following statement:

char *p = (char*)malloc(1000); // here p is a pointer of type char

Question 6
How many bytes are occupied by near, far and huge pointers (DOS)?

A
near=2 far=4 huge=4
B
near=4 far=8 huge=8
C
near=2 far=4 huge=8
D
near=4 far=4 huge=8
Correct Answer: Option A

near=2, far=4 and huge=4 pointers exist only under DOS. Under windows and Linux every pointers is 4 bytes long.

Question 7
In which header file is the NULL macro defined?

A
stdio.h
B
stddef.h
C
stdio.h and stddef.h
D
math.h
Correct Answer: Option C

The macro "NULL" is defined in locale.h, stddef.h, stdio.h, stdlib.h, string.h, time.h, and wchar.h.

Question 8
What would be the equivalent pointer expression for referring the array element a[i][j][k][l]

A
((((a+i)+j)+k)+l)
B
*(*(*(*(a+i)+j)+k)+l)
C
(((a+i)+j)+k+l)
D
((a+i)+j+k+l)
Correct Answer: Option B

If we want to refer one dimensional we just use single pointer ie like *p, if we need to refer two dimensional we have to use double pointer reference ie *(*p),,if we have to refer 4 at a time just use 4 pointer statement i.e. *(*(*(*(p))))and so on.....

ptr[0]------>*(ptr+0);
ptr[1]------>*(ptr+1);

Like that:

a[i]---->*(a+i);
a[i][j]---->*(*(a+i)+j);
a[i][j][k]---->*(*(*(a+i)+j)+k);

Question 9
Consider the 32 bit compiler. We need to store address of integer variable to integer pointer. What will be the size of integer pointer ?

A
6 Bytes
B
2 Bytes
C
4 Bytes
D
10 Bytes
Correct Answer: Option B

32 bit compiler will take 2 bytes to store address of integer.

Question 10

Comment on the following pointer declaration?

int *ptr, p;


A
ptr is a pointer to integer, p is not
B
ptr and p, both are pointers to integer
C
ptr is a pointer to integer, p may or may not be
D
ptr and p both are not pointers to integer
Correct Answer: Option A

ptr is a pointer to integer, p is not

1 2 Next
Page 1 of 2