Conditional and Looping Statements: Answer: Explanation

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 10

70.

find equivalent *(*(s+x)+y) ==> s[x][y]


Answer : content of s[x][y]

71.
int num[5];
num[5]=;
The 5 in the first and the second statements denotes ?

CONDITIONAL AND LOOPING STATEMENTS

72. main()
{
float me = 1.1;
double you = 1.1;
if(me==you)
printf("I love SWEETS");
else
printf("I hate MEDICINES");
}
Answer:
I hate MEDICINES
Explanation:
For floating point numbers (float, double, long double) the values cannot be
predicted exactly. Depending on the number of bytes, the precession with of the value represented
varies. Float takes 4 bytes and long double takes 10 bytes. So float stores 0.9 with less precision than
long double.
Rule of Thumb:
Never compare or at-least be cautious when using floating point numbers with
relational operators (== , >, <, <=, >=,!= ) .

73. main()
{
int i=0;

for(;i++;printf("%d",i)) ;
printf("%d",i);
}
Answer:
1
Explanation:
before entering into the for loop the checking condition is "evaluated". Here it
evaluates to 0 (false) and comes out of the loop, and i is incremented (note the
semicolon after the for loop).

74. main()
{
int i = 3;
for (;i++=0;) printf(“%d”,i);
}

Answer:
Compiler Error: Lvalue required.

www.Technicalsymposium.com
Explanation:
As we know that increment operators return rvalues and hence it cannot
appear on the left hand side of an assignment operation.

75. main()
{
int y;
scanf("%d",&y); // input given is 2000
if( (y%4==0 && y%100 != 0) || y%100 == 0 )
printf("%d is a leap year");
else
printf("%d is not a leap year");
}
Answer: 2000 is a leap year
Explanation: An ordinary program to check if leap year or not.

76. main()
{
int a= 0;int b = 20;char x =1;char y =10;
if(a,b,x,y)
printf("hello");
}
Answer:
hello
Explanation:
The comma operator has associativity from left to right. Only the rightmost value is
returned and the other values are evaluated and ignored. Thus the value of last
variable y is returned to check in if. Since it is a non zero value if becomes true so,
"hello" will be printed.

77. void main()


{
while(1){
if(printf("%d",printf("%d")))
break;
else
continue;
}
}
Answer:
Garbage values
Explanation:
The inner printf executes first to print some garbage value. The printf returns no of
characters printed and this value also cannot be predicted. Still the outer printf prints
something and so returns a non-zero value. So it encounters the break statement and
comes out of the while statement.

78. #include<conio.h>
main()
{
int x,y=2,z,a;
if(x=y%2) z=2;
a=2;

www.Technicalsymposium.com
printf("%d %d ",z,x);
}
Answer:
Garbage-value 0
Explanation:
The value of y%2 is 0. This value is assigned to x. The condition reduces to if (x) or
in other words if(0) and so z goes uninitialized.
Thumb Rule: Check all control paths to write bug free code.

79. main()
{
int i=10,j=20;
j = i, j?(i,j)?i:j:j;
printf("%d %d",i,j);
}

Answer:
10 10
Explanation:
The Ternary operator ( ? : ) is equivalent for if-then-else statement. So the question
can be written as:
if(i,j)
{
if(i,j)
j = i;
else
j = j;
}
else
j = j;

80. main()
{
int i = 3;
for (;i++=0;) printf(“%d”,i);
}

Answer:
Compiler Error: Lvalue required.
Explanation:
As we know that increment operators return rvalues and hence it cannot
appear on the left hand side of an assignment operation.

81. Find the output for the following C program


main()
{
         i=20,k=0;
        for(j=1;j<i;j=1+4*(i/j))
         {
k+=j<10?4:3;
      }
      printf("%d", k);

www.Technicalsymposium.com
}
    Answer . k=4

 
82. Find the output for the following C program
       int i =10
       main()
      {
int i =20,n;
      for(n=0;n<=i; )                                                                                                         

      {
int i=10;
        i++;
         }
       printf("%d", i);
}
      Answer :. i=20

 83.Find the output for the following C program 


main()

{
      int Y=10;
       if( Y++>9 && Y++!=10 && Y++>10)
printf("%d", Y);
     else
      printf("%d", Y); 
  

    }
     Answer : 13

84. Input a,b


s=0;
c=a;
d=b;
while(d<>0)
{
s=s+c;
d=d-1;
}
Print s

for given values of a,b find the output


5questions for different values of a,b

85.if((2a+b)<100)...... 5 ques were given with different values of a and b.

86. print '*' like this

www.Technicalsymposium.com
*
***
*****
*******
n times

87. Find the output


main()
{
int r;
if(r==5!==4)
printf("inside");
else
printf("outside");
printf("%d",r);
}
Answer :

88. Find the output


for(i==0,j==0;i<10,j<10;i++,j+);
printf("%d",j);
(compilation error,run time error,0)
Answer :

89. What will be the result of the following program?


main()
{
char p[]="String";
int x=0;
if(p=="String")
{
printf("Pass 1");
if(p[sizeof(p)-2]=='g')
printf("Pass 2");
else
printf("Fail 2");
}
else
{
printf("Fail 1");
if(p[sizeof(p)-2]=='g')
printf("Pass 2");
else
printf("Fail 2");
}
}

a) Pass 1, Pass 2
b) Fail 1, Fail 2
c) Pass 1, Fail 2
d) Fail 1, Pass 2

www.Technicalsymposium.com
e) syntax error during compilation
Answer :

