Printf & Scanf

Programming Multiple Choice Question – Printf & Scanf

1.What is the purpose of the printf function in C?

  1. To read input from the user
  2. To write output to the screen
  3. To perform arithmetic operations
  4. To allocate memory

Answer: b) To write output to the screen

Explanation: The printf function in C is used to print output to the screen. It allows you to format the output in a specific way, such as adding new lines, tabs, or other characters.

2. What is the output of this program?

#include <stdio.h>
int main()
{
int main = 4;
printf(“%d”, main);
return 0;
}

A. 4
B. Compile time error
C. Run time error
D. give garbage value

Ans : A
Explanation: A C program can have same function name and same variable name so it will print 4

3. Find out the output of following program? if input a is 3 and b is 4

#include <stdio.h>
int main()
{
int a, b;
printf(“%d”, scanf(“%d %d”,&a,&b));
return 0;
}

A. 3
B. 2
C. runtime error
D. compile time error

Ans : B
Explanation: In C, scanf returns the number of inputs it has successfully read it means here is two variable 3 and 4 so scanf return 2(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. world
heworld
B. hello
helloworld
C. helloworld
hellold
D. None of the mentioned

Ans : A
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(“alfatechlab”);
i = printf(“%d “, i);
printf(“%d “, i);
return 0;
}

A. Alfatechlab 11 3
B. Alfatechlab 14 2
C. Alfatechlab 12 3
D. Compilation Error

Ans : A
Explanation: In C, printf() returns the number of characters successfully written on the output. Here Alfatechlab contain 11 character therefore i = 11 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.
i = printf(“alfatechlab”); // print 11
i = printf(“%d “, i);// 3

6. Output of this statement is :

printf ( “%d” , printf ( “Alfatechlab” ) );

A. Syntex Error
B. Alfatechlab11
C. gives garbage value
D. print hello and terminates

Ans : B
Explanation: Firstly inner printf executes and will print Alfatechlab now this return the number of character printed that is 11 and this will be printed by outer printf . Hence output is Alfatechlab11

7. What is the output of this program?

#include <stdio.h>
# define scanf “%s Learn Coding “
main()
{
printf(scanf, scanf);
return 0;
}

A. %s Learn Coding Learn Coding
B. %s Learn Coding, %s Learn Coding
C. Invalid Syntex
D. Run time error

Ans : B
Explanation: printf statement will become printf(“%s Learn Coding, %s Learn Coding “);

8. What is the output of this program?

#include <stdio.h>
int main()
{
printf(“%d”, main);
return 0;
}

A. Goes in infinite loop
B. Compilation Error
C. Gives garbage value
D. Gives Address of function main

Ans : D
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 = 4, 5, 6;
printf(“%d”, i);
return 0;
}

A. 4
B. 5
C. 6
D. Invalid Syntax


Ans : A
Explanation: Associativity of comma operator is from left to right, but = operator has higher precedence than comma operator.Therefore the statement i = 4, 5, 6 is treated as i = 4 .

10. Comment on the given statment:

scanf(“%d”,i);

A. Will give Segmentation fault
B. Will execute without any error
C. Will give Compilation Error
D. None of the above

Ans : A
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.

11. What is the output of this program?

#include <stdio.h>
int main()
{
int x = 1, y = 2;
printf(“%d,%d”, x, y);
return 0;
}

A. 12
B. 1,2
C. Compilation Error
D. Garbage Value

Ans : A
Explanation: Printf function print all %d value of passing variable

12. What is the output of this program?

#include <stdio.h>
int main()
{
int x = 3, y = 5;
printf(“%*d”, x, y);
return 0;
}

A. 3
B. 5
C. Compilation Error
D. Garbage Value

Ans : B
Explanation: the “*” above means or is used to specify the length of space required to print the integer and printf now takes two arguments one for space and other for integer so printf(“%*d”, x, y); will be print 5

13. What is the output of this program?

#include <stdio.h>
int main()
{
char str[25];
printf(” %d “,printf(“c-hello”));
return 0;
}

A. 7 c-hello
B. 9 c-hello
C. c-hello 9
D. c-hello 7

Ans : D
Explanation: Inner printf() will print first and then outer printf will display the length of the inner printf’s string.

14. What is the output of this program?

#include <stdio.h>
# define loop while(true)
int main()
{
loop;
printf(“c-alfatechlab”);
return 0;
}

A. Compilation error
B. c-alfatechlab
C. program never ends
D. None of the above

Ans : A
Explanation: error: ‘true’ undeclared (first use in this function)

