1.
1.What will be output if you will execute following c code?
#include
intmain(){
inti;
for(i=0;i<5;i++){
inti=10;
printf(" %d",i);
i++;
}
return0;
}
Correct Answer
B. 10 10 10 10 10
Explanation
The code will output "10 10 10 10 10".
In the for loop, the variable "i" is initialized to 0 and incremented by 1 in each iteration. However, inside the loop, the variable "i" is re-initialized to 10 in each iteration. Therefore, when the loop is executed 5 times, the value of "i" will always be 10.
The printf statement inside the loop will print the value of "i" in each iteration, resulting in the output "10 10 10 10 10".
2.
2.What will be output if you will execute following c code?
#include
intmain(){
registera,b,x;
scanf("%d %d",&a,&b);
x=a+~b;
printf("%d",x);
return0;
}
Correct Answer
E. Compilation error
Explanation
The given code will result in a compilation error. This is because there is a syntax error in the code. The line "intmain()" should be changed to "int main()". The keyword "int" is missing a space before "main". Once this error is fixed, the code should compile and run correctly.
3.
3.What will be output if you will execute following c code?
#include
autointa=5;
intmain(){
intx;
x=~a+a&a+a<
Correct Answer
E. Compilation error
4.
4.What will be output if you will execute following c code?
#include
voidmain(){
registerinta,b;
intc;
scanf("%d%d",&a,&b);
c=~a + ~b + ++a + b++;
printf(" %d",c);
}
//User input is: 1 2
Correct Answer
E. Compilation error
Explanation
The given code will result in a compilation error. This is because the main function is declared as "voidmain()" instead of "int main()". The correct declaration for the main function in C is "int main()".
5.
5.What will be output if you will execute following c code?
#include
#include
voidmain(){
intarr[3]={10,20,30};
intx=0;
x=++arr[++x]+ ++x+arr[--x];
printf("%d ",x);
}
Correct Answer
E. Compilation error
Explanation
The code will result in a compilation error because the "voidmain()" function is not a valid function declaration. The correct syntax should be "int main()".
6.
6.What will be output if you will execute following c code?
#include
#include
voidmain(){
inta[]={10,20,30,40};
inti=3,x;
x=1*a[--i]+2*a[--i]+3*a[--i];
printf("%d",x);
}
Correct Answer
A. 60
Explanation
The code initializes an array "a" with values 10, 20, 30, and 40. It then initializes "i" to 3 and "x" to 0. The expression "x=1*a[--i]+2*a[--i]+3*a[--i];" is evaluated from left to right. The first "--i" decrements "i" to 2, so it becomes "x=1*a[2]+2*a[--i]+3*a[--i];". Then, "--i" decrements "i" to 1, so it becomes "x=1*a[2]+2*a[1]+3*a[--i];". Finally, "--i" decrements "i" to 0, so it becomes "x=1*a[2]+2*a[1]+3*a[0];". The values of "a[2]", "a[1]", and "a[0]" are 30, 20, and 10 respectively. Evaluating the expression gives "x=1*30+2*20+3*10", which equals 60. Therefore, the output will be 60.
7.
7.What will be output if you will execute following c code?
#include
#include
voidmain(){
staticinta[][2][3]={0,1,2,3,4,5,6,7,8,9,10,11,12};
inti=-1;
intd;
d=a[i++][++i][++i];
printf("%d",d);
}
Correct Answer
B. 10
Explanation
The code initializes a 3-dimensional static array `a` with values 0 to 12. Then, it declares an integer variable `i` and initializes it with -1. After that, it declares another integer variable `d` without initializing it. The value of `d` is assigned as `a[i++][++i][++i]`.
The order of evaluation of the operands in this expression is unspecified, meaning it is not guaranteed which operand is evaluated first. In this case, the behavior is undefined because the value of `i` is modified multiple times between sequence points. This leads to undefined behavior, and the output cannot be predicted. Therefore, the correct answer is "Compilation error".
8.
8.What will be output if you will execute following c code?
#include
#include
voidmain(){
inti=3,val;
val=sizeof(f(i)+ +f(i=1)+ +f(i-1));
printf("%d %d",val,i);
}
intf(intnum){
returnnum*5;
}
Correct Answer
C. 2 3
Explanation
The code is using the sizeof operator to calculate the size of the expression f(i) + + f(i=1) + + f(i-1). In this expression, i is being used in multiple places, including as a parameter for the function f(). The function f() takes an integer parameter and returns the parameter multiplied by 5.
In the expression, f(i) will be replaced by f(3), f(i=1) will be replaced by f(1), and f(i-1) will be replaced by f(2).
So, the expression becomes 3*5 + 1*5 + 2*5, which is equal to 15 + 5 + 10, which is equal to 30.
Therefore, the value of val will be 30.
The value of i is not changed after the expression, so it remains as 3.
Hence, the output will be "30 3".
9.
9.What will be output if you will execute following c code?
#include
#include
voidmain(){
intx,a=3;
x=+ +a+ + +a+ + +5;
printf("%d %d",x,a);
}
Correct Answer
B. 11 3
Explanation
The output of the code will be "11 3". This is because the expression "x=+ +a+ + +a+ + +5" can be simplified as "x = +a + +a + +5", which further simplifies to "x = a + a + 5". Since the initial value of "a" is 3, the expression evaluates to "x = 3 + 3 + 5", resulting in "x = 11". The value of "a" remains unchanged at 3.
10.
10.What will be output if you will execute following c code?
#include
#include
voidmain(){
intnum,i=0;
num=-++i+ ++-i;
printf("%d",num);
}
Correct Answer
E. Compilation error
Explanation
The given code will result in a compilation error. This is because the code contains syntax errors. The main function is missing the return type and the opening and closing braces are not properly placed. Additionally, there is an incorrect use of the increment and decrement operators in the expression assigned to 'num'.
11.
11.What will be output if you will execute following c code?
#include
#include
voidmain(){
intnum,a=5;
num=-a--+ +++a;
printf("%d %d",num,a);
}
Correct Answer
D. 0 5
Explanation
The code snippet initializes the variable "a" with a value of 5. The expression "num=-a--+ +++a" is then evaluated. In this expression, the postfix decrement operator "--" is applied to "a", followed by the prefix increment operator "+++" applied to "a".
The postfix decrement operator decrements the value of "a" by 1, resulting in "a" being equal to 4.
The prefix increment operator increments the value of "a" by 1, resulting in "a" being equal to 5 again.
The expression "num=-a--+ +++a" can now be simplified to "num = -4 + 6", which evaluates to "num = 2".
Therefore, the output of the printf statement will be "2 5".
12.
12.What will be output if you will execute following c code?
#include
#include
voidmain(){
intnum,a=15;
num=- - - -a--;
printf("%d %d",num,a);
}
Correct Answer
D. 15 14
Explanation
The given code snippet is written in C language. It declares a variable 'num' and initializes 'a' with a value of 15. The line 'num=- - - -a--;' is a bit confusing due to the multiple negative signs. However, the '--' operator is a post-decrement operator, which means it will decrement the value of 'a' after the assignment. The multiple negative signs cancel each other out, resulting in a positive value. So, 'num' will be assigned the value of 15, and 'a' will be decremented to 14. Therefore, the output will be "15 14".
13.
13.What will be output if you will execute following c code?
#include
#include
voidmain(){
intx,a=2;
x=++a,++a,a++;
printf("%d %d",x,a);
}
Correct Answer
B. 3 5
Explanation
The code snippet increments the value of 'a' three times before assigning it to 'x'. Since 'x' is assigned the value of 'a' after the first increment, the value of 'x' will be 3. After that, 'a' is incremented twice more, resulting in a final value of 5. Therefore, the output of the code will be "3 5".
14.
14.What will be output if you will execute following c code?
#include
#include
voidmain(){
intx,i=2;
x=~-!++i;
printf("%d",x);
}
Correct Answer
C. 0
Explanation
The code snippet first increments the value of i by 1, making it 3. Then, it applies the logical NOT operator (!) to i, which will result in 0 since i is not equal to 0. Next, it applies the bitwise NOT operator (~) to the result of the logical NOT operation, resulting in -1. Finally, the value of x is assigned to -1. When the printf function is called, it prints the value of x, which is -1. Therefore, the correct output is -1.
15.
15.What will be output if you will execute following c code?
#include
intmain(){
staticdouble*p,*q,*r,*s,t=5.0;
double**arr[]={&p,&q,&r,&s};
inti;
*p=*q=*r=*s=t;
for(i=0;i<4;i++)
printf("%.0f ",**arr[i]);
return0;
}
Correct Answer
A. 5 5 5 5 5
Explanation
The code declares four static double pointers p, q, r, and s and initializes them to the same value t = 5.0. It then creates an array of double pointers arr[] and assigns the addresses of p, q, r, and s to it. The code then uses a for loop to iterate through the arr[] array and prints the dereferenced value of each pointer, which is 5.0 for all four pointers. Therefore, the output will be "5 5 5 5 5".
16.
16.What will be output if you will execute following c code?
intmain(){
floatx;
x=0.35==3.5/10;
printf("%f",x);
return0;
}
Correct Answer
B. 1.000000
Explanation
The output of the code will be 1.000000. This is because the expression "0.35==3.5/10" is comparing the value of 0.35 with the value of 3.5/10, which is also 0.35. Since the comparison is true, the result of the comparison is 1, which is then assigned to the variable x. When the value of x is printed using printf, it will be displayed as 1.000000.
17.
17.What will be output if you will execute following c code?
#include
#include
voidmain(){
intarr[]={6,12,18,24};
intx=0;
x=arr[1]+(arr[1]=2);
printf("%d",x);
}
Correct Answer
A. 4
Explanation
The code snippet is trying to assign the value 2 to `arr[1]` and then add it to the previous value of `arr[1]`. However, this code is not valid and will result in undefined behavior. The order of evaluation of expressions is not guaranteed in C, so it is not clear whether the assignment or the addition will be performed first. Therefore, the output of this code cannot be determined and it may vary depending on the compiler and platform being used.
18.
18.What will be output if you will execute following c code?
#include
#include
voidmain(){
inta=1,x;
x=sq(++a)+sq(a++)+sq(a++);
printf("%d",x);
}
intsq(intnum){
returnnum*num;
}
Correct Answer
C. 17
Explanation
The code snippet is calculating the value of x by calling the sq function three times with different values of a. The sq function calculates the square of the input number.
In the given code, the value of a is incremented three times in the statement x=sq(++a)+sq(a++)+sq(a++). The value of a is incremented twice as a++ is a post-increment operation.
So, the value of a will be 4 when the sq function is called the first time, 4 when called the second time, and 3 when called the third time.
Therefore, the output will be 17, which is the sum of the squares of 4, 4, and 3.
19.
19.What will be output if you will execute following c code?
voidmain(){
printf("%c",*"abcde");
}
Correct Answer
A. Acbcd
Explanation
The given C code will output "acbcd". This is because the printf function is used to print a character, specified by the %c format specifier. The argument passed to printf is "*abcde", which is a pointer to the string "abcde". When the pointer is dereferenced with *, it gives the value at the memory location it points to, which is 'a'. So, the first character 'a' is printed. Then, the pointer is incremented to point to the next character 'b', and this process continues until all the characters in the string are printed, resulting in "acbcd" as the output.
20.
20.What will be output if you will execute following c code?
voidmain(){
printf("%d","abcde"-"abcde");
}
Correct Answer
B. -1
Explanation
The given code is trying to subtract two string literals "abcde" from each other and then print the result using the format specifier "%d" which is used for integers. Since string literals are represented as arrays of characters, subtracting two string literals will result in a pointer arithmetic operation, which is not valid. Therefore, the code will result in a compilation error.