C-PROGRAMMING PART 2 (quiz 2)

 




Comment on the addresses printed

by below statements

int main()

{

char ch[20];

printf("%p %p",ch, &ch);

}



Answer:

Addresses printed are same. ch is pointer to an element which is same as &ch[0]. &a is a pointer to an array. Adding 1 will show the difference.


-------------------------------------------------------------


What is the output?

int main()

{ char ch[20] = "CODEMUMMY";

char ch1[] = ch;

printf(“%s”,ch1);

}

Runtime Error

CompileTime Error

CODEMUMMY

Undefined Behaviour


Ans: B) CompileTime Error No Empty Array in C



-------------------------------------------------------------


What is the output?

void v1(int* q);

int main()

{ int a = 100; int *p = &a;

v1(p);


printf("%d and %d\n", a, *p); return 0;

}

void v1(int* q)

{ int temp = 999;

*q = temp; }

100 and 999

100 and 100

999 and 999

Runtime Error



Answer: C) 999 and 999




-------------------------------------------------------------

What is the output?

int v1(int* q);

int main()

{ int a = 100; int *p = &a;

int res = v1(p);

printf("%d and %d\n", *p, res);

return 0;


}

int v1(int* q)

{ int temp = 999; q = &temp;

return *q;

}

A) 100 and undefined value B) 100 and 100

100 and 999

Runtime Error


Answer: C) 100 and 999


-------------------------------------------------------------



What is the output?

int* v1(int* q);

int main()

{ int a = 100; int *p = &a;

int *res = v1(p);

printf("%d and %d\n", *p, *res);

return 0;


}

int* v1(int* q)

{ int temp = 999; q = &temp;

return q;

}

Answer: res is a dangling pointer Dereferencing dangling pointer results in undefined behaviour



-------------------------------------------------------------


What is the output?

float* f2(float* x);


int main()


{ float f = 10.5;


float *p; p = f2(&f);


printf("%4.1f\n", *p);


return 0;}


float* f2(float* x)



{ return x; }

Answer: 10.5



-------------------------------------------------------------


What is the output?

int main()

{ char ch[20] = "codemummy";

char *ch1 = ch;

printf(“%s”,ch1);

}

Runtime Error

CompileTime Error

codemummy

Undefined Behaviour


Ans: C) codemummy



-------------------------------------------------------------


char a[] = "whatsapp" ; char *b = "when" ;

int i;

for (i = 0 ; *b != '\0' ; i++,b++) { a[i] = *(b+i); } printf("%s",a);


What is the output ?

when

what

we

wwww


Answer: C) we



-------------------------------------------------------------


Say True or False


Shrinking and expanding of memory allocated for arrays is possible.



Answer: False



-------------------------------------------------------------


What is the output of below code segment? int a[]={23,45,44};

a++;

printf("%d",*a);



Answer: Compile-time Error Array is a constant pointer



-------------------------------------------------------------


What is the output of below code segment?

 int[] f1(int p[])

{ return p; }


int main()

int a[] = {23,12,67}; int b[] = f1(a);

printf(“%d”,b[2]);

}


67

12

Compiletime Error

Undefined

Behaviour



Answer: C) Compiletime Error



-------------------------------------------------------------


Find the output.

int* f1(int a[],int n);

int main()

{ int p[]={2,3,1,2,6}; int i;

int n = sizeof(p)/sizeof(*p);

int *p1 = f1(p,n);

for(i = 0;i<n;i++)

printf("%d\t",*p1++);

}


int* f1(int a[],int n)

{ int i;

for(i = 0;i<n;i++) *a = (*a) * 2;

return a;

}


Answer:

64 3 1 2 6


-------------------------------------------------------------


What is the output of below code segment? int a[]={23,45,44};

int *b=a;

b++;

printf("%d",*b);



Answer: 45


b is a pointer which can be incremented to perform pointer arithmetic



-------------------------------------------------------------


What gets printed?

int a[]={23,33,56,12};


printf("%d ",*a);


*(a+2)=90;


printf("%d",*(a+2));


23 56


Undefined behaviour


23 90


Compile time Error


Answer: C) 23 90



-------------------------------------------------------------


What is the output of below code segment? int a[5] = {0, 1, 11, 111, 1111};

