C Programming :: Control Instructions

NA
SHSTTON
10
Solv. Corr.
2
Solv. In. Corr.
12
Attempted
0 M:0 S
Avg. Time

1 / 33

What is the output of the following 'C' program?
#include<stdio.h>
void main()
{
int i=3;

switch(i)
{
default:printf("zero");
 case 1: printf("one");
 break;
case 2:printf("two");
break;
case 3: printf("three");
break;
}
}

Azero three

Bthree

Czero one

Dzero

Answer: Option B

Explanation:

The default case can be placed anywhere inside the loop. It is executed only when all other cases doesn't match.

Workspace

NA
SHSTTON
9
Solv. Corr.
8
Solv. In. Corr.
17
Attempted
0 M:0 S
Avg. Time

2 / 33

What is the output of the following 'C' program?
#include<stdio.h>
main()
{
int i=1,j=2;
switch(i)
{
case 1:  printf("GOOD");
break;
case j:  printf("BAD");
break;
}
}

ACompiler error: case label does not reduce to an integer constant

BGOOD

CBAD

DNone of these

Answer: Option A

Explanation:

The case statement can have only constant expressions (this implies that we cannot use variable names directly so an error).

Workspace

NA
SHSTTON
2
Solv. Corr.
13
Solv. In. Corr.
15
Attempted
0 M:0 S
Avg. Time

3 / 33

What is the output of the following 'C' program?
#include<stdio.h>
void main()
{
int i=10,j=20;
j=i,j?(i,j)?i:j:j;
printf("%d %d",i,j); 
}

A10 10

B10 20

C20 10

DCompilation Error

Answer: Option A

Explanation:

  1. notice the comma operator when variables are separated by them they are evaluated from left to right and only last value is considered ( it has less precedence than assignment operator)
    so due to the precedence of assignment being high the assignment is done first and then comma operator is evaluated be the expression finally evaluates to j=i,(j?(i,j)?i:j:j);
  2. so the value of i is assigned to j
    you can run the same program  with one more variable C the value of j will be equal to the value of C as per above logic

#include

void main()

{

int i=10,j=20;

int c=10000;

j=c,j?(i,j)?i:j:j;

printf("%d %d",i,j);

}


Workspace

NA
SHSTTON
10
Solv. Corr.
6
Solv. In. Corr.
16
Attempted
0 M:0 S
Avg. Time

4 / 33

What is the output of the following 'C' program?
#include<stdio.h>
void main() 
{
float i=1.5;
switch(i)
{
case 1: printf("1"); 
case 2: printf("2"); 
default : printf("0");
}
}

A1 2 0

B2

CCompiler error: switch quantity not an integer

D1

ENone of these

Answer: Option C

Explanation:

Switch statements can be applied only to integral types.

Workspace

NA
SHSTTON
23
Solv. Corr.
17
Solv. In. Corr.
40
Attempted
28 M:23 S
Avg. Time

5 / 33

How many times "Nagarro" will get printed?
#include<stdio.h>
int main()
{
   int x;

for (x=-1; x<10;x++)
{
if (x<5)
continue;
else
break;
printf("Nagarro");
}
return 0;
}

AInfinite times

B11 times

C0 times

D10 times

Answer: Option C

Explanation:

0 times as due to continue statement  the lower part of loop is ignored and next iteration continue when x is greater than or equal to 5  else part of loop execute and due to the break statement the loop terminates

Workspace

NA
SHSTTON
16
Solv. Corr.
15
Solv. In. Corr.
31
Attempted
0 M:0 S
Avg. Time

6 / 33

Choose the correct Option.

The use of the break statement in switch statement is


Aoptional

Bcompulsory

Cnot allowed. It gives an error message

Dto check an error

ENone of the above

Answer: Option A

Explanation:

Here is no explanation for this answer

Workspace

NA
SHSTTON
12
Solv. Corr.
4
Solv. In. Corr.
16
Attempted
0 M:0 S
Avg. Time

7 / 33

Choose the correct option

The type of the controlling statement of a switch statement cannot be of the type


Aint

Bchar

Cshort

Dfloat

ENone of these

Answer: Option D

Explanation:

Here is no explanation for this answer

Workspace

NA
SHSTTON
13
Solv. Corr.
3
Solv. In. Corr.
16
Attempted
0 M:0 S
Avg. Time

8 / 33

What will be the O/P?
void main()
{
    int i=0;
    switch(i){
        case 0 : printf("%d",i);break;
        case 1 : printf("%d",i);break;
    }
}

ACompile-time error

B0

C1

DRun-time error

Answer: Option B

Explanation:

Here is no explanation for this answer

Workspace

NA
SHSTTON
10
Solv. Corr.
4
Solv. In. Corr.
14
Attempted
0 M:0 S
Avg. Time

9 / 33

Given the following C program, which one of the alternatives is correct?
main()
{
char status; 
int balance; 
balance = 1000;
status= (balance>= 1000)? 'C': 'O'
}

Astatus = 'O'

Bstatus= 'C'

Cstatus= O;

Dstatus = NIL

Answer: Option B

Explanation:

the ternary statement (condition)? value1 : value2 can be converted to a similar if else function as follow
    if(condition==true){

        return value1;

    else{
        return value2;

    }

so as per above logic the given ternary statement is converted as
    if(balance>=1000){

        return 'C';

    }else{

        return 'O';

    }    

Workspace

NA
SHSTTON
4
Solv. Corr.
8
Solv. In. Corr.
12
Attempted
0 M:0 S
Avg. Time

10 / 33

What is the output of the following program?
void main()
{
int B, X, Y, Z;
X=1; Y=2; Z=3;
jf ((X > 1) || (Y > 1))
if (Z > 1)
printf ("O.K /n");
else break;
if((X > 1 && (Z > 3))
printf ("Bye /n");
printf ("Hello!"); 
}

AO.K. bye

BBye.

CO.K. Hello

DHello.

Answer: Option A

Explanation:

The program itself is wrong. 

Workspace