0% found this document useful (0 votes)
680 views12 pages

Answers To C-Questions

The document contains 20 questions related to C programming. It provides the questions along with the expected output or answers. Some of the questions test knowledge of operators, data types, structures, pointers, unions and other core C programming concepts. The questions range from basic to more advanced topics and are designed to help assess a developer's understanding of the C language.

Uploaded by

deepanjankoul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
680 views12 pages

Answers To C-Questions

The document contains 20 questions related to C programming. It provides the questions along with the expected output or answers. Some of the questions test knowledge of operators, data types, structures, pointers, unions and other core C programming concepts. The questions range from basic to more advanced topics and are designed to help assess a developer's understanding of the C language.

Uploaded by

deepanjankoul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C Questions

1. What is the output of C Program?

int myshow(int);
void main()
{
int a=10;
myshow(a);
myshow(&a);
}
int myshow(int b)
{
printf("Received %d, ", b);
}

Answer: Received 10, Received RANDOMNumber, with a compiler warning


2. What is the output of C Program?

int main()
{
int a=0, b=0;
while(++a < 4)
printf("%d ", a);
while(b++ < 4)
printf("%d ", b);
return 0;
}

Answer: 1 2 3 1 2 3 4
3. What is the Priority of C Logical Operators? NOT (!), AND (&&) and OR(||)

Answer: NOT (!) > AND (&&) > OR (||)

4. What is the output of C Program?

#include <stdio.h>
#include <math.h>
const double PI = 4*atan(1.);
double ours(double);
int main() {
double (*f)(double);
f = sin;
printf("sin(PI/2) = %f\n", (*f)(PI/2));
f = &cos;
C Questions

printf("cos(PI) = %f\n", f(PI));


f = ours;
printf ("ours (3) = %f\n", f (3));

}
double ours (double x) {
return x*x;}

Answer: sin(PI/2) = 1.000000 cos(PI) = -1.000000 ours (3) = 9.000000

5. Assuming, integer is 2 bytes, what will be the output of the program?

#include<stdio.h>
int main()
{
printf("%x\n", -1>>1);
return 0;
}
Answer: ffffffff or 4294967295
C Questions

6. What is the output of C Program?

#include <stdio.h>
int main ()
{
union a
{
int i;
char ch[2];
};
union a u;
[Link][0]=3;
[Link][1]=2;
printf("%d, %d, %d\n", [Link][0], [Link][1], u.i);
return 0;
}
Answer: 3, 2, 515

7. Which of the following statements correct about the below C program?

#include<stdio.h>
int main()
{
struct emp
{
char name[25];
int age;
float sal;
};
struct emp e[2];
int i=0;
for(i=0; i<2; i++)
scanf("%s %d %f", e[i].name, &e[i].age, &e[i].sal);

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


scanf("%s %d %f", e[i].name, e[i].age, e[i].sal);
return 0;
}

A. Error: scanf() function cannot be used for structures elements.


B. The code runs successfully.
C. Error: Floating point formats not linked Abnormal program termination.
D. Error: structure variable must be initialized.

Answer: C
C Questions

8. What is the output of C Program?

#include<stdio.h>
int main()
{
static int arr[] = {0, 1, 2, 3, 4};
int *p[] = {arr, arr+1, arr+2, arr+3, arr+4};
int **ptr=p;
ptr++;
printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
*ptr++;
printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
*++ptr;
printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
++*ptr;
printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
return 0;
}
Answer: 1, 1, 1
2, 2, 2
3, 3, 3
3, 4, 4

9. Write a C program to check whether the number is EVEN or ODD, without using any
arithmetic or relational operators.

Answer:

#include<stdio.h>
int main() {
int x;
printf("Enter a number:");
scanf("%d", &x);
(x&1)?printf("\nOdd"):printf("\nEven");
return 0; }
C Questions

10. What is the output of C Program?

#include<stdio.h>
int main(){
unsigned int res;
res = (64 >>(2+1-2)) & (~(1<<2));
printf("%d\n", res);
return 0;}
Answer: 32

11. What is the output of C Program?

#include <stdio.h>
int main()
{
int a = 1, b = 2, c = 3;
printf("%d", a + = a + = 3 *5 + a);
}
Answer: 34

12. What is size of the below C structure?

struct f1 {
struct f5 {
char *p;
short x;
} inner;
char c;
};

Answer: 24

13. What is the output of C Program?