int *p = a + 3 ;

printf("%d", *++p);


111

1111

Undefined Behaviour

Compile time Error


Answer: B) 1111


-------------------------------------------------------------


What is the output of below code segment? float arr[5] = {12.5, 10.0, 13.5, 90.5, 0.5}; float *ptr1 = &arr[0];

float *ptr2 = ptr1 + 3;

printf("%f %d", *ptr2, ptr2 - ptr1);


13.500000 1

90.500000 0

12.500000 0

90.500000 3


Answer: D) 90.500000 3


-------------------------------------------------------------



What is the output of below code? void f(int *p, int *q) { p = q; *p = 2; } int main() {

int i = 0, j = 1;

f(&i, &j);

printf("%d %d \n", i, j);

return 0; }

0 2

2 2

2 1

Error


Answer: A) 0 2



-------------------------------------------------------------


What is the output of below code? int arr[] = {10, 20, 30, 40, 50};

int *p = arr;

++*p; printf("%d ", *p);

p += 2; printf("%d", *p);


11 30

20 40

10 31

10 30


Answer: A) 11 30




-------------------------------------------------------------

What gets printed?

int main()

{ char *a="ATMATRISHA";

printf("%s ",a);

*(a+4)='t';

printf("%s",a);

}

ATMATRISHA ATMAtRISHA

Undefined Behaviour

ATMATRISHA ATMATRISHA

None of these


Answer: B) Undefined Behaviour



-------------------------------------------------------------


What gets printed?

int main()

{ char a[]="ATMATRISHA";

printf("%s ",a);

*(a+4)='t';

printf("%s",a);

}

ATMATRISHA ATMAtRISHA

Undefined Behaviour

Compiletime Error

RuntimeError


Answer: A) ATMATRISHA ATMAtRISHA




-------------------------------------------------------------


What gets printed when executed? int main()

{ char a[5] = "what" ; char *p = a+4;

printf("%d", strlen(p));

}


Runtime Error


Compile-time Error


0


1


4


Answer: C) 0




-------------------------------------------------------------


What gets printed when executed? int main()

{ char n[10] = {'1','2','0','4', ’3’, ’0’};

printf("%s",n); }


Undefined Behaviour


12


120430


None of these


Answer: C) 120430



-------------------------------------------------------------


If the size of the character is 1 byte, What gets printed when executed?


int main()

{ char arr[10] = {'1','2','0','4','3'};

printf("%d",sizeof(arr)); }


5


1


10


None of these


Answer: C) 10



-------------------------------------------------------------


char a[5] = "what" ;

char *b = "when" ;

int i;

for (i = 0 ; b[i] != '\0' ; i++)

{

a[i] = *b;

}

Value of a outside the loop is ? A) when

what

wwww

wwwww


Answer: C) wwww



-------------------------------------------------------------


float a = 10.5;

void *p;

p = &a;

printf("%f",*(float*)p);

What gets printed?


10.5

10.500000

Compile-time Error

0

A or B



Answer: E) A or B



-------------------------------------------------------------


char a[5] = "what" ;

char *b = "when" ;

int i;

for (i = 0 ; *(b+i) != '\0' ; i++)

{ a[i] = *(b+i); } printf("%s",a);


What is the output ?

when

what

we

wwww


Answer: A) when




-------------------------------------------------------------

# include <stdio.h>

void fun(int *ptr){ *ptr = 30;}

int main(){ int y = 20; fun(&y); printf("%d", y); return 0;} What is the output ?

20

Compile time Error

30

Undefined behaviour



Answer: C) 30




-------------------------------------------------------------


int *ptr; int x; ptr = &x; *ptr = 0; printf("%d %d\n", x, *ptr);

*ptr += 5;

printf("%d %d\n", x, *ptr);

(*ptr)++;

printf("%d %d\n", x, *ptr);


What is the output of above code snippet ?



Answer: 0 0

5 5

6 6



-------------------------------------------------------------


void f(int* p, int m){ m = m + 5; *p = *p + m;} int main(){ int i=5, j=10; f(&i, j); printf("%d", i+j); return 0;

}

What is the output of above code snippet ?


15

20

30

Compile time Error


Answer: C) 30


-------------------------------------------------------------





darkmode