Sample questions
Question 1Medium
Which of the following share a similarity in syntax?
1. Union,
2. Structure
3. Arrays
4. Pointers
- 3 and 4
- 1 and 2
- 1 and 3
- 1, 3 and 4
Answer: B. 1 and 2
Explanation
Syntax:
syntax of union is.
union union_name
{
-------; Body
-------; of
-------; union
}instance;
syntax of structure is.
struct structur_name
{
-------; Body
-------; of
-------; structure
}instance;
Both are the same.
Step-by-step solution
- Union and structure are declared with the same struct-style form: a keyword, a braced list of members, and a name.
- Arrays use square brackets and pointers use the star operator, so they have different declaration syntax.
- So union and structure share a syntax, which is items 1 and 2.
- So the answer is 1 and 2.
Why the other options are wrong
- A. Items 3 and 4 are arrays and pointers, which have different syntax.
- C. Arrays (item 3) do not share the union and structure syntax.
- D. Arrays and pointers (items 3 and 4) do not share the struct-style syntax.
Tip: Union and structure are declared in the same struct-style form.
Question 2Medium
What will be the output of the program?
#include <stdio.h>
int main()
{
union a
{
int i;
char ch[2];
};
union a u;
u.ch[0]=3;
u.ch[1]=2;
printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i);
return 0;
}
(Assume Turbo C, a 16-bit compiler.)
- 3, 2, 515
- 515, 2, 3
- 3, 2, 5
- 515, 515, 4
Answer: A. 3, 2, 515
Explanation
The char array shares its memory with the int. u.ch[0] is 3 and u.ch[1] is 2. On a common little-endian machine the int reads as 3 + 2 x 256 = 515, which assumes its other two bytes are zero. The program prints 3, 2, 515.
3, 2, 515
Step-by-step solution
- The union members share the same memory: int i takes 4 bytes and char ch[2] uses the first 2.
- u.ch[0] = 3 sets the first byte to 3, and u.ch[1] = 2 sets the second byte to 2.
- On a little-endian machine, the first byte is the lowest byte of i, so i = 3 + 2 × 256 = 515, assuming the untouched upper bytes are zero.
- Printf prints ch[0] as 3, ch[1] as 2 and i as 515, so the output is 3, 2, 515, which is option A.
Why the other options are wrong
- B. The order is wrong: ch[0] is 3, not 515.
- C. The value of i is 3 + 2 × 256 = 515, not 5.
- D. ch[0] and ch[1] are 3 and 2, not 515 and 515, and 4 is wrong.
Tip: In a union, all members share the same bytes; on little-endian, the first byte is the lowest.
Question 3Medium
What is the output of this C code?
#include <stdio.h>
union p
{
int x;
char y;
};
int main()
{
union p p, b;
p.y = 60;
b.x = 12;
printf("%d\n", p.y);
}
- Compile time error
- Depends on the compiler
- 60
- Undefined behaviour
Answer: C. 60
Explanation
Here p.y = 60; in this statement variable p is a instance of the union p.So it will defiantly print output 60.
Step-by-step solution
- p.y = 60 stores 60 in the char member of the union p.
- A union shares one block of memory, so reading p.y gives back the value just stored.
- The value 60 is printed as 60, and b.x = 12 does not change p.
- So the output is 60, which is option C.
Why the other options are wrong
- A. The program compiles without error.
- B. The result is defined: reading the member last written gives 60.
- D. Reading the member last written is defined behaviour, so it is not undefined.
Tip: Reading the same union member you just wrote is always safe.
Question 4Medium
What is the output of this C code?
#include <stdio.h>
union
{
int x;
char y;
}p;
int main()
{
p.y = 60;
printf("%d\n", sizeof(p));
}
- Compile time error
- sizeof(int) + sizeof(char)
- Depends on the compiler
- sizeof(char)
Answer: C. Depends on the compiler
Explanation
The single instance of the union required size is the maximum size of there member variable.
The size of the integer is depends on the operating system. Operating system's are 16,32,64.
So output Depends on the compiler
Step-by-step solution
- A union has one block of memory shared by all its members.
- Its size is at least the size of its largest member, which is int, usually 4 bytes.
- The size of int depends on the compiler and platform, so the exact result changes.
- So the answer is Depends on the compiler.
Why the other options are wrong
- A. The code compiles, since sizeof is valid on a union.
- B. The union size is not the sum of its members, because they share memory.
- D. A char is 1 byte, but the union is at least the size of an int.
Tip: A union takes the size of its largest member, not the sum of all members.
Question 5Medium
What is the output of this C code?
#include <stdio.h>
union p
{
int x;
char y;
}k = {y = 97};
int main()
{
printf("%d\n", k.y);
}
- Compile time error
- 97
- a
- Depends on the standard
Answer: A. Compile time error
Explanation
Compile Time error will Occur here.
The Error is y undeclared here (not in a function)
k = {y = 97};
Y is used without declaration.
Step-by-step solution
- The initializer {y = 97} is given to a union whose members are x and y.
- Inside the braces, y is a member name, not a variable in scope, so y is undeclared.
- So the initializer is invalid at file scope.
- The compiler reports an error, so the program does not compile.
- The answer is Compile time error.
Why the other options are wrong
- B. 97 would only print if the initializer were valid, and it is not.
- C. The program does not compile, so nothing runs and no character is printed.
- D. This is a compile-time error in every standard-conforming compiler, not a standard-dependent result.
Tip: Union and struct initializers must use values for declared variables, not member names.
Question 6Medium
What is the output of this C code?
#include <stdio.h>
union p
{
int x;
float y;
};
int main()
{
union p p, b;
p.x = 10;
printf("%f\n", p.y);
}
- Compile time error
- Implementation dependent
- 10.000000
- 0.000000
Answer: D. 0.000000
Explanation
x and y share the same 4 bytes in the union.
The bit pattern of the int 10 read as a float is a tiny number (about 1.4 × 10^-44).
%f prints 6 decimal places, so it shows 0.000000.
Step-by-step solution
- p.x = 10 stores the int 10 in the union, and the float y shares that memory.
- The int 10 has the bit pattern 00000000 00000000 00000000 00001010.
- Read as a float, those bits form a tiny denormal number, about 1.4 × 10^−44.
- Printf with %f shows it to six decimal places, as 0.000000.
- So the output is 0.000000.
Why the other options are wrong
- A. The code compiles, since the union and printf are valid.
- B. Reading a member other than the last written one is undefined in the standard, but on common systems the printed result is 0.000000.
- C. The bits of int 10 are not the float 10, so 10.000000 is not printed.
Tip: Reading a union member after writing a different one gives a bit-pattern result, not a conversion.
Question 7Medium
What is the output of this C code?
#include <stdio.h>
union utemp
{
int a;
double b;
char c;
}u;
int main()
{
u.c = 'A';
u.a = 1;
printf("%d", sizeof(u));
}
The output will be: ?
(Assuming size of char = 1, int = 4, double = 8)
- 1
- 4
- 8
- 13
Answer: C. 8
Explanation
In union, highest data member memory is allocated to union and all members share the same memory
Step-by-step solution
- A union allocates memory based on its largest member.
- The largest member here is a double, which takes 8 bytes.
- Therefore, the size of the union u is 8 bytes.
Why the other options are wrong
- A. A char only takes 1 byte, not 8.
- B. An int takes 4 bytes, not 8.
- D. The union size is the largest member's size, not a sum of all members.
Tip: Remember: Union size = size of largest member.
Question 8Medium
What is the output of this C code?
#include <stdio.h>
union
{
int x;
char y;
}p;
int main()
{
p.x = 10;
printf("%d\n", sizeof(p));
}
- Compile time error
- sizeof(int) + sizeof(char)
- Depends on the compiler
- sizeof(int)
Answer: D. sizeof(int)
Explanation
In union, highest data member memory is allocated to union and all members share the same memory
Step-by-step solution
- P is a union with an int member x and a char member y.
- The size of a union is the size of its largest member, so the size is the size of int.
- The members do not add up, so sizeof(p) is not sizeof(int) + sizeof(char).
- So the answer is sizeof(int).
Why the other options are wrong
- A. The code compiles, because p.x and sizeof are valid.
- B. A union does not add its members together.
- C. On the usual compilers the size is sizeof(int), so the result is not really compiler dependent here.
Tip: A union takes the size of its largest member, not the sum of the members.
Practise all 8 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 →