Sample questions
Question 1Medium
Which of the following is not possible under any scenario?
s1 = &s2;s1 = s2;(*s1).number = 10;- None of the mentioned
Answer: D. None of the mentioned
Explanation
All the expression mention above is possible with the instance of a structure.
The instance may be pointer variable or not.
Step-by-step solution
- s1 = &s2 is valid when s1 is a pointer to the same type as s2.
- s1 = s2 is valid when s1 and s2 are both structs of the same type.
- (*s1).number = 10 is valid when s1 is a pointer to a struct that has a number member.
- Each statement is possible under some declaration of s1 and s2, so none is impossible.
- So the answer is None of the mentioned.
Why the other options are wrong
- A. It is possible if s1 is a pointer and s2 is a struct of the same type.
- B. It is possible if s1 and s2 are both structs of the same type.
- C. It is possible if s1 is a pointer to a struct with a number member.
Tip: Check each statement against some valid declaration before calling it impossible.
Question 2Medium
What is the output of this C code?
#include <stdio.h>
void main()
{
struct student
{
int no;
char name[20];
};
struct student s;
no = 8;
printf("%d", no);
}
- Nothing
- Compile time error
- Junk
- 8
Answer: B. Compile time error
Explanation
To access structure variable we use (.) operator. Here variable no is accessed without using structure and (.) operator.
Step-by-step solution
- The struct student declaration has members no and name, but no variable called no exists outside it.
- The statement no = 8 uses the name no, which is not declared anywhere in main.
- C requires every name to be declared before it is used, so the compiler stops with an error.
- So the answer is Compile time error.
Why the other options are wrong
- A. The program does not run, so nothing is printed.
- C. A junk value needs the program to run, but it does not compile.
- D. 8 would need a valid variable no, which is not declared.
Tip: A struct member is not a variable outside its struct; declare the variable before you use it.
Question 3Medium
Point out the error in the program?
#include <stdio.h>
int main()
{
struct emp
{
char name[20];
float sal;
};
struct emp e[10];
int i;
for(i=0; i<=9; i++)
scanf("%s %f", e[i].name, &e[i].sal); return 0;
}
(Assume Turbo C, a 16-bit compiler.)
- Error: invalid structure member
- Error: Floating point formats not linked
- No error
- None of above
Answer: B. Error: Floating point formats not linked
Explanation
At run time it will show an error then program will be terminated.
Sample output: Turbo C (Windows)
c:\>myprogram
Sample
12.123
scanf: floating point formats not linked
Abnormal program termination
Step-by-step solution
- The loop reads a name and a float with scanf, using the format %s %f and the address &e[i].sal.
- On a Turbo C style compiler, the floating point conversion code is linked only when the program uses floating point in a way the linker can see, and here the float is read only by scanf.
- The link step then fails with the message Floating point formats not linked.
- So the answer is Error: Floating point formats not linked.
Why the other options are wrong
- A. The member names name and sal are valid, so there is no invalid structure member.
- C. Modern compilers accept this code, but on the Turbo C style compiler the float formats are not linked, so there is an error.
- D. Option B is the error, so none of the above is wrong.
Tip: A float read with scanf can fail at link time on old DOS compilers; that is a link error, not a syntax error.
Question 4Medium
Which of the following return-type cannot be used for a function in C?
- char *
- struct
- void
- None of the mentioned
Answer: D. None of the mentioned
Explanation
In C, functions can return various types including pointers (like char *), structures (struct), and void (no return value). 'None of the mentioned' is incorrect because all the options are valid return types in C.
Step-by-step solution
- A C function can return a pointer, such as char *.
- A C function can return a struct by value, and it can return void.
- All three listed return types are valid in C, so none of them is the one that cannot be used.
- So the answer is None of the mentioned.
Why the other options are wrong
- A. Char * is a pointer type, and pointers can be returned.
- B. A struct type can be returned by value in C.
- C. Void is valid as the return type of a function that returns nothing.
Tip: Check each listed type against the rule before picking the odd one out.
Question 5Medium
What is the output of this C code?
#include <stdio.h>
struct
{
int k;
char c;
} p;
int p = 10;
int main()
{
p.k = 10;
printf("%d %d\n", p.k, p);
}
- Compile time error
- 10 10
- Depends on the standard
- Depends on the compiler
Answer: A. Compile time error
Explanation
Explanation:
We cannot create same name variable.P is structure instance so its invalid to create any other variable same name of P at global scope.
Step-by-step solution
- P is declared twice at file scope: once as a struct variable and once as int p = 10.
- Two declarations of the same name with different types in the same scope are not allowed.
- The compiler reports a redeclaration error, so the program does not compile.
- So nothing runs and no output is printed.
- The answer is Compile time error.
Why the other options are wrong
- B. 10 10 would need the program to compile, and the duplicate p stops it.
- C. This is an error under the standard, so the result does not depend on the standard.
- D. The error is the same on all compilers, so it does not depend on the compiler.
Tip: Two file-scope variables with the same name and different types are a compile error.
Question 6Medium
What is the output of this C code?
#include <stdio.h>
struct p
{
int k;
char c;
float f;
};
int p = 10;
int main()
{
struct p x = {1, 97};
printf("%f %d\n", x.f, p);
}
- Compile time error
- 0.000000 10
- Some garbage value 10
- 0 10
Answer: B. 0.000000 10
Explanation
In above code structure variable declare using initialization list, struct p x = {1, 97}; here only two members of structure are initialized using initialization list, variable float f implicitly initialize to 0.00000.
Step-by-step solution
- X is a struct p whose k is 1 and whose c is 97.
- The third member f is not given a value, so it is set to 0.0.
- The file-scope int p is 10, and the struct tag p does not clash with it.
- Printf prints x.f as 0.000000 and p as 10.
- So the output is 0.000000 10.
Why the other options are wrong
- A. The code compiles, since the tag and variable names do not clash.
- C. Members left out of an initializer are set to zero, so no garbage value is printed.
- D. %f prints six decimal places, so the float prints as 0.000000, not 0.
Tip: Members left out of a struct initializer are set to zero.
Question 7Medium
What is the output of this C code?
#include <stdio.h>
struct
{
int k;
har c;
};
int main()
{
struct p;
p.k = 10;
printf("%d\n", p.k);
}
- Compile time error
- 10
- Undefined behaviour
- Segmentation fault
Answer: A. Compile time error
Explanation
Anonymous structure variable is createdat time of declaration of structure. It is invalid to create anonymous structure instance after declaration of structure.
Step-by-step solution
- The struct has no tag, and its member type is har, which is not a C type.
- The name har is not declared, so the compiler reports an error on the member line.
- Struct p; only declares a tag, and p is then used as a variable, which is not declared.
- The compiler stops with an error before the program runs.
- The answer is Compile time error.
Why the other options are wrong
- B. The program does not compile, so 10 is never printed.
- C. Undefined behaviour needs a program that compiles and runs; this one is rejected.
- D. A segmentation fault needs a program that runs; this one never runs.
Tip: A struct tag and a variable name are different things; check each name is declared.
Question 8Medium
Which of the following operation is illegal in structures?
- Typecasting of structure
- Pointer to a variable of same structure
- Dynamic allocation of memory for structure
- All of the mentioned
Answer: A. Typecasting of structure
Explanation
A struct cannot be typecast to another type, so typecasting a structure is illegal in C. A pointer to a struct of the same type and malloc() for a struct are both allowed.
Step-by-step solution
- Typecasting a struct to another type is not allowed in C.
- A pointer to a struct of the same type is allowed, as used in linked lists.
- Dynamic allocation with malloc is allowed for a struct.
- So the illegal operation is Typecasting of structure.
Why the other options are wrong
- B. A pointer to the same struct type is legal, as in linked lists.
- C. Malloc can allocate memory for a struct, so this is legal.
- D. Options B and C are legal, so not all of them are illegal.
Tip: Structs can be pointed to and allocated, but not cast to another type.
Question 9Medium
What is the output of this C code?
#include <stdio.h>
struct p
{
int k;
char c;
};
int p = 10;
int main()
{
struct p x;
x.k = 10;
printf("%d %d\n", x.k, p);
}
- Compile time error
- 10 10
- Depends on the standard
- Depends on the compiler
Answer: B. 10 10
Explanation
Scope of int p and structure instance is different its valid to declare same name variable at different scope.
Step-by-step solution
- Struct p is a tag, and tags are in a separate namespace from ordinary variable names.
- So int p = 10 is a separate variable named p, and it does not clash with the tag.
- x.k is set to 10, and p is still 10.
- Printf prints x.k and then p, giving 10 10.
- The answer is 10 10.
Why the other options are wrong
- A. The tag and the variable have different namespaces, so there is no compile error.
- C. The output is fixed by the code here, so it does not depend on the standard.
- D. The output does not depend on the compiler here.
Tip: A struct tag and a variable can share a name in C.
Question 10Medium
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);
}
- Compile time error
- Nothing
- Junk values
- st st
Answer: D. st st
Explanation
Unlike array, Structure allow to assignment operation on structure instances.
Step-by-step solution
- A struct can be assigned to another struct of the same type.
m = s copies the pointer name, so both point to "st".printf("%s%s", ...) prints both strings.- Both print "st", so option D.
Why the other options are wrong
- A. Struct assignment is allowed in C.
- B. The printf prints two strings.
- C. Both pointers hold a valid string, not junk.
Tip: Structs can be copied with =, unlike arrays.
Practise all 15 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →