Use primitives and wrapper classes. Evaluate arithmetic and boolean expressions, using the Math API and by applying precedence rules, type conversions, and casting.
Manipulate text, including text blocks, using String and StringBuilder classes.
1. The correct answer is C.
Explanation:
double
can be directly assigned to a float
without casting.
double
cannot be directly assigned to a float
without casting because double
has a larger range and precision than a float
.boolean
can be cast to an int
.
boolean
values cannot be cast to int
in Java. They are not compatible types.String
can be assigned to an Object
reference variable.
String
is an instance of the Object
class, and hence it can be assigned to an Object
reference variable.char
is a reference data type.
char
is a primitive data type, not a reference data type.int
can store a long
value without any explicit casting.
int
cannot store a long
value without explicit casting because long
has a larger range than int
.2. The correct answer is A.
Explanation:
Let’s break down the expression a + b * c / a - b
step-by-step according to the order of operations:
b * c
= 10 * 15
= 150
150 / a
= 150 / 5
= 30
a + 30
= 5 + 30
= 35
35 - b
= 35 - 10
= 25
So, the value of result
is 25
, and the program prints 25
.
25
35
20
15
3. The correct answer is D.
Explanation:
StringBuilder
objects are immutable.
StringBuilder
objects are mutable, meaning they can be changed after they are created.String
objects can be modified after they are created.
String
objects are immutable, meaning once a String
object is created, it cannot be modified. Any modification results in a new String
object.StringBuilder
is synchronized and thread-safe.
StringBuilder
is not synchronized and is not thread-safe. If synchronization is required, StringBuffer
should be used instead.StringBuilder
provides methods for mutable sequence of characters.
StringBuilder
provides methods for a mutable sequence of characters, allowing for modification of the object without creating new instances.String
and StringBuilder
have the same performance characteristics for string manipulation.
String
and StringBuilder
do not have the same performance characteristics for string manipulation. StringBuilder
is generally more efficient for such operations because it is mutable and does not create new instances with each modification.4. The correct answers are A and B.
Explanation:
String
can be used, not just within methods. They can be part of class fields, method parameters, etc.5. The correct answer is D.
Explanation:
Math.round()
method returns a double
.
Math.round()
method returns a long
when given a double
argument and an int
when given a float
argument.Math.random()
method returns a random integer.
Math.random()
method returns a double
value between 0.0 (inclusive) and 1.0 (exclusive).Math.max()
method can only be used with integers.
Math.max()
method can be used with various numeric types, including int
, long
, float
, and double
.Math.pow()
method returns the result of raising the first argument to the power of the second argument.
Math.pow()
method returns the result of raising the first argument to the power of the second argument. Both arguments are of type double
.Math.abs()
method can only be used with positive numbers.
Math.abs()
method can be used with negative numbers to return their absolute value, and it works with various numeric types including int
, long
, float
, and double
.Do you like what you read? Would you consider?
Do you have a problem or something to say?