C Programming Questions and Answers – Structures and Functions
C Programming Questions and Answers – Structures and Functions
1.What is the output of this C code?
#include <stdio.h>
struct student
{
char *name;
};
struct student s;
struct student fun(void)
{
s.name = “newton”;
printf(“%s\n”, s.name);
s.name = “alan”;
return s;
}
void main()
{
struct student m = fun();
printf(“%s\n”, m.name);
m.name = “turing”;
printf(“%s\n”, s.name);
}
a) newton alan alan
b) alan newton alan
c) alan alan newton
d) Compile time error
View Answer
Answer:a
2.What is the output of this C code?
#include <stdio.h>
struct student
{
char *name;
};
void main()
{
struct student s, m;
s.name = “st”;
m = s;
printf(“%s%s”, s.name, m.name);
}
a) Compile time error
b) Nothing
c) Junk values
d) st st
View Answer
Answer:d
3.Which of the following return-type cannot be used for a function in C?
a) char *
b) struct
c) void
d) None of the mentioned
View Answer
Answer:d
4.What’s the output of the following code?
#include <stdio.h>
struct temp
{
int a;
} s;
void func(struct temp)
{
s.a = 10;
printf(“%d\t”, s.a); s
}
main()
{
func(s);
printf(“%d\t”, s.a);
}
a) 10 (Garbage Value)
b) 0 10
c) 10 0
d) (Garbage Value) 10
View Answer
Answer:c
5.Which of the following is not possible under any scenario?
a) s1 = &s2;
b) s1 = s2;
c) (*s1).number = 10;
d) None of the mentioned
View Answer
Answer:d
6.Which of the following operation is illegal in structures?
a) Typecasting of structure
b) Pointer to a variable of same structure
c) Dynamic allocation of memory for structure
d) All of the mentioned
View Answer
Answer:a
7.Presence of code like “s.t.b = 10” indicate.
a) Syntax Error
b) structure
c) double data type
d) An ordinary variable name
View Answer
Answer:b
8.The output of the code below is
#include <stdio.h>
struct student
{
char *name;
};
struct student fun(void)
{
struct student s;
s.name = “alan”;
return s;
}
void main()
{
struct student m = fun();
s.name = “turing”;
printf(“%s”, m.name);
}
a) alan
b) Turing
c) Compile time error
d) Nothing
View Answer
Answer:c
9.What is the output of this C code?
#include <stdio.h>
struct point
{
int x;
int y;
};
int main()
{
struct point p = {1};
struct point p1 = {1};
if(p == p1)
printf(“equal\n”);
else
printf(“not equal\n”);
}
a) Compile time error
b) equal
c) depends on the standard
d) not equal
View Answer
Answer:a
10.What is the output of this C code?
#include <stdio.h>
struct point
{
int x;
int y;
};
struct notpoint
{
int x;
int y;
};
struct point foo();
int main()
{
struct point p = {1};
struct notpoint p1 = {2, 3};
p1 = foo();
printf(“%d\n”, p1.x);
}
struct point foo()
{
struct point temp = {1, 2};
return temp;
}
a) Compile time error
b) 1
c) 2
d) Undefined behaviour
View Answer
Answer:a
11.What is the output of this C code?
#include <stdio.h>
struct point
{
int x;
int y;
};
struct notpoint
{
int x;
int y;
};
int main()
{
struct point p = {1};
struct notpoint p1 = p;
printf(“%d\n”, p1.x);
}
a) Compile time error
b) 1
c) 0
d) Undefined
View Answer
Answer:a
12.What is the output of this C code?
#include <stdio.h>
struct point
{
int x;
int y;
};
struct notpoint
{
int x;
int y;
};
void foo(struct point);
int main()
{
struct notpoint p1 = {1, 2};
foo(p1);
}
void foo(struct point p)
{
printf(“%d\n”, p.x);
}
a) Compile time error
b) 1
c) 0
d) Undefined
View Answer
Answer:a
13.What is the output of this C code?
#include <stdio.h>
struct point
{
int x;
int y;
};
void foo(struct point*);
int main()
{
struct point p1 = {1, 2};
foo(&p1);
}
void foo(struct point *p)
{
printf(“%d\n”, *p.x++);
}
a) Compile time error
b) Segmentation fault/code crash
c) 2
d) 1
View Answer
Answer:a
14.What is the output of this C code?
#include <stdio.h>
struct point
{
int x;
int y;
};
void foo(struct point*);
int main()
{
struct point p1 = {1, 2};
foo(&p1);
}
void foo(struct point *p)
{
printf(“%d\n”, *p->x++);
}
a) Compile time error
b) 1
c) Segmentation fault/code crash
d) 2
View Answer
Answer:a
15.What is the output of this C code?
#include <stdio.h>
struct student fun(void)
{
struct student
{
char *name;
};
struct student s;
s.name = “alan”;
return s;
}
void main()
{
struct student m = fun();
printf(“%s”, m.name);
}
a) Compile time error
b) alan
c) Nothing
d) Varies
View Answer
Answer:a
16.What is the output of this C code?
#include <stdio.h>
struct student
{
char *name;
};
struct student fun(void)
{
struct student s;
s.name = “alan”;
return s;
}
void main()
{
struct student m = fun();
printf(“%s”, m.name);
}
a) Nothing
b) alan
c) Run time error
d) Varies
View Answer
Answer:b