1.4 Exercises
- True/False: All variables, parameters, and methods must have a declared type in Java, and the type can never change.
- Suppose we have a function
smaller(a, b)that takes in twointargumentsaandband returns the smaller of the two. What would the expressionString x = smaller(10, 20) + 3;output? - Choose all statements that are true in Java:
- All code must be part of a class.
- The end and beginning of code segments are delimited using curly brackets
{}. - All statements in Java end with a semi-colon
;. - Any code we want to run must be inside of a function
public static void main(String[] args).
Solutions
- True. See “Static Typing” for more information.
- This line of code would cause a compilation error because the declared type
Stringis incompatible with the type returned bysmallerand adding3, which would be anint. - All the following statements are true.