0% found this document useful (0 votes)
95 views

Fundamental Classes 2

The document contains 10 questions about fundamental Java classes like String, Integer, Float, etc. Each question provides multiple choice answers about the output or errors when compiling and running example code involving object instantiation and method calls. The questions test knowledge of class constructors, wrapper class behavior, and method compatibility.

Uploaded by

rahul rastogi
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views

Fundamental Classes 2

The document contains 10 questions about fundamental Java classes like String, Integer, Float, etc. Each question provides multiple choice answers about the output or errors when compiling and running example code involving object instantiation and method calls. The questions test knowledge of class constructors, wrapper class behavior, and method compatibility.

Uploaded by

rahul rastogi
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 14

Fundamental Classses

Question 1
class SRC104 {
public static void main (String[] args) {
System.out.print(Math.round(Float.NaN));
}}

What is the result of attempting to compile and run the program?

a.  Prints: NaN


b.  Prints: 0.0
c.  Prints: 0
d.  Compile-time error
e.  Run-time error
f.  None of the above

ANSWER
If NaN is the argument passed to Math.round, then the return value is
Prints:
1 c  zero. If the argument type is float, then the return type is int. If the

argument type is double, then the return type is long.  

Question 2
class D {
public static void main (String args[]) {
Byte a = new Byte("1");
byte b = a.byteValue();
short c = a.shortValue();
char d = a.charValue();
int e = a.intValue();
long f = a.longValue();
float g = a.floatValue();
double h = a.doubleValue();
System.out.print(b+c+d+e+f+g+h);
}}

What is the result of attempting to compile and run the program?

a.  Prints: 7
b.  Prints: 7.0
c.  Compile-time error
d.  Run-time error
e.  None of the above

ANSWER
A compile-time error is generated, because the Byte class does not
Compile-
2 c  have a charValue method. The Byte class extends the Number class
time error 
and implements all six of the methods declared in Number.  

Question 3
class A {
public static void main (String args[]) {
Integer i1 = new Integer(1);
Integer i2 = new Integer(i1);
System.out.print(i1.equals(i2));
}}

What is the result of attempting to compile and run the program?

a.  Prints: false


b.  Prints: true
c.  Compile-time error
d.  Run-time error
e.  None of the above

ANSWER
The Integer class has only two constructors: one accepts a primitive
Compile-
3 c  int, and the other accepts a String. There is no constructor that
time error 
accepts an argument that is an instance of type Integer.  

Question 4
class C {
public static void main (String args[]) {
Long a = new Long(1);
byte b = a.byteValue();
short s = a.shortValue();
char c = a.charValue();
int d = a.intValue();
long e = a.longValue();
float f = a.floatValue();
double g = a.doubleValue();
System.out.print(b+s+c+d+e+f+g);
}}
What is the result of attempting to compile and run the program?

a.  Prints: 7
b.  Prints: 7L
c.  Prints: 7.0
d.  Compile-time error
e.  Run-time error
f.  None of the above

ANSWER
Long is a subclass of the abstract class Number, and Long
implements all of the methods of Number: byteValue, shortValue,
Compile-
4 d  intValue, longValue, floatValue and doubleValue. The attempt to
time error 
invoke the charValue method on an instance of Long generates a
compile-time error, because there is no charValue method.  

Question 5
class B {
public static void main (String args[]) {
Double a = new Double(0xFFFF);
byte b = a.byteValue();
short c = a.shortValue();
int e = a.intValue();
long f = a.longValue();
float g = a.floatValue();
double h = a.doubleValue();
System.out.print(b+","+c+","+ (e+f+g+h == 4 * 0xFFFF));
}}

What is the result of attempting to compile and run the program?

a.  Prints: 0xFFFF,0xFFFF,false


b.  Prints: 0xFFFF,0xFFFF,true
c.  Prints: -1,-1,false
d.  Prints: -1,-1,true
e.  Compile-time error
f.  Run-time error
g.  None of the above

ANSWER
5 d  Prints: Double is a subclass of the abstract class Number, and implements all of
-1,- the methods of Number such as byteValue, shortValue, floatValue, etc.
1,true  In this case, the Double instance contains the value 0xFFFF. When that
value is converted to type byte the result is 0xFF which is also the two's
complement representation of the byte value -1. Similarly, 0xFFFF is
the two's complement representation of the short value -1. Please note
there is no Double.charValue method.  

Question 6
Which of the instance creation expressions produce a run-time error?

a.  new Float('A')


