Java certification Quiz
Java Quiz - 30

« 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?

    int x = 100;
    int a = x++;
    int b = ++x;
    int c = x++;
    int d = a<b?(a<c)?a:(b<c)?b:c;
    System.out.println(d);
a) 100
b) 101
c) 102
d) Compile error
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is d) Compile error : The ternary expression is not complete. It's like, if(a<b) {if(a<c) a; else if(b<c) b; else c; } else ...
Correct!
Compile error : The ternary expression is not complete. It's like, if(a<b) {if(a<c) a; else if(b<c) b; else c; } else ...

2. What is the output?

int n = 10;
do {
  System.out.print(n-- + " ");
} while (n-- >0);
a) 10 8 6 4 2
b) 10 8 6 4 2 0
c) 9 7 5 3 1 -1
d) 9 7 5 3 1

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is b) 10 8 6 4 2 0
Correct!
10 8 6 4 2 0

3. To get output : true Done

1. boolean option=true;
2. switch (option) {
3.   case true:
4.    System.out.println("true");
5.    break;
6.  default:
7.    System.out.println("default");
8.    break;
9. }
10. System.out.println("Done");
A. line 1: String option="true";
B. line 1: int option= 1;
C. line 3: case "true":
D. line 3: case 1:
E. line 3: case false
a) A with C or B with D
b) A with C only
c) B with D only
d) No change needed

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is a) A with C or B with D
switch accepts byte, char, String, enum or int values only
Correct! A with C or B with D
switch accepts byte, char, String, enum or int values only

4. What changes can get output 43210?

public static void main(String... args) {
    int n = 5;
    while (isAvailable(n)) {
      System.out.print(n);
    }
  }
  public static boolean isAvailable(int n) {
    return n-->0?true:false;
  }
a) Change isAvailable(n) to isAvailable(n--)
b) Change System.out.print(n) to System.out.print(--n)
c) Any of the above
d) None of the above

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is c) Any of the above
Correct! Any of the above

5. What is the output?

String s1 = "A ";
s1 += "B ";
String s2 = "C ";
s1 = s1.concat(s2);
s1.replace('C', 'D');
s1 = s1.concat(s2);
System.err.println(s1);
a) A B C C
b) A B D C
c) A C D C
d) B C C

Wrong! Try again..
Wrong Again! Try one more chance.
Wrong!
The correct answer is a) A B C C : String is immutable
Correct!
A B C C : String is immutable

6. What is output?

System.out.print("5 + 4 = " + 5 + 4);
System.out.println(" & 5 + 4 = " + (5 + 4));
a) Will not compile
b) Exception thrown
c) 5 + 4 = 54 & 5 + 4 = 9
d) 5 + 4 = 9 & 5 + 4 = 9
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is c) 5 + 4 = 54 & 5 + 4 = 9 : + operator for adding and concatination
Correct!
5 + 4 = 54 & 5 + 4 = 9 : + operator for adding and concatination

7. What happens if the code is run?

 List<String> list = new ArrayList<>();
 String[] arr;
 try {
   while(true) {
     list.add("My String");
   }
 }
 catch(RuntimeException e) {
   System.out.println("RuntimeException caught");
 }
 catch (Exception e) {
   System.out.println("Exception caught");
 }
 System.out.println("Done");
a) RuntimeException caught, Done
b) Exception caught, Done
c) OutOfMemoryError
d) Compile error, arr not initialized
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is c) OutOfMemoryError : infinite while loop
Correct!
OutOfMemoryError : infinite while loop