Java Quiz - 22
« 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 »
1. What is the output?
StringBuilder sb = new StringBuilder();
String s ="";
if(sb.equals(s)) {
System.out.println("matches 1");
}
else if(sb.toString().equals(s)) {
System.out.println("matches 2");
}
else {
System.out.println("matches none");
}
a) matches 1
b) matches 2
c) matches none
d) Compile error
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is b) matches 2 : String is not a likely argument for StringBuilder
Correct! matches 2 : String is not a likely argument for StringBuilder
2. What is the output?
public static void main(String... args) {
Test t = new Test();
int id = 1234;
t.checkId(id);
t.readId(id);
}
private void readId(int id) throws Exception{
System.out.println("read id");
}
private void checkId(int id) throws RuntimeException{
System.out.println("check id");
}
a) check id & read id
b) read id & check id
c) Compile error
d) Exception thrown
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is c) Compile error : t.readId(id); should be in try catch
Correct! Compile error : t.readId(id); should be in try catch
3. What is the output?
String[][] arr = new String[2][2];
arr[0][0] = "blue";
arr[1][0] = "green";
arr[0][1] = "red";
arr[1][1] = "orange";
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] +" ");
}
}
a) blue red green orange
b) blue green red orange
c) Compile error
d) Exception thrown
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is a) blue red green orange : array is an object, 2-dim array is an array of arrays
Correct! blue red green orange : array is an object, 2-dim array is an array of arrays
4. What is the output?
float f1 = 100f;
float f2 = (float)1_11.0;
float f3 = 100;
double d1 = 23.44;
float f4 = (float)d1;
int i1 = 100;
float f5 = i1;
System.out.print(f1 + " " + f2);
a) Compile error
b) 100.0f 111.0f
c) 100.0 1_11.0
d) 100.0 111.0
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is d) 100.0 111.0 : All are correct
Correct! 100.0 111.0 : All are correct
5. What is the output?
String message = "elementary";
System.out.print(message.replace("e", "X") +
" " + message.replace('a', 'Y'));
a) XlXmXntary XlXmXntYry
b) elementary elementary
c) XlXmXntary elementYry
d) Compile error
Wrong! Try again..
Wrong Again! Try one more chance.
Wrong! The correct answer is c) XlXmXntary elementYry : replace() returns string with matching replaced characters
Correct! XlXmXntary elementYry : replace() returns string with matching replaced characters
6. What is output?
StringBuilder message = new StringBuilder("Lion");
System.out.print(message.replace(0, 2, "t") +
" " + message.reverse());
a) tton nott
b) ton not
c) tttn nttt
d) tn nt
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is b) ton not : Li is replaced with t
Correct! ton not : Li is replaced with t
7. What is the output?
int[] arr = {1,2,3,4,5,6};
int i=0;
for (; i < arr.length;) {
System.out.print(i+++" ");
}
a) Compile error
b) Exception thrown
c) 1 2 3 4 5 6
d) 0 1 2 3 4 5
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is d) 0 1 2 3 4 5 : i can be declared outside and increment need not be inside for()
Correct! 0 1 2 3 4 5 : i can be declared outside and increment need not be inside for()
|