SCJP Questions

SCWCD Questions

Friday, April 10, 2009

SCJP Mock Questions : Autoboxing and unboxing

********************************************************************
original from www.techfaq360.com
SCJP Mock Questions : Autoboxing/unboxing
********************************************************************
Question No :1
What is output of the code bellow ?
public class Test {
public static void main(String[] args) {
List list = new ArrayList();
list.add(0, 59);
int total = list.get(0);
System.out.println(total);
}
}
(Choose correct one from multiple below)
1. Compile time error, because you have to do
int total = ((Integer)(list.get(0))).intValue();
2. 59
3. Runtime Exceptions
4. None of the above
correct is :2
Explanations :Manual conversion between primitive types (such as an int) and wrapper classes
(such as Integer) is necessary when adding a primitive data type to a collection in jdk1.4 but
The new autoboxing/unboxing feature eliminates this manual conversion in jdk 1.5
------------------------------------------------------------------------------
Question No :2
What is the output of the bellow code?
public class Test {
public static void main(String[] args) {
Boolean expr = true;
if (expr) {
System.out.println("true");
} else {
System.out.println("false");
}
}
}
(Choose correct one from multiple below)
1. true
2. Compile with error because you can't use Boolean object in if()
3. Runtime Exception
4. None of the above
correct is :1
Explanations :In the if statement, condition can be Boolean in jdk1.5
------------------------------------------------------------------------------
Question No :3
Is the bellow code compile without error ?
public class Test {
public static void main(String[] args) {
Boolean expr = true;
while (expr) {
expr = !expr;
}
}
}
(Choose correct one from multiple below)
1. No, while condition can't allow Boolean object, it allows boolean primitive type
2. Yes, while condition allow Boolean object
3. Can't say
4. None of the above
correct is :2
Explanations :In the while, do-while and for statements, the condition can be Boolean
------------------------------------------------------------------------------
Question No :4
What is the output for the bellow code ?
public class Test {
public static void main(String[] args) {
String[] words = new String[] {"aaa", "bbb", "ccc", "aaa"};
Map m = new TreeMap();
for (String word : words) {
Integer freq = m.get(word);
m.put(word, freq == null ? 1 : freq + 1);
}
System.out.println(m);
}
}
(Choose correct one from multiple below)
1. {aaa=2, bbb=1, ccc=1}
2. Compile with Error
3. Runtime Exception
4. None of the above
correct is :1
Explanations :Boxing and unboxing in collections
------------------------------------------------------------------------------
Question No :5
String[] words = new String[] {"aaa", "bbb", "ccc", "aaa"};
which bellow statement is same as
for (int i=0;i(Choose correct one from multiple below)
1. for (String word : words)
2. for (String word : words.length)
3. Can't say
4. None of the above
correct is :1
Explanations :for (int i=0;ifor (String word : words) in jdk 1.5
------------------------------------------------------------------------------
Question No :6
public class Test {
public static void main(String[] args) {
String[] words = new String[] {"aaa", "bbb", "ccc", "aaa"};
Map m = new TreeMap();
for (int i=0;iInteger freq = m.get(words[i]);
m.put(words[i], freq == null ? 1 : freq + 1);
}
System.out.println(m);
}
}
is same as ?
(Choose correct one from multiple below)
1. public class Test {
public static void main(String[] args) {
String[] words = new String[] {"aaa", "bbb", "ccc", "aaa"};
Map m = new TreeMap();
for (String word : words) {
Integer freq = m.get(word);
m.put(word, freq == null ? 1 : freq + 1);
}
System.out.println(m);
}
}
2. public class Test {
public static void main(String[] args) {
String[] words = new String[] {"aaa", "bbb", "ccc", "aaa"};
Map m = new TreeMap();
for (String word : words.length) {
Integer freq = m.get(word);
m.put(word, freq == null ? 1 : freq + 1);
}
System.out.println(m);
}
}
3. public class Test {
public static void main(String[] args) {
String[] words = new String[] {"aaa", "bbb", "ccc", "aaa"};
Map m = new TreeMap();
for (String word : words) {
Integer freq = m.get(word[i]);
m.put(word[i], freq == null ? 1 : freq + 1);
}
System.out.println(m);
}
}
4. None of the above
correct is :1
Explanations :public class Test {
public static void main(String[] args) {
String[] words = new String[] {"aaa", "bbb", "ccc", "aaa"};
Map m = new TreeMap();
for (int i=0;iInteger freq = m.get(words[i]);
m.put(words[i], freq == null ? 1 : freq + 1);
}
System.out.println(m);
}
}
is same as
public class Test {
public static void main(String[] args) {
String[] words = new String[] {"aaa", "bbb", "ccc", "aaa"};
Map m = new TreeMap();
for (String word : words) {
Integer freq = m.get(word);
m.put(word, freq == null ? 1 : freq + 1);
}
System.out.println(m);
}
}
------------------------------------------------------------------------------
Question No :7
ArrayList list = new ArrayList();
list.add(0, new Integer(42));
int total = (list.get(0)).intValue();
is same as in jdk1.5 is?
(Choose correct one from multiple below)
1. ArrayList list = new ArrayList();
list.add(0, 42);
int total = list.get(0);
2. ArrayList list = new ArrayList();
list.add(0, 42);
int total = (int)list.get(0);
3. ArrayList list = new ArrayList();
list.add(0, 42);
int total = list.get(0).int();
4. None of the above
correct is :1
Explanations :In JDK 1.5 Don't need to cast
------------------------------------------------------------------------------
Question No :8
What is the output of the bellow code ?
public class Test {
public static void main(String[] args) {
Integer in = new Integer(null);
System.out.println(in.intValue());
}
}
(Choose correct one from multiple below)
1. 0
2. Compile with error because , Integer in = new Integer(null);
3. java.lang.NumberFormatException
4. None of the above
correct is :3
Explanations :An Integer expression can have a null value. If your program tries to autounbox null,
it will throw a NullPointerException
------------------------------------------------------------------------------
Question No :9
What is the output of bellow code ?
public class Test {
public static void main(String[] args) {
Integer i5 = new Integer(5);
Integer j5 = new Integer(5);
if(i5 == j5){
System.out.println("equal");
}else{
System.out.println("false");
}
}
}
(Choose correct one from multiple below)
1. true
2. false
3. Compile with error
4. None of the above
correct is :2
Explanations :checks references , so both are different
------------------------------------------------------------------------------
Question No :10
What is the output of the bellow code?
public class Test {
public static void main(String[] args) {
Integer i5 = new Integer(5);
Integer j5 = new Integer(5);
if(i5.equals(j5)){
System.out.println("equal");
}else{
System.out.println("false");
}
}
}
(Choose correct one from multiple below)
1. equal
2. false
3. Compile with error
4. None of the above
correct is :1
Explanations :checks for content
------------------------------------------------------------------------------
Question No :11
What is the output?
public class Test {
public static void main(String[] args) {
Integer i5 = 2;
Integer j5 = 2;
System.out.println(i5 == j5);
if(i5 == j5){
System.out.println("equal");
}else{
System.out.println("not equal");
}
}
}
(Choose correct one from multiple below)
1. false
not equal
2. true
not equal
3. true
equal
4. None of the above
correct is :1
Explanations :No Explanations
------------------------------------------------------------------------------
Question No :12
What is the output ?
public class Test {
public static void main(String[] args) {
Short expr = 1;
switch (expr) {
case 1:
System.out.println("ONE"); break;
case 2:
System.out.println("TWO"); break;
case 3:
System.out.println("THREE"); break;
default:
assert false;
}
}
}
(Choose correct one from multiple below)
1. ONE
2. Compile With error because you can't use sort in switch.
3. Runtime Exceptions
4. None of the above
correct is :1
Explanations :In the switch statement, the switch expression can be Character, Byte, Short or
Integer in JDK 1.5
------------------------------------------------------------------------------
Question No :13
What is the output ?
public class Test {
public static void main(String[] args) {
Integer i = null;
int j = i;
System.out.println(j);
}
}
(Choose correct one from multiple below)
1. 0
2. null
3. Compile with error
4. java.lang.NullPointerException
correct is :4
Explanations :trying to outbox null
------------------------------------------------------------------------------
Question No :14
What is the output of the bellow code ?
public class Test {
public static void main(String[] args) {
doSomething(1);
doSomething(2.0);
}
public static void doSomething(double num) {
System.out.println("double : " + num);
}
public static void doSomething(Integer num) {
System.out.println("Integer : " + num);
}
}
(Choose correct one from multiple below)
1. double : 1.0
double : 2.0
2. Integer : 1
double : 2.0
3. Integer : 1
Integer : 2
4. None of the above
correct is :1
Explanations :No Explanation avilable
------------------------------------------------------------------------------
Question No :15
What is the output ?
public class Test {
public static void main(String[] args) {
Integer i=1;
doSomething(i);
doSomething(2.0);
}
public static void doSomething(double num) {
System.out.println("double : " + num);
}
public static void doSomething(Integer num) {
System.out.println("Integer : " + num);
}
}
(Choose correct one from multiple below)
1. Integer : 1
double : 2.0
2. double : 1.0
double : 2.0
3. double : 1.0
Integer: 2
4. None of the above
correct is :1
Explanations :No Explanation
------------------------------------------------------------------------------
Question No :16
What is the output?
public class Test {
public static void main(String[] args) {
Character c1 = '\u0000';
Character c2 = '\u0000';
System.out.println("c1 == c2 : " + (c1 == c2));
}
}
(Choose correct one from multiple below)
1. c1 == c2 : true
2. c1 == c2 : false
3. Compile with error
4. None of the above
correct is :1
Explanations :No Exp
------------------------------------------------------------------------------
Question No :17
What is the output?
public class Test {
public static void main(String[] args) {
Character c11 = '\u00FF';
Character c12 = '\u00FF';
System.out.println("c11 == c12 : " + (c11 == c12));
}
}
(Choose correct one from multiple below)
1. c11 == c12 : false
2. c11 == c12 : true
3. Compile with error
4. None of the above
correct is :1
Explanations :Character can be check by == but the value range of Character '\u0000' to '\u007F'.
u00FF is crossed the range.
------------------------------------------------------------------------------
Question No :18
What is true about StringBuilder ?
(Choose correct one from multiple below)
1. StringBuilder is a drop-in replacement for StringBuffer in cases where thread safety is not an
issue.
2. StringBuilder is NOT synchronized
3. StringBuilder offers FASTER performance than StringBuffer
4. All of the above
correct is :4
Explanations :J2SE5.0 added the StringBuilder class, which is a drop-in replacement for
StringBuffer in cases where thread safety is not an issue. Because StringBuilder is NOT
synchronized, it offers FASTER performance than StringBuffer.
------------------------------------------------------------------------------
Question No :19
Which statement is true ?
(Choose correct one from multiple below)
1. Instances of StringBuilder are not safe for use by multiple threads
2. Instances of StringBuilder are safe for use by multiple threads
3. Both are true
4. None of the above
correct is :1
Explanations :Instances of StringBuilder are not safe for use by multiple threads. If such
synchronization is required then it is recommended that StringBuffer be used
------------------------------------------------------------------------------
Question No :20
Which statement is true about String ?
(Choose correct one from multiple below)
1. String objects are immutable, meaning that once created they cannot be altered
2. String objects are mutable, meaning that once created they can be altered
3. All of the above
4. None of the above
correct is :1
Explanations :String objects are immutable, meaning that once created they cannot be altered.
------------------------------------------------------------------------------
Question No :21
What is the output?
public class Test {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("ssss");
StringBuffer sb_2 = new StringBuffer("ssss");
System.out.println("sb equals sb_2 : " + sb.equals(sb_2));
}
}
(Choose correct one from multiple below)
1. sb equals sb_2 : false
2. sb equals sb_2 : true
3. Can't say
4. None of the above
correct is :1
Explanations :StringBuffer class DOES NOT override the equals() method. Therefore, it uses
Object class' equals(), which only checks for equality of the object references
------------------------------------------------------------------------------
Question No :22
What is the output?
public class Test {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("ssss");
String st = new String("ssss");
System.out.println("st equals sb : " + st.equals(sb));
}
}
(Choose correct one from multiple below)
1. st equals sb : false
2. st equals sb : true
3. can't say
4. None of the above
correct is :1
Explanations :String's equals() method checks if the argument if of type string, if not it returns false
------------------------------------------------------------------------------
Distribution of this pdf is illegal without permission of techfaq360.com

No comments:

Post a Comment