#include<stdio.h>
int main()
{
int x = 100, y = 100%80, j;
for(j=1; j<10; j++)
if(x != y);
printf("x = %d y = %d\n", x, y);
return 0;
}
- The program produce the output x=100 y=20
- The printf function run for 10 times
- The semi colon(;) after the if(x!=y) will not produce any error
- The program will produce no output
The options are given below:
Explanation:
The first statement is accurate as the program yields x=100 and y=20.
The second statement is incorrect as the printf function is located outside the for loop. Consequently, the printf statement executes only once.
The third assertion holds true as the semicolon is employed to conclude a conditional statement. Hence, the statement if(x!=y); is permissible in the C programming language.
The assertion at point 4 is inaccurate as the code is generating the result x=100 and y=20.
Hence, only statement 1 and 3 are accurate statements.
17) Which of the following statements are correct about for loop in C-program?
- All things that can be done using a for loop can also be done using a while loop.
- for loop can be used if we want statements in a loop get executed at least once.
- for loop works faster than a while loop.
- for(;;); implements an infinite loop.
The options are given below:
- 1, 2, 3
- 2, 3, 4
- 1, 2, 4
Explanation:
For loops are employed when it is necessary for the statements within the loop to execute at least once. Thus, the for loop operates at a slower pace compared to a while loop, making statement 3 inaccurate.
Remaining 3 statements about for loop is correct.
Hence, statements 1, 2, and 4 are accurate.
18) What is the output of the given program, if a short int is 2 bytes wide?
#include<stdio.h>
int main()
{
short int i = 0;
for(i<=5 && i>=-1; ++i; i>0)
printf("%u,", i);
return 0;
}
- Expression syntax error
- 1 .... 65535
- 0, 1, 2, 3, 4, 5
- No output
Explanation:
In a for loop statement such as for (i<=5 && i>=-1; ++i; i>0), the condition i<=5 && i>=-1 determines the loop condition. The prefix increment operator ++i is responsible for increasing the value of the expression.
In the provided for loop condition, the loop initiates from 1 and continues to run until reaching the maximum value of an integer, which is 65535.
Thus, the program will output numbers from 1 to 65535.
19) Can we use switch statement to switch on strings in C?
Explanation:
In a switch statement, the cases are required to be either a constant expression or an integer constant.
Hence, utilizing a switch statement to toggle between strings is not permissible in C programming.
20) What is the output of the given program?
#include<stdio.h>
int main()
{
int a=5;
do
{
printf("%d\n",a);
a= -1;
}while (a>0);
return 0;
}
- Compile error
Explanation:
A do-while loop enables the execution of the loop's body before evaluating the condition. Consequently, the initial value of "a," which is 5, is displayed first, followed by the execution of the statement "a = -1."
Since the value of -1 is not higher than 0, the condition fails, leading to the loop ending at the point where a equals 5.
Therefore the output of program is 5.