Control Structures(C Program ) Questions and Answers

Question 1. What will be the output of the given program?
#include
void main()
{
int i=10;
printf("i=%d", i);
{
int i=20;
printf("i=%d", i);
i++;
printf("i=%d", i);
}
printf("i=%d", i);
}
  1.    10 10 11 11
  2.    10 20 21 21
  3.    10 20 21 10
  4.    10 20 21 20
Explanation:-
Answer: Option C. -> 10 20 21 10
The scope of second declaration of i is limited to the block in which it is defined. Outside of the block variable is not recognized.

Question 2. What is the output of given program if user enter value 99?
#include
void main()
{
int i;
printf("Enter a number:");
scanf("%d", &i); // 99 is given as input.
if(i%5 == 0){
printf("nNumber entered is divisible by 5");
}
}
  1.    Enter a number:99
  2.    Enter a number:99 Number is divisible by 5
  3.    complier error
  4.    Run time error
Explanation:-
Answer: Option A. -> Enter a number:99
since this program isn't having any syntax error so program is executed. It is clearly seen that 99 is not divisible by 5. So if statement will not execute and program will terminate.

Question 3. What is the output of given program if user enter "xyz" ?
#include
void main()
{
float age, AgeInSeconds;
printf("Enter your age:");
scanf("%f", &age);
AgeInSeconds = 365 * 24 * 60 * 60 * age;
printf("You have lived for %f seconds", AgeInSeconds);
}
  1.    Enter your age: xyz You have lived for 0 seconds
  2.    Enter your age: xyz You have lived for 0.00000 seconds
  3.    Enter your age: xyz "after that program will stop"
  4.    Run time error
Explanation:-
Answer: Option B. -> Enter your age: xyz You have lived for 0.00000 seconds
When we give scanf() a "%f" format string, that means "We want you to try and get us a floating point number. When we provide input like 'xyz', it's not going to match anything, because 'xyz' is not a valid floating-point number.

Question 4. What will be the value of i and j after execution of following program?
#include
void main()
{
int i, j;
for(i=0,j=0;i
  1.    10 10
  2.    10 20
  3.    20 20
  4.    Run time error
Explanation:-
Answer: Option C. -> 20 20
comma operator is executed from left to right so until j

Question 5. What is the output of given program if user enter "xyz" ?
#include
void main()
{
float age, AgeInSeconds;
int value;
printf("Enter your age:");
value=scanf("%f", &age);
if(value==0){
printf("\\nYour age is not valid");
}
AgeInSeconds = 365 * 24 * 60 * 60 * age;
printf("\\n You have lived for %f seconds", AgeInSeconds);
}
  1.    Enter your age : xyz Your age is not valid
  2.    Enter your age: xyz You have lived for 0 seconds
  3.    Enter your age: xyz Your age is not valid
  4.    Complier error
Explanation:-
Answer: Option C. -> Enter your age: xyz Your age is not valid
When we give scanf() a "%f" format string, that means "We want you to try and get us a floating point number. When we provide input like 'xyz', it's not going to match anything, because 'xyz' is not a valid floating-point number.

Question 6. What will be the output of given program?
#include
void main()
{
int a=3;
for(;a;printf("%d ", a--);
}
  1.    no output
  2.    3 2 1 0
  3.    3 2 1
  4.    infinity loop
Explanation:-
Answer: Option C. -> 3 2 1
Decrement operator in for loop statement are executed until the condition is true.
So it is executed till "a" not equal to zero and printf statement inside for loop print a value of "a"

Question 7. What will be the output of given program?
#include
void main()
{
int i=1, j=-1;
if((printf("%d", i)) < (printf("%d", j)))
printf("%d", i);
else
printf("%d", j);
}
  1.    1 -1 1
  2.    1 -1 -1
  3.    1
  4.    -1
  5.    complier error
Explanation:-
Answer: Option A. -> 1 -1 1
Here if statement is executed since we know that printf() function return the number of character print.
Here printf("%d", i) return 2 because it print 1 and newline i.e 2.
And, printf("%d', j) return 3 because it print -1 and newline i.e number of character is 3.
Therefore if statement look like if(2

Question 8. What will be the output of the following piece of code?
for(i = 0; i
  1.    10
  2.    0123456789
  3.    Syntax error
  4.    0
  5.    Infinite loop
Explanation:-
Answer: Option A. -> 10
Due to semicolon(;) at the end of the for loop, after 10 iterations for loop will be terminated and the result will be 10.

Question 9. What will be the output of the following code?
#include
void main()
{
int s=0;
while(s++
  1.    1 2 3 4 5 6 7 8 9
  2.    1 2 3 10
  3.    4 5 6 7 8 9 10
  4.    4 5 6 7 8 9
  5.    None of these
Explanation:-
Answer: Option C. -> 4 5 6 7 8 9 10
'If' statement only true when value of 's' is less then 4

Question 10. What will be the output of given program?
#include
void main()
{
int a=1;
if("%d=hello", a);
}
  1.    complier error
  2.    no error no output
  3.    0
  4.    1
Explanation:-
Answer: Option B. -> no error no output
the if is conditional and it checks for expression whether it is zero or non-zero so it doesn't print any data it is clear and it is clearly seen that there is not a single syntax error therefore no complier error.
After compling the program will be executed but there is not a single printf statement so it is executed but will not give any output.