Sunteți pe pagina 1din 39

Chapter – 05

Decision Making and Branching

What is the output of the following program?

#include <stdio.h>
int main()
{
int x = 40, y = 30, z = 80;
if (x < y < z)
printf("REC");
else
printf("RIT");
return 0;
}

A. REC
B. RIT
C. Compile time error
D. None of these

ANSWER: A
What is the output of the following C code?

#include <stdio.h>
int main()
{
if (0) { printf("First statement"); }
if (-10) { printf("Second statement"); }
if (80 - 10 * 8) { printf("Third statement"); }
return 0;
}

A. Only First statement is printed


B. Only Second statement is printed
C. Both Second statement and Third statement are printed
D. No lines are printed

ANSWER: B

2 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


What is the output of the following C code?

#include <stdio.h>
int main()
{
float a = .5, b = .7;
if (b <= 7)
if (a < .5)
printf("REC");
else
printf("RIT");
else
printf("RSA");
return 0;
}

A. RIT
B. REC
C. RSA
D. None of these

ANSWER: A

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 3


What is the output of the following C code?

#include <stdio.h>
int main()
{
int x = 7, y = 10;
if (!(!x) && x)
printf("%d", x);
else
printf("%d", y);
return 0;
}

A. 1
B. 0
C. 7
D. 10

ANSWER: C

4 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


What is the output of the following C code?

#include <stdio.h>
int main()
{
int x = 7, y = 10;
if (x && y > 20)
printf("%d", x);
else
printf("%d", y);
return 0;
}

A. 1
B. 0
C. 7
D. 10

ANSWER: D

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 5


What will be the output of the following C code?

#include <stdio.h>
int main()
{
int x = 1;
if (x--)
printf("Hello");
--x;
else
printf("%d", x);
return 0;
}

A. Compiler error
B. 1
C. Hello
D. 0

ANSWER: A

6 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


What will be the output of the C program?

#include <stdio.h>
int main()
{
int i = 1;
switch (i)
{
case 1:
printf("Hai ");
default:
printf("Bye");
}
return 0;
}

A. Compilation Error
B. Bye
C. Hai
D. Hai Bye

ANSWER: D

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 7


What will be the output of the C program?

#include <stdio.h>
int main()
{
int i = 65;
switch (i)
{
case 65:
printf("Integer 65");
break;
case 'A':
printf("Char 65");
break;
default:
printf("Bye");
}
return 0;
}

A. Char 65
B. Integer 65
C. Bye
D. Compilation Error

ANSWER: D

8 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


What will be the output produced by the following C Code?

#include <stdio.h>
int main()
{
int x, y, z;
z = 2;
x = 3;
y = 4;
if (x - y)
z = z++;
z = z--;
printf("z = %d" , z );
return 0;
}

A. z =2
B. z=3
C. z=1
D. z=4

ANSWER: A

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 9


Consider the following C code. What will be assigned to the variable D?

#include <stdio.h>
int main()
{
int A = 5, B = 6, C = 4;
int D = 0;
if (((A - B) * 4) >= C)
D = C * 2;
else
D = C / 2 * 5;
return 0;
}

A. 0
B. 8
C. 2
D. 10

ANSWER: D

10 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Consider the following C code. What will be assigned to the variable S?

#include <stdio.h>
int main()
{
int P = 2, Q = 6, R = 3;
int S = 1;
if (((P + Q) / R) > S)
S = S + Q;
if ((P - Q) > S)
S = S - Q;
else
S = 5;
R = 9;
return 0;
}

A. 1
B. 7
C. -5
D. 5

ANSWER: D

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 11


Consider the following C code. What will be assigned to the variable A?

#include <stdio.h>
int main()
{
int P = 12, Q = 9, A = 4;
int M = 3;
if ((A - P) < M)
if ((P / Q) < A)
A = A + Q;
else
A = M;
else
A = 5;
Q = 13;
return 0;
}

A. 13
B. 5
C. 3
D. 8

ANSWER: A

12 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Consider the following C Code. What output will it produce if one gives 'b' as input?

#include <stdio.h>
int main()
{
char Ch_choice;
switch (Ch_choice = getchar())
{ /* begin switch */
case 'r':
case 'R':
{ /* begin Block #2 */
printf("In Case=%c", Ch_choice);
printf("Red");
break;
} /* end Block #2 */
case 'b':
case 'B':
{ /* begin Block #4 */
printf("In Case=%c", Ch_choice);
printf("Blue");
break;
} /* end Block #4 */
case 'g':
case 'G':
{ /* begin Block #5 */
printf("In Case=%c", Ch_choice);
printf("Green");
break;
} /* end Block #5 */
default:
{ /* begin DEFAULT Block */
printf("In Black");
} /* end DEFAULT Block */
} /* end switch */
return 0;
}

A. Red
B. Green
C. Blue
D. Black

ANSWER: C

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 13


Find the output of the following program:

#include <stdio.h>
int main()
{
char c = 'a';
int x = 5, y = 2;
int z;
switch (c)
{
case 'a':
z = x + y;
printf("%d", z);
break;
case 'b':
z = x - y;
printf("%d", z);
break;
case 'c':
z = x * y;
printf("%d", z);
break;
default:
printf("no value assigned");
}
return 0;
}

