Implement IsOdd function in Java

50 %
50 %
Information about Implement IsOdd function in Java
Education

Published on November 30, 2007

Author: rohitsghatol

Source: authorstream.com

Implement isOdd function:  Implement isOdd function By Rohit Ghatol rohitsghatol@gmail.com Interview Begins:  Interview Begins Interviewer: Write a function to check if a number is Odd or not Interview Begins:  Interview Begins Interviewer: Write a function to check if a number is Odd or not Candidate: That’s simple Interview Begins:  Interview Begins Interviewer: Write a function to check if a number is Odd or not Candidate: That’s simple public boolean isOdd(int num){ if(num%2==1){ return true; } else { return false; } } Interview Begins:  Interview Begins Interviewer: Ok, what happens when I pass a negative value. Candidate : Ohh!! Yes, let me change it Interview Begins:  Interview Begins Interviewer: Ok, what happens when I pass a negative no. Candidate : Ohh!! Yes, let me change it public boolean isOdd(int num){ if(num%2==1 || num%2==-1){ return true; } else { return false; } } Interview Begins:  Interview Begins Interviewer: Ok!!...hmm.. Can you make it more efficient Candidate : hmmm…. Can’t say much Interview Begins:  Interview Begins Interviewer: What is more efficient than arithmetic operations? Interview Begins:  Interview Begins Interviewer: What is more efficient than arithmetic operations? Candidate : On what terms efficient? Interviewer: CPU cycles… Candidate : Can’t say.. Interviewer : How about bitwise operations? Candidate : Ok so the hint is use bitwise operator!! Interview Begins:  Interview Begins Interviewer: Yes, Can you try? Candidate: let me see.. public boolean isOdd(int num){ if(num&1==1) return true; } else { return false; } } Interview Begins:  Interview Begins Interviewer: Explain Candidate: In binary, the rule for odd number is, last bit is always 1. I applied same rule here and used and operator. num&1 will only be 1 (0001) when the last bit of num is 1 (not 0) indicating it is odd Interviewer: Super, you nailed it!!

Add a comment

Related presentations

Related pages

Is there a method that calculates a factorial in Java ...

Is there a method that calculates a factorial in Java? ... Now let's implement it in Java: ... Factorial is highly increasing discrete function.So I think ...
Read more

Java - isPrime function - Stack Overflow

Java - isPrime function. ... What is wrong with the function? java function methods primes. share | improve this question. ... Java 8: (Example with lambda ...
Read more

Object (Java Platform SE 7 ) - Oracle

equals(java.lang.Object), System.identityHashCode(java.lang.Object) ... First, if the class of this object does not implement the interface Cloneable, ...
Read more

Java Lambda Expressions vs Method References - DZone Java

Now we can use lambda expressions to implement functional ... Java Lambda Expressions vs Method References ... interface named Function, ...
Read more

CS132 - Lab #7 - Recursion

It is fairly straight forward to implement an ... an iterative implementation of the function as a Java ... The isEven and isOdd methods should accept a ...
Read more

Symbolic Methods - Introduction to Programming in Java: An ...

9.2 Symbolic Methods. ... An elementary function is one that can be obtained from rational-valued functions by a ... re-implement Rational.java, ...
Read more

Math (Java Platform SE 7 ) - Oracle

The class Math contains methods for performing basic numeric operations such as the ... new java.util .Random() This new ... Returns the signum function of ...
Read more

October 2015: JavaScript Iterators and Generators :: OCI

JavaScript Iterators and Generators ... class or write a function like the ... long way toward being able to use and implement ...
Read more