Which of the following statement is true after the execution of following program?
int a[5]={2,3},*c;
c=a;
(*c)–;
the value of a[0] will be 1
he function malloc can be used to dynamically allocate memory for an array.
True
What will be the output of the program ?
#include
int main()
{
char str[] = “peace”;
char *s = str;
printf(“%s\n”, s++ +3);
return 0;
}
Ce
What will be the value of x after execution of the following program?
void main()
{
int x,*p;
p=&x;
*p=2;
printf(“\nValue of x=%d”,x);
}
x=2
The code
char *a[]={“ABC”, “def”};
is invalid.
False
what is the output from the following program:
#include
void f(int *p, int *q)
{
p = q;
*p = 2;
}
int i = 0, j = 1;
int main()
{
f(&i, &j);
printf(“%d %d \n”, i, j);
return 0;
}
0 2
When we declare array elements without initialization, then its elements are set to zero.
False
Assume that float takes 4 bytes, predict the output of following program.
#include
int main()
{
float arr[5] = {12.5, 10.0, 13.5, 90.5, 0.5};
float *ptr1 = &arr[0];
float *ptr2 = ptr1 + 3;
printf(“%f “, *ptr2);
printf(“%d”, ptr2 – ptr1);
return 0;
}
90.500000 3
When we add a value x to a pointer p, the value of the resultant expression is p + x*sizeof(*p) where sizeof(*p) means size of data type pointed by p. That is why ptr2 is incremented to point to arr[3] in the above code. Same rule applies for subtraction. Note that only integral values can be added or subtracted from a pointer. We can also subtract or compare two pointers of same type
suppose that the address space is 32-bit and int type takes 4 bytes.
what is the output from the program :
#include
void fun(int arr[])
{
int i;
int arr_size = sizeof(arr)/sizeof(arr[0]);
for(i = 0; i < arr_size; i++)
printf("%d ", arr[i]);
}
int main()
{
int i;
int arr[4] = {10, 20 ,30, 40};
fun(arr);
return 0;
}
10
In any array named arr, its i th element can be accessed using the
*(arr+i) .
*(i+arr)
arr[i]
Pointer of any data type occupies the same number of bytes.
True
The expression sizeof(int) refers to
the number of bytes needed to store an int value.
When an array is passed to function, what gets passed?
base address of the array
The array name itself is pointer to
0th element ( the element with index 0 in C)
The pointer is a special type of variable which holds the value of address of other variable.
True
The reason for using pointers in a C program is
Pointers allow different functions to share and modify their local variables.
Pointers enable complex “linked" data structures like linked lists and binary trees.
To pass large structures so that complete copy of the structure can be avoided.
Correct!
All of them
strlen() counts the number of characters including \0 (null) .
False
What is output of following program?
int main(){
int *ptr;
int x;
ptr = &x;
*ptr = 0;
printf(" x = %d\n", x);
printf(" *ptr = %d\n", *ptr);
*ptr += 5;
printf(" x = %d\n", x);
printf(" *ptr = %d\n", *ptr);
(*ptr)++;
printf(" x = %d\n", x);
printf(" *ptr = %d\n", *ptr);
}
x = 0 *ptr = 0 x = 5 *ptr = 5 x = 6 *ptr = 6
When we assign one array to other, array elements of one are directly copied in to the other array.
False
In C, the end of a string is marked by
the null character '\0'.
Array contains elements of the same data type which are stored contiguously in memory.
Yes
If str is a character pointer variable that holds the string "Read SparkNotes", then str[3] refers to the character 'a'.
False
What will be the value of the variables a1 and a2 after execution?
void main()
{
int a1,a2,c=3,*pt;
pt=&c;
a1=3*(c+5);
a2=3*(*pt+5);
printf(“ln a1=%d, a2=%d”,a1,a2);
}
a1=24, a2=24
Which of the following are equivalent, assuming that arr is an integer array and that ptr is an integer pointer pointing to the start of the array?
arr[5] and (ptr + 4)[1]
What will be the result of the following pro gram? (check the table of precedence and associativity of operators)
int a=8,b=2,c,*p;
c=(a=a+b,b=a/b,a=a*b,b=a-b);
p=&c;
printf(“\n %d”,++*p);
46
If we declare static array without initialization, then its element are set to garbage value.
False
In C programming, a string is actually a
array of characters
In C, when an array is passed to a function, only the address of the array is passed to the function.
True
If we increment pointer, it will point to the next byte in memory.
False
What will be the output of the program ?
#include
int main()
{
int i=3, *j, k;
j = &i;
printf(“%d\n”, i**j*i+*j);
return 0;
}
30
What will be the output of the following program?
void main()
{
char txt[]=“12345\0abcdef”;
printf(“%s”,txt);
}
12345
Every variable is associated with an address in memory.
True
Consider a compiler where int takes 4 bytes, char takes 1 byte and pointer takes 4 bytes.
int main()
{
int arri[] = {1, 2 ,3};
int *ptri = arri;
char arrc[] = {1, 2 ,3};
char *ptrc = arrc;
printf(“sizeof arri[] = %d “, sizeof(arri));
printf(“sizeof ptri = %d “, sizeof(ptri));
printf(“sizeof arrc[] = %d “, sizeof(arrc));
printf(“sizeof ptrc = %d “, sizeof(ptrc));
return0;
}
sizeof arri[] = 12 sizeof ptri = 4 sizeof arrc[] = 3 sizeof ptrc = 4
In C, an array with ten elements is indexed from 1 through 10.
False
The address of a variable cannot be stored in another variable.
False
In C, a string is equivalent to a pointer to a character.
True
What will happen if in a C program you assign a value to an array element whose subscript exceeds the size of array?
The program may crash if some important data gets overwritten.
Output of following program?
# include
void fun(int *ptr)
{
*ptr = 30;
}
int main()
{
int y = 20;
fun(&y);
printf(“%d”, y);
return 0;
}
30
Which is the correct way to declare a pointer?
int *ptr
What will be the output of the program
void main()
{
char nm[]={‘A’,‘N’,‘S’,‘I’,0,‘C’,‘\0’};
int x=0;
while (nm[x]!=‘\0’)
printf (“%c”,nm[x++]);
}
ANSI