A. 7
B. 10
C. 3
D. no value assigned

ANSWER: A

14 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Find the output:

#include <stdio.h>
int main()
{
int a, b;
a = 3;
b = 3;
if (a == b)
printf("a and b are equal");
else if (a > b)
printf("a is greater than b");
else
printf("b is greater than a");
return 0;
}

A. a and b are equal


B. a is greater than b
C. b is greater than a
D. None of the above

ANSWER: A

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 15


What will be the output?

#include <stdio.h>
int main()
{
int a = 4, b = 15, c = 29;
if (c > b > a)
printf("TRUE");
else
printf("FALSE");
return 0;
}

A. TRUE
B. FALSE
C. Syntax Error
D. Compilation Error

ANSWER: B

(c > b > a) is treated as ((c > b) > a), associativity of '>' is left to right. Therefore, the value
becomes ((29 > 15) > 4) which becomes (1 > 4) thus FALSE.

16 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


What will be output?

#include <stdio.h>
int main()
{
int i = 12;
if (i == 10)
printf("i is 10");
else if (i == 15)
printf("i is 15");
else if (i == 20)
printf("i is 20");
else
printf("i is not present");
return 0;
}

A. i is 10
B. i is 15
C. i is 20
D. i is not present

ANSWER: D

None of the conditionals are true hence final else is executed

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 17


What will be the output?

#include <stdio.h>
int main()
{
int x = 2;
if (x = 1)
printf("TRUE");
else
printf("FALSE");
return 0;
}

A. TRUE
B. FALSE
C. Compilation Error
D. Compiler Dependent

ANSWER: A

TRUE if(x=1)... "=" is an assignment operator, so 1 will be assigned to x and condition will be
true due to if(1).

18 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


What will be the output?

#include <stdio.h>
int main()
{
if ((-10 && 10) && (4 > 3))
printf("Condition is true.");
else
printf("Condition is false.");
return 0;
}

A. Condition is true
B. Condition is false
C. Error
D. No output possible

ANSWER: A

Any non-zero value is treated as true for condition. Consider the expressions: if( (-10 && 10)
&& (3>4) ) =if( (1) && (1) ) =if(1)

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 19


What will be the output?

#include <stdio.h>
int main()
{
int x = 7, y = 10;
if (!(!x) && y)
printf("%d", x);
else
printf("%d", y);
return 0;
}

A. 7
B. 10
C. 1
D. 0

ANSWER: A

!(!x) = x i.e. x and y both are non-Zero value and equal to 1 (True). Therefore, the statement is
True.

20 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


What is the output of the following C code?

#include <stdio.h>
int main() {
int x = 1;
if (x == 0)
if (x >= 0)
printf("true");
else
printf("false");
return 0;
}

A. true
B. false
C. Depends on computer
D. Nothing is printed

ANSWER: D

x is initialized with 1 and the “if” statement compares it with 0, thus the “if condition” is “false”
and the nested “if” statements do not get executed. Hence, the program do not prints anything.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 21


What is the output of the following C code?

#include <stdio.h>
int main()
{
int a = 1;
if (a--)
printf("True\n");
if (++a)
printf("False\n");
return 0;
}

A. ‘True’ will be printed


B. ‘False’ will be printed
C. Both ‘True’ and ‘False’ will be printed
D. Nothing will be printed

ANSWER: C

‘a--’ post-increment the value of a. Thus, the if statement is executed as the value of a is
considered as 1 which is true. ‘++a’ pre-increment the value of a. Thus, the decremented value
of a (which is 0) is incremented first and then assigned. So, both the if statements are
executed ad correspondingly both True and False will be printed.

22 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Which statement is correct for the following lines?

switch(month)
{
case < 30:
printf(“It’s February”);
default:
printf(“It’s not February”);
}

A. It’s perfectly fine


B. It will print both statements
C. It will give a compilation error
D. More information required

ANSWER: C

Compilation error. Because only equality can be checked in case. Not <,>,<=,>= etc.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 23


What will be the output of the following program?

#include <stdio.h>
int main()
{
int x = 1;
switch (x)
{
case 1:
printf("Choice is 1 \n");
default:
printf("Choice other than 1 \n");
}
return 0;
}

A. Choice is 1
B. Choice other than 1
C. Both (a) and (b)
D. Syntax error

ANSWER: C

Since “break;” statement is not used after print statement, it will execute the default
instruction as well.

24 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


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

ANSWER: C

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.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 25


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