90. What is the ouptut in the following program


main()
{
char c=-64;
int i=-32
unsigned int u =-16;
if(c>i)
{
printf("pass1,");
if(c<u)
printf("pass2");
else
printf("Fail2");
}
else
printf("Fail1);
if(i<u)
printf("pass2");
else
printf("Fail2")
}
a) Pass1,Pass2
b) Pass1,Fail2
c) Fail1,Pass2
d) Fail1,Fail2
e) None of these
Answer : (c)

91. main()
{
int l, b, con;
l=b=2;
con= (b==l) ? 1 : 0;
printf("%d",con);
}
what will be the value of con?
Answer :

92. main program:


i=foo(2) foo(int s)
{
if(!s)
return s;
else
{ int i=5; return i}
}
Answer : 5

93. Find the output


main()

www.Technicalsymposium.com
{
int k=0 i=0j=1;
if(i0)&&(k=2) printf(k);
if(i0|| k=0)
printf(k);
}
Answer : 2 2

94.Find the output:

main()
{
char ch;
int count=0;
while((ch = getch() != '\n')
{
while (ch == ' ')
ch = getch();
while((ch!=' ') && (ch!='.'))
{
count++;
ch=getch();
}
}
Answer : 11

95. What is the final value of i and how many times loop is
Executed ?

#include<stdio.h>
main()
{
int i,j,k,l,lc=0;

/* the input is given as 1234 567 */

printf("Enter the number string:<1234 567 >\n");


scanf("%2d%d%1d",&i,&j,&k);
for(;k;k--,i++)
for(l=0;l> printf("%d %d\n",i,l);)
printf("LOOPS= %d\n", lc-1);
}
Answer : ( i = 17, and loop is executed for 169 times)

96. Find the output

#include<stdio.h>
main()
{
int i;
for (i=1;i<100; i++)

www.Technicalsymposium.com
printf("%d %0x\n",i,i);
}
Answer: i is from 1 to 99 for the first format,
for the second format 1to9, ato f, 10 to 19,1ato1f, 20 to 29, etc

97. C program code

int zap(int n)
{
if(n<=1)then zap=1;
else zap=zap(n-3)+zap(n-1);
}
then the call zap(6) gives the values of zap
[a] 8 [b] 9 [c] 6 [d] 12 [e] 15

Answer : b

OPERATORS:
98. main()
{
int i=10;
i=!i>14;
printf ("i=%d",i);
}
Answer:
i=0
Explanation:
In the expression !i>14 , NOT (!) operator has more precedence than ‘ >’ symbol. ! is a unary logical
operator. !i (!10) is 0 (not of true is false). 0>14 is false (zero).

99. main()
{
int i=-1;
+i;
printf("i = %d, +i = %d \n",i,+i);
}
Answer:
i = -1, +i = -1
Explanation:
Unary + is the only dummy operator in C. Where-ever it comes you can just ignore
it just because it has no effect in the expressions (hence the name dummy operator).

100. main()
{
int i=-1;
-i;
printf("i = %d, -i = %d \n",i,-i);
}
Answer:
i = -1, -i = 1
Explanation:

www.Technicalsymposium.com
-i is executed and this execution doesn't affect the value of i. In printf first you just
print the value of i. After that the value of the expression -i = -(-1) is printed.

101. main()
{
int i=0;
while(+(+i--)!=0)
i- =i++;
printf("%d",i);
}
Answer: -1
Explanation:
Unary + is the only dummy operator in C. So it has no effect on the expression and
now the while loop is, while(i--!=0) which is false and so breaks out of while
loop. The value –1 is printed due to the post-decrement operator.

102. Comment:
     int a ,b=7
          a=b<4?b<<1:b>4?7>>1:a          
                                                     
            Answer.3

103. What is the value assigned to the variable a if b is 7


a=b>8?b<<2:b>4?b>>1:b;
Answer : 3

104. the value of the following expr (2^3)+(a^a) is


a) 1 b)2 c) 3 d) insufficient data
Answer :

105.The redirection operators > and >>

a) do the same function


b) differ : > overwrites, while >> appends
c) differ : > is used for input while >> is used for output
d) differ : > write to any file while >> write only to standard output
e) None of these
Answer : b

106. In C, x-=y+1 how will u represent it..


a. x=x+y+1 b. -x-y-1 c. -x+y+1
Answer :

107.Comment
(7^2)^3 and 7^2^3. What is the relation between the 2 . ie >,<,=
Answer : (7^2)^3<7^2^3.

108.Find the output


main()
{

www.Technicalsymposium.com
int x=5;
printf("%d %d %d\n",x,x<<2,x>>2);
}
Answer :

109. What will be the output of the following code?


int i=7. ,
printf("%d\n", i++ * i++);

A)49 8)56 C)64

Answer :

110. The statement 17 + (21 -6) * 2 returns

A)47.0
8)64
C)47
Answer :

INT, CHAR & STRINGS (INCLUDING PRINTF)

111. What is th output of the following program?                                                             


         int x= 0x65;
         main()
            {
             char x;
             printf("%d\n",x)
           }
      
a) compilation error     b) 'A'     c) 65       d) unidentified
Answer : d

112. What is the output of the following program


         main()

             {
              int a=10;
              int b=6;
              if(a=3)
              b++;
              printf("%d %d\n",a,b++);
              }

          a) 10,6 b)10,7 c) 3,6 d) 3,7 e) none

           Answer : d) 3,7

www.Technicalsymposium.com

You might also like