b.  new Float("A")
c.  new Float(1L)
d.  new Float("1L")
e.  new Float(0x10)
f.  new Float("0x10")
g.  new Float("010")

ANSWER
The Float constructor is overloaded: one version accepts a
primitive of type float; one accepts a primitive of type
double; one accepts a String representation of a floating-point
literal. The primitive char literal 'A' is converted to a float,
and is accepted by the constructor that declares a parameter
of type float. The String literals "NaN" and "Infinity" are
accepted by the Float constructor. A sign (+ or -) is optional.
new Float("A")  The API specification states that any other String must

new Float("1L")  represent a floating-point value; however, a little
6 d 
new experimentation proves that a String is acceptable if it can be

Float("0x10")  parsed as a decimal integer value. The leading 0 of an octal
value is ignored, and the String is parsed as a decimal value.
A String representation of a hexadecimal value is not
acceptable. The String "A" does not represent a floating-point
literal value; therefore, a NumberFormatException is thrown.
Arguments of type String can not contain an integer type
suffix, L or l. A floating-point suffix, F, f, D or d, is
acceptable, but the suffix has no impact on the result.

Question 7
class C {
public static void main(String[] args) {
Boolean b1 = Boolean.valueOf(true);
Boolean b2 = Boolean.valueOf(true);
Boolean b3 = Boolean.valueOf("TrUe");
Boolean b4 = Boolean.valueOf("tRuE");
System.out.print((b1==b2) + ",");
System.out.print((b1.booleanValue()==b2.booleanValue()) + ",");
System.out.println(b3.equals(b4));
}}

What is the result of attempting to compile and run the program?

a.  Prints: false,false,false


b.  Prints: false,false,true
c.  Prints: false,true,false
d.  Prints: false,true,true
e.  Prints: true,false,false
f.  Prints: true,false,true
g.  Prints: true,true,false
h.  Prints: true,true,true
i.  Compile-time error
j.  Run-time error
k.  None of the above

ANSWER
The Boolean class contains two public static final Boolean
instances: Boolean.FALSE wraps the primitive boolean value
false; Boolean.TRUE wraps the primitive boolean value true.
Depending on the value of the argument, the Boolean.valueOf
method returns a reference to either Boolean.FALSE or
Prints:
7 h  Boolean.TRUE. Reference variables b1 and b2 are both initialized
true,true,true 
with a reference value returned by the method invocation
expression Boolean.valueOf(true); so the equality expression
b1==b2 is true. Please note that the valueOf method that accepts
an argument of type primitive boolean was introduced in the 1.4
version of Java.  

Question 8
Which of the following class instance creation expressions would generate a run-time
error?

a.  new Short("1")


b.  new Short("-1")
c.  new Short("+1")
d.  new Short("1.0")
e.  new Short("0x1")
f.  new Short("011")

ANSWER
The Short class has only two constructors: one accepts a
primitive short; the other accepts a String. A String
argument must represent an integral primitive type. A
leading minus sign can be added to indicate a negative
value. A leading plus sign generates a run-time error. The
c  new Short("+1") 
constructor is not able to determine the radix of the String
8 d  new Short("1.0") 
value by examing a prefix such as 0 or 0x. The 0 prefix used
e  new Short("0x1") 
to identify octal values is accepted, but the String is parsed
as a decimal value. The prefix 0x generates a run-time error.
A run-time error is generated if the String argument is not
formatted as a decimal integer. A floating-point format
results in a run-time error.  

Question 9
Which of the following are not methods of the java.lang.String class?

a.  append
b.  concat
c.  delete
d.  insert
e.  replace
f.  substring
g.  valueOf

ANSWER
a  The StringBuffer class has methods named append, delete and
append  delete 
9 c  insert, but the String class does not. A typical trick question will
insert 
d  attempt to invoke StringBuffer methods on a String instance.  

Question 10
class SRC105 {
public static void main(String[] args) {
double d1 = Math.ceil(0.5);
double d2 = Math.ceil(1.5);
System.out.print(d1 + "," + d2);
}}
What is the result of attempting to compile and run the program?

a.  Prints: 0.0,1.0


b.  Prints: 0.0,2.0
c.  Prints: 1.0,1.0
d.  Prints: 1.0,2.0
e.  Compile-time error
f.  Run-time error
g.  None of the above

ANSWER
The Math.ceil method name is not overloaded. The Math.ceil method
Prints: accepts an argument of type double and returns a double. The
10 d 
1.0,2.0  returned value is the smallest whole number that is greater than or
equal to the argument.  

