#include <stdio.h> void main() { int a[3] = {1, 2, 3}; int *p = a; printf(\"%p \
ID: 3816823 • Letter: #
Question
#include <stdio.h>
void main()
{
int a[3] = {1, 2, 3};
int *p = a;
printf("%p %p", p, a);
}
7.
#include <stdio.h>
void main()
{
char *s = "hello";
char *p = s;
printf("%p %p", p, s);
}
8.
9.
int main()
{
const int ary[4] = {1, 2, 3, 4};
int *p;
p = ary + 3;
*p = 5; //WONT COMPILE because const
printf("%d ", ary[3]);
}
10.
The elements in the array of the following code are:
int array[5]={5};
a) 5, 5, 5, 5, 5
b) 5, 0, 0, 0, 0
c) 5, (garbage), (garbage), (garbage), (garbage)
d) (garbage), (garbage), (garbage), (garbage), 5
11.
#include <stdio.h>
int main()
{
int ary[4] = {1, 2, 3, 4};
int p[4];
p = ary;
printf("%d ", p[1]);
}
12. If there is any error while opening a file, fopen will return
a) Nothing
b) EOF
c) NULL
d) Depends on compiler
13. Remember when the question is length of a string, you don’t need to count the null terminator (‘’). But is the question is how many characters are there in the string( or character array) you count null terminator (‘’)
This is not a question. It is an FYI.
14.
The value obtained in the function is given back to main by using ________ keyword?
15.
Identify the incorrect file opening mode from the following.
A - r
B - w
C - x
D - a
16.
Printf(“%d”,1+(rand()%11)); ------ ???
Next question – if rand() returns 123
17. x address 2016
c address 3012
Integer type is 4 bytes long on this system.
Int x[8]={11,8,2,3,5,6,4,1};
int *c=x;
1.printf(“%d”,x[4]); //5
2.printf(“%d”,*(x+4)); //5
3.printf(“%d”,&x); //COMPILE ERROR BECAUSE EXPECTS AN INT
4.printf(“%p”,&x); //2016
5.printf(“%p”,&c); //3012
6.printf(“%p”,c); //2016
7.printf(“%d”,*(c+3)); //3
8.C++;
9.printf(“%p”,c); //2020
10.printf(“%d”,*c); //8
11. C+=4; //c=c+4
12.printf(“%d”,*(c+0)); same as *c and it is 6
13.printf(“%d”,*(c+1)); //4
14.printf(“%p”,c); // 2036
15.printf(“%d”,x);//compile error
Explanation / Answer
For the first question, it will display the address of memory location of a[0] twice, as a is a pointer to a[0] and int *p=a assigns the address of a[0] to p. Hence as both p and a point to the same location of a[0], the output will be something like "0x7ffddd047890 0x7ffddd047890" (The hexadecimal address of the location of a[0] twice).
For the second part, Doing char* p = s assigns the pointer of s to p. When we do printf("%p %p", p, s), it displays the pointer addresses of both the pointers p and s. As they both point to the same string, a hexadecimal address is printed twice. The string isn't printed as the printf contains %p, not %s (%p is the format specifier for a pointer in hexadecimal form, %s is the format specifier for a string). Hence the output will be something like this - "0x80484d0 0x80484d0".
For Question 9 , doing the assignment of p = ary + 3 discards the const value of the array ary and makes it liable to change. It is very risky as using this, a const array can be changed. So when you write *p = 5; , it changes the value of the 4th index from 3 to 5 despite it being a const array. And in the printf statement, the new value 5 is printed. So the output is "5".
For Question 10, when we don't give an initializing value to some of the values in an array, by default they get a value 0. So when we do int arr[5] = {5}, only the first index gets value 5 and all the other 4 indexes get value 0. So the contents of the array are: {5,0,0,0,0}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.