0% found this document useful (0 votes)
149 views13 pages

Fundamental Classes 4

At line 1, invoking Math.min produces a return value of type int which cannot be assigned to the short variable b1, causing a compile-time error. At line 2, parsing a String to a Byte variable results in a compile-time error as the return type of parseByte is primitive byte. Printing the results of comparing Integer instances using equals and == shows true, true, false as equals compares values and == compares references.

Uploaded by

rahul rastogi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
149 views13 pages

Fundamental Classes 4

At line 1, invoking Math.min produces a return value of type int which cannot be assigned to the short variable b1, causing a compile-time error. At line 2, parsing a String to a Byte variable results in a compile-time error as the return type of parseByte is primitive byte. Printing the results of comparing Integer instances using equals and == shows true, true, false as equals compares values and == compares references.

Uploaded by

rahul rastogi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 13

Fundamental Classes

Question 1
class SRC109 {
public static void main (String[] args) {
short x3 = 0; short x4 = 1;
short b1 = Math.min(x3,x4); // 1
int c1 = Math.min(0,1); // 2
long d1 = Math.min(0L,+1L); // 3
System.out.print(b1+","+c1+","+d1);
}}

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

a.  Prints: 0,0,0


b.  Compile-time error at 1
c.  Compile-time error at 2
d.  Compile-time error at 3
e.  Run-time error
f.  None of the above

ANSWER
At line 1, the invocation of the Math.min method produces a return
value of type int. The variable b1 is of type short; so the assignment
expression results in a compile-time error. The Math class declares
four versions of the min method; each declares a pair of parameters
of the same type. The parameter pair can be of type int, long, float
or double. The return type is the same as the argument types. At
Compile-
run-time, the arguments might not match the declared parameter
1 b  time error at
types; so one argument or both might require an implicit conversion

to an acceptable type. If both arguments are of type byte, short or
char, then both will be promoted to type int. If only one argument is
of type byte, short or char, then it will be promoted to the type of the
other argument. If both arguments are of type int, long, float or
double but the types differ, then a primitive widening conversion
will be applied to one of the two arguments.  

Question 2
class C {
public static void main (String args[]) {
String s1 = "1", s2 = "2";
Byte b1 = Byte.parseByte(s1);
Byte b2 = Byte.parseByte(s2);
System.out.print(b1.byteValue() + b2.byteValue());
}}

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

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

ANSWER
The Byte.parseByte method returns a primitive byte. A compile-
Compile-
2 c  time error is generated as a result of the attempt to assign a
time error 
primitive byte to a Byte reference variable.  

Question 3
class A {
public static void main (String[] args) {
String s = "11";
int i1 = Integer.parseInt(s);
System.out.print(new Integer(i1).equals(new Integer(i1)) + ",");
System.out.print(new Integer(i1).equals(new Integer(s)) + ",");
System.out.print(new Integer(i1) == new Integer(i1));
}}

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
Integer.equals overrides Object.equals. The Integer.equals
method compares the data values contained in the Integer
instances. If the argument is of type Integer and if the value
contained in the argument is the same as the value contained in
Prints: the instance on which the method is invoked, then the result is
3 g 
true,true,false  true. The equality operator, ==, does not compare data values.
Instead, the equality operator compares references. Distinct
instances of any two objects can not have the same reference
value; so the expression new Integer(i1) == new Integer(i1) is
false.  

Question 4
Which of the method invocation expressions would produce a run-time error?

a.  Long.parseLong("1")
b.  Long.parseLong("1L")
c.  Long.parseLong("010")
d.  Long.parseLong("0x10")
e.  Long.parseLong("1.0")

