Question 1
class Black {
public static void main(String[] args) {
short s1 = 1; //1
char c1 = 1; //2
byte b1 = s1; //3
byte b2 = c1; //4
final short s2 = 1; //5
final char c2 = 1; //6
byte b3 = s2; //7
byte b4 = c2; //8
}}Compile-time errors are generated at which lines?
a. 1
b. 2
c. 3
d. 4
e. 5
f. 6
g. 7
h. 8
Question 2
class Magenta {
static byte a = (byte)127, b = (byte)128, c = (byte)255, d = (byte)256;
public static void main(String args[]) {
System.out.print(a + " " + b + " " + c + " " + d);
}}What is the result of attempting to compile and run the program?
a. Prints: 127 128 255 256
b. Prints: 127 128 255 0
c. Prints: 127 -1 -127 0
d. Prints: 127 -128 -1 0
e. Run-time error
f. Compile-time error
g. None of the above
Question 3
class Primitives {
static void printFloat(float f) {System.out.println(f);}
static void printDouble(double d) {System.out.println(d);}
public static void main(String[] args) {
byte b = 1; // 1
short s = b; // 2
char c = s; // 3
int i = c; // 4
long l = i; // 5
float f = l; // 6
printFloat(i); // 7
printFloat(l); // 8
printDouble(l); // 9
}}A compile-time error is generated at which line?
a. 1
b. 2
c. 3
d. 4
e. 5
f. 6
g. 7
h. 8
i. 9
j. None of the above
Question 4
class Maroon {
public static void main (String[] args) {
int a = 1; // 1
short b = 1; // 2
long c = 1; // 3
a = c + a; // 4
c = b + a; // 5
}}A compile-time error is generated at which line?
a. 1
b. 2
c. 3
d. 4
e. 5
f. None of the above
Question 5
class Teal {
public static void main (String[] args) {
byte b = 1; // 1
long l = 1000; // 2
b += l; // 3
}}A compile-time error is generated at which line?
a. 1
b. 2
c. 3
d. None of the above
Question 6
class Sienna {
static double a; static float b; static int c; static char d;
public static void main(String[] args) {
a = b = c = d = 'a';
System.out.println(a+b+c+d == 4 * 'a');
}}What is the result of attempting to compile and run the program?
a. Prints: true
b. Prints: false
c. Compile-time error
d. Run-time error
e. None of the above
Question 6
class Sienna {
static double a; static float b; static int c; static char d;
public static void main(String[] args) {
a = b = c = d = 'a';
System.out.println(a+b+c+d == 4 * 'a');
}}
What is the result of attempting to compile and run the program?
a. Prints: true
b. Prints: false
c. Compile-time error
d. Run-time error
e. None of the above
Question 7
class UltraViolet {
public static void main (String[] args) {
char a = '\u002a', b = '\u0024'; // a = Asterisk *; b = Dollar Sign $
System.out.print(a + b); // 1
System.out.print(" ABC" + a + b); // 2
}}What is the result of attempting to compile and run the program?
a. Prints: 78 ABC*$
b. Prints: *$ ABC*$
c. Prints: 78 ABC78
d. Compile-time error
e. Run-time error
f. None of the above
No.
|
Answer
|
Remark
|
|
1
|
c d
|
3 4
|
This question demonstrates a variety of assignment
conversions. The compiler will implicitly do a narrowing conversion for an
assignment statement if the right hand operand is a compile time constant of
type byte, short, char, or int and the value falls within the range
of the variable on the left and if the variable on the left is of type byte, short,
or char. In this case, variables s1 and c1
are not compile time constants so the compiler will not do an implicit
narrowing conversion. However, variables s2
and c2 are compile time constants
that fall within the range of the left hand operand. For more information,
please see JLS section 5.2.
|
2
|
d
|
Prints: 127 -128 -1 0
|
Bytes are stored as 8 bit two's complement signed
integers. When an int primitive is cast to a byte, the three most significant
bytes are discarded and only the least significant byte remains. The most
significant bit of the remaining byte becomes the new sign bit. byte a =
(byte)127; // 01111111. byte b = (byte)128; // 10000000. byte c = (byte)255;
// 11111111. byte d = (byte)256; // 00000000.
|
3
|
c
|
3
|
Short is signed
and char is not signed so an
explicit cast is necessary when a short
is assigned to a char and vice
versa.
|
4
|
d
|
4
|
The assignment expression, a
= c + a, requires an explicit cast to type int. If one of the two operands of a numeric expression is of
type long and if the other operand
is of type int, short, char
or byte; then it will be promoted to
type long, and the result of the expression
will be of type long. (Note: The
rule does not apply to the shift operator.) The type long result can not be assigned to a variable of type int without an explicit cast.
|
5
|
d
|
None of the above
|
The compound assignment operators include an implicit cast
to the type of the left hand operand. The expression at line 3, b += l, does not require an explicit cast
to convert the right hand operand from type long
to type byte.
|
6
|
a
|
Prints: true
|
The literal, 'a',
is promoted to type int; and is then
multiplied by the value of the left operand, 4.
If one of the two operands of a numeric expression is of type int, then the other operand will be
promoted to type int if it is of
type short, char or byte.
|
7
|
a
|
Prints: 78 ABC*$
|
When char variables
a and b
are converted to String types they
are printed as *$. When not
converted to String types they are
promoted to type int, and are
printed as the numeric sum of 0x2a
and 0x24. At line 1, the expression,
a + b, can be evaluated as follows: 0x2a + 0x24 = 42 + 36 = 78. At line 2, the
first operand of the expression, "
ABC" + a + b, is of type String.
Since one operand of the first addition operator is of type String the other operand must be converted
to type String: (" ABC" + "*") + b = " ABC*" + b. The process is
repeated for the second addition operation: "
ABC*" + b = " ABC*" +
"$" = " ABC*$"
|
|
A Guide to the Perfect titanium headers - Titanium-Arts
ReplyDeleteThe titanium grey Titanium Armature is ford edge titanium a special type of titanium meaning titanium head mounted head and is attached how strong is titanium to a large metal plate. The body of a titanium titanium price per ounce head
b619t9bfqdk871 women sex toys,wolf dildo,dog dildo,horse dildo,couples sexy toys,love dolls,wholesale sex toys,dildos,sex toys w721b6adsya366
ReplyDelete