Java certification Quiz
Java Quiz - 28

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

public class Test {
  public static void main(String[] args) {
    String[] strArr = new String[2];
    int indx = 0;
    for (String str : strArr) {
      strArr[indx].concat(" element "+indx);
      indx++;
    }
    for (int i = 0; i < strArr.length; i++) {
      System.out.println(strArr[i]);
    }
  }
}
a) Compile error
b) NullPointerException thrown
c) element 0, element 1
d) null element 0, null element 1
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is b) NullPointerException thrown : The array elements are initialized to null by default
Correct!
NullPointerException thrown : The array elements are initialized to null by default

2. What is the output?

public class Test {
  public static void main(String... args) {
    System.out.println("String");
  }
  public static void main(int... args) {
    System.out.println("int");
  }
  public static void main(Object... args) {
    System.out.println("Object");
  }
}
a) String
b) int
c) Object
d) Compile error

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is a) String : main with String[] is run
Correct!
String : main with String[] is run

3. What is the output?

public class Test {
  public static void main(String... args) {
    main(10,20,30);
  }
  public static void main(Integer... args) {
    System.out.println("Integer");
  }
  public static void main(Object... args) {
    System.out.println("Object");
  }
}
a) Compile error
b) StackOverFlow error
c) Object
d) Integer

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is d) Integer : matching method is called with main()
Correct! Integer
matching method is called with main()

4. What is the output?

public class Test extends Parent{
  public void calculate() {
    System.out.println("in Test");
  }
  public static void main(String[] args) {
    Test p = (Test) new Parent();        
    p.calculate();
  }  
}
public class Parent {
  protected void calculate() {
  System.out.println("in Parent");
  }
}
a) Compile error
b) throws Exception
c) in Test
d) in Parent

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is b) throws Exception Cannot cast Parent to child
Correct! in Test throws ClassCastException
Cannot cast Parent to child

5. What is the output?

To get output 135 what code should be inserted at XXX
int[] arr = {1,2,3,4,5};
for(XXX) {
  System.out.print(arr[i]); 
}
a) int i=1;i<=5;i+=1
b) int i=0;i<=4;i++
c) int i=0;i<5;i+=2
d) int i=1;i<=4;i++

Wrong! Try again..
Wrong Again! Try one more chance.
Wrong!
The correct answer is c) int i=0;i<5;i+=2
Correct!
int i=0;i<5;i+=2

6. What is output?

String str=" ";
str.trim();
System.out.println(str.equals("") 
   +" " + str.isEmpty());
a) false false
b) true true
c) true false
d) false true
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is a) false false : String is immutable
Correct!
false false : String is immutable

7. What changes can make code compile?

1.  abstract class Doctor {
2.   protected void operate(){
3.   } 
4.   abstract void cure();
5.  }
6.  class Surgeon extends Doctor {
7.   void operate(){
8.   }
9.   protected void cure(){
10.  }
11. }
A. public void operate() at line 7
B. protected void operate() at line 7
C. void operate() at line 2
D. void cure() at line 9
a) A or B or C
b) A or B
c) A or C
d) Any of them
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is a) A or B or C : protected has higher access than default
Correct!
A or B or C : protected has higher access than default