1. The statement that transfers control to the beginning of the loop is
a) break
b) continue
c) goto
d) none
Solution: (b) continue
2. Compute the printed values of i and j of the C program given below:
#include <stdio.h>
int main()
{
int i = 0, j = 8;
while (i < 2, j > 5)
{
i++;
j--;
}
printf("%d, %d\n", i, j);
return 0;
}
a) 1, 7
b) 3, 4
c) 3, 5
d) 0, 8
Solution: (c) The while condition checks the last condition (i.e. j>5) and till the condition is satisfied the block inside the loop is executed. Thus the loop runs 3 times. Thus i is incremented by 3 and j is decremented by 3. So i=3, j=5.
3. The following program takes ‘n’ as a positive integer input. What is the purpose of the program?
#include <stdio.h>
int main()
{
int n, i;
unsigned long long result = 1;
printf("Enter an integer: ");
scanf("%d",&n);
for(i=1; i<=n; ++i)
{
result*= i;
}
printf("The output of the program is %llu", result);
return 0;
}
a) n multiplied n times
b) factorial of n
c) display factors of n
d) display Fibonacci series upto n.
Solution: (b) In the for loop, 1 to n is multiplied. This computes the factorial of the number n.
4. What will be the output after execution of the program?
#include <stdio.h>
int main()
{
int i=5;
while (i)
printf("Hello");
printf("World\n");
return 0;
}
a) No output
b) Infinite time print of ‘Hello’.
c) Infinite time print of ‘World’.
d) Infinite time print of ‘HelloWorld’.
Solution: (b) As any non-zero value inside while statement is considered as true, the while statement will always be executed. There is no terminating condition for the while loop, thus the program will print ‘Hello’ infinite number of times.
5. What is the output of the following C program?
#include <stdio.h>
int main()
{
int x = 0, i = 0;
for (i = 0;i < 5; i+=0.5)
{
x++;
continue;
}
printf("%d",x);
return 0;
}
a) 5
b) 10
c) No output
d) Compilation error
Solution: (c) As i is initialized as an integer variable, integer value of i after the operation (i=i+0.5) will be zero. Thus, the loop will never be ended and the control will not come to the printf statement at all. So, nothing will be printed.
6. The following program is used to find the reverse of a number using C language. Find the missing condition inside while statement (indicated as ‘xxxx’).
#include <stdio.h>
int main()
{
int n, reversedNumber = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while(xxxx)
{
remainder = n%10;
reversedNumber = reversedNumber*10 + remainder;
n /= 10;
}
printf("Reversed Number = %d", reversedNumber);
return 0;
}
a) n==0
b) n%10==0
c) n!=0
d) n/10==0
Solution: (c) The loop should be continued till the value of n becomes zero. Thus, the right option is n!=0.
7. What will be the final value of i after the execution of the program below
#include <stdio.h>
int main()
{
int i=1, j;
for(j=0; j<=10; j+=i)
{
i=i+j;
}
return 0;
}
Ans: 13
Solution: The value of j will reach to 8 and i will be 13. In the next iteration, j will become 8+13=21 and the condition inside for loop will be invalid, thus the compilation will come out of the loop. So, the value of i will be 13.
8. What is the output of the below C program?
#include <stdio.h>
int main()
{
short int k=1, j=1;
while (k <= 10 && j <= 10)
{
k=k+2;
j+=1;
}
printf("%d,%d",k,j);
return 0;
}
Answer: 11,6
Solution: The loop will be continued till both the condition k<=10 and j<=10 is satisfied. So, the loop will be executed 5 times. Thus, the value of k and j would be 11 and 6.
9. What will be the output?
#include <stdio.h>
int main()
{
int x = 0;
switch (x)
{
case '0': printf("Case 0");
break;
case '1': printf("Case 1");
break;
default: printf("Case default");
}
return 0;
}
a) Case 0
b) Case 1
c) Case default
d) Error
Solution: (c) Case default
At first look, the output of the program seems to be Case 0. But, the cases are labelled with characters which gets converted to their ascii values 48(for 0) and 49(for 1). None of the cases is labelled with value 0. So, the control goes to the default block and Case default is printed.
10. What will be the output?
#include <stdio.h>
int main()
{
int i=0;
for(;i<=10;++i)
{
if(i>=5)
continue;
printf("%d ",++i);
}
return 0;
}
a) 0 1 2 3 4
b) 1 3 5
c) 1 3
d) 1 2 3 4 5 6 7 8 9 10
Solution: (b) The printed value of i will be 1 3 and 5 as the value of i is incremented 2 times and due to the continue statement, the printf statement will always be skipped after the value of i becomes greater than 5.
11. What will be the output?
#include <stdio.h>
int main()
{
int x;
x = 4 > 5 ? 1: 2;
printf("%d", x);
return 0;
}
Solution: 2 will be printed. 4 > 5 ? is false. So the second value (i.e. 2) will be assigned to x. This is called ternary operator.
12. What will be the output of the following program?
#include <stdio.h>
int main()
{
int x, y=1;
for(x=11; x>0; x=x-3)
{
y=y%x;
}
printf("x= %d ,y= %d",x,y);
return 0;
}
a) x= -1, y= 1
b) x= 1, y= 1
c) x= 3, y= 2
d) x= 2, y= 1
Solution: (a) The value of x is decremented from 11 to -1 (each in step is determine by 3). The value of y will be 1 mod 2 (The last value of x inside loop)=1
13. In C three way transfer of control using a single statement is possible by using
a) Unary operator
b) Logical operator
c) Ternary operator
d) None
Solution: (c) Ternary operator
14. What will the output?
#include <stdio.h>
int main()
{
int x=5, y=6, z=7;
if(x-y)
z = z++;
z = --z;
printf("%d", z);
return 0;
}
Answer: 6
Solution: (x-y)=-1 which is not equal to 0. Hence, if condition will be executed, which will store the value of z as 7 (as it is post increment). Finally, due to pre-decrement of z, the final value of z will be 6.
15. Find the output of the following C program
#include <stdio.h>
main()
{
int i = 0;
if(i==0)
{
i=i+1;
break;
}
printf("%d",i);
}
a) 0
b) 1
c) No output
d) Compiler error
Solution: (d) ‘break’ statement is applicable in loop and switch statements. It is not allowed inside if statement. Thus the program will show compiler error.
a) break
b) continue
c) goto
d) none
Solution: (b) continue
2. Compute the printed values of i and j of the C program given below:
#include <stdio.h>
int main()
{
int i = 0, j = 8;
while (i < 2, j > 5)
{
i++;
j--;
}
printf("%d, %d\n", i, j);
return 0;
}
a) 1, 7
b) 3, 4
c) 3, 5
d) 0, 8
Solution: (c) The while condition checks the last condition (i.e. j>5) and till the condition is satisfied the block inside the loop is executed. Thus the loop runs 3 times. Thus i is incremented by 3 and j is decremented by 3. So i=3, j=5.
3. The following program takes ‘n’ as a positive integer input. What is the purpose of the program?
#include <stdio.h>
int main()
{
int n, i;
unsigned long long result = 1;
printf("Enter an integer: ");
scanf("%d",&n);
for(i=1; i<=n; ++i)
{
result*= i;
}
printf("The output of the program is %llu", result);
return 0;
}
a) n multiplied n times
b) factorial of n
c) display factors of n
d) display Fibonacci series upto n.
Solution: (b) In the for loop, 1 to n is multiplied. This computes the factorial of the number n.
4. What will be the output after execution of the program?
#include <stdio.h>
int main()
{
int i=5;
while (i)
printf("Hello");
printf("World\n");
return 0;
}
a) No output
b) Infinite time print of ‘Hello’.
c) Infinite time print of ‘World’.
d) Infinite time print of ‘HelloWorld’.
Solution: (b) As any non-zero value inside while statement is considered as true, the while statement will always be executed. There is no terminating condition for the while loop, thus the program will print ‘Hello’ infinite number of times.
5. What is the output of the following C program?
#include <stdio.h>
int main()
{
int x = 0, i = 0;
for (i = 0;i < 5; i+=0.5)
{
x++;
continue;
}
printf("%d",x);
return 0;
}
a) 5
b) 10
c) No output
d) Compilation error
Solution: (c) As i is initialized as an integer variable, integer value of i after the operation (i=i+0.5) will be zero. Thus, the loop will never be ended and the control will not come to the printf statement at all. So, nothing will be printed.
6. The following program is used to find the reverse of a number using C language. Find the missing condition inside while statement (indicated as ‘xxxx’).
#include <stdio.h>
int main()
{
int n, reversedNumber = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while(xxxx)
{
remainder = n%10;
reversedNumber = reversedNumber*10 + remainder;
n /= 10;
}
printf("Reversed Number = %d", reversedNumber);
return 0;
}
a) n==0
b) n%10==0
c) n!=0
d) n/10==0
Solution: (c) The loop should be continued till the value of n becomes zero. Thus, the right option is n!=0.
7. What will be the final value of i after the execution of the program below
#include <stdio.h>
int main()
{
int i=1, j;
for(j=0; j<=10; j+=i)
{
i=i+j;
}
return 0;
}
Ans: 13
Solution: The value of j will reach to 8 and i will be 13. In the next iteration, j will become 8+13=21 and the condition inside for loop will be invalid, thus the compilation will come out of the loop. So, the value of i will be 13.
8. What is the output of the below C program?
#include <stdio.h>
int main()
{
short int k=1, j=1;
while (k <= 10 && j <= 10)
{
k=k+2;
j+=1;
}
printf("%d,%d",k,j);
return 0;
}
Answer: 11,6
Solution: The loop will be continued till both the condition k<=10 and j<=10 is satisfied. So, the loop will be executed 5 times. Thus, the value of k and j would be 11 and 6.
9. What will be the output?
#include <stdio.h>
int main()
{
int x = 0;
switch (x)
{
case '0': printf("Case 0");
break;
case '1': printf("Case 1");
break;
default: printf("Case default");
}
return 0;
}
a) Case 0
b) Case 1
c) Case default
d) Error
Solution: (c) Case default
At first look, the output of the program seems to be Case 0. But, the cases are labelled with characters which gets converted to their ascii values 48(for 0) and 49(for 1). None of the cases is labelled with value 0. So, the control goes to the default block and Case default is printed.
10. What will be the output?
#include <stdio.h>
int main()
{
int i=0;
for(;i<=10;++i)
{
if(i>=5)
continue;
printf("%d ",++i);
}
return 0;
}
a) 0 1 2 3 4
b) 1 3 5
c) 1 3
d) 1 2 3 4 5 6 7 8 9 10
Solution: (b) The printed value of i will be 1 3 and 5 as the value of i is incremented 2 times and due to the continue statement, the printf statement will always be skipped after the value of i becomes greater than 5.
11. What will be the output?
#include <stdio.h>
int main()
{
int x;
x = 4 > 5 ? 1: 2;
printf("%d", x);
return 0;
}
Solution: 2 will be printed. 4 > 5 ? is false. So the second value (i.e. 2) will be assigned to x. This is called ternary operator.
12. What will be the output of the following program?
#include <stdio.h>
int main()
{
int x, y=1;
for(x=11; x>0; x=x-3)
{
y=y%x;
}
printf("x= %d ,y= %d",x,y);
return 0;
}
a) x= -1, y= 1
b) x= 1, y= 1
c) x= 3, y= 2
d) x= 2, y= 1
Solution: (a) The value of x is decremented from 11 to -1 (each in step is determine by 3). The value of y will be 1 mod 2 (The last value of x inside loop)=1
13. In C three way transfer of control using a single statement is possible by using
a) Unary operator
b) Logical operator
c) Ternary operator
d) None
Solution: (c) Ternary operator
14. What will the output?
#include <stdio.h>
int main()
{
int x=5, y=6, z=7;
if(x-y)
z = z++;
z = --z;
printf("%d", z);
return 0;
}
Answer: 6
Solution: (x-y)=-1 which is not equal to 0. Hence, if condition will be executed, which will store the value of z as 7 (as it is post increment). Finally, due to pre-decrement of z, the final value of z will be 6.
15. Find the output of the following C program
#include <stdio.h>
main()
{
int i = 0;
if(i==0)
{
i=i+1;
break;
}
printf("%d",i);
}
a) 0
b) 1
c) No output
d) Compiler error
Solution: (d) ‘break’ statement is applicable in loop and switch statements. It is not allowed inside if statement. Thus the program will show compiler error.
Comments
Post a Comment