Question 11
class E {
public static void main (String[] args) {
Byte b1 = new Byte("1"), b2 = new Byte("1");
System.out.print((b1==b2)+","+(b1.equals(b2)));
}}

What is the result of attempting to compile and run the program?

a.  Prints: false,false


b.  Prints: false,true
c.  Prints: true,false
d.  Prints: true,true
e.  Compile-time error
f.  Run-time error
g.  None of the above

ANSWER
The expression b1==b2 compares the references of two instances of
Byte. The result is false, because the instances are distinct. The
Prints:
11 b  expression b1.equals(b2) compares the contents of two instances of
false,true 
Byte. The result is true, because the two instances contain the same
value.  

Question 12
class B {
public static void main (String args[]) {
Integer a = new Integer(256);
byte b = a.byteValue();
short c = a.shortValue();
int e = a.intValue();
long f = a.longValue();
float g = a.floatValue();
double h = a.doubleValue();
System.out.print(b+","+c+","+ (e+f+g+h == 4 * 256));
}}

What is the result of attempting to compile and run the program?

a.  Prints: 0,0,false


b.  Prints: 0,0,true
c.  Prints: 0,-1,false
d.  Prints: 0,-1,true
e.  Prints: -1,0,false
f.  Prints: -1,0,true
g.  Prints: -1,-1,false
h.  Prints: -1,-1,true
i.  Compile-time error
j.  Run-time error
k.  None of the above

ANSWER
The binary representation of 256 is one bit that is set to one followed
None of by eight bits that are set to zero. When 256 is converted to an eight bit
12 k  the byte value, the bit that is set to one is lost and only the bits that are set
above  to zero remain. When 256 is converted to a short, no information is
lost; so the value remains 256.  

Question 13
class F {
static String m(long i) {return "long";}
static String m(Long i) {return "Long";}
static String m(double i) {return "double";}
static String m(Double i) {return "Double";}
static String m(String i) {return "String";}
public static void main (String[] args) {
System.out.print(m(Long.parseLong("1")));
}}

What is the result of attempting to compile and run the program?


a.  Prints: long
b.  Prints: Long
c.  Prints: double
d.  Prints: Double
e.  Prints: String
f.  Compile-time error
g.  Run-time error
h.  None of the above

ANSWER
13 a  Prints: long  The Long.parseLong method returns a primitive long.  

Question 14
class D {
static boolean m(double v) {
return(v != v == Double.isNaN(v));
}
public static void main (String args[]) {
double d1 = Double.NaN;
double d2 = Double.POSITIVE_INFINITY;
double d3 = Double.MAX_VALUE;
System.out.print(m(d1) + "," + m(d2) + "," + m(d3));
}}

What is the result of attempting to compile and run the program?

a.  Prints: false,false,false


b.  Prints: false,false,true
c.  Prints: false,true,false
d.  Prints: false,true,true
e.  Prints: true,false,false
f.  Prints: true,false,true
g.  Prints: true,true,false
h.  Prints: true,true,true
i.  Compile-time error
j.  Run-time error
k.  None of the above

ANSWER
NaN is the only value that is not equal to itself. The
Prints:
14 h  Double.isNaN method returns the result of the expression (v !
true,true,true 
= v).  
Question 15
class E {
public static void main (String args[]) {
String s1 = Float.toString(1.0); // 1
String s2 = Float.toString(1.0f); // 2
String s3 = Float.toString(0xf); // 3
String s4 = Float.toString(010); // 4
String s5 = Float.toString('A'); // 5
}}

What is the result of attempting to compile and run the program?

a.  Compile-time error at 1


b.  Compile-time error at 2
c.  Compile-time error at 3
d.  Compile-time error at 4
e.  Compile-time error at 5
f.  Run-time error at 1
g.  Run-time error at 2
h.  Run-time error at 3
i.  Run-time error at 4
j.  Run-time error at 5
k.  None of the above

ANSWER
The Float.toString method is overloaded: one declares no
Compile-
parameters and returns the value wrapped by the Float instance;
15 a  time error at
the other accepts a primitive of type float. The literal, 1.0, is of

type double and can not be implicitly narrowed to type float.  

Question 16
class D {
public static void main (String[] args) {
Boolean b1 = new Boolean("trUE"); // 1
Boolean b2 = new Boolean("What's This?"); // 2
Boolean b3 = new Boolean(null); // 3
System.out.print(b1 + "," + b2 + "," + b3);
}}

What is the result of attempting to compile and run the program?

a.  Prints: false,false,false


