All Computer Science Resources
Example Questions
Example Question #1 : Standard Data Structures
int x = 10;
int y = 4;
int z = x / y;
What is the value of z
?
There are two reasons why this will not produce the expected output, 2.5:
- When dividing by two integer operands, the result will be returned as an
int
. Solve this by casting either of the operands (or the entire expression) to the float or double type. - When storing a floating-point value in an integer variable, the decimal is truncated (discarded, cut off, not considered). Even if was configured as suggested in step 1, and returned 2.5, the value 2 would be the value stored in
z
. Solve this value by declaringz
as a float.
Example Question #2 : Standard Data Structures
How do we set a method to return an Int in Swift(iOS)?
func() -> Int{}
func method() -> Int {}
func method->Int{}
method->Int {}
func method() -> Int {}
In Swift, all methods must first say what they are. They are functions, so they are prefixed with func. Next, methods must have a name. In this case, we named it method. All methods need to specify parameters, even if there are no parameters. So, method() i.e. no parameters. Finally, we wanted to return an Int. So we set the return type using -> Int.
Example Question #2 : Standard Data Structures
How do we set a method to return an Int in Swift(iOS)?
func() -> Int{}
func method() -> Int {}
func method->Int{}
method->Int {}
func method() -> Int {}
In Swift, all methods must first say what they are. They are functions, so they are prefixed with func. Next, methods must have a name. In this case, we named it method. All methods need to specify parameters, even if there are no parameters. So, method() i.e. no parameters. Finally, we wanted to return an Int. So we set the return type using -> Int.
Certified Tutor