ExamVeda
Login
Home
11
What will be the output of the following C code?
#include <stdio.h>
int x;
void main()
{
    if (x)
        printf("hi");
    else
        printf("how are u");
}
Discuss
Answer & Solution
Answer: Option B
Solution:
#include <stdio.h>: This line includes the standard input/output library, which is needed for the printf function.
int x;: This declares an integer variable named x. Crucially, x is declared globally but *not* initialized. This means it will have a default value.
void main(): This is the main function where the program execution begins.
if (x): This is the core of the question. The if statement checks the value of x. In C, any non-zero value is treated as true, and zero is treated as false.
printf("hi");: This line will be executed if the condition x is true.
else printf("how are u");: This line will be executed if the condition x is false.

Now, the key point is that since x is a global variable and is not explicitly initialized, it's automatically initialized to 0 by the C compiler.
Therefore, the condition if (x) is equivalent to if (0), which is false.
So, the else block will be executed, and the program will print "how are u".

Therefore, the correct answer is Option B: how are you
12
What will be the output of the following C code?
#include <stdio.h>
int main()
{
    int i = 0;
    for (foo(); i == 1; i = 2)
        printf("In for loop\n");
        printf("After loop\n");
}
int foo()
{
    return 1;
}
Discuss
Answer & Solution
Answer: Option A
No explanation is given for this question. Let's Discuss on Board
13
What will be the output of the following C code?
#include <stdio.h>
int main()
{
    float f = 1;
    switch (f)
    {
       case 1.0:
          printf("yes\n");
          break;
       default:
          printf("default\n");
    }
}
Discuss
Answer & Solution
Answer: Option D
No explanation is given for this question. Let's Discuss on Board
14
What will be the output of the following C code?
#include <stdio.h>
int main()
{
    int i = 0;
    char c = 'a';
    while (i < 2)
    {
        i++;
        switch (c) 
        {
           case 'a':
               printf("%c ", c);
               break;
               break;
        }
    }
    printf("after loop\n");
}
Discuss
Answer & Solution
Answer: Option B
No explanation is given for this question. Let's Discuss on Board
15
What will be the output of the following C code?
#include <stdio.h>
int main()
{
    int a = 1;
    if (a--)
        printf("True");
        if (a++)
            printf("False");
}
Discuss
Answer & Solution
Answer: Option A
No explanation is given for this question. Let's Discuss on Board
16
What will be the output of the following C code?
#include <stdio.h>
int main()
{
    printf("%d ", 1);
    goto l1;
    printf("%d ", 2);
}
void foo()
{
    l1: printf("3 ", 3);
}
Discuss
Answer & Solution
Answer: Option D
No explanation is given for this question. Let's Discuss on Board
17
What will be the output of the following C code?
#include <stdio.h>
void main()
{
    char *str = "";
    do
    {
        printf("hello");
    } while (str);
}
Discuss
Answer & Solution
Answer: Option D
No explanation is given for this question. Let's Discuss on Board
18
What will be the output of the following C code?
#include <stdio.h>
void main()
{
    int i = 0;
    for (i = 0;i < 5; i++)
        if (i < 4)
        {
            printf("Hello");
            break;
        }
}
Discuss
Answer & Solution
Answer: Option C
No explanation is given for this question. Let's Discuss on Board
19
Which of the following is an invalid if-else statement?
Discuss
Answer & Solution
Answer: Option A
No explanation is given for this question. Let's Discuss on Board
20
What will be the output of the following C code?
#include <stdio.h>
int main()
{
    short i;
    for (i = 1; i >= 0; i++)
        printf("%d\n", i);

}
Discuss
Answer & Solution
Answer: Option C
No explanation is given for this question. Let's Discuss on Board