b.  Prints: false,false,true
c.  Prints: false,true,false
d.  Prints: false,true,true
e.  Prints: true,false,false
f.  Prints: true,false,true
g.  Prints: true,true,false
h.  Prints: true,true,true
i.  Compile-time error
j.  Run-time error
k.  None of the above

ANSWER
The Boolean constructor is overloaded: one version accepts a
primitive boolean argument; the other accepts a String. If the
String value is the word true, then the new Boolean instance
Prints:
16 e  will contain the value true. Both upper and lower case letters
true,false,false 
are acceptable. If the String contains any word other than true
or if the reference is null, then the new instance will contain
the value false.  

Question 17
class F {
public static void main (String[] args) {
Short s1 = new Short("1"), s2 = new Short("1");
int a1 = s1.hashCode(), b1 = s2.hashCode();
System.out.print((s1==s2)+","+(s1.equals(s2))+","+(a1==b1));
}}

What is the result of attempting to compile and run the program?

a.  false,false,false
b.  false,false,true
c.  false,true,false
d.  false,true,true
e.  true,false,false
f.  true,false,true
g.  true,true,false
h.  true,true,true
i.  Compile-time error
j.  Run-time error
k.  None of the above

ANSWER
The equality expression s1==s2 compares the reference values
of two distinct instances of type Short. Since the instance are
distinct, the equality expression is false. The expression
s1.equals(s2) compares the values of two instances of type Short.
17 d  false,true,true 
Since both instances contain the value 1, the returned value is
true. The expression a1==b1 compares the hash codes of two
instances of Short. The result is true, because the two instances
contain the same value.  

Question 18
class MWC104 {
public static void main (String[] args) {
char[] c = {'a','b','c','d'};
String s1 = new String(c);
boolean b = true;
for (int i = 0; i < s1.length; i++) {
b &= (c[i] == s1.charAt(i));
}
System.out.print(b);
}}

What is the result of attempting to compile and run the program?

a.  Prints: false


b.  Prints: true
c.  Compile-time error
d.  Run-time error
e.  None of the above

ANSWER
Compile-time A compile-time error is generated due to the attempt to access
18 c 
error  the length method of the String class as though it were a variable.

Question 19
class MWC202 {
public static void main (String[] args) {
StringBuffer sb1 = new StringBuffer("ABC");
StringBuffer sb2 = new StringBuffer("ABC");
System.out.print((sb1==sb2)+","+sb1.equals(sb2));
}}

What is the result of attempting to compile and run the program?


a.  Prints: false,false
b.  Prints: false,true
c.  Prints: true,false
d.  Prints: true,true
e.  Compile-time error
f.  Run-time error
g.  None of the above

ANSWER
StringBuffer.equals does not override Object.equals. The
Prints: StringBuffer.equals method compares the reference values--not the
19 a 
false,false  contents of the StringBuffer instances. The expressions sb1==sb2
and sb1.equals(sb2) produce the same results

Question 20
class MWC203 {
public static void main (String[] args) {
String s1 = new String("ABC"), s2 = new String("ABC");
StringBuffer sb1 = new StringBuffer(s1);
StringBuffer sb2 = new StringBuffer(s2);
boolean b1 = s1.hashCode() == s2.hashCode();
boolean b2 = sb1.hashCode() == sb2.hashCode();
System.out.print(b1 + "," + b2);
}}

What is the result of attempting to compile and run the program?

a.  Prints: false,false


b.  Prints: false,true
c.  Prints: true,false
d.  Prints: true,true
e.  Compile-time error
f.  Run-time error
g.  None of the above

ANSWER
20 c  Prints: The StringBuffer class does not override the equals and hashCode
true,false  methods of the Object class. The Object.equals method does not
return the value true unless the argument is a reference to the same
object on which the method is invoked. For example, the method
invocation expression obj1.equals(obj2) only produces the value
true when obj1 == obj2 is also true. The Object.hashCode method
tends to return distinct hashcode values for distinct objects
regardless of the internal contents of the object. Suppose that the
reference variables sb1 and sb2 are of type StringBuffer. The
expression sb1.hashCode() == sb2.hashCode() will not produce the
value true unless the expression sb1 == sb2 is also true. The String
class does override the equals and hashCode methods of the Object
class. The String.hashCode method returns a hashcode value that is
computed based on the contents of the String object. Suppose that
the reference variables s1 and s2 are of type String. The expression
s1.hashCode() == s2.hashCode() must produce the value true
anytime the method invocation expression s1.equals(s2) produces
the value true.  

You might also like