ANSWER
Long.parseLong is overloaded: one version
accepts a String argument that represents an
integral value; the other accepts both a String
argument and an argument of type int. The int
argument represents the radix (i.e. base) of the
String argument. The Long.parseLong method is
not able to determine the type of the String value
by examing a suffix such as L. Any such suffix
b  Long.parseLong("1L") 
results in a run-time error. The Long.parseLong
4 d  Long.parseLong("0x10") 
method is not able to determine the radix of the
e  Long.parseLong("1.0") 
String value by examing a prefix such as 0 or 0x.
The 0 prefix used to identify octal values is
accepted, but the String is parsed as a decimal
value. The prefix 0x generates a run-time error.
The Long.parseLong method generates a run-
time error if the String argument is not formatted
as a decimal integer. A floating-point format
results in a run-time error.  

Question 5
Which method of the java.lang.Double class returns an instance of type Double?

a.  doubleValue
b.  floatValue
c.  intValue
d.  longValue
e.  parseDouble
f.  toString(double)
g.  valueOf
h.  None of the above

ANSWER
5 g  valueOf 

Question 6
class MWC107 {
public static void main(String[] s) {
String s1 = "A", s2 = " B ", s3 = "C";
s2.trim(); s3.concat("D");
System.out.print(s1 + s2 + s3);
}}

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

a.  Prints: ABC


b.  Prints: A B C
c.  Prints: ABCD
d.  Prints: ABDC
e.  Prints: A B CD
f.  Prints: A B DC
g.  Compile-time error
h.  Run-time error
i.  None of the above

ANSWER
Strings are immutable. No method will change the contents of a String
instance. Some String methods such as concat, replace, substring and
Prints:
6 b  trim will return a new String instance with the desired modifications. In
A B C 
this case, the String instances returned by the methods trim and concat
are ignored.  
Question 7
class SRC110 {
public static void main (String[] args) {
byte x1 = 0; byte x2 = 1;
byte a1 = Math.min(x1,x2); // 1
int c1 = Math.min(0,1); // 2
long d1 = Math.min(0L,+1L); // 3
System.out.print(a1+","+c1+","+d1);
}}

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

a.  Prints: 0,0,0


b.  Compile-time error at 1
c.  Compile-time error at 2
d.  Compile-time error at 3
e.  Run-time error
f.  None of the above

ANSWER
At line 1, the invocation of the Math.min method produces a return
value of type int. The variable a1 is of type byte; so the assignment
expression results in a compile-time error. The Math class declares
four versions of the min method; each declares a pair of parameters
of the same type. The parameter pair can be of type int, long, float
or double. The return type is the same as the argument types. At
Compile-
run-time, the arguments might not match the declared parameter
7 b  time error at
types; so one argument or both might require an implicit conversion

to an acceptable type. If both arguments are of type byte, short or
char, then both will be promoted to type int. If only one argument is
of type byte, short or char, then it will be promoted to the type of the
other argument. If both arguments are of type int, long, float or
double but the types differ, then a primitive widening conversion
will be applied to one of the two arguments.  

Question 8
class B {
public static void main (String[] args) {
byte b1 = 11;
System.out.print(new Integer(b1).equals(new Integer(b1)) + ",");
System.out.print(new Integer(b1).equals(new Short(b1)) + ",");
System.out.print(new Integer(b1).equals(new Byte(b1)));
}}
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
Integer.equals overrides Object.equals. The Integer.equals
method compares the data values contained in the Integer
instances. If the argument is of type Integer and if the value
Prints:
8 e  contained in the argument is the same as the value contained in
true,false,false 
the instance on which the method is invoked, then the result is
true. If the argument is not of type Integer then the result is
false.  

Question 9
Which of the method invocation expressions would produce a run-time error?

a.  Long.parseLong("-1")
b.  Long.parseLong("+1")
c.  Long.parseLong("01")
d.  Long.parseLong("1L")
e.  Long.parseLong("1.0")

