1.
What is the output of this C code?
#include
void main()
{
int x=1,y=0,z=5;
int a=x&&y||z++;
printf("%d",z);
}
Correct Answer
A. 6
Explanation
The output of this C code is 6. This is because the logical AND operator (&&) has higher precedence than the logical OR operator (||). Therefore, the expression "x && y" is evaluated first. Since x is 1 and y is 0, the result of this expression is 0. Then, the logical OR operator is applied to the result of "x && y" and z++. Since the left operand is 0, the right operand (z++) is evaluated. This increments the value of z from 5 to 6. Finally, the result of the logical OR operation is 1, which is assigned to the variable "a". However, the value of "a" is not printed, and instead, the value of z is printed, which is 6.
2.
What is the output of this C code?
#include
void main()
{
int x=1,z=3;
int y=x<<3;
printf("%d\n",y);
}
Correct Answer
C. 8
Explanation
x = 1: This initializes the variable x to 1.
y = x << 3: The << operator is the left shift operator, which shifts the bits of x to the left by 3 positions.
Since x = 1, in binary, it is represented as 0001.
Shifting 0001 left by 3 positions results in 1000, which is 8 in decimal.
printf("%d\n", y);: This will print the value of y, which is 8.
3.
What will be the output?
void main()
{
i=0x10+010+10;
printf("\nx=%x",i);
}
Correct Answer
B. X=22
Explanation
The C code snippet calculates the value of i by summing three numbers expressed in different numeral systems:
0x10: This is a hexadecimal (base-16) representation which equals 16 in decimal.
010: This is an octal (base-8) representation which equals 8 in decimal.
10: This is a decimal representation which is 10.
Adding these values together: 16+8+10=3416+8+10=34.
The printf function is then used to output this decimal value (34) as a hexadecimal number:
%x format specifier in printf converts the decimal number 34 to its hexadecimal equivalent, which is 22.
Thus, the output printed to the screen will be x=22. This reflects how different numeral systems are interpreted and displayed in C programming.
4.
Int i=20; Printf(“%X”,i); What is the output?
Correct Answer
C. 14
Explanation
The code uses the format specifier "%X" in the printf function, which is used to print an integer in hexadecimal format. The variable "i" has a value of 20, which is equivalent to 0x14 in hexadecimal. Therefore, the output of the code will be "14".
5.
What will be the size of the following union declaration?
Union test
{
int x;
char y;
float z;
};
Correct Answer
B. 4
Explanation
To determine the size of the given union declaration, you need to find the size of the largest member, as the size of a union is determined by the size of its largest member.
In the provided union declaration:
int x typically requires 4 bytes (assuming a 32-bit system).
char y usually requires 1 byte.
float z typically requires 4 bytes.
Therefore, the size of the union would be the size of the largest member, which is float z, so the size of the union declaration would be 4 bytes.
6.
What will be the size of the following union declaration?
struct test
{
int x;
char y;
float z;
};
Correct Answer
A. 4
Explanation
The size of a union in C is determined by the size of its largest member because all members share the same memory location. Let's calculate the size of each member:
int x typically takes 4 bytes on most systems.
char y typically takes 1 byte.
float z typically takes 4 bytes.
Among these, float z is the largest member in terms of size (4 bytes).
Therefore, the size of the union struct test will be:
4 bytes
7.
What will be the output of the following C code?
#include
int main()
{
int x = 1, y = 2;
int z = x & y == 2;
printf("%d\n", z);
}
Correct Answer
B. 1
Explanation
This code snippet involves operator precedence and the bitwise AND operator (&).
First, y == 2 evaluates to 1 (true) because y is indeed 2.
Then, x & 1 performs a bitwise AND between 1 (the value of x) and 1. This results in 1.
Finally, z is assigned the value of 1.
Therefore, the output of the code will be 1.
8.
The function _________ obtains block of memory dynamically.
Correct Answer
C. Both calloc &malloc
Explanation
Both calloc and malloc are functions in C programming language that can be used to dynamically obtain a block of memory. The calloc function is used to allocate and initialize a block of memory with zeros, while the malloc function is used to allocate a block of memory without initializing its contents. Therefore, both calloc and malloc can be used to dynamically allocate memory based on the specific requirements of the program. The free function, on the other hand, is used to deallocate the previously allocated memory.
9.
What will be the output of the following C code?
#include
void main()
{
int x=0;
int *ptr=&x;
printf("%d\n",*ptr);
}
Correct Answer
B. Zero
Explanation
The given C code initializes an integer variable x with the value 0. It then declares a pointer variable ptr and assigns the address of x to it. Finally, it prints the value pointed to by ptr using the printf function. Since ptr points to the variable x, which has a value of 0, the output of the code will be "Zero".
10.
What will be the output of the C code?
#include
int main()
{
int a=1;
if(a--)
printf("True");
if(a++)
printf("False");
}
Correct Answer
A. True
Explanation
The code starts by initializing the variable a to 1. The first if statement checks if a is true (non-zero), and since a is currently 1, the condition is true and "True" is printed. After that, the value of a is decremented by 1. The second if statement then checks if a is false (zero), but since a is now 0, the condition is false and "False" is not printed. Therefore, the output of the code will be "True".
11.
For 16-bit compiler allowable range for integer constants is______?
Correct Answer
D. -32768 to 32767
Explanation
The correct answer is -32768 to 32767. In a 16-bit compiler, the range for integer constants is determined by the number of bits available for storing the value. In this case, with 16 bits, the range is from -32768 to 32767. This is because the most significant bit is used to represent the sign of the number, allowing for one additional negative value compared to the positive values.
12.
Python program
s='python'print(s*2)print(s*0)print(s*-2)
Correct Answer
C. Pythonpython
Explanation
The given Python program declares a variable 's' and assigns it the value 'python'. It then prints the value of 's' multiplied by 2, which results in 'pythonpython'. Next, it tries to print the value of 's' multiplied by 0, which would result in an empty string since any number multiplied by 0 is 0. Finally, it tries to print the value of 's' multiplied by -2, which also results in 'pythonpython' since multiplying a string by a negative number simply repeats the string. Therefore, the correct answer is 'pythonpython'.
13.
Print(max('quiz2code')
Correct Answer
C. Z
Explanation
The correct answer is "z" because the max() function returns the maximum value in a sequence. In this case, the sequence is the string "quiz2code". The maximum value in this sequence is "z" because "z" comes after all the other characters in the alphabet.
14.
#include
int main()
{
int i = 1;
if (i++ && (i == 1))
printf("No\n");
else
printf("Yes\n");
}
Correct Answer
A. Yes
Explanation
The given code snippet will output "Yes". This is because the condition in the if statement is evaluating the expression "i++ && (i == 1)". The post-increment operator "i++" will increment the value of i after it is evaluated in the expression. Since i initially has a value of 1, the condition "(i == 1)" will evaluate to true. Therefore, the overall condition will be true and the code inside the if statement will not be executed. Instead, the code inside the else statement will be executed, which prints "Yes".
15.
What will be the final value of J in the following C code?
#include<stdio>
int main()
{
int i=10,j=0;
if(I||(J=I+10))
//DO SOMETING
;
}
Correct Answer
A. Zero
Explanation
In the given code, the variable "i" is initialized to 10 and the variable "j" is initialized to 0. Inside the if statement, there is a logical OR operator (||) which checks the truth value of the condition "I" (which should be "i" instead, as "I" is not a valid variable). Since the value of "i" is 10, it is considered as true. Therefore, the second part of the condition, "(J=I+10)" is not evaluated. Hence, the value of "j" remains 0, resulting in the final value of "j" being zero.
16.
Waht will be the output of the following C code?
#include <stdio.h>
int main()
{
int a=2,b=0;
int y=(b==0)?a:(a>b)?(b=1):a;
printf("%d",y);
}
Correct Answer
C. Two
Explanation
The code will output "two".
The code first checks if b is equal to 0. Since it is not, it moves on to the next condition. The condition (a>b) is true since a is 2 and b is 0. Therefore, the code assigns the value 1 to b. The value of y is then assigned as the value of a, which is 2. Finally, the code prints the value of y, which is 2.
17.
What will be the output of the following C code?
#include <stdio.h>
int main()
{
if(7&8)
printf("Honest");
if((~7&0*000f)==8)
printf("is the best policy");
}
Correct Answer
B. Honest
Explanation
The first if statement checks the condition 7 & 8. In this condition, the bitwise AND operation (&) is applied to the numbers 7 and 8. The result is 0 because the binary representation of 7 is 0111, and the binary representation of 8 is 1000. When you perform a bitwise AND operation, you get 0 because there are no common set bits. So, the first if condition is false, and "Honest" is not printed.
The second if statement checks the condition (~7 & 0*000f) == 8. Here, ~7 represents the bitwise NOT of 7, which is -8 in two's complement representation. 0*000f is simply 0. When you apply the bitwise AND operation to -8 and 0, you get 0. Finally, the condition 0 == 8 is false, so "is the best policy" is not printed.
Therefore, only "Honest" is printed as the output.
18.
What will the output of the following C code be?
#include
int main()
{
int k=8;
int m=7;
k
Correct Answer
B. Compiletime error
Explanation
The given code will result in a compile-time error. This is because the code is incomplete and there is a syntax error in the line "k
". The less than symbol should be followed by a value or a variable to compare with. Since it is incomplete, the compiler will throw an error and the code will not compile successfully.
19.
Waht will be the output of this?
#include
int main()
{
int a[]={2,3};
int b[]={4,6};
if(a)
{
int a=7;
int b=8;
printf("%d",a);
}
}
Correct Answer
A. 7
Explanation
In this code, the variable "a" is declared twice - once as an array and once as an integer within the if statement. Inside the if statement, a new local variable "a" is declared and assigned the value 7. Therefore, when the printf statement is executed, it will print the value of the local variable "a" which is 7.
20.
What will be the output of the following c code?
int main()
{
int x=-2;
x=x>>1;
printf("%d\n",x);
}
Correct Answer
B. -1
Explanation
The given C code initializes the variable x with the value -2. Then, it performs a right shift operation on x by 1 bit. This operation shifts all the bits of x to the right by 1 position. Since the variable x is a signed integer, the right shift operation preserves the sign bit. In this case, the sign bit is 1 because x is negative. Therefore, the output of the code will be -1.