1.
What will be output if you will compile and execute the following c code?
#include<stdio.h>
int main(){
register int i,x;
scanf("%d",&i);
x=++i + ++i + ++i;
printf("%d",x);
return 0;
}
Correct Answer
E. Compiler Error
Explanation
The code will result in a compiler error. The reason for this is the use of the "register" keyword before the variable declaration. In modern C programming, the "register" keyword is considered obsolete and is no longer necessary. Some compilers may still support it, but in this case, it causes a conflict with the use of the "++" operator multiple times on the same variable in the expression "++i + ++i + ++i". This results in undefined behavior and a compiler error.
2.
What will be output if you will compile and execute the following c code?
#include<stdio.h>
int main(){
int a=5;
int b=10;
{
int a=2;
a++;
b++;
}
printf("%d %d",a,b);
return 0;
}
Correct Answer
C. 5 11
Explanation
The code declares two variables, a and b, and initializes them with the values 5 and 10 respectively. Then, inside a block of code, a new variable a is declared and initialized with the value 2. The value of this inner a is then incremented by 1, but it does not affect the value of the outer a. Similarly, the value of b is incremented by 1. Finally, the values of the outer a and b are printed, which are 5 and 11 respectively.
3.
What will be output if you will compile and execute the following c code?
#include<stdio.h>
int main(){
float f=3.4e39;
printf("%f",f);
return 0;
}
Correct Answer
C. +INF
Explanation
The code declares a variable 'f' of type float and assigns it the value 3.4e39, which is a very large number. The printf function is then used to print the value of 'f'. Since the value of 'f' exceeds the maximum representable value for a float, it will be represented as positive infinity (+INF) when printed.
4.
What will be output if you will compile and execute the following c code?
#include<stdio.h>
int main(){
enum color{
RED,GREEN=-20,BLUE,YELLOW
};
enum color x;
x=YELLOW;
printf("%d",x);
return 0;
}
Correct Answer
B. -18
Explanation
The code defines an enumeration called "color" with four possible values: RED, GREEN, BLUE, and YELLOW. The values are assigned implicitly, with RED being 0, GREEN being -20, BLUE being -19, and YELLOW being -18. The variable "x" is declared as an enum color and is assigned the value YELLOW. The printf statement then prints the value of "x", which is -18.
5.
What will be output if you will compile and execute the following c code?
#include<stdio.h>
int main(){
asm{
mov bx,8;
mov cx,10
add bx,cx;
}
printf("%d",_BX);
return 0;
}
Correct Answer
A. 18
Explanation
The given C code uses inline assembly to perform addition of the values stored in the registers bx and cx. The result, 18, is then printed using the printf function. Therefore, the output of the code will be 18.
6.
What will be output if you will compile and execute the following c code?
#include<stdio.h>
int main(){
enum xxx{
a,b,c=32767,d,e
};
printf("%d",b);
return 0;
}
Correct Answer
D. Compiler error
Explanation
The code will not compile because the enum value "b" is not assigned a value.
7.
What will be output if you will compile and execute the following c code?
#include<stdio.h>
int main(){
char *url="c:\tc\bin\rw.c";
printf("%s",url);
return 0;
}
Correct Answer
E. W.c in
Explanation
The given code declares a character pointer variable named "url" and assigns it the value "c:\tc\bin\rw.c". The printf statement then prints the value of "url", which is "c:\tc\bin\rw.c". Therefore, the output of the code will be "c:\tc\bin\rw.c".
8.
What will be output if you will compile and execute the following c code?
#include<stdio.h>
int main(){
goto abc;
printf("main");
return 0;
}
void dispaly(){
abc:
printf("display");
}
Correct Answer
E. Compiler error
Explanation
The code will result in a compiler error because the label "abc" is defined within the function "dispaly()" but it is being used outside of that function in the "main()" function. Labels can only be used within the function they are defined in.
9.
What is the final value of x when the code int x; for(x=0; x<10; x++) {} is run?
Correct Answer
A. 10
Explanation
This code uses a for loop to increment the value of x. Here's how it works:
Initialization: x = 0; The variable x is initialized to 0.
Condition: x < 10; The loop continues as long as x is less than 10.
Increment: x++ After each iteration of the loop, x is incremented by 1.
The loop runs 10 times, with x taking on the values 0, 1, 2, 3,...9. After the 10th iteration, x becomes 10, which fails the condition x < 10, causing the loop to terminate. Therefore, the final value of x is 10.
10.
What will be output if you will compile and execute the following c code?
#include<stdio.h>
void call(int,int,int);
int main(){
int a=10;
call(a,a++,++a);
return 0;
}
void call(int x,int y,int z){
printf("%d %d %d",x,y,z);
}
Correct Answer
B. 12 11 12
Explanation
This code snippet demonstrates the behavior of post-increment (a++) and pre-increment (++a) operators in C, along with the order of evaluation of function arguments.
Pre-increment (++a): The value of a is incremented before it's used in the expression.
Post-increment (a++): The value of a is incremented after it's used in the expression.
Order of evaluation:
While the order of evaluation of function arguments in C is unspecified, in this particular case, it seems the compiler evaluates them from right to left. Here's the breakdown:
++a: a is incremented to 11, then to 12. This 12 is passed as the third argument (z).
a++: The current value of a (12) is passed as the second argument (y). Then, a is incremented to 13 (but this value is unused).
a: The current value of a (12) is passed as the first argument (x).
Therefore, the call function receives the values 12, 11, and 12 for x, y, and z respectively, resulting in the output "12 11 12".