ANSWER
9 b  Long.parseLong("+1")  Long.parseLong is overloaded: one version
d  Long.parseLong("1L")  accepts a String argument that represents an
e  Long.parseLong("1.0")  integral value; the other accepts both a String
argument and an argument of type int. The int
argument represents the radix (i.e. base) of the
decimal integral value represented by the String
argument. The Long.parseLong method is not able
to determine the type of the String value by
examing a suffix such as L. Any such suffix
results in a run-time error. The Long.parseLong
method is not able to determine the radix of the
String value by examing a prefix such as 0 or 0x.
The 0 prefix used to identify octal values is
accepted, but the String is parsed as a decimal
value. The prefix 0x generates a run-time error. A
leading minus sign (-) can be added to indicate a
negative value. A leading plus sign (+) results in a
run-time error. The Long.parseLong method
generates a run-time error if the String argument is
not formatted as a decimal integer. A floating-
point format results in a run-time error.

Question 10
Which methods of the java.lang.Double class declare a parameter of type String?

a.  doubleValue
b.  floatValue
c.  intValue
d.  longValue
e.  parseDouble
f.  valueOf

ANSWER
10 e  f  parseDouble  valueOf 

Question 11
class MWC108 {
public static void main(String[] s) {
String s1 = "A", s2 = " B ", s3 = "C";
s2.trim(); s3.append("D");
System.out.print(s1 + s2 + s3);
}}

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

a.  Prints: ABC


b.  Prints: A B C
c.  Prints: ABCD
d.  Prints: ABDC
e.  Prints: A B CD
f.  Prints: A B DC
g.  Compile-time error
h.  Run-time error
i.  None of the above

ANSWER
The StringBuffer class has an append method, but the String class
Compile-
11 g  does not. A compile-time error is generated due to the attempt to
time error 
invoke the append method on an instance of type String.  

Question 12
class SRC111 {
static String m(byte i) {return "byte";}
static String m(short i) {return "short";}
static String m(char i) {return "char";}
static String m(int i) {return "int";}
static String m(double i) {return "double";}
public static void main (String[] args) {
byte b = 0; short s = 0; char c = 0;
System.out.print(m(Math.min(b,b))+",");
System.out.print(m(Math.min(s,s))+",");
System.out.print(m(Math.min(c,c)));
}}

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

a.  Prints: byte,byte,byte


b.  Prints: short,short,short
c.  Prints: int,int,int
d.  Prints: byte,short,int
e.  Prints: short,short,int
f.  Prints: byte,short,char
g.  Compile-time error
h.  Run-time error
i.  None of the above

ANSWER
12 c  Prints: The Math class declares four versions of the min method; each
int,int,int  declares a pair of parameters of the same type. The parameter pair
can be of type int, long, float or double. The return type is the same
as the argument types. At run-time, the arguments might not match
the declared parameter types; so one argument or both might require
an implicit conversion to an acceptable type. If both arguments are
of type byte, short or char, then both will be promoted to type int. If
only one argument is of type byte, short or char, then it will be
promoted to the type of the other argument. If both arguments are of
type int, long, float or double but the types differ, then a primitive
widening conversion will be applied to one of the two arguments.  

Question 13
Which of the following methods of the java.lang.Integer class returns a primitive int
type?

a.  intValue
b.  parseInt
c.  valueOf

ANSWER
intValue  Integer.valueOf returns an instance of the Integer wrapper
13 a  b 
parseInt  class.  

Question 14
Which methods of the java.lang.Double class declare a NumberFormatException in the
throws clause?

a.  doubleValue
b.  floatValue
c.  intValue
d.  longValue
e.  parseDouble
f.  toString(double)
g.  valueOf

ANSWER
14 e  g  parseDouble  valueOf 

Question 15
class SRC112 {
static String m(byte i) {return "byte";}
static String m(int i) {return "int";}
static String m(long i) {return "long";}
static String m(double i) {return "double";}
public static void main (String[] args) {
byte b = 0;
System.out.print(m(Math.min(b,b))+",");
System.out.print(m(Math.min(b,1))+",");
System.out.print(m(Math.min(b,1L)));
}}

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

