Java certification Quiz
Java Quiz - 26

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

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class DateTest {
  public static void main(String[] args) {
    LocalDate date1 = LocalDate.of(2016, 12, 23);
    LocalDate date2 = LocalDate.parse("2017-04-03",
          DateTimeFormatter.ISO_DATE);
    System.out.println("date1 : " + date1);
    System.out.println("date2 : " + date2);
  }
}
a) date1 : 2016-Dec-23, date2 : 2017-04-03
b) date1 : 2016-12-23, date2 : 2017-04-03
c) date1 : 2016-12-23, date2 : 03-Apr-2017
d) date1 : 2016-Dec-23, date2 : 2017-Apr-03
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is b) date1 : 2016-12-23, date2 : 2017-04-03
Correct!
date1 : 2016-12-23, date2 : 2017-04-03

2. What is the output?

public class ForLoopTest {
  public static void main(String[] args) {
    int ii = 0;
    int jj = 7;
    for (ii = 0; ii < jj-1; ii=ii+2) {
      System.out.print(ii +" ");
    }
  }
}
a) 0 3 6
b) 0 2 4 6
c) 0 2 4
d) Compile error

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is c) 0 2 4
Correct! 0 2 4

3. What is the output?

public class Printer {
  int copy;
  static int cp;
  public Printer(int copy) {
    if(cp<copy) {
      cp=copy;
      this.copy = copy;
    }
  }
  void print() {
    System.out.println("copy="+copy+" & cp="+cp);
  }  
}
public class Test {
  public static void main(String[] args) {
    Printer p1 = new Printer(25);
    Printer p2 = new Printer(75);
    Printer p3 = new Printer(50);
    p1.print();
    p2.print();
    p3.print();
  }
}
a) copy=25 & cp=75, copy=75 & cp=75, copy=0 & cp=75
b) copy=25 & cp=25, copy=75 & cp=75, copy=50 & cp=50
c) copy=25 & cp=50, copy=75 & cp=50, copy=0 & cp=50
d) copy=50 & cp=50, copy=50 & cp=50, copy=50 & cp=50

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is a) copy=25 & cp=75, copy=75 & cp=75, copy=0 & cp=75
cp is static and shared by all instances
Correct! copy=25 & cp=75, copy=75 & cp=75, copy=0 & cp=75
cp is static and shared by all instances

4. What is the output?

public class Test {
  public static void main(String[] args) {
    String[] alphabets = {"A", "B", "C", "D", "E"};
    for (int i = 0; i < alphabets.length; i++) {
      System.out.print(alphabets[i] +" ");
      if(alphabets[i].equals("C")) {
        continue;
      }
      System.out.println("Done");
      break;
    }
  }
}
a) A B C D Done
b) A B C Done
c) A Done
d) ArrayIndexOutOfBoundsException

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is c) A Done
'break' stops the loop
Correct! A Done
'break' stops the loop

5. What code should be placed at XX to get output 10:20 ?

public class Test {
  public static void main(String[] args) {
    XX
    arr[0] = 10;
    arr[1] = 20;
    System.out.println(arr[0]+":"+arr[1]);
  }
}
a) int[] arr = new int[2]{10,20};
b) int arr = new int[2];
c) int arr[2];
d) int[] arr = new int[2];

Wrong! Try again..
Wrong Again! Try one more chance.
Wrong!
The correct answer is d) int[] arr = new int[2];Arrays have to be declared and created with size
Correct!
int[] arr = new int[2]; Arrays have to be declared and created with size

6. What is the output?

public class Test {
  public static void main(String[] args) {
    List<String> colors= new ArrayList<>();
    colors.add("yellow");
    colors.add("green");
    colors.add("red");
    colors.add("blue");
    if(colors.remove("red")) {
      colors.remove("green");
    }
    System.out.println(colors);
  }
}
a) [yellow, green, blue]
b) [yellow, blue]
c) [yellow, green, red, blue]
d) Compile error
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is b) [yellow, blue] : remove() returns true if successful
Correct!
[yellow, blue] : remove() returns true if successful

7. To make the code compile:

public interface Liftable { 
   public void liftLoad();
   public void setHook();// AA
}
public abstract class Vehicle implements Liftable { // BB
  public void liftLoad() {}
  // XX
}
public class Crane extends Vehicle { 
  public void liftLoad() {}
  // YY
}
a) Change code at line AA to : default public void setHook();
b) Change code at line BB to : public class Vehicle implements Liftable
c) Add code at line XX: public abstract void setHook() {}
d) Add code at line YY: public void setHook() {}
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is d) Add code at line YY: public void setHook() {}
abstract method has to be implemented
Correct!
Add code at line YY: public void setHook() {} abstract method has to be implemented