15. What is the output of this program?

#include <stdio.h>
int main()
{
printf(“%d”, 5.00);
return 0;
}

A. Compilation error
B. 5.00
C. 5
D. Garbage value

Ans : D
Explanation: The output will be garbage value.

16. What is the output of this program?

#include <stdio.h>
int main()
{
printf(“%d”,5.25);
return 0;
}

A. Compilation error
B. Garbage value
C. 5
D. 0

Ans : B
Explanation: Give Garbage value becausse 5.25 is neitheir a pure integer number(5) nor impure integer number(5.00).

17. What is the output of this program?

#include <stdio.h>
int main()
{
int a = 3;
printf(“%d”);
return 0;
}

A. Garbage value
B. Compilation error
C. 3
D. Runtime error

Ans : A
Explanation: As there is no declaration of variable a in printf function, a standard C compiler printf some garbage value as an output. This program is tested under Dev cpp and in standard online c compiler.

18. What is the output of this program?

#include <stdio.h>
int main()
{
char *ptr = “Welcome”;
printf(ptr+2);
return 0;
}

A. pointer cannot be initialized
B. lcome
C. Runtime error
D. Welco

Ans : B
Explanation: In printf, initial address of *ptr is incremented by 2 , thus it neglect “We” to welcome

19. What is the output of this program 32 bit c compiler ?

#include <stdio.h>
int main()
{
int x = 1;
printf(“%d %p”,x,x);
return 0;
}

A. Runtime error
B. 1 0x1
C. 1 00000001
D. %p is not a format specifier

Ans : B
Explanation: %p is a format specifier to print a pointer in printf or sprintf. For 64 bit c compiler, the output will be 1 0x1

20. What is the output of this program?

 

#include <stdio.h>
static struct student
{
int x;
int y;
}
struct_var{2,3};
int main()
{
printf(“%d %d”,struct_var.x,struct_var.y);
return 0;
}

A. Runtime Error
B. Improper representation of structure variable
C. 2 3
D. Compilation error

Ans : D
Explanation: It will give compilation error.

20. What is the output of this program?

#include <stdio.h>
static struct student
{
int x;
int y;
}
struct_var{2,3};
int main()
{
printf(“%d %d”,struct_var.x,struct_var.y);
return 0;
}

A. Runtime Error
B. Improper representation of structure variable
C. 2 3
D. Compilation error

A. Runtime Error
B. Improper representation of structure variable
C. 2 3
D. Compilation error

Ans : D
Explanation: It will give compilation error.

21. What is the output of this program?

int main() {
int i=1;
i=2+2*i++;
printf(“%d”,i);
return 0;
}

A. 2
B. 3
C. 4
D. 5

Ans : C
Explanation: This is undefined behaviour, since i is modified more than once between sequence points. For instance, this compiler gives 4 as the answer, because it puts the increment after the assignment. Another reasonable answer is 6, if the increment is before the assignment.

22. What is the output of this program?

main ( )
{
int i;
i=1;
i=i+2*i++;
printf( “%d” , i);
}

A. 2
B. 3
C. 4
D. 5

Ans : C
Explanation: This is undefined behavior, since i is modified more than once between sequence points. For instance, this compiler gives 4 as the answer, because it puts the increment after the assignment

23. What is the output of this program?

main()
{
int a;
a = 4+3, 4>3, 1;
printf( “%d” , a);
}

A. 7
B. 4
C. 3
D. 5

Ans : A
Explanation: 7 is the output of this program.

24. What is the output of this program?

main ()
{
int a =6;
printf( “%d %d %d ” , a,a<<2,a>>2);
}

A. 6 24 1
B. 5 1 20
C. 5 20 20
D. 5 1 1

Ans : A
Explanation: << and >> operation applied on the value of “i” and we get the output.6 24 1

25. What is the output of this program?

main ( )
{
int a=8;
printf( “%d %d %d ” , a,a<<2,a<<2);
}

A. 8 32 32
B. 5 1 20
C. 5 20 20
D. 5 1 1

Ans :A
Explanation: << operation applied on the value of “i” and we get the output.8 32 32

26. What is the output of this program?

int main()
{
int i=2;
printf( “%d %d %d ” , i,i<2,i>2);
return 0;
}

A. Compilation error
B. Garbage value
C. 5 0 1
D. 2 0 0

Ans : C
Explanation: The code will successfully compiled and give output 2 0 0

27. What is the output of this program?

int main()
{
int i=7;
printf( “%d %d %d ” , i,i&&2,i||2);
return 0;
}