a.  Prints: byte,byte,byte


b.  Prints: byte,int,long
c.  Prints: int,int,int
d.  Prints: int,int,long
e.  Prints: long,long,long
f.  Compile-time error
g.  Run-time error
h.  None of the above

ANSWER
The Math class declares four versions of the min method; each
declares a pair of parameters of the same type. The parameter pair
can be of type int, long, float or double. The return type is the
same as the argument types. At run-time, the arguments might not
match the declared parameter types; so one argument or both
Prints: might require an implicit conversion to an acceptable type. If both
15 d 
int,int,long  arguments are of type byte, short or char, then both will be
promoted to type int. If only one argument is of type byte, short or
char, then it will be promoted to the type of the other argument. If
both arguments are of type int, long, float or double but the types
differ, then a primitive widening conversion will be applied to one
of the two arguments.  

Question 16
class F {
public static void main (String[] args) {
Integer i1 = new Integer("1");
Integer i2 = new Integer("1");
int h1 = i1.hashCode(), h2 = i2.hashCode();
StringBuffer sb1 = new StringBuffer("1");
StringBuffer sb2 = new StringBuffer("1");
int h3 = sb1.hashCode(), h4 = sb2.hashCode();
System.out.print((h1==h2)+","+(h3==h4));
}}

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
Integer.hashCode overrides Object.hashCode. The Integer.hashCode
method calculates the hash code based on the value contained in the
Integer instance. The StringBuffer.hashCode method does not
Prints:
16 c  override Object.hashCode. The StringBuffer.hashCode method
true,false 
typically returns a value that is based on the internal address of the
StringBuffer instance. Two instances of StringBuffer will not have
the same hash code.  

Question 17
class F {
public static void main (String args[]) {
Double d1 = new Double("0x10"); // 1
double d2 = Double.parseDouble("0x10"); // 2
Double d3 = Double.valueOf("0x10"); // 3
System.out.print(d1+","+d2+","+d3);
}}

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

a.  Prints: 10,10,10


b.  Prints: 16,16,16
c.  Compile-time error at line 1
d.  Compile-time error at line 2
e.  Compile-time error at line 3
f.  Run-time error
g.  None of the above

ANSWER
17 f  Run- An argument of type String must represent a floating-point value. The
time argument "0x10" represents a hexadecimal integer literal; so a
error  NumberFormatException would be thrown at run-time.  

Question 18
class MWC110 {
static void m1(String s1, String s2) {
s1.insert(1,"D"); s1 = s1 + s2;
}
public static void main(String[] s) {
String s1 = "A", s2 = "B";
m1(s1,s2);
System.out.print(s1 + s2);
}}

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

a.  Prints: AB
b.  Prints: ABB
c.  Prints: ADB
d.  Prints: ADBB
e.  Compile-time error
f.  Run-time error
g.  None of the above

ANSWER
The StringBuffer class has an insert method, but the String class
Compile-
18 e  does not. A compile-time error is generated due to the attempt to
time error 
invoke the insert method on an instance of type String.  

Question 19
class MWC109 {
public static void main(String args[]) {
String a = "A", b = "B", c = a+b, d = a+b;
System.out.print(((a+b)==(a+b)) + ",");
System.out.print((c==d) + ",");
System.out.print(c.equals(d));
}}

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

ANSWER
At run-time, the expression a+b is evaluated four times. Each
evaluation produces a new String instance containing the value
"AB". Each of the four instances has a unique reference. If any
two of the four instances appear as the operands of the equality
operator, then the result is always false. The left and right
Prints: operands of the equality expression (a+b)==(a+b) reference
19 b 
false,false,true  unique String instances. Since the two references are not the
same, the equality expression produces the value false.
Similarly, the expression c==d produces the value false. Since
the contents of the String instances referenced by c and d are
the same, the method invocation expression c.equals(d)
produces the value true.  

You might also like