πŸ“Š C Programming
Q. In the following C code, the union size is decided by?
Code:
union temp
    {
        char ch;
        int n;
        float f;
    };
  • (A) float
  • (B) int
  • (C) both float and int
  • (D) char
πŸ’¬ Discuss
βœ… Correct Answer: (A) float

Explanation:

The correct answer is:

βœ… (A) float

Explanation:

  • In a union, all members share the same memory space, and its size is determined by the largest data type inside it.
  • In this case, the union contains:
    • char ch; β†’ 1 byte
    • int n; β†’ typically 4 bytes
    • float f; β†’ typically 4 bytes

How is Union Size Determined?

  • A union only allocates one memory block large enough to store the largest member.
  • Here, both int and float require 4 bytes each (assuming standard memory sizes).
  • So, the union size is determined by float, which is the largest.
  • If padding is involved (architecture-specific), it might be slightly larger to align the memory.

Example Code:

#include <stdio.h>

union temp {
char ch;
int n;
float f;
};

int main() {
printf("Size of union: %lu\n", sizeof(union temp));
return 0;
}

Expected Output:

Size of union: 4

(If there's padding, it might be 8 bytes on some architectures.)

Final Answer:

βœ… (A) float

πŸ“Š C Programming
Q. Loss in precision occurs for typecasting from____________.
  • (A) float to int
  • (B) long to float
  • (C) float to double
  • (D) char to short
πŸ’¬ Discuss
βœ… Correct Answer: (A) float to int

Explanation:

Loss of precision happens when a type conversion leads to loss of fractional or significant digits. Let's analyze each option:

(A) float to int β†’ βœ… Loss of precision

  • Converting from float (which has a decimal part) to int (which only stores whole numbers) results in truncation (removal of the fractional part).
  • Example:

(B) long to float β†’ ⚠️ Possible loss of precision

  • A long integer might have more significant digits than a float can accurately store.
  • Example:
  • But for smaller numbers, no loss occurs.

(C) float to double β†’ ❌ No loss of precision

  • double has more precision than float, so no data loss occurs.
  • Example:

(D) char to short β†’ ❌ No loss of precision

  • char (1 byte) can easily fit into a short (2 bytes), so no precision loss occurs.

Final Answer:

βœ… (A) float to int (because fractional values are lost).

float f = 3.14159f;
double d = (double) f;
printf("%lf", d); // No precision loss
long l = 9876543210;
float f = (float) l;
printf("%f", f); // May lose precision for very large values
float f = 5.99;
int i = (int) f;
printf("%d", i); // Output: 5 (fractional part is lost)

πŸ“Š C Programming
Q. What is the size of float in a 32-bit compiler?
  • (A) 8
  • (B) 4
  • (C) 2
  • (D) 1
πŸ’¬ Discuss
βœ… Correct Answer: (B) 4

Explanation:

In a 32-bit compiler, the size of a float data type is 4 bytes (32 bits) because:

  • A float in C follows the IEEE 754 single-precision floating-point standard, which uses:
    • 1 bit for the sign
    • 8 bits for the exponent
    • 23 bits for the mantissa (fraction)

Verification with C Code:

You can check the size of float using:

#include <stdio.h>
int main() {
printf("Size of float: %zu bytes\n", sizeof(float));
return 0;
}

Output:

Size of float: 4 bytes

Final Answer:

βœ… (B) 4

πŸ“Š C Programming
Q. In a 32-bit compiler, which 2 types have same size?
  • (A) float and double
  • (B) short and int
  • (C) char and short
  • (D) int and float
πŸ’¬ Discuss
βœ… Correct Answer: (D) int and float

Explanation:

In a 32-bit compiler, the typical sizes of fundamental data types are:

  • int and float both take 4 bytes in a 32-bit compiler.
  • double is 8 bytes, so option (A) is incorrect.
  • short is 2 bytes while int is 4 bytes, so (B) is incorrect.
  • char is 1 byte, while short is 2 bytes, so (C) is incorrect.

Final Answer:

βœ… (D) int and float

πŸ“Š C Programming
Q. Who is father of C Language?
  • (A) bjarne stroustrup
  • (B) dennis ritchie
  • (C) james a. gosling
  • (D) dr. e.f. codd
πŸ’¬ Discuss
βœ… Correct Answer: (B) dennis ritchie
πŸ“Š C Programming
Q. C Language developed at _____?
  • (A) at & t\s bell laboratories of usa in 1972
  • (B) at & t\s bell laboratories of usa in 1970
  • (C) sun microsystems in 1973
  • (D) cambridge university in 1972
πŸ’¬ Discuss
βœ… Correct Answer: (A) at & t\s bell laboratories of usa in 1972
πŸ“Š C Programming
Q. For 16-bit compiler allowable range for integer constants is ______ ?
  • (A) -3.4e38 to 3.4e38
  • (B) -32767 to 32768
  • (C) -32768 to 32767
  • (D) -32668 to 32667
πŸ’¬ Discuss
βœ… Correct Answer: (C) -32768 to 32767
πŸ“Š C Programming
Q. C programs are converted into machine language with the help of
  • (A) an editor
  • (B) a compiler
  • (C) an operating system
  • (D) none of the above
πŸ’¬ Discuss
βœ… Correct Answer: (B) a compiler
πŸ“Š C Programming
Q. A C variable cannot start with
  • (A) an alphabet
  • (B) a number
  • (C) a special symbol other than underscore
  • (D) both (b) and (c)
πŸ’¬ Discuss
βœ… Correct Answer: (D) both (b) and (c)
πŸ“Š C Programming
Q. Which of the following is allowed in a C Arithmetic instruction
  • (A) []
  • (B) {}
  • (C) ()
  • (D) none of the above
πŸ’¬ Discuss
βœ… Correct Answer: (C) ()