ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
Do you have a collection of Interview Questions and interested to share with us!!
Please send that collection to along with your userid / name. ThanQ
Google
 
Categories  >>  Software  >>  Programming Languages  >>  C
 
 


 

 
 C interview questions  C Interview Questions
 C++ interview questions  C++ Interview Questions
 VC++ interview questions  VC++ Interview Questions
 Delphi interview questions  Delphi Interview Questions
 Programming Languages AllOther interview questions  Programming Languages AllOther Interview Questions
Question
value = 0xabcd;
   for (loop = 1; (value >> 1) & 1 | loop & 1; loop++) {
        foo();
        if (loop & 1)
                value >>= 1;
   }

   how many times is foo() executed?
 Question Submitted By :: Praveen
I also faced this Question!!     Rank Answer Posted By  
 
  Re: value = 0xabcd; for (loop = 1; (value >> 1) & 1 | loop & 1; loop++) { foo(); if (loop & 1) value >>= 1; } how many times is foo() executed?
Answer
# 1
5 times
 
Is This Answer Correct ?    0 Yes 0 No
Guest
 
  Re: value = 0xabcd; for (loop = 1; (value >> 1) & 1 | loop & 1; loop++) { foo(); if (loop & 1) value >>= 1; } how many times is foo() executed?
Answer
# 2
how it can execute 5 times
 
Is This Answer Correct ?    0 Yes 0 No
Kartik
 
 
 
  Re: value = 0xabcd; for (loop = 1; (value >> 1) & 1 | loop & 1; loop++) { foo(); if (loop & 1) value >>= 1; } how many times is foo() executed?
Answer
# 3
It is 5, as stated... anybody can figure that out by just compiling the code. I 
doubt that guy actually knows why.

This problem is heavy on bitwise operations, so you'll need to convert the 
numbers into binary.  The 0xABCD = 10, 11, 12, 13 = 1010  1011  1100  
1101.  Now make a table to hold the binary values of loop and value on each 
iteration and walk through the code.  Notice that "value" is shifted every other 
iteration.

At the start of the 6th iteration, the value of "value" is 1010101111001 and 
the "loop" is obviously 110.  This makes the for-loop check:

((value >> 1) & 1) | (110 & 1)
(101010111100 & 1) | (110 & 1)
0 | 0
0

And it exits the loop at this point.
 
Is This Answer Correct ?    2 Yes 0 No
Adam
 
  Re: value = 0xabcd; for (loop = 1; (value >> 1) & 1 | loop & 1; loop++) { foo(); if (loop & 1) value >>= 1; } how many times is foo() executed?
Answer
# 4
This is a very neat example of program where you need to
note some interesting things about values in order to easily
estimate how many times it will be executed.
So, here it goes
0. first of all, were looking for the value of LOOP for
which (value >> 1) & 1 | loop & 1 will be false.
1. loop variable in for loop will sequentially take values
that are : 
odd (1), even (2), odd (3), even (4) ...etc
so (loop & 1) part of for loop condition will sequentially
take values:
true(loop=1), false(loop=2),true,false...etc
2. the only thing to figure now is how (value >> 1) & 1 part
will behave. It is a good choice to convert hex abcd to bit
pattern, so we will have 1010101111001101
now only thing we have to do is see what will the boolean
part of condition will initially be:
1010101111001101 >> 1 = 101010111100110
101010111100110 & 1 = 0
initially, the (value >> 1) & 1 part is then FALSE.
3. now, we only have see 3 things:
a) in for loop body there is :
if (loop & 1)
      value >>= 1;
so EVERY TIME loop condition : (loop & 1) is TRUE (as we've
noticed earlier for each loop iteration, it has sequentially
values : true, false, true, false, true, false..etc), 
we CHANGE the value of '(value >> 1) & 1' part of condition
to its opposite (if it was FALSE, it becomes true)
b) were looking for the situation where both parts of
condition will be false, so all we have to do now is write
down how those parts behave starting from loop = 1.

