Sail E0 Webinar

MCQs

Total Questions : 30 | Page 3 of 3 pages
Question 21.


Which of the following errors would be reported by the compiler on compiling the program given below?


#include<stdio.h>
int main()
{
int a = 5;
switch(a)
case 1:
printf("First");
case 2:
printf("Second");
case 3 + 2:
printf(Third");
case 5:
printf("Final");
break;
}
return 0;
}
  1.    There is no break statement in each case.
  2.    Expression as in case 3 + 2 is not allowed.
  3.    Duplicate case case 5:
  4.    No error will be reported.
 Discuss Question
Answer: Option C. -> Duplicate case case 5:

Because, case 3 + 2: and case 5: have the same constant value 5.



Question 22.


Point out the error, if any in the while loop.


#include<stdio.h>
int main()
{
int i=1;
while()
{
printf("%d\n", i++);
if(i>10)
break;
}
return 0;
}
  1.    There should be a condition in the while loop
  2.    There should be at least a semicolon in the while
  3.    The while loop should be replaced with for loop.
  4.    No error
 Discuss Question
Answer: Option A. -> There should be a condition in the while loop

The while() loop must have conditional expression or it shows "Expression syntax" error.

Example: while(i > 10){ ... }


Question 23.


Point out the error, if any in the program.


#include<stdio.h>
int main()
{
int a = 10;
switch(a)
{
}
printf("This is c program.");
return 0;
}
  1.    Error: No case statement specified
  2.    Error: No default specified
  3.    No Error
  4.    Error: infinite loop occurs
 Discuss Question
Answer: Option C. -> No Error

There can exists a switch statement, which has no case.


Question 24.


Point out the error, if any in the for loop.


#include<stdio.h>
int main()
{
int i=1;
for(;;)
{
printf("%d\n", i++);
if(i>10)
break;
}
return 0;
}
  1.    There should be a condition in the for loop
  2.    The two semicolons should be dropped
  3.    The for loop should be replaced with while loop.
  4.    No error
 Discuss Question
Answer: Option D. -> No error

Step 1: for(;;) this statement will genereate infinite loop.

Step 2: printf("%d`setminus`n", i++); this statement will print the value of variable i and

 increement i by 1(one).

Step 3: if(i>10) here, if the variable i value is greater than 10, then the for loop breaks.

Hence the output of the program is
1
2
3
4
5
6
7
8
9
10



Question 25.


What will be the output of the program?


#include<stdio.h>
int main()
{
int x, y, z;
x=y=z=1;
z = ++x || ++y && ++z;
printf("x=%d, y=%d, z=%d\n", x, y, z);
return 0;
}
  1.    x=2, y=1, z=1
  2.    x=2, y=2, z=1
  3.    x=2, y=2, z=2
  4.    x=1, y=2, z=1
 Discuss Question
Answer: Option A. -> x=2, y=1, z=1

Step 1: x=y=z=1; here the variables x ,y, z are initialized to value '1'.

Step 2: z = ++x || ++y && ++z; becomes z = ( (++x) || (++y && ++z) ). Here ++x

becomes 2. So there is no need to check the other side because ||(Logical OR) condition is 

satisfied.(z = (2 || ++y && ++z)). There is no need to process ++y && ++z. Hence it returns

 '1'. So the value of variable z is '1'

Step 3: printf("x=%d, y=%d, z=%d`setminus`n", x, y, z); It prints "x=2, y=1, z=1". here x is increemented

 in previous step. y and z are not increemented.



Question 26.


What will be the output of the program?


#include<stdio.h>
int main()
{
char j=1;
while(j < 5)
{
printf("%d, ", j);
j = j+1;
}
printf("\n");
return 0;
}
  1.    1 2 3 ... 127
  2.    1 2 3 ... 255
  3.    1 2 3 ... 127 128 0 1 2 3 ... infinite times
  4.    1, 2, 3, 4
 Discuss Question
Answer: Option D. -> 1, 2, 3, 4


Question 27.


What will be the output of the program?


#include<stdio.h>
int main()
{
int i = 1;
switch(i)
{
printf("Hello\n');
case 1:
printf("Hi\n");
break;
case 2:
printf("\nBye\n");
break;
}
return 0;
}
  1.    Hello Hi
  2.    Hello Bye
  3.    Hi
  4.    Bye
 Discuss Question
Answer: Option C. -> Hi

switch(i) has the variable i it has the value '1'(one).

Then case 1: statements got executed. so, it prints "Hi". The break; statement make the 

program to be exited from switch-case statement.

switch-case do not execute any statements outside these blocks case and default

Hence the output is "Hi".


Question 28.



#include<stdio.h>
int main()
{
int i=4;
switch(i)
{
default:
printf("This is default\n");
case 1:
printf("This is case 1\n");
break;
case 2:
printf("This is case 2\n");
break;
case 3:
printf("This is case 3\n");
}
return 0;
}
  1.    This is default This is case 1
  2.    This is case 3 This is default
  3.    This is case 1 This is case 3
  4.    This is default
 Discuss Question
Answer: Option A. -> This is default This is case 1

Answer: Option(A)

In the very begining of switch-case statement default statement is encountered. 

So, it prints "This is default".

In default statement there is no break; statement is included. So it prints the case 

1 statements. "This is case 1".

Then the break; statement is encountered. Hence the program exits from the switch-case block.


Question 29.


What will be the output of the program?


#include<stdio.h>
int main()
{
int x = 10, y = 20;
if(!(!x) && x)
printf("x = %d\n", x);
else
printf("y = %d\n", y);
return 0;
}
What will be the output of the program?
  1.    y =20
  2.    x = 0
  3.    x = 10
  4.    x = 1
 Discuss Question
Answer: Option C. -> x = 10

The logical not operator takes expression and evaluates to true if the expression is false and 

evaluates to false if the expression is true. In other words it reverses the value of the expression.

Step 1: if(!(!x) && x)
Step 2: if(!(!10) && 10)
Step 3: if(!(0) && 10)
Step 3: if(1 && 10)
Step 4: if(TRUE) here the if condition is satisfied. Hence it prints x = 10.


Question 30.


What will be the output of the program?


#include<stdio.h>
int main()
{
int i=3;
switch(i)
{
case 1:
printf("Hello\n");
case 2:
printf("Hi\n");
case 3:
continue;
default:
printf("Bye\n");
}
return 0;
}
  1.    Error: Misplaced continue
  2.    Bye
  3.    No output
  4.    Hello Hi
 Discuss Question
Answer: Option A. -> Error: Misplaced continue

Answer: Option A

The keyword continue cannot be used in switch case. It must be used in for or while or do 

while loop. If there is any looping statement in switch case then we can use continue.


Latest Videos

Latest Test Papers