Welcome Guest | Sign in | Register

Home > Java Programming > java.lang.class > Questions and Answers

Exercise:

Section 1

01. What will be the output of the program?

public class WrapTest
{
public static void main(String [] args)
{
int result = 0;
short s = 42;
Long x = new Long("42");
Long y = new Long(42);
Short z = new Short("42");
Short x2 = new Short(s);
Integer y2 = new Integer("42");
Integer z2 = new Integer(42);

if (x == y) /* Line 13 */
result = 1;
if (x.equals(y) ) /* Line 15 */
result = result + 10;
if (x.equals(z) ) /* Line 17 */
result = result + 100;
if (x.equals(x2) ) /* Line 19 */
result = result + 1000;
if (x.equals(z2) ) /* Line 21 */
result = result + 10000;

System.out.println("result = " + result);
}
}
A. result = 1 B. result = 10
C. result = 11 D. result = 11010

Answer and Explanation

Answer: result = 10

Explanation:
Line 13 fails because == compares reference values, not object values. Line 15 succeeds because both String and primitive wrapper constructors resolve to the same value (except for the Character wrapper). Lines 17, 19, and 21 fail because the equals() method fails if the object classes being compared are different and not in the same tree hierarchy.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
02. 2. What will be the output of the program?

public class ObjComp
{
public static void main(String [] args )
{
int result = 0;
ObjComp oc = new ObjComp();
Object o = oc;

if (o == oc)
result = 1;
if (o != oc)
result = result + 10;
if (o.equals(oc) )
result = result + 100;
if (oc.equals(o) )
result = result + 1000;

System.out.println("result = " + result);
}
}
A. 1 B. 10
C. 101 D. 1101

Answer and Explanation

Answer: 1101

Explanation:
Even though o and oc are reference variables of different types, they are both referring to the same object. This means that == will resolve to true and that the default equals() method will also resolve to true.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
03. What will be the output of the program?

String x = "xyz";
x.toUpperCase(); /* Line 2 */
String y = x.replace('Y', 'y');
y = y + "abc";
System.out.println(y);
A. abcXyZ B. abcxyz
C. xyzabc D. XyZabc

Answer and Explanation

Answer: xyzabc

Explanation:
Line 2 creates a new String object with the value "XYZ", but this new object is immediately lost because there is no reference to it. Line 3 creates a new Stringobject referenced by y. This new String object has the value "xyz" because there was no "Y" in the String object referred to by x. Line 4 creates a new Stringobject, appends "abc" to the value "xyz", and refers y to the result.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
04. Which of the following are valid calls to Math.max? 

1. Math.max(1,4) 
2. Math.max(2.3, 5)
3. Math.max(1, 3, 5, 7)
4. Math.max(-1.5, -2.8f) 
A.  1,2 and 4 B. 2,3 and 4
C. 1,2 and 3 D. 3 and 4

Answer and Explanation

Answer:  1,2 and 4

Explanation:
(1), (2), and (4) are correct. The max() method is overloaded to take two arguments of type int, long, float, or double.
(3) is incorrect because the max() method only takes two arguments.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
05. Which of the following would compile without error?


A.  int a = Math.abs(-5);
B. int b = Math.abs(5.0);
C. int c = Math.abs(5.5F);
D. int d = Math.abs(5L);

Answer and Explanation

Answer:  int a = Math.abs(-5);

Explanation:
The return value of the Math.abs() method is always the same as the type of the parameter passed into that method.
In the case of A, an integer is passed in and so the result is also an integer which is fine for assignment to "int a".
The values used in B, C & D respectively are a double, a float and a long. The compiler will complain about a possible loss of precision if we try to assign the results to an "int".

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
06. Which of the following will produce an answer that is closest in value to a double, d, while not being greater than d?
A. (int)Math.min(d); B. (int)Math.max(d);
C. (int)Math.abs(d); D. (int)Math.floor(d);

Answer and Explanation

Answer: (int)Math.floor(d);

Explanation:
The casting to an int is a smokescreen. Use a process of elimination to answer this question:
Option D is the correct answer, it is syntathecially correct and will consistently return a value less than d.
Option A and B are wrong because both the min() and max() methods require 2 arguments whereas here they are passed only one parameter.
Option C is wrong because it could return a value greater than d (if d was negative).

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum



Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved 2012-2015 SoftLucent.