casting interview questions
        
            Top casting frequently asked interview questions
           
               
           
            
        
            
             
              
      
                 
                 
            
                 
                
                
            
            
What are the proper uses of:
- static_cast
- dynamic_cast
- const_cast
- reinterpret_cast
- C-style cast (type)value
- Function-style cast type(value)
How does one decide which to use in which specific cases?
        Source: (StackOverflow)
                  
                 
            
                 
                
                
            
            
Using PHP, what's the fastest way to convert a string like this: "123" to an integer?
Why is that particular method the fastest? What happens if it gets unexpected input, such as "hello" or an array?
        Source: (StackOverflow)
                  
                 
            
                 
                
                
            
            
What's the most idiomatic way in Java to verify that a cast from long to int did not lose any information?
This is my current implementation:
public static int safeLongToInt(long l) {
    int i = (int)l;
    if ((long)i != l) {
        throw new IllegalArgumentException(l + " cannot be cast to int without changing its value.");
    }
    return i;
}
        Source: (StackOverflow)
                  
                 
            
                 
                
                
            
            
I've heard that, in C++, the static_cast function should be preferred to C-style or simple function-style casting.  Is this true?  Why?
        Source: (StackOverflow)
                  
                 
            
                 
                
                
            
            
Consider the following code:
void Handler(object o, EventArgs e)
{
   // I swear o is a string
   string s = (string)o; // 1
   //-OR-
   string s = o as string; // 2
   // -OR-
   string s = o.ToString(); // 3
}
What is the difference between the three types of casting(okay, 3rd one is not a casting, but you get the intent... ), and which one should be preferred?
        Source: (StackOverflow)
                  
                 
            
                 
                
                
            
            
What is the best possible way to check if a string can be represented as a number in Python? 
The function I currently have right now is:
def is_number(s):
    try:
        float(s)
        return True
    except ValueError:
        return False
Which, not only is ugly and slow, seems clunky. However I haven't found a better method because calling float in the main function is even worse. 
        Source: (StackOverflow)
                  
                 
            
                 
                
                
            
    This question already has an answer here:
    
 
            I've been writing C and C++ code for almost twenty years, but there's one aspect of these languages that I've never really understood. I've obviously used regular casts i.e.
MyClass *m = (MyClass *)ptr;
all over the place, but there seem to be two other types of casts, and I don't know the difference. What's the difference between the following lines of code?
MyClass *m = (MyClass *)ptr;
MyClass *m = static_cast<MyClass *>(ptr);
MyClass *m = dynamic_cast<MyClass *>(ptr);
        Source: (StackOverflow)
                  
                 
            
                 
                
                
            
            
How can i convert an array like this to object?
    [128] => Array
        (
            [status] => Figure A.
 Facebook's horizontal scrollbars showing up on a 1024x768 screen resolution.
        )
    [129] => Array
        (
            [status] => The other day at work, I had some spare time
        )
)
        Source: (StackOverflow)
                  
                 
            
                 
                 
            
                 
                
                
            
            
In this question, someone suggested in a comment that I should not cast the results of malloc, i.e:
int *sieve = malloc(sizeof(int)*length);
rather than:
int *sieve = (int *)malloc(sizeof(int)*length);
Why would this be the case?
        Source: (StackOverflow)
                  
                 
            
                 
                
                
            
            
For example, lets say you have two classes:
public class TestA {}
public class TestB extends TestA{}
I have a method that returns a List<TestA> and I would like to cast all the objects in that list to TestB so that I end up with a List<TestB>.
        Source: (StackOverflow)
                  
                 
            
                 
                
                
            
            
NSInteger myInt = 1804809223;
NSLog(@"%i", myInt); <==== 
The code above produces an error:
Values of type "NSInteger" should not be used as format arguments: add an explicit cast to 'long' instead.
The correct NSLog message is actually NSLog(@"%lg", (long) myInt); Why do I have to convert the integer value of myInt to long if I want the value to display?  
        Source: (StackOverflow)
                  
                 
            
                 
                
                
            
            
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)
                  
                 
            
                 
                
                
            
            
Any way to cast java.lang.Double to java.lang.Integer? 
It throws an exception 
  "java.lang.ClassCastException: java.lang.Double incompatible with java.lang.Integer"
        Source: (StackOverflow)