#include <stdio.h>
#define DefExp(x,y)
#define x ((y)+1)
#define DefExp(x) (printf("%s = %d\n", #x, (x)))
int main(int argc, char **argv){
DefExp(1*3);
return 0;}
Answer: 1*3 = 3
C Questions

14. What is the output of C Program?

#include <stdio.h>
int main()
{
union sample{
int m;
float n;
char ch;
};
union sample u;
u.m = 25;
printf("%d %f %c\n", u.m, u.n, [Link]);
u.n = 0.2;
printf("%d %f %c\n", u.m, u.n, [Link]);
[Link] = 'p';
printf("%d %f %c\n", u.m, u.n, [Link]);
printf("The size of union = %d\n", sizeof(u));
return 0;
}

Answer:
25 0.000000
1045220557 0.200000 �
1045220464 0.199999 p
C Questions

15. What is the output of C Program?

#include <stdio.h>
#include <stdlib.h>
int G = 0;
int main(int argc, char **argv)
{
static int s;
int a;
int *p;

p = malloc(sizeof(int));

printf("&G= %p\n", (void *) &G);


printf("&s= %p\n", (void *) &s);
printf("&a= %p\n", (void *) &a);
printf("&p= %p\n", (void *) &p);
printf("p= %p\n", (void *) p);
printf("main=%p\n", (void *) main);

free(p);

return 0;
}
Answer:
&G= 0x55ddb4e46014
&s= 0x55ddb4e46018
&a= 0x7ffc4103ba5c
&p= 0x7ffc4103ba60
p= 0x55ddb51522a0
main=0x55ddb4e431a9
C Questions

16. What is the output of C Program?

#include <stdio.h>
int main () {
int x = 10, y = 20, z, sum=7;
int *ptr1 = &x;
int *ptr2 = &y;
z = *ptr1 * *ptr2;
y=*ptr1**ptr2;
sum=sum+*ptr1;
*ptr2=*ptr2+10;
*ptr1=*ptr1+*ptr2;
*ptr1=*ptr2-*ptr1;
z=5*-*ptr2/ *ptr1;
printf("%d %d %d", x,y,z);
return 0;
}
Answer: -10 210 105
C Questions

17. What is the output of C Program?

#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <string.h>

typedef struct {
char *model;
int capacity;
} Car;

int main ()
{
Car af1;
Car af2;
Car af3;

[Link] = (char*)malloc(strlen("Daimler") + 1);


strcpy([Link], "Daimler");
[Link] = 320;
af2 = af1;
strcpy([Link], "BMW");
[Link] = (char*)malloc(strlen("Daimler") + 1);
strcpy([Link], [Link]);
[Link] = [Link];
strcpy([Link], "Daimler");
strcpy([Link], "BMW");
printf("%s %s %s \n", [Link], [Link], [Link]);

return 0;
}
Answer: Daimler BMW Daimler
C Questions

18. What is the output of C Program?

#include <stdio.h>
typedef struct tagT{
int a:4;
int b:4;
int c:8;
int d:16;
}T;
int main()
{
char data[]={0x12,0x34,0x56,0x78};
T *t = (T*)data;
printf("a =0x%x\n" ,t->a);
printf("b =0x%x\n" ,t->b);
printf("c =0x%x\n" ,t->c);
printf("d =0x%x\n" ,t->d);
return 0;
}
Answer:
a =0x2
b =0x1
c =0x34
d =0x7856
C Questions

19. What is the output of C Program?

#include <stdio.h>
int main()
{
int A=5;
int B=7;
int C;
C = A&B;
printf("1st:%d\n",C);
C = A|B;
printf("2nd:%d\n",C);
C = A^B;
printf("3rd:%d\n",C);
C = ~A;
printf("4th:%d\n",C);
C = B<<3;
printf("5th:%d\n",C);
C = B^5&A|B<<A;
printf("6th:%d\n",C);
return(0);
}

Answer:
1st:5
2nd:7
3rd:2
4th: -6
5th:56
6th:226
C Questions

20. What is the output of C Program?

#include <stdio.h>
#include <stdio.h>
int main(){
int k=8;
switch(k){
case 1==8:
printf("DELTA\t");
break;
case 1 && 2:
printf("INDIA\t");
break;
default:
printf("ENERGY\t");
}
printf("SYSTEMS");
}
Answer: ENERGY SYSTEMS

You might also like