integer interview questions
Top integer frequently asked interview questions
I have two integer values a
and b
, but I need their ratio in floating point. I know that a<b
and I want to calculate a/b
, so if I use integer division I'll always get 0 with a remainder of a
.
How can I force c
to be a floating point number in Python in the following?
c = a / b
Source: (StackOverflow)
Why doesn't Java include support for unsigned integers?
It seems to me to be an odd omission, given that they allow one to write code that is less likely to produce overflows on unexpectedly large input.
Furthermore, using unsigned integers can be a form of self-documentation, since they indicate that the value which the unsigned int was intended to hold is never supposed to be negative.
Lastly, in some cases, unsigned integers can be more efficient for certain operations, such as division.
What's the downside to including these?
Source: (StackOverflow)
Is there any way to tell whether a string represents an integer (e.g., '3'
, '-17'
but not '3.14'
or 'asfasfas'
) Without using a try/except mechanism?
is_int('3.14') = False
is_int('-7') = True
Source: (StackOverflow)
I know that if you compare a boxed primitive Integer with a constant such as:
Integer a = 4;
if (a < 5)
a
will automatically be unboxed and the comparison will work.
However, what happens when you are comparing two boxed Integers
and want to compare either equality or less than/greater than?
Integer a = 4;
Integer b = 5;
if (a == b)
Will above code result in checking to see if they are the same object, or will it auto-unbox in that case?
What about:
Integer a = 4;
Integer b = 5;
if (a < 5)
?
Source: (StackOverflow)
I want to get, given a character, its ASCII
value.
For example, for the character a
, I want to get 97
, and vice versa.
Source: (StackOverflow)
Possible Duplicate:
Python “is” operator behaves unexpectedly with integers
Today I tried to debug my project and after a few hours of analysing I'd got this:
>>> (0-6) is -6
False
but,
>>> (0-5) is -5
True
Could you explain to me, why?
Maybe this is some kind of bug or very strange behavior.
> Python 2.7.3 (default, Apr 24 2012, 00:00:54) [GCC 4.7.0 20120414 (prerelease)] on linux2
>>> type(0-6)
<type 'int'>
>>> type(-6)
<type 'int'>
>>> type((0-6) is -6)
<type 'bool'>
>>>
Source: (StackOverflow)
Is there a way to declare an unsigned int in java.
or the question may be framed as this as well:
What is the Java equivalent of unsigned?
Just to tell you the context I was looking at the java's implementation of String.hashcode(). I wanted to test the possibility of collision if the integer were 32 unsigned int.
Source: (StackOverflow)
This question already has an answer here:
I was wondering if there was an alternative to itoa()
for converting an integer to a string because when I run it in visual Studio I get warnings, and when I try to build my program under Linux, I get a compilation error.
Source: (StackOverflow)
I want to create string using integer appended to it, in a for loop. Like this:
for i in range [1,10]:
string="string"+i
But it returns an error:
TypeError: unsupported operand type(s) for +: 'int' and 'str'
What's the best way to concatenate the String and Integer?
Source: (StackOverflow)
I was just sitting at my local Borders sipping coffee and reading More Joel on Software (for free) when I came across Joel Spolsky saying something about a particular type of programmer knowing the difference between an int and an Integer in Java/C# (Object Oriented Programming Languages).
After a quick 'brain check,' I realized, to my dismay, that I didn't know the answer.
Source: (StackOverflow)
I have a tuple of tuples from MySQL query like this:
T1 = (('13', '17', '18', '21', '32'),
('07', '11', '13', '14', '28'),
('01', '05', '06', '08', '15', '16'))
I'd like to convert all the string elements into integers and put it back nicely to list of lists this time:
T2 = [[13, 17, 18, 21, 32], [7, 11, 13, 14, 28], [1, 5, 6, 8, 15, 16]]
I tried to achieve it with "eval" but didn't get any decent result yet.
Source: (StackOverflow)
What is the difference between tinyint, smallint, mediumint, bigint and int in MySQL?
In what cases should these be used?
Source: (StackOverflow)
I have no idea why these lines of code return different values:
System.out.println(Integer.valueOf("127")==Integer.valueOf("127"));
System.out.println(Integer.valueOf("128")==Integer.valueOf("128"));
System.out.println(Integer.parseInt("128")==Integer.valueOf("128"));
The output is:
true
false
true
Why does the first one return true
and the second one return false
? Is there something different that I don't know between 127
and 128
? (Of course I know that 127
< 128
.)
Also, why does the third one return true
?
I have read the answer of this question, but I still didn't get how it can return true
, and why the code in second line returns false
.
Source: (StackOverflow)