ExamVeda
Login
Home
21
What will be the output of the following C code?
#include <stdio.h>
void main()
{
    float x = 0.1;
    printf("%d, ", x);
    printf("%f", x);
}
Discuss
Answer & Solution
Answer: Option B
No explanation is given for this question. Let's Discuss on Board
22
What will be the output of the following C code?
#include <stdio.h>
void main()
{
    int x = 4, y, z;
    y = --x;
    z = x--;
    printf("%d%d%d", x,  y, z);
}
Discuss
Answer & Solution
Answer: Option B
No explanation is given for this question. Let's Discuss on Board
23
What will be the output of the following C code?
#include <stdio.h>
int main()
{
    int x = 1, y = 0, z = 3;
    x > y ? printf("%d", z) : return z;
}
Discuss
Answer & Solution
Answer: Option C
No explanation is given for this question. Let's Discuss on Board
24
Comment on the output of the following C code.
#include <stdio.h>
int main()
{
    int i, n, a = 4;
    scanf("%d", &n);
    for (i = 0; i < n; i++)
        a = a * 2;
}
Discuss
Answer & Solution
Answer: Option B
No explanation is given for this question. Let's Discuss on Board
25
What will be the final values of i and j in the following C code?
#include <stdio.h>
int x = 0;
int f()
{
    if (x == 0)
        return x + 1;
    else
        return x - 1;
}
int g()
{
    return x++;
}
int main()
{
    int i = (f() + g()) || g();
    int j = g() || (f() + g());
}
Discuss
Answer & Solution
Answer: Option A
Solution:
Understanding the C code:
The code involves three functions:
`f()`: This function checks the value of a global variable `x`. If `x` is 0, it returns 1; otherwise, it returns `x - 1`.
`g()`: This function returns the current value of `x` and then increments `x` (post-increment).
`main()`: This is where the magic happens. It calculates `i` and `j` using the `f()` and `g()` functions and the logical OR operator (`||`).

Let's trace the execution step-by-step:
Initially, `x` is 0.
For `i`:
1. `f()` is called first. Since `x` is 0, `f()` returns 1.
2. `g()` is called next. `g()` returns 0 (the current value of `x`) and then increments `x` to 1.
3. The expression becomes `1 + 0 || 0`. Since `1 + 0` evaluates to 1 (which is true in boolean context), the `||` operation short-circuits and the result is 1. Therefore, `i` becomes 1.

For `j`:
1. `g()` is called first. `g()` returns 1 (the current value of `x`, which is now 1) and increments `x` to 2.
2. The `||` operation checks the left side (which is now 1, meaning true). Because of short-circuiting of `||`, the right side (`f() + g()`) is not evaluated. The result is 1. Therefore `j` becomes 1.

Conclusion:
The final values are: `i = 1` and `j = 1`. Therefore, the correct option is A.
26
What will be the output of the following C code?
#include <stdio.h>
int main()
{
    int x = 2, y = 0;
    int z = (y++) ? 2 : y == 1 && x;
    printf("%d\n", z);
    return 0;
}
Discuss
Answer & Solution
Answer: Option B
No explanation is given for this question. Let's Discuss on Board
27
What will be the output of the following C code?
#include <stdio.h>
void main()
{
    char a = 'a';
    int x = (a % 10)++;
    printf("%d\n", x);
}
Discuss
Answer & Solution
Answer: Option C
No explanation is given for this question. Let's Discuss on Board
28
What will be the output of the following C code?
#include <stdio.h>
void main()
{
    1 < 2 ? return 1 : return 2;
}
Discuss
Answer & Solution
Answer: Option D
No explanation is given for this question. Let's Discuss on Board
29
What will be the output of the following C code?
#include <stdio.h>
int main()
{
    int x = 3; //, y = 2;
    const int *p = &x;
    *p++;
    printf("%d\n", *p);
}
Discuss
Answer & Solution
Answer: Option C
No explanation is given for this question. Let's Discuss on Board
30
What will be the output of the following C code?
#include <stdio.h>
void main()
{
    int a = 2 + 3 - 4 + 8 -  5 % 4;
    printf("%d\n", a);
}
Discuss
Answer & Solution
Answer: Option B
No explanation is given for this question. Let's Discuss on Board