Q. What is the first stage of compilation of a C program?

  • (A) linking
  • (B) assembling
  • (C) preprocessing
  • (D) compiling
πŸ’¬ Discuss
βœ… Correct Answer: (C) preprocessing
Explanation:

The compilation of a C program goes through several stages:

Preprocessing:

  • The preprocessor handles directives like #include, #define, and #ifdef.
  • It replaces macros, expands header files, and processes conditional compilation.

Compilation:

  • Converts the preprocessed C code into assembly language.

Assembly:

  • Translates the assembly code into machine code (object file).

Linking:

  • Combines multiple object files and libraries into a single executable.

Since preprocessing is the first stage in this sequence, the correct answer is (C) Preprocessing.

Q. What is a standard library function to read 1 char at a time?

  • (A) putchar()
  • (B) printf()
  • (C) getchar()
  • (D) write()
πŸ’¬ Discuss
βœ… Correct Answer: (C) getchar()
Explanation:

In C language, getchar() is a standard library function used to read a single character from the standard input (keyboard).

Usage of getchar()

#include <stdio.h>

int main() {
char ch;
printf("Enter a character: ");
ch = getchar(); // Reads a single character
printf("You entered: %c\n", ch);
return 0;
}

Why Not the Other Options?

  • (A) putchar() β†’ Outputs (prints) a single character, does not read.
  • (B) printf() β†’ Prints formatted output, does not read characters.
  • (D) write() β†’ A low-level system call, not a standard library function for character input.

Thus, (C) getchar() is the correct answer!

Q. What is the value of character constant 'A' of ASCII character set?

  • (A) 1
  • (B) 65
  • (C) 101
  • (D) 20
πŸ’¬ Discuss
βœ… Correct Answer: (B) 65
Explanation:

In the ASCII character set, each character is assigned a unique integer value.

  • The character 'A' has an ASCII value of 65.
  • Similarly:
    • 'B' = 66
    • 'C' = 67
    • 'a' (lowercase) = 97

Example in C:

#include <stdio.h>

int main() {
printf("ASCII value of 'A' is: %d\n", 'A'); // Output: 65
return 0;
}

Why Not the Other Options?

  • (A) 1 β†’ Incorrect, as 'A' corresponds to 65 in ASCII.
  • (C) 101 β†’ Incorrect, 101 is the ASCII value of 'e'.
  • (D) 20 β†’ Incorrect, 20 is not the ASCII value of 'A'.

Thus, the correct answer is (B) 65.

Q. Which of the following is the correct output for the program given below?

Code:
#include <studio.h>
int main ()
{
         int a = 40, b = 50, c = 10, x;
         x = a < b < c;
         printf("%d\n", x);
         return 0;
}
  • (A) 0
  • (B) 1
  • (C) Error
  • (D) None of the above
πŸ’¬ Discuss
βœ… Correct Answer: (B) 1
Explanation:

There is a mistake in the code:
#include <studio.h> should be #include <stdio.h>.

Now, let's analyze the corrected code:

#include <stdio.h>  // Corrected header file

int main() {
int a = 40, b = 50, c = 10, x;
x = a < b < c; // Evaluating this expression
printf("%d\n", x);
return 0;
}

Step-by-Step Execution:

The key part is this expression:

x = a < b < c;

which is equivalent to:

x = (a < b) < c;

First comparison:

  • a < b β†’ 40 < 50 β†’ True (1)

Second comparison:

  • Now, we evaluate 1 < c β†’ 1 < 10 β†’ True (1)

Thus, x = 1, and the output is:

1

Correct Answer:

(B) 1 βœ…

Q. What is the range of a signed char variable in C?

  • (A) 0 to 255
  • (B) -128 to 127
  • (C) 0 to 127
  • (D) -128 to 255
πŸ’¬ Discuss
βœ… Correct Answer: (B) -128 to 127
Explanation:

In C, the range of a signed char variable depends on its storage size, which is 1 byte (8 bits).

  • The most significant bit (MSB) is used for the sign (0 for positive, 1 for negative).
  • The remaining 7 bits store the value.

Correct Answer:

(B) -128 to 127 βœ…

Q. Which of the following is the correct output for the program given below?

Code:
#include <stdio.h>
int main ( )
{
      float floatvalue = 8.25;
      printf ("%d\n " , (int) floatvalue);
      return 0;
}
  • (A) 0
  • (B) 0.0
  • (C) 8.0
  • (D) 8
πŸ’¬ Discuss
βœ… Correct Answer: (D) 8
Explanation:

In the given C program:

#include <stdio.h>
int main ( )
{
float floatvalue = 8.25;
printf ("%d\n", (int) floatvalue);
return 0;
}
  1. floatvalue is initialized as 8.25 (a float type).
  2. (int) floatvalue typecasts 8.25 to an int, which removes the decimal part, resulting in 8.
  3. The %d format specifier is used in printf(), which expects an int. Since we are explicitly typecasting floatvalue to int, it prints 8.

Correct Output:

(D) 8 βœ…

Q. Which of the following is valid range of long double ?

  • (A) 3.4E-4932 to 1.1E+4932
  • (B) 3.4E-4932 to 3.4E+4932
  • (C) 1.1E-4932 to 1.1E+4932
  • (D) 1.7E - 308 to 1.7E + 308
πŸ’¬ Discuss
βœ… Correct Answer: (D) 1.7E - 308 to 1.7E + 308
Explanation:

The valid range of long double depends on the system and compiler, but typically for most modern compilers (like GCC with x86-64 architecture using 80-bit extended precision), the range is:

Correct Answer:

βœ… (A) 3.4E-4932 to 1.1E+4932

Explanation:

  • long double in most modern systems uses 80-bit extended precision (sometimes 128-bit on specific architectures).
  • Its range is significantly larger than double, which has a range of approximately 1.7E-308 to 1.7E+308.
  • The values 3.4E-4932 to 1.1E+4932 match the standard long double range on many x86-64 systems.

Would you like to confirm the range for a specific compiler (like GCC, Clang, or MSVC)?

Q. Which of the following is the correct datatype for the variable n in the statement given below?
n = 35.29 ;

  • (A) float
  • (B) double
  • (C) long double
  • (D) Depends upon the memory model that you are using
πŸ’¬ Discuss
βœ… Correct Answer: (A) float
Explanation:

 

  • In C language, implicit typing depends on the context.
  • The value 35.29 is a double literal by default in C.
  • However, the datatype of n will depend on how it's declared.

Cases:

  1. If n is declared as float β†’ n = 35.29; will store it as a single-precision floating-point number.
  2. If n is declared as double β†’ It remains a double (default for floating-point literals in C).
  3. If n is declared as long double β†’ The value will be promoted to long double (extended precision, depending on the system).

Q. Which of the following is the correct output for the program given below?

Code:
#include <stdio.h>
int main ( )
{
     printf ("%d %d %d\n" , sizeof(2.19f), sizeof(2.19), sizeof (2.19l));
     return 0 ;
}
  • (A) 4 4 4
  • (B) 4 8 8
  • (C) 4 8 10
  • (D) 4 8 12
πŸ’¬ Discuss
βœ… Correct Answer: (B) 4 8 8
Explanation:

The sizeof() operator returns the size (in bytes) of a data type or expression. Let's analyze each argument inside printf:

Breakdown of sizeof() calls:

  1. sizeof(2.19f) β†’ 2.19f is a float, which typically takes 4 bytes.
  2. sizeof(2.19) β†’ 2.19 is a double (default type for floating-point literals in C), which typically takes 8 bytes.
  3. sizeof(2.19l) β†’ 2.19l (or 2.19L) is a long double, which usually takes 10 bytes on most systems (but may be 12 or 16 bytes on some architectures).

Thus, the output is 4 8 10.

Q. Which of the following is the correct output for the program given below?

Code:
#include <stdio.h>
int main ( ) 
{
    float n = 5.375 ;
    printf("%f %e %E\n" , n, n, n) ;
    return 0 ;
}
  • (A) 5.375 5.375 5.375
  • (B) 5.375000 5.375000 5.375000
  • (C) 5.375000 5.375000e+000 5.375000E+000
  • (D) 5.375000 5.375000E+000 5.375000e+000
πŸ’¬ Discuss
βœ… Correct Answer: (C) 5.375000 5.375000e+000 5.375000E+000
Explanation:

The printf() function is used to print a floating-point number in different formats:

  • %f β†’ Prints the number in decimal notation (default floating-point format).
  • %e β†’ Prints the number in scientific notation (lowercase 'e').
  • %E β†’ Prints the number in scientific notation (uppercase 'E').

Breakdown of Output:

For n = 5.375:

  1. %f β†’ 5.375000 (default 6 decimal places)
  2. %e β†’ 5.375000e+000
  3. %E β†’ 5.375000E+000

Thus, the output is:

5.375000 5.375000e+000 5.375000E+000