Write a code to check whether a number is even or odd

Q1.Write a code to check whether a number is even or odd

Here you will find an algorithm and program to check whether a number is an even number or an odd number. First let us understand what is even and odd number.

Even Number :- Even numbers are the numbers which are divisible by 2. For Example :- 2, 4, 6, 8…

Odd Number :- Odd numbers are the numbers which are not divisible by 2. For Example :- 1, 3, 5, 7…

Now let us understand what logic we need to use in this –
As we know that, to know number is even or odd we used to check its divisibility with 2, so here we will check the remainder we get on dividing the input number with 2 if we get remainder as 0 then it is divisible by 2 and hence it is even number else it is odd number.

For Example :-

Input : 10
Output : Even Number
Explanation : As we can see that the number given in input, which is 10, on dividing with 2 we get remainder 0 therefore 10 is Even Number.

Algorithm to check number is even or odd

\\Algorithm to check number is even or odd.
START
Step 1: [ Take Input ] Read: Number N
Step 2: Check: If N%2 == 0 Then
Print : N is an Even Number.
Else
Print : N is an Odd Number.
STOP

Program to check whether the number is even or odd

Output

The number 10 is Even number

Recommended Programs
Program to find factorial of a number
Program to count number of digits in a number

Array & String

Array & String

Programming Multiple Choice Question -Array & String

This section focuses on the “Array & String” 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. How many keywords are there in c ?

A. 31
B. 64
C. 32
D. 63

Ans : C
Explanation: It will give compile time error because a is not declared.

2. Which of the following is true for variable names in C?

A. Reserved Word can be used as variable name
B. Variable can be of any length
C. They can contain alphanumeric characters as well as special characters
D. Variable names cannot start with a digit

Ans : A
Explanation: Variable names cannot start with a digit in C Programming language.

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

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?

void main()
{
int x = 10;
float x = 10;
printf(“%d”, x)
}

A. 10,10
B. 10
C. Compilations Error
D. 10.1

Ans : C
Explanation: Since the variable x is defined both as integer and as float, it results in an Compilations Error

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.

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.

Categories C

Variables & Datatypes

Variables And Datatypes

Programming Multiple Choice Question –Variables And Datatypes

This section focuses on the “Variables and Datatypes” 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. How many keywords are there in c ?

A. 31
B. 64
C. 32
D. 63

Ans : C
Explanation: It will give compile time error because a is not declared.

2. Which of the following is true for variable names in C?

A. Reserved Word can be used as variable name
B. Variable can be of any length
C. They can contain alphanumeric characters as well as special characters
D. Variable names cannot start with a digit

Ans : A
Explanation: Variable names cannot start with a digit in C Programming language.

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

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?

void main()
{
int x = 10;
float x = 10;
printf(“%d”, x)
}

A. 10,10
B. 10
C. Compilations Error
D. 10.1

Ans : C
Explanation: Since the variable x is defined both as integer and as float, it results in an Compilations Error

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.

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.

Categories C

Technical MCQ -C

C MCQ Questions

A language that doesn’t affect the way you think about programming is not worth knowing.  Alan J. Perlis

C is a general purpose, procedural programming language. C was developed by Dennis M. Ritchie at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972.

It was mainly developed as a system programming language to write operating system. The main features of C language include low-level access to memory, simple set of keywords, and clean style, these features make C language suitable for system programming like operating system or compiler development. C programming is highly efficient. That’s the main reason why it’s very popular despite being more than 40 years old. Standard C programs are portable. The source code written in one system works in another operating system without any change.

C is a successor of B language which was introduced around the early 1970s. The language was formalized in 1988 by the American National Standard Institute (ANSI). Today’s most popular Linux OS and RDBMS MySQL have been written in C.

Practice C MCQ Questions here on alfatechlab which will help you to clear your concepts and also to prepare for technical rounds, interviews, competitive exams etc.

Categories C

Printf & Scanf

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.
Categories C