Java certification Quiz
Java Quiz - 25

« 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 static void main(String[] args) {
    String[] vehicles = { "Car", "Truck", "Van", "Tractor" };
    System.out.print(vehicles.length +", ");
    System.out.println(vehicles[3].length());
}
a) 4, 3
b) 3, 5
c) 4, 7
d) ArrayIndexOutOfBoundsException
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is c) 4, 7
Correct!
4, 7
2. What should be placed at ?? to display only last 4 digits and all others masked by x ?
public class CrCdMask {
  public static void main(String[] args) {
    System.out.println(maskCardNo("1234-5678-9012-3423"));
  }
  private static String maskCardNo(String cardNo) {
    String val = "xxxx-xxxx-xxxx-";
    ??
    return val;
  }
}
A. val = val + cardNo.substring(15, 19);
B. val = new StringBuilder(val).append(cardNo, 15,19).toString();
C. val = val + cardNo.substring(16,19);
D. val = val + cardNo.substring(16,4);
a) A or B
b) C or D
c) A only
d) B only

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is a) A or B
val appended with a substring of cardNo from index 15 to count 19

Correct! A or B
val appended with a substring of cardNo from index 15 to count 19
3. What is the output?
public class CheckEqual {
  public static void main(String[] args) {
    String s1 = "School";
    String s2 = new String("school");
    if(s1.equalsIgnoreCase(s2)) {
      System.out.println("Same");
    }
    else {
      System.out.println("Not Same");
    }
  }
}
a) Compile Error
b) Run time error
c) Not Same
d) Same

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is d) Same
s1 and s2 are compared with case ignored
Correct! Same
s1 and s2 are compared with case ignored
4. What is the output?
public class SumTest {
  public static void main(String[] args) {
    doSum(10, 20);
    doSum(10.4, 10.6);
  }
  public static void doSum(int n , int m) {
    System.out.println("int sum is : " + (n+m));
  }
  public static void doSum(Integer n , Integer m) {
    System.out.println("Integer sum is : " + (n+m));
  }
  public static void doSum(float n , float m) {
    System.out.println("float sum is : " + (n+m));
  }
  public static void doSum(double n , double m) {
    System.out.println("double sum is : " + (n+m));
  }
  public static void doSum(Object n , Object m) {
    System.out.println("Object sum is : " + (""+n+m));
  }
}
a) int sum is : 30 & float sum is : 21.0
b) Integer sum is : 30 & float sum is : 21.0
c) int sum is : 30 & double sum is : 21.0
d) double sum is : 30 & double sum is : 21.0

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is c) int sum is : 30 & double sum is : 21.0
The nearest match is int and double
Correct! int sum is : 30 & double sum is : 21.0
The nearest match is int and double
5. What is the output if following is run as below? 
         java HelloTest java
public class HelloTest {
  public static void main(String[] args) {
    if(args[0].equals("java")?false:true) {
      System.out.println("Success");
    }
    else {
      System.out.println("Fail");
    }
  }
}
a) Fail
b) Success
c) Runtime error
d) Compile error

Wrong! Try again..
Wrong Again! Try one more chance.
Wrong!
The correct answer is a) Fail : Ternary operator is like if else
Correct!
Fail Ternary operator is like if else
6. To get d=0.5 for q 90 or more AND d=0.2 for q 
between 80 and 90, else zero which are the options that can be written at XX? public class DiscountTest { public static void main(String[] args) { double d = 0; int q = 0; XX System.out.println(q + " : "+ d); } } A. if(q>=90) {d = 0.5;} if(q>80 && q<90){d = 0.2;} B. d = (q>= 90)?0.5:(q>80)?0.2:0; C. d = (q>= 90)?0.5:0; d = (q> 80)?0.2:0; D. if(q>80 || q<90){d = 0.2;} else { d=0.5; }
a) B, D
b) A, D
c) A, B
d) A, C
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is c) A, B
Correct!
A, B
7. What is the output?
public class CountTest {
  static int count=0;
  int n = 0;
  public void changeCount(int chances) {
    while (n < chances) {
     n++;
     if(n%2==0)
       count++;
     }
  }
  public static void main(String[] args) {
    CountTest ct1 = new CountTest();
    CountTest ct2 = new CountTest();
    ct1.changeCount(5);
    ct2.changeCount(10);
    System.out.println(ct1.count + " : "+ct2.count);
  }
}
a) 10: 10
b) 7 : 7
c) 5 : 5
d) 2 : 5
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is b) 7 : 7 static variable is shared
Correct!
7 : 7 static variable is shared