C Programming MCQ Question 1
Consider the C program:
#include <stdio.h>
int main()
{
printf(7 + "BYJUS’SEXAMPREP");
return 0;
}
What is the output of the above code?
- BYJUS'SEXAMPREP
- EXAMPREP
- 5BYJUS'SEXAMPREP
- Compiler error
Answer: B. EXAMPREP
Solution
The stdio.h header file defines the library function printf. Through the expression 7 + "BYJUS'SEXAMPREP," the compiler adds 7 to the string's base address. The standard library function is then given the string "EXAMPREP" as an argument.
C Programming MCQ Question 2
What is the output of the program?
#include <stdio.h>
int main()
{
int i = 3;
printf("%d", (++i)++);
return 0;
}
- 3
- 4C
- 5
- Compile-time error
Answer: D. Compile-time error
Solution
Prefix and postfix operators in the C language require l-value in order to operate and return r-value. When the expression (++i)++ is used, the value of the variable i (an l-value) is increased, and an r-value is returned. When the compiler tries to post-increment the value of an r-value, it produces the error "l-value needed."
C Programming MCQ Question 3
What is the output of the program?
#include <stdio.h>
#if X == 3
#define Y 3
#else
#define Y 5
#endif
int main()
{
printf("%d", Y);
return 0;
}
- 3
- 5
- 3 or 5 depending on the value of X
- Compile-time error
Answer: B. 5
Solution
The absence of macro X initially causes the output to appear to be a compile-time error. In C, a macro's default value is set to 0 if the pre-processor does not define it. The control then shifts to the conditional else clause, where the number 5 is written.
C Programming MCQ Question 4
Consider the following pseudocode:
x:=1;
i:=1;
while (x ≤ 500)
begin
x:=2x ;
i:=i+1;
end
What is the value of i at the end of the pseudocode?
- 4
- 5
- 6
- 7
Answer: B. 5
Solution
The program executes as:
In the first iteration: x = 2 and i = 2
In the second iteration: x = 4 and i = 3
In the third iteration: x = 16 and i = 4
In the fourth iteration: x = 16*16 and i = 5
In the fifth iteration: Condition incorrect.
So, option (B) is correct.
C Programming MCQ Question 5
Consider the code
#include “stdio.h”
int main()
{
printf(“BYJU’SEXAMPREP”);
main();
return 0;
}
Which of the following statement is correct?
- We can't use main() inside main(), and the compiler will catch it by showing a compiler error.
- BYJU'SEXAMPREP would be printed 2147483647 times which is (2 to the power 31) - 1.
- BYJU'SEXAMPREP would be printed only once. Because when main() is used inside main(), it's ignored by the compiler at run time. This is to make sure that main() is called only once.
- BYJU'SEXAMPREP would be printed until stack overflow happens for this program.
Answer: D. BYJU'SEXAMPREP would be printed until stack overflow happens for this program.
Solution
Firstly, calling main() from another function is not restricted. Therefore main() can recursively call. However, this section does not state the recursion's explicit termination condition. So, after printing BYJU'SEXAMPREP, main() would call main(). This will continue until the program's stack is filled. Please be aware that a running program's internal stack records the calling function sequence—that is, the order in which one function is called another—so that control can be returned when the called function completes its work. Because of this, main() in this program would keep calling main() until the entire stack had been used or until a stack-overflow had occurred.
☛ Related Questions:
Comments
write a comment