1.
Point out the error, if any in the for loop
#include
int main()
{
int i=1;
for(;;) {
printf("%d\n", i++);
if(i>10)
break; }
return 0;
}
Correct Answer
D. No error
Explanation
The given for loop does not have any errors. It is a valid infinite loop that will continue executing until the condition "i > 10" is met, at which point it will break out of the loop. The code will print the value of "i" and increment it by 1 in each iteration.
2.
Pick the right alternative
main()
{
int c=- -2;
while(--c)
printf("c=%d",c);
}
Correct Answer
B. C=1
Explanation
The code snippet initializes the variable c with the value - -2, which is equivalent to 2. The while loop decrements c by 1 each time it loops until c becomes 0. Since the initial value of c is 2, the loop will execute once and the value of c will become 1. Therefore, the output will be "c=1".
3.
What will be the output?
main()
{
char s[ ]="m";
int i;
for(i=0;s[ i ];i++)
printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]);
}
Correct Answer
A. Mmmm
Explanation
The given code defines a character array `s` with the value "m". It then declares an integer variable `i` and initializes it to 0.
The `for` loop iterates as long as `s[i]` is not a null character. In this case, since `s` only contains one character "m", the loop will execute once.
Inside the loop, the `printf` statement prints the characters `s[i]`, `*(s+i)`, `*(i+s)`, and `i[s]`.
In this case, all four characters will be "m" because `s[i]` is equivalent to `*(s+i)` is equivalent to `*(i+s)` is equivalent to `i[s]`, which all refer to the same memory location in the character array `s`.
Therefore, the output will be "mmmm".
4.
What main()
{
int i=0;
while(+(+i--)!=0)
i-=i++;
printf("%d",i);
}
Correct Answer
C. -1
Explanation
The code snippet starts with initializing the variable i to 0. Then, it enters a while loop where it checks if the value of +(+(i--)) is not equal to 0. The double negative sign before i-- is used to decrement the value of i by 1. However, the unary plus operator before the double negative sign does not have any effect on the value. Inside the loop, i is further decremented by 1 using i-=i++. Finally, the printf statement prints the value of i, which is -1.
5.
Void main()
{
static int i;
while(i2)?i++:i--;
printf(“%d”, i);
}
Correct Answer
A. 32767
Explanation
The given code snippet initializes the variable 'i' as a static variable. It then enters a while loop where it checks if 'i' is greater than 2. If it is, 'i' is incremented by 1, otherwise, it is decremented by 1. Since the initial value of 'i' is not specified, it can be assumed that it starts at the maximum value for a signed 16-bit integer, which is 32767. Therefore, the code will continue to increment 'i' until it reaches the maximum value, and then it will print the value of 'i', which is 32767.
6.
What is the output of this program?
void main()
{
int i=122;
while(i!=97)
i--;
while(i>65)
i--;
printf(“%c”,i);
}
Correct Answer
B. A
Explanation
The program starts with the variable i initialized to 122. It then enters a while loop that decrements i until it reaches 97. After that, it enters another while loop that continues to decrement i until it reaches 65. Finally, it prints the character corresponding to the ASCII value of i, which is 65. Therefore, the output of the program is A.
7.
What is the output of this program?
void main()
{
int i=99;
while(i
Correct Answer
C. D
8.
#include
void main()
{
double k = 0;
for (k = 0.0; k < 3.0; k++);
printf("%lf", k);
}
Correct Answer
B. 3.000000
9.
What is the output of this program?
void main()
{
int x=1;
x=!x;
while(x!=!x)
{
printf(“%d”,i);
}
}
Correct Answer
A. 0
Explanation
The program initializes the variable x to 1. It then uses the logical negation operator "!" to change the value of x to 0. In the while loop condition, it checks if x is not equal to the logical negation of x. Since the logical negation of x is 1, the condition becomes x != 1, which is false. Therefore, the while loop is not executed and nothing is printed. Hence, the output of the program is 0.
10.
What is the output of this program?
void main()
{
int k;
for(k=10; k%2! =0 && k%3!=0;k--)
if(k==1)
break;
printf(“%d”,k);
}
Correct Answer
B. 10
Explanation
The program starts with initializing the variable k to 10. Then it enters a for loop where it checks two conditions: k%2 != 0 (k is not divisible by 2) and k%3 != 0 (k is not divisible by 3). If both conditions are true, it decrements the value of k by 1.
In this case, since 10 is divisible by 2, the condition k%2 != 0 is false, and the loop is not executed. Therefore, the value of k remains 10.
Finally, the program prints the value of k, which is 10.
11.
What is the output of this program?
void main()
{
long int i=3;
while(++i
Correct Answer
C. 3
Explanation
The output of this program is 3. This is because the variable i is initially set to 3. In the while loop, the expression (++i < 0) is evaluated. Since i is incremented before the comparison, it becomes 4. However, 4 is not less than 0, so the condition is false and the loop is not executed. Therefore, the program does not output anything other than the initial value of i, which is 3.
12.
What is the output of this program?
void main()
{
int i, j;
for(i=0,j=20; i!=j; i++,j--)
{
printf(“%c”,i+48);
}
}
Correct Answer
A. Digits
Explanation
The program is using a for loop to iterate through the values of i and j. The loop will continue as long as i is not equal to j. Inside the loop, the program is printing the character representation of i+48. Since i starts at 0 and increments by 1 each iteration, the program will print the digits 0 to 9. Therefore, the output of the program will be the digits.
13.
What is the output of this program?
void main()
{
int i=0, j=1;
while(i==!j)
{
printf(“%d %d”,!i,!j);
i++; j++;
}
}
Correct Answer
C. 1 0
Explanation
The output of the program will be "1 0". This is because the while loop condition is checking if i is equal to the logical NOT of j. Initially, i is 0 and j is 1. The logical NOT of j is 0. Since i is not equal to 0, the condition is false and the loop does not execute. Therefore, the printf statement is not executed and there is no output.
14.
What is the output of this program?
void main()
{
int k;
for(k=32768; k
Correct Answer
A. -32768
Explanation
The output of this program is -32768. This is because the for loop starts with k=32768 and continues until k is less than -32768. However, since the initial value of k is already greater than the condition, the loop will not execute and the value of k will remain as -32768.
15.
The following code stands for what?
void main()
{
int m=60,n=25;
if(m!=n)
{
while(m>n)
m-=n;
interchange(m,n); // assume the function interchange content of m & n
}
printf(“%d”,m);
}
Correct Answer
B. GCD(m,n)
Explanation
The code is checking if m is not equal to n, and if so, it enters a while loop where it subtracts n from m until m becomes less than n. This process is similar to finding the greatest common divisor (GCD) of m and n. Therefore, the code is calculating the GCD of m and n and the correct answer is GCD(m,n).
16.
Void main()
{
int a[5] = {1, 2, 3, 4, 5};
int i;
for (i = 0; i < 5; i++)
if ((char)a[i] == '5')
printf("%d\n", a[i]);
else
printf("FAIL\n");
}
Correct Answer
D. Program prints FAIL for 5 times
Explanation
The given code will print "FAIL" five times. This is because the code is using the "%d" format specifier in the printf function, which is used for printing integers. However, the elements of the array "a" are of type int, not char. When the code tries to compare the integer value with the character '5', it will not be equal and the "else" block will be executed, printing "FAIL" each time.
17.
What is the output of this program?
#include
#include
void main()
{
printf(“%d”, (2*(1+rand()%4));
}
Correct Answer
A. Any even digit
Explanation
The program generates a random number between 1 and 4 using the rand() function. It then multiplies this number by 2 and adds 1 to it. Since the range of the random number is limited to 1-4, the possible outputs of the expression are 3, 5, 7, and 9. All of these numbers are odd digits. Therefore, the correct answer is "any odd digit".
18.
What is the output of this program?
void main()
{
int i=0,j=1,k;
while(i
Correct Answer
C. Fibonacci nos
Explanation
The output of this program will be "Fibonacci nos". This is because the program is incomplete and the while loop condition is not provided. However, based on the given options "Prime nos", "Perfect nos", "Fibonacci nos", and "Armstrong no", it can be inferred that the program is likely checking for and printing Fibonacci numbers.
19.
Fill in the blank:
#include
#include
void main()
{
int i,n;
scanf(“%d”,&n);
for(i=2;isqrt(n))
printf(“%d is ________”,n);
}
Correct Answer
D. Prime no
Explanation
The given code is missing a condition in the for loop. It should be "i
20.
What is the output of this program?
void main()
{
int i;
for(i=10;--i%3==0;i--)
printf(“%d”,i);
}
Correct Answer
C. 9
Explanation
The program starts with the variable i initialized to 10. In the for loop condition, the expression --i%3==0 is evaluated. The --i decrements i by 1 and then checks if the result is divisible by 3. Since 10 is not divisible by 3, the condition is false and the loop exits. Therefore, the program only prints the value of i once, which is 9.