C Programming Multiple Choice Question - Printf & Scanf
This section focuses on the "Printf And Scanf" of the C programming. These Multiple Choice Questions (mcq) should be practiced to improve the C programming skills required for various interviews (campus interview, walk-in interview, company interview), placement, entrance exam and other competitive examinations.
1. What is the output of this program?
#include <stdio.h>
int main()
{
printf("variable! %d", x);
return 0;
}
A. variable! x
B. variable! followed by a junk value
C. Compile time error
D. variable!
View Answer
Ans : C
Explanation: It will give compile time error since x is not declared.
2. What is the output of this program?
#include <stdio.h>
int main()
{
int main = 3;
printf("%d", main);
return 0;
}
A. 3
B. Compile time error
C. Run time error
D. give garbage value
View Answer
Ans : A
Explanation: A C program can have same function name and same variable name.
3. What is the output of this program? if input a is 1 and b is 2
#include <stdio.h>
int main()
{
int a, b;
printf("%d", scanf("%d %d",&a,&b));
return 0;
}
A. 1
B. 2
C. runtime error
D. compile time error
View Answer
Ans : B
Explanation: In C, scanf returns the number of inputs it has successfully read.
4. What is the output of this program?
#include <stdio.h>
void main()
{
printf("hello\rworld\n");
printf("hello\b\b\bworld\n");
}
A. hello
helloworld
B. world
heworld
C. helloworld
hellold
D. None of the mentioned
View Answer
Ans : B
Explanation: "" for a backspace, means if u print it, cursor will print and come back 1 character . '
' is used for carriage return to come 1 line back or in start of line.
5. What is the output of this program?
#include <stdio.h>
int main()
{
int i;
i = printf("letsfindcourse");
i = printf("%d ", i);
printf("%d ", i);
return 0;
}
A. letsfindcourse 15 3
B. letsfindcourse 14 2
C. letsfindcourse 14 3
D. Compilation Error
View Answer
Ans : C
Explanation: In C, printf() returns the number of characters successfully written on the output. Here letsfindcourse contain 14 character therefore i = 14 and then i is printed now again value of printf is assign but this time it print 3 since it contain 3 character %,d and space.
6. Output of this statment is :
printf ( "%d" , printf ( "hello" ) );
A. Syntex Error
B. hello5
C. gives garbage value
D. print hello and terminates
View Answer
Ans : B
Explanation: Firstly inner printf executes and will print hello now this return the number of character printed that is 5 and this will be printed by outer printf . Hence output is hello5
7. What is the output of this program?
#include <stdio.h>
# define scanf "%s Find best course "
main()
{
printf(scanf, scanf);
return 0;
}
A. %s Find best course Find best course
B. %s Find best course %s Find best course
C. Invalid Syntex
D. Run time error
View Answer
Ans : A
Explanation: printf statement will become printf("%s Find best course ", "%s Find best course ");
8. Which statment is true about the given code ?
#include <stdio.h>
int main()
{
printf("%d", main);
return 0;
}
A. Goes in infinite loop
B. Gives Address of function main.
C. Gives garbage value
D. Compilation Error
View Answer
Ans : B
Explanation: Name of the function is actually a pointer variable to the function and prints the address of the function.
9. What is the output of this program?
#include <stdio.h>
int main()
{
int i;
i = 1, 2, 3;
printf("%d", i);
return 0;
}
A. 1
B. 2
C. 3
D. Invalid Syntax
View Answer
Ans : A
Explanation: Associativity of comma operator is from left to right, but = operator has higher precedence than comma operator.Therefore the statement i = 1, 2, 3 is treated as i = 1 .
10. Comment on the given statment:
scanf("%d",i);
A. Will execute without any error
B. Will give Segmentation fault
C. Will give Compilation Error
D. None of the above
View Answer
Ans : B
Explanation: A segmentation fault occurs when a program attempts to access a memory location that it is not allowed to access, or attempts to access a memory location in a way that is not allowed.So as we can see segmentation fault occurs because program attempts to access a memory location that it is not allowed to access.
Also check :
Discussion