search
Log In
7 votes
2.5k views

Consider the following $C$ code.

#include <stdio.h> 
#include <math.h> 
void main ()  
{  
      double pi = 3.1415926535;   
      int a = 1;  
      int i;    
      for (i=0; i < 3; i++) 
            if (a = cos(pi * i/2)) 
                  printf("% d", 1);  
            else printf("%d", 0); 
}

What would the program print?

  1. $000$
  2. $010$
  3. $101$
  4. $111$
in Programming
edited by
2.5k views
0
@makhdoom, Is there any typo, I mean check the condition in if. It is single = or double ==. Verify this?

4 Answers

12 votes
 
Best answer

Initially I thought there is a typo in this line

if (a = cos(pi * i/2))

Then I checked the ISRO paper and found that it is correct i.e. @makhdoom has not done any mistake.

According to that,

i = 0  : Cos(0), i.e. a = 1 i.e. condition will be True, hence it will print 1.

i = 1  : Cos(Pi/2) i.e. a = 0 i.e. Condition will be False, Hence It will print 0.

i = 2  : Cos(Pi) i.e. a = -1, i.e. Condition will be True, Hence It will print 1.

Hence answer is C. 101


selected by
1
@rude if condition is having assignment operator and not equality operator. So answer should be D). And even if we think as a==cos(pi*i/2) it would still not be 0 exactly it will wil 0.0000 sometihng. So answer should be D) right??
0

In that case answer will be 100, which is not in the option. 

0
@rude your answer is correct. Here bcoz data type of a is int answer will be 101. When a=0 then false bcoz it becomes equivalent to if(a) other wise it is true.
0
When I=2 than how 3.1*I/2=-1 give some explanation
7 votes

for i=0  

if(a=cos0)  that means  if(1) which means true . //here a is use for assignment. and on the basis of assigning value of a if condition is checked

then print 1

then for i=1

if(a=cos(pi/2) that means if(0) which is false then print 0 bcz it is treat as if(a) here a is 0

then for i=2

if(a=cos(pi)) that means if(-1) which is true then print 1  same thing hereit is treat as if(a) here a is -1

so answer is 101

0
plz verify @arjun sir ,@manojk, @srestha
0
Please recheck, conditional statements are false only in case of 0. Other that 0, whatever integer value you pass to conditional statement (if-condition), it will treat it as true. Therefore, the correct answer is C. 101
0

@ranjan

if(a = cos(pi * i/2))

here a is assignment ot condition check?

is it typo?

1
@ sreshta  now see my edit solution
0

but @ranjan tell me one thing u r checking condition with ==

and given

a = cos(pi * i/2)

why?

0
@rajan. look at the condition in if condition. Its =, not ==.
0
@srestha and @rude  

o shit!!  i forget to change it .so its blunder now see the solution yr
0
ya Now done :)
0
I think printf() stmt also wrong.if we want to print 0 and 1 wats the need of use %d. 0 and 1 are not variables.
1 vote
I am answering through elimination technique

cos(pi*0/2)=cos(0)=1

if(a=1) prints 1 //if(a=0) then only will not enter if condition else it enters//

so a and b options are eliminated.

cos(pi/2) surely not 1 (because cos 0=1 )

so answer is c

comments are welcome!!!
0 votes

Quoting C11 Assignment operators

An assignment operator stores a value in the object designated by the left operand. An assignment expression has the value of the left operand after the assignment,111) but is not an lvalue.

therefore, answer is d 111.

but, anyway i am not 100% sure. stackoverflow is also supporting option d.

Answer:

Related questions

3 votes
0 answers
1
578 views
Suppose we have variable logical records of lengths of 55 bytes, 1010 bytes and 2525 bytes while the physical block size in disk is 1515 bytes. What is the maximum and minimum fragmentation seen in bytes? 2525 and 55 1515 and 55 1515 and 00 1010 and 5 what is ... haven't mentioned that we have 2 blocks. All those who is supporting answer D uses 2 blocks. Some please give the concrete solution.
asked Nov 25, 2016 in Operating System cse7 578 views
4 votes
2 answers
2
2.8k views
In graphics, the number of vanishing points depends on The number of axes cut by the projection plane The center of projection The number of axes which are parallel to the projection plane The perspective projections of any set of parallel lines that are not parallel to the projection plane
asked Jun 5, 2016 in Computer Graphics makhdoom ghaya 2.8k views
3 votes
1 answer
3
2.4k views
The built-in base class in java, which is used to handle all exceptions is Raise Exception Error Throwable
asked Jun 5, 2016 in Java makhdoom ghaya 2.4k views
1 vote
1 answer
4
2k views
Which of the following is not provided as a service in cloud computing? Infrastructure as a service Architecture as a service Software as a service Platform as a service
asked Jun 5, 2016 in Cloud Computing makhdoom ghaya 2k views
...