LOOP PART : true , false , true , false , true , false(!)
OTHER PART: false , true, true, true, false , FALSE(!)

since we have FALSE and FALSE in both parts of for
condition, the for loop ends.
Now, we count how many times the for loop body (and so - foo
function) executed. Right, it IS 5.

It may sound a bit complicated, but this shows the way how
to solve algorithm problems that sound complicated at first
and use bit patterns (on interviews, no-one expects you to
do complicated binary/hex equatations in memory, you only
have to note some things, and simplify).
 
Is This Answer Correct ?    4 Yes 0 No
Jaroosh
 
  Re: value = 0xabcd; for (loop = 1; (value >> 1) & 1 | loop & 1; loop++) { foo(); if (loop & 1) value >>= 1; } how many times is foo() executed?
Answer
# 5
Shouldn't it be || though?
for (loop = 1; (value >> 1) & 1 || loop & 1; loop++)

????
 
Is This Answer Correct ?    0 Yes 0 No
Sbay
 

 
 
 
Other C Interview Questions
 
  Question Asked @ Answers
 
which operator having lowest precedence?? a.)+ b.)++ c.)= d.)%  4
pgm to find middle element of linklist(in efficent manner) Huawei2
writw a program to insert an element in the begning of a doubly linked list  1
how to convert binary to decimal and decimal to binary in C lanaguage  4
Would you rather wait for the results of a quicksort, a linear search, or a bubble sort on a 200000 element array? 1) Quicksort 2) Linear Search 3) Bubble Sort  2
how memory store byte Huawei3
what is the difference between arrays and linked list Tech-Mahindra15
10. Study the code: void show() main() { show(); } void show (char *s) { printf("%sn",s); } What will happen if it is compiled & run on an ANSI C Compiler? A)It will compile & nothing will be printed when it is executed B)it will compile but not link C)the compiler will generate an error D)the compiler will generate a warning Accenture4
#include main() { int i=1,j=2; switch(i) { case 1: printf("GOOD"); break; case j: printf("BAD"); break; } } ME5
main() { printf("\n %d %d %d",sizeof('3'),sizeof("3"),sizeof(3)); } wat is the o/p and how?  7
what is the associativity of bitwise OR operator?  1
Here is alphabets : abcdefgh 1) how to reverse. as hgfedcba 2) after reversal, how to group them in a pair hg fe dc ba.  2
the factorial of non-negative integer n is written n! and is defined as follows: n!=n*(n-1)*(n-2)........1(for values of n greater than or equal to 1 and n!=1(for n=0) Perform the following 1.write a c program that reads a non-negative integer and computes and prints its factorial. 2. write a C program that estimates the value of the mathematical constant e by using the formula: e=1+1/!+1/2!+1/3!+.... 3. write a c program the computes the value ex by using the formula ex=1+x/1!+xsquare/2!+xcube/3!+.... Ignou2
What is the main differences between C and Embedded C?  2
How to convert a binary number to Hexa decimal number?? (Note:Do not convert it into binary and to Hexadecimal) Subex1
Write a program to find the given number is odd or even without using any loops(if,for,do,while)  2
main() { int i = 1; int num[] = {1,2,3,4}; num[i] = i++; printf("%d", num[i]); } what will be the output? } NDS15
what is diff b/w huge & far & near pointer?? HCL1
Can you think of a way when a program crashed before reaching main? If yes how?  2
Blade logic interview question. 1st round is a written tests with 15 multiple questions from c and c++. All are simple basic question. Like int main () { Int i=65; Return printf(“%c”, i); } 2nd and 3rd round is technical interview. The position for which I was interview was core UNIX and c. Yes it is for system programming. The company has product name blade server. For their server they are creating their own command for their purpose. Example cd command. We can implement it in a c program by using the chdir() function. So the question asks related to PID, fork, pipe, shared memory, signal. Write a program in c which will act as cp command. BladeLogic1
 
For more C Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com