1.
What will be output if you will compile and execute the following c code?
void main()
{ float a=5.2;
if(a==5.2)
printf("Equal");
else if(a
Correct Answer
A. Less than
Explanation
The code will output "Less than" because the value of "a" is not exactly equal to 5.2 due to floating point precision.
2.
What will be output if you will compile and execute the following c code?
void main()
{
int a=2;
if(a==2)
{ a=~a+2
Correct Answer
D. Compilation error
Explanation
The code will result in a compilation error. This is because the line "a=~a+2" is not a valid expression in C. The "~" operator is the bitwise complement operator, which inverts the bits of a value. However, it cannot be used with the addition operator "+". Therefore, the code will fail to compile.
3.
What will be output if you will compile and execute the following c code?
void main()
{
int i=10;
static int x=i;
if(x==i)
printf("Equal");
else if(x>i)
printf("Greater than");
else
printf("Less than");
}
Correct Answer
B. Equal
Explanation
The code declares a variable i with a value of 10 and a static variable x with the value of i. The if statement checks if x is equal to i, and if so, it prints "Equal". Since the value of x is assigned as i, which is 10, the condition is true and "Equal" is printed as the output.
4.
What will be output if you will compile and execute the following c code?
main()
{
float me = 1.1;
double you = 1.1;
if(me==you)
printf("I love U");
else
printf("I hate U");
}
Correct Answer
B. I hate U
Explanation
The output will be "I hate U" because the float data type has less precision than the double data type. Although both "me" and "you" are assigned the same value of 1.1, the float data type may not be able to accurately represent the number and may have a slight difference in value compared to the double data type. Therefore, the condition "me==you" will evaluate to false and the program will execute the else statement, printing "I hate U".
5.
What will be output if you will compile and execute the following c code?
main()
{
static int var = 5;
printf("%d ",var--);
if(var)
main();
}
Correct Answer
C. 5 4 3 2 1
Explanation
The code defines a static variable "var" with an initial value of 5. It then prints the value of "var" and decrements it by 1. After that, it checks if "var" is still non-zero and if so, it calls the "main" function recursively. This process continues until "var" becomes 0. Therefore, the output will be 5 4 3 2 1.
6.
What will be output if you will compile and execute the following c code?
main()
{
int i=1;
while (i2)
goto here;
i++; }}
fun()
{
here: printf("PP");
}
Correct Answer
C. Compiler Error
Explanation
The code will result in a compiler error because the function "fun" is defined after it is being used in the "goto" statement. In C, functions need to be declared or defined before they are used. Since the function "fun" is not declared or defined before the "goto" statement, the compiler will throw an error.
7.
What will be output if you will compile and execute the following c code?
#define FALSE -1
#define TRUE 1
#define NULL 0
main()
{
if(NULL)
puts("NULL");
else if(FALSE)
puts("TRUE");
else
puts("FALSE");
}
Correct Answer
A. 1
Explanation
The output of this code will be "TRUE". In C, any non-zero value is considered as true and zero is considered as false. In this code, the condition `NULL` is equivalent to `0`, which is false. The condition `FALSE` is equivalent to `-1`, which is true. Therefore, the second condition is true and the code will output "TRUE".
8.
What will be output if you will compile and execute the following c code?
#include
int x;
void main()
{
if (x)
printf("hi");
else
printf("how are u");
}
Correct Answer
B. How are you
Explanation
The output of the given C code will be "how are you". This is because the variable "x" is not initialized, so its value is undefined. In C, any non-zero value is considered as true and zero is considered as false. Since the value of "x" is not explicitly set, it will have an indeterminate value which is treated as false. Therefore, the code will execute the "else" block and print "how are you".
9.
Predict the output/error.
main()
{
int a = 4, b = 4;
switch(a)
{
case 3 : printf(“\n a is 3”);
break;
case b : printf(“\n a is 4”); // case b : is not possible break; // because constant is needed
default : printf(“\n a is not 3 or 4”);
}
return 0;
}
Correct Answer
A. Compilation error
Explanation
The given code will result in a compilation error. This is because the switch statement in C requires constant expressions as case labels. In this code, the case label "b" is not a constant expression as it is a variable. Therefore, the code will not compile.
10.
The output of the code below is(when 1 is entered)
#include
void main()
{
double ch;
printf("enter a value btw 1 to 2:");
scanf("%lf", &ch);
switch (ch) // ch is declared as duble, but switch will accept
{ // always an integral value
case 1: printf("1");
break;
case 2: printf("2");
break;
}
}
Correct Answer
A. Compile time error
Explanation
The code will result in a compile time error because the switch statement requires an integral value, but the variable "ch" is declared as a double.
11.
What will be output if you will compile and execute the following c code?
void main()
{
int a=2;
if(a==2)
{
a=~a+2
Correct Answer
A. It will print nothing
Explanation
The code snippet declares a variable 'a' and assigns it the value 2. It then checks if 'a' is equal to 2. Since 'a' is indeed equal to 2, the if statement is true. Inside the if statement, the bitwise complement operator (~) is applied to 'a' and then 2 is added to the result. The bitwise complement of 2 is -3 in two's complement representation. Therefore, the expression evaluates to -3 + 2, which is -1. However, there is no code to print or output this value, so the program will not display anything.
12.
The output of the following code will be
main()
{
int y;
scanf("%d",&y); // input given is 2000
if( (y%4==0 && y%100 != 0) || y%100 == 0 )
printf("%d is a leap year");
else
printf("%d is not a leap year");
}
Correct Answer
A. 2000 is a leap year
Explanation
The output of the code will be "2000 is a leap year" because the condition in the if statement is true. The year 2000 is divisible by 4 and 100, so the first part of the condition is true. Since the first part is true, the overall condition is true and the code will execute the printf statement that says "2000 is a leap year".
13.
The output of the following code will be
main()
{
char c=' ', x, convert(z);
getc(c);
if((c>='a') && (c
Correct Answer
C. Compiler error
Explanation
The code snippet provided has several syntax errors. The function `getc()` is being used without any arguments, which is incorrect. Additionally, the function `convert()` is being called with an undefined variable `z`. These errors will result in a compiler error.
14.
The output of the following code will be
main()
{
int i =0;j=0;
if(i && j++)
printf("%d..%d",i++,j);
printf("%d..%d,i,j);
}
Correct Answer
B. 0..0
Explanation
The output of the code will be "0..0".
In the code, the variable i is initialized to 0 and j is not declared properly. Therefore, it will cause a compilation error. However, assuming that it is a typo and j is intended to be declared as int j = 0, the if condition i && j++ will evaluate to false since i is 0. Therefore, the printf statement inside the if condition will not be executed. The final printf statement will print the values of i and j, which are both 0. Hence, the output will be "0..0".
15.
The output of the following code will be
main()
{
int a= 0;int b = 20;char x =1;char y =10;
if(a,b,x,y)
printf("hello");
}
Correct Answer
A. Hello
Explanation
The code will output "hello". The if statement in the code is using the comma operator, which evaluates all the expressions but only returns the result of the last expression. In this case, the last expression is "y", which has a value of 10. Since the value of "y" is not 0, the if statement evaluates to true and the printf statement is executed, resulting in the output "hello".
16.
The output of the following code will be
void main()
{
static int i=5;
if(--i){
main();
printf("%d ",i);
}
Correct Answer
C. 0 0 0 0
Explanation
The output of the code will be "0 0 0 0".
The code uses a recursive function main() that is called multiple times. The static variable i is initially set to 5. In each recursive call, the value of i is decremented by 1 before the if condition is checked. Since i is initially 5, the condition (--i) will be true for the first 5 recursive calls.
Inside each recursive call, the main() function is called again, resulting in more recursive calls.
Once i becomes 0, the if condition evaluates to false and the recursive calls stop.
After that, each recursive call prints the value of i, which will be 0 in all cases.
Therefore, the output will be "0 0 0 0".
17.
The output of the following code will be
void main()
{
int i;
char a[]="\0";
if(printf("%s\n",a))
printf("Ok here \n");
else
printf("Forget it\n"); }
Correct Answer
A. OK here
Explanation
The output of the code will be "OK here". This is because the if statement checks the result of the printf function, which prints the string stored in the variable "a". Since the string is empty ("\0"), the printf function will return 0, which is considered false in the if statement. Therefore, the else block will not be executed and the code will print "OK here".
18.
The output of the following code will be
void main()
{
while(1){
if(printf("%d",printf("%d")))
break;
else
continue; }
Correct Answer
A. Garbage value
Explanation
The output of the code will be a garbage value. This is because the inner printf statement will print the number of characters printed, which is the return value of the outer printf statement. Since the outer printf statement does not have any arguments, it will return 0, which will be printed by the inner printf statement. However, the value of 0 does not have any significance in terms of printing a meaningful value, hence it is considered as a garbage value.
19.
The output of the following code will be
#include
main()
{
int x,y=2,z,a;
if(x=y%2) z=2;
a=2;
printf("%d %d ",z,x);
}
Correct Answer
D. Garbage 0
Explanation
The output of the code will be "Garbage 0". This is because the variable "x" is not initialized, so its value is undefined. When the expression "x=y%2" is evaluated, the value of "y" is 2 and the remainder of 2 divided by 2 is 0. Therefore, "x" will be assigned the value 0. Since "z" is not assigned a value before the printf statement, its value will be garbage or undefined. The final output will be "Garbage 0".
20.
The output of the following code will be
main()
{
int i=10,j=20;
j = i, j?(i,j)?i:j:j;
printf("%d %d",i,j);
}
Correct Answer
B. 10 10
Explanation
The output of the code will be "10 10". This is because the expression "j = i, j?(i,j)?i:j:j" can be broken down as follows:
- First, the value of i (which is 10) is assigned to j.
- Then, the ternary operator is evaluated. Since j is not equal to 0 (which is considered as false), the expression "(i,j)?i:j" is evaluated.
- In this expression, the comma operator is used. The comma operator evaluates both expressions and returns the value of the second expression. So, the value of (i,j) is 10.
- Since the value of (i,j) is not equal to 0, the ternary operator returns the value of i, which is 10.
- Finally, the values of i and j are printed, which are both 10.