Decision Making
- Which of the following is the correct output for the program given below?
#include <studio.h>
int main()
{
int a = 50, b = 60;
if(!(!a) &&a)
printf("a = %d\n",a)'
else
printf("b = %d\n",b);
return 0;
}
-
View Hint View Answer Discuss in Forum
The logical not operator takes expression and evaluates to true if the expression is false and evaluates to false if the expression is true.
Correct Option: C
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(!(!a) && a)
Step 2: if(!(!50) && 50)
Step 3: if(!(0) && 50)
Step 3: if(1 && 50)
Step 4: if(TRUE) here the if condition is satisfied. Hence it prints a = 50.
- Which of the following is the correct output for the program given below ?
#include <studio.h>
int main ( )
{
char ch;
if ((ch = printf ("")));
printf ("I am inside if \n");
else
printf ("I am inside else\n");
return 0;
}
-
View Hint View Answer Discuss in Forum
printf("")
returns the number of characters successfully printed. So the conditionCorrect Option: B
printf("")
returns the number of characters successfully printed. So the conditionif(ch = printf(""))
fails as ch is set to 0. Hence, the else clause is getting executed.
- Which of the following is the correct output for the program given below?
#include <studio.h>
int main ( )
{
int i = 5;
switch (i)
{
printf ("outside case\n");
case 5:
printf ("case 5\n");
break;
case 10:
printf ("case 10\n");
break;
}
return 0;
}
-
View Hint View Answer Discuss in Forum
Only matching case will be executing.
Correct Option: C
Only matching case will be executing.
So ans is case 5
- Which of the following is the correct output for the program given below?
#include <stdio.h>
int main ( )
{
int x = 600, y = 200, z;
if (!x >= 400)
y = 300;
z = 450;
printf ("y = %d z = %d\n", y, z);
return 0;
}
-
View Hint View Answer Discuss in Forum
Initially variables x = 600, y = 200 and c is not assigned.
Step 1: if(!x >= 400)
Step 2: if(!600 >= 400)
Step 3: if(0 >= 400)
Step 4: if(FALSE) Hence the if condition is failed.Correct Option: D
Initially variables x = 600, y = 200 and c is not assigned.
Step 1: if(!x >= 400)
Step 2: if(!600 >= 400)
Step 3: if(0 >= 400)
Step 4: if(FALSE) Hence the if condition is failed.
Step 5: So, variable z is assigned to a value '450'.
Step 6: printf("y = %d z = %d\n", y, z); It prints value of y and z.
Hence the output is "y = 200, z = 200"
- Which of the following is the correct output for the program given below ?
#include <stdio.h>
int main ()
{
int k, number = 50;
k = (number >10 ? (number <= 70 ? 220 : 710) : 650);
printf ("%d\n", k);
return 0;
}
-
View Hint View Answer Discuss in Forum
Both conditions are true.
Correct Option: A
Both conditions are true.
so output will be 220