1.
Predict the output of following program. Assume that the numbers are stored in 2's complement form.
#include<stdio.h>
int main()
{
unsigned int x = -1;
int y = ~0;
if(x == y)
printf("same");
else
printf("not same");
return 0;
}
Correct Answer
A. Same
Explanation
The program initializes an unsigned integer variable "x" with the value -1. In 2's complement form, -1 is represented as a sequence of all 1's. The program also initializes an integer variable "y" with the bitwise complement of 0, which is also a sequence of all 1's. In the if statement, it checks if "x" is equal to "y". Since both "x" and "y" have the same value of all 1's, the condition is true and the program prints "same". Therefore, the output of the program is "Same".
2.
Which of the following is not a valid declaration in C?1. short int x; 2. signed short x; 3. short x; 4. unsigned short x;
Correct Answer
D. All are valid
Explanation
All of the given options are valid declarations in C. Option 1 declares a short integer variable named x. Option 2 declares a signed short integer variable named x. Option 3 declares a variable named x of type short. Option 4 declares an unsigned short integer variable named x. Therefore, all the given options are valid declarations in C.
3.
Predict the output#include <stdio.h> int main() { float c = 5.0; printf ("Temperature in Fahrenheit is %.2f", (9/5)*c + 32); return 0; }
Correct Answer
B. Temperature in Fahrenheit is 37.00
Explanation
The code is calculating the temperature in Fahrenheit using the formula (9/5)*c + 32, where c is the temperature in Celsius. In this case, c is initialized to 5.0. The expression (9/5)*c + 32 is evaluated as (1)*5.0 + 32, which results in 37.00. Therefore, the output is "Temperature in Fahrenheit is 37.00".
4.
Predict the output of following C program#include <stdio.h> int main() { char a = '\012'; printf("%d", a); return 0; }
Correct Answer
C. 10
Explanation
The program is using the format specifier "%d" in the printf() function to print the value of the variable 'a'. The variable 'a' is assigned the character '\012', which is an octal representation of the decimal value 10. Therefore, the output of the program will be 10.
5.
In C, sizes of an integer and a pointer must be same.
Correct Answer
B. False
Explanation
In C, the sizes of an integer and a pointer are not required to be the same. The size of an integer is typically 4 bytes, while the size of a pointer can vary depending on the system architecture. On a 32-bit system, the size of a pointer is usually 4 bytes, while on a 64-bit system, it is typically 8 bytes. Therefore, the statement that the sizes of an integer and a pointer must be the same is false.
6.
Output?int main() { void *vptr, v; v = 0; vptr = &v; printf("%v", *vptr); getchar(); return 0; }
Correct Answer
B. Compiler error
Explanation
The given code will result in a compiler error. This is because the format specifier used in the printf statement is incorrect. The correct format specifier for printing a void pointer is "%p", not "%v". Since the code is trying to print the value of a void pointer using an incorrect format specifier, it will result in a compiler error.
7.
Assume that the size of char is 1 byte and negatives are stored in 2's complement form#include<stdio.h> int main() { char c = 125; c = c+10; printf("%d", c); return 0; }
Correct Answer
C. -121
Explanation
In this code, the variable 'c' is declared as a char with an initial value of 125. Then, 'c' is assigned a new value by adding 10 to its current value. However, since the range of a char in 2's complement form is -128 to 127, adding 10 to 125 will result in an overflow. This means that the value will wrap around to the minimum value in the range, which is -128. Therefore, the final value of 'c' is -121.
8.
#include <stdio.h> int main() { if (sizeof(int) > -1) printf("Yes"); else printf("No"); return 0; }
Correct Answer
B. No
Explanation
The code checks if the size of an integer is greater than -1. Since the size of an integer is always a positive value, the condition will always be true. Therefore, "Yes" will never be printed and "No" will always be printed.
9.
Output of following program?#include<stdio.h> int main() { float x = 0.1; if ( x == 0.1 ) printf("IF"); else if (x == 0.1f) printf("ELSE IF"); else printf("ELSE"); }
Correct Answer
A. ELSE IF
Explanation
The output of the program is "ELSE IF". This is because the variable x is declared as a float and assigned the value 0.1. However, the if statement checks if x is equal to 0.1, which is a double precision value. Since the float value 0.1 is not exactly equal to the double value 0.1, the condition in the if statement evaluates to false. The else if statement checks if x is equal to 0.1f, which is the float value of 0.1. Since x is equal to 0.1, the condition in the else if statement evaluates to true and the corresponding printf statement is executed, resulting in the output "ELSE IF".