(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.

26 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Find the output of the following C program

#include <stdio.h>
int main()
{
int i = 0;
if (i == 0)
{
i = i + 1;
break;
}
printf("%d", i);
return 0;
}

A. 0
B. 1
C. No output
D. Compiler error

ANSWER: D

‘break’ statement is applicable in loop and switch statements. It is not allowed inside if
statement. Thus the program will show compiler error.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 27


The output of the following program will be

#include <stdio.h>
int main()
{
int a = 0, b = 1, c = -1;
if (a)
printf("IITKGP \n");
if (b)
printf("IITM \n");
if (c)
printf("IITR \n");
return 0;
}

A. IITKGP
B. IITM IITR
C. IITM
IITR
D. IITKGP
IITR

ANSWER: C

+1 and -1 is taken as true inside the if statement. Whereas 0 will be considered as false. Thus
the printf corresponding to b and c will be printed.

28 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


What is the output of the following program?

#include<stdio.h>
int main()
{
int i;
if (i = 0, 2, 3)
printf("NPTEL ");
else
printf("Programming on C ");
printf("%d\n", i);
return 0;
}

A. Programming on C 0
B. NPTEL 0
C. NPTEL 3
D. Compilation error

ANSWER: B

At first zero will assign in ‘i’ then comma operator returns the last value which is 3 and
condition becomes true.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 29


Find the output of the following program.

#include<stdio.h>
int main()
{
int x = 5;
if (x >= 6);
printf("hello");
return 0;
}

A. It will display hello


B. It will throw an error
C. Nothing will be displayed
D. Compiler dependent

ANSWER: A

The condition inside the if statement is false. The line is ended after if statement. Thus the
compilation comes to the next line and hello will be printed. Thus the program prints “hello”.

30 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


What will be the output?

#include <stdio.h>
int main()
{
int a = 100, b = 200, c = 300;
if (c > b > a)
printf("TRUE");
else
printf("FALSE");
return 0;
}

A. TRUE
B. FALSE
C. Syntax Error
D. Compilation Error

ANSWER: B

FALSE :: (c > b > a) is treated as ((c > b) > a), associativity of '>'
is left to right. Therefore the value becomes ((300 > 200) > 100) which becomes (1 > 100)
thus FALSE

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 31


What will be the output?

#include <stdio.h>
int main()
{
int x = 2;
if (x = 1)
printf("TRUE");
else
printf("FALSE");
return 0;
}

A. TRUE
B. FALSE
C. Compilation Error
D. Compiler Dependent

ANSWER: A

TRUE if(x=1)... "=" is an assignment operator, so 1 will be assigned to x and condition will be
true due to if(1).

32 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


What will be the output?

#include <stdio.h>
int main()
{
if ((-10 && 10) || (20 && -20))
printf("Condition is true.");
else
printf("Condition is false.");
return 0;
}

A. Condition is true
B. Condition is false
C. Error
D. No output possible

ANSWER: A

Any non-zero value is treated as true for condition. Consider the expressions: if( (-10 &&
10)||(20 && -20) ) =if( (1) || (1) ) =if(1)

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 33


What will be the output?

#include <stdio.h>
#define TRUE 1
int main()
{
if (TRUE)
printf("1");
printf("2");
else
printf("3");
printf("4");
return 0;
}

A. 1 2 3 4
B. 1 2
C. Compilation error
D. 3 4

ANSWER: C

Illegal else without matching if. You can use only one statement within the if( )without
parenthesis {...} .

34 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


What will be the output?

#include <stdio.h>
int main()
{
int x = -0.5;
if (!x)
printf("hello");
return 0;
}

A. hello
B. Compilation error
C. Nothing will be displayed
D. Compiler dependent

ANSWER: A

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 35


What is the output of the following C code?

#include <stdio.h>
int main()
{
int x = 0;
if (x == 1)
if (x >= 0)
printf("true\n");
else
printf("false\n");
return 0;
}

A. true
B. false
C. Depends on computer
D. Nothing is printed.

ANSWER: D

x is initialize with 0 and the if statement compares it with 1, thus the if condition is false and
the nested if statements do not get executed. Hence, the program do not prints anything.

36 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Is there anything wrong in the following program segment?

switch(someCharacter)
{
case ‘a’:
case ‘e’:
case ‘i’:
case ‘o’:
case ‘u’:
printf(“Vowel”);
break;
default:
printf(“Consonant”);
}

A. Yes- case blocks are not defined


B. No- it’s all O.K.
C. Compiler Dependent
D. More info required

ANSWER: B

It’s all fine. Unlike with for loops, or ifelse blocks, multiple statements can follow each other
without curly braces

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 37


What is wrong with the following program segment?

switch(temperature < 15 && windy == TRUE)


{
case !windy:
printf(“cold day”);
break;
default:
printf(“hot day”);
}

A. Nothing is wrong
B. Variable cannot be used in individual cases
C. Will print both the statements
D. Break is not allowed when using variable in a case checking

ANSWER: B

The switch portion can contain any constant or expression, but the individual case conditions
can only include constants, or expressions of constants (no variables allowed).

38 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


What is the output of the following C code?

#include <stdio.h>
int main()
{
int a = 1;
if (a--)
printf("True\n");
if (++a)
printf("False\n");
return 0;
}
A. True
B. False
C. Both ‘True’ and ‘False’ will be printed
D. Compilation error

ANSWER: C

‘a--’ post-decrement the value of a. Thus, the if statement is executed as the value of a is
considered as 1 which is true. ‘++a’ pre-increment the value of a. Thus, the decremented value
of a (which is 0) is incremented first and then assigned. So, both the if statements are
executed ad correspondingly both True and False will be printed.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 39

S-ar putea să vă placă și