A. 5 1 1
B. Compilation error
C. Garbage value
D. 7 1 1

Ans : D
Explanation: The code will successfully compiled and give output 7 1 1 .

28. What is the output of this program?

int main()
{
int i=6;
printf( “%d %d ” , i,i/2);
return 0;
}

A. 5 7
B. 6 3
C. Garbage value
D. 5 0

Ans : B
Explanation: The code will successfully compiled and give output 6 3 .

29. What is the output of this program?

int main()
{
int i=2;
i=i+i*i++;
printf(“%d”,i);
return 0;
}

A. 2
B. 9
C. 4
D. 5

Ans : B
Explanation: The code will successfully compiled and give output 9.

30. What is the output of this program?

int main()
{
int i=-5;
i=i+i*i++;
printf(“%d”,i);
return 0;
}

A. -2
B. 1
C. -1
D. 16

Ans : D
Explanation: The code will successfully compiled and give output 16.

31. What is the output of this program?

int main()
{
int k=5;
printf(“%d == 1 is” “%s”, k, k==1?”TRUE”:”FALSE”);
return 0;
}

A. k == 1 is TRUE
B. 1 == 1 is TRUE
C. 1 == 1 is FALSE
D. 5 == 1 isFALSE

Ans : D
Explanation: the output of the program is 5 == 1 isFALSE

32. What is the output of this program?

int main()
{
int k=0;
printf(“%d == 1 is” “%s”, k, k==1?”TRUE”:”FALSE”);
return 0;
}

A. k == 1 is TRUE
B. 2 == 1 is TRUE
C. 0 == 1 isFALSE
D. K == 1 is FALSE

Ans : C
Explanation: the output of the program is 0 == 1 isFALSE

33. What is the output of this program?

char *str = “char *str = %c%s%c; main(){ printf(str, 2, str, 2);}”;
int main()
{
printf(str, 4, str, 4);
return 0;
}

A. char *str = char *str = %c%s%c; main(){ printf(str, 2, str, 2);}; main(){ printf(str, 2, str, 2);}
B. char *str = %c%s%c; main(){ printf(str, 4, str, 4);}
C. No output
D. Error in program

Ans : A
Explanation: char *str = char *str = %c%s%c; main(){ printf(str, 2, str, 2);}; main(){ printf(str, 2, str, 2);} is the output of this program.

34. What is the output of this program?

main ( )
{
float a=3.2547;
printf(“%2.3f”, a);
return 0;
}

A. 4
B. 4.159
C. 3.255
D. 4

Ans : C
Explanation: float a=3.2547; The variable a is declared as an float data type and initialized to value 3.2547; printf(“%2.3f “, a); The precision specifier tells .3f tells the printf function to place three number after the .(dot). Hence the output is 3.255

35. What is the output of this program?

main ( )
{
float a=6.3214;
printf(“%2.1f”, a);
return 0;
}

A. 4
B. 4.1
C. 6.3
D. 4

Ans : C
Explanation: float a=6.3214; The variable a is declared as an float data type and initialized to value 6.3214; printf(“%2.1f “, a); The precision specifier tells .1f tells the printf function to place one after the .(dot). Hence the output is 6.3

36. What is the output of this program?

int main()
{
printf(“%c”, ~(‘D’*-1));
return 0;
}

A. A
B. B
C. C
D. D

Ans : C
Explanation: The code will successfully compiled and give output C.

37. What is the output of this program?

int main()
{
printf(“%c”, ~(‘p’*-1));
return 0;
}

A. o
B. s
C. m
D. n

Ans : A
Explanation: The code will successfully compiled and give output o.

38. What is the output of this program?

int main()
{
char *p;
p=”%d”;
p++;
p++;
printf(p-2, 20);
return 0;
}

A. 11
B. 20
C. Error
D. No output

Ans : B
Explanation: The code will successfully compiled and give output 20

39. What is the output of this program?

int main()
{
printf(“%%%%%%”);
return 0;
}

A. %%%
B. %%
C. No output
D. Error

Ans : A
Explanation: The code will successfully compiled and give output %%%.

40. What is the output of this program?

int main()
{
int a=120;
printf(“%1d<0x1d>”, a);
return 0;
}

A. 1250
B. 2
C. 50
D. 120<0x1d>

Ans : D
Explanation: int a=120; The variable a is declared as an integer type and initialized to value 120. printf(“%1d<0x1d> “, a); It prints the value of variable a. Hence the output of the program is 120.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.