EzDevInfo.com

int interview questions

Top int frequently asked interview questions

Java int to String - Integer.toString(i) vs new Integer(i).toString()

Sometimes java puzzles me.
I have a huge amount of int initializations to make.

What's the real difference?

  1. Integer.toString(i)
  2. new Integer(i).toString()

Source: (StackOverflow)

How to convert float to int with Java

I used the following line to convert float to int, but it's not as accurate as I'd like:

 float a=8.61f;
 int b;

 b=(int)a;

The result is : 8 (It should be 9)

When a = -7.65f, the result is : -7 (It should be -8)

What's the best way to do it ?


Source: (StackOverflow)

Advertisements

Java Array Sort descending?

Is there any EASY way to sort an array in descending order like how they have a sort in ascending order in the Arrays class?

Or do I have to stop being lazy and do this myself :[


Source: (StackOverflow)

How can I convert String to Int?

I have

TextBoxD1.Text

and I want to convert it to 'int' to store it in a database. How can I do this?


Source: (StackOverflow)

Swift - Converting String to Int

So my application is very basic but I'm new, new to the Xcode platform. The application basically calculates acceleration by inputting Initial and final velocity and time and then use a formula to calculate acceleration. However, since the values in the text boxes are string, I am unable to convert them to integers.

@IBOutlet var txtBox1 : UITextField
@IBOutlet var txtBox2 : UITextField
@IBOutlet var txtBox3 : UITextField
@IBOutlet var lblAnswer : UILabel


@IBAction func btn1(sender : AnyObject) {

    let answer1 = "The acceleration is"
    var answer2 = txtBox1
    var answer3 = txtBox2
    var answer4 = txtBox3

Source: (StackOverflow)

Convert int to string?

How can I convert an int datatype into a string datatype in C#?


Source: (StackOverflow)

Convert Int to String in Swift

I'm trying to work out how to cast an Int into a String in Swift.

I figure out a workaround, using NSNumber but I'd love to figure out how to do it all in Swift.

let x : Int = 45
let xNSNumber = x as NSNumber
let xString : String = xNSNumber.stringValue

Source: (StackOverflow)

Why does int i = 1024 * 1024 * 1024 * 1024 compile without error?

The limit of int is from -2147483648 to 2147483647.

If I input

int i = 2147483648;

then Eclipse will prompt a red underline under "2147483648".

But if I do this:

int i = 1024 * 1024 * 1024 * 1024;

it will compile fine.

public class Test {
    public static void main(String[] args) {        

        int i = 2147483648;                   // error
        int j = 1024 * 1024 * 1024 * 1024;    // no error

    }
}

Maybe it's a basic question in Java, but I have no idea why the second variant produces no error.


Source: (StackOverflow)

Way to get number of digits in an int?

Is there a neater way for getting the length of an int as this?

int length = String.valueOf(1000).length();

Source: (StackOverflow)

Easiest way to convert int to string in C++

What is the easiest way to convert from int to equivalent string in C++. I am aware of two methods. Is there any easier way?

1.

int a = 10;
char *intStr = itoa(a);
string str = string(intStr);

2.

int a = 10;
stringstream ss;
ss << a;
string str = ss.str();

Source: (StackOverflow)

"is" operator behaves unexpectedly with integers

Why does the following behave unexpectedly in Python?

>>> a = 256
>>> b = 256
>>> a is b
True           # This is an expected result
>>> a = 257
>>> b = 257
>>> a is b
False          # What happened here? Why is this False?
>>> 257 is 257
True           # Yet the literal numbers compare properly

I am using Python 2.5.2. Trying some different versions of Python, it appears that Python 2.3.3 shows the above behaviour between 99 and 100.

Based on the above, I can hypothesize that Python is internally implemented such that "small" integers are stored in a different way than larger integers and the is operator can tell the difference. Why the leaky abstraction? What is a better way of comparing two arbitrary objects to see whether they are the same when I don't know in advance whether they are numbers or not?


Source: (StackOverflow)

Convert boolean to int in Java

What is the most accepted way to convert a boolean to an int in Java?


Source: (StackOverflow)

Why can I pass 1 as a short, but not the int variable i?

Why does the first and second Write work but not the last? Is there a way I can allow all 3 of them and detect if it was 1, (int)1 or i passed in? And really why is one allowed but the last? The second being allowed but not the last really blows my mind.

Demo to show compile error

using System;
class Program
{
    public static void Write(short v) { }
    static void Main(string[] args)
    {
        Write(1);//ok
        Write((int)1);//ok
        int i=1;
        Write(i);//error!?
    }
}

Source: (StackOverflow)

Java - Convert integer to string [duplicate]

Given a number:

int number = 1234;

Which would be the "best" way to convert this to a string:

String stringNumber = "1234";

I have tried searching (googling) for an answer but no many seemed "trustworthy".


Source: (StackOverflow)