Java quiz questions

Java interview questions

  • 1.

    Which of the following is true?

    public class A implements Comparable {
    
      final int a;
    
      A(final int a) {
        this.a = a;
      }
    
      public int compareTo(A that) {
        return this.a - that.a;
      }
    
      public static void main(String[] args) {
        A a1 = new A(1);
        A a2 = new A(2);
        System.out.println(a1 > a2);
      }
    
    }

     

    1. member variable can not be final

    2. constructor parameter cannot be final

    3. compareTo must take Object as parameter

    4. operator > is not defined for type A

    5. nothing is wrong with this code

    Answer
  • 2.

    Which of the following is true?

    public static final class Class {
      static final int id = 1;
    
      Class() {
      }
    
      static public int id() {
        return id;
      }
    
    }
    1. class cannot be instantiated because of non-public constructor

    2. class cannot be final

    3. class cannot be static

    4. class cannot be named Class because it is a reserved keyword

    5. nothing is wrong with this code

    Answer
  • 3.

    Which of the following is true?

    public class Main {
      public static void main() {
        System.out.println("1234".length());
      }
    }
    1. The code will print "4"

    2. The code will print "1234"

    3. The code won't compile

    4. The code will fail at run time

    Answer
  • 4.

    Which of the following is true?

    public final class Main {
    
      static final String s = "hello";
    
      public void main(String[] args) {
        System.out.println(s);
      }
    
    }
    1. The code will print "hello"

    2. The code won't compile

    3. The code will fail at run time

    Answer
  • 5.

    Which of the following is true?

    public class A {
      public String foo() {
        return "a";
      }
    }
    
    public class B extends A {
      public String foo() {
        return "b";
      }
    }
    
    public class Main {
      public static void main(String[] args) {
        A a = (A) new B();
        System.out.println(a.foo());
      }
    }
    1. The code will print "a"

    2. The code will print "b"

    3. The code won

    4. The code will fail at run time

    Answer
  • 6.

    Which of the following is true?

    public class Base {
      public String toString() {
        return "Base";
      }
    }
    
    public class Derived extends Base {
      public String toString() {
        return "Derived";
      }
    }
    
    public class Main {
      public static void main(String[] args) {
        Base b = new Derived();
        System.out.println(b);
      }
    }
    1. The code won't compile

    2. The code will fail at run time

    3. The code will print "Base"

    4. The code will print "Derived"

    Answer
  • 7.

    Which of the following is true?

    public interface I {
      public void method();
    }
    
    public abstract class C implements I {
      private C() {
      }
    
      public static C getInstance() {
        return new C();
      }
    
      public void method() {
      }
    }
    1. class C cannot be instantiated because of private constructor

    2. class C cannot be instantiated because it is abstract

    3. method getInstance() cannot be static

    4. there is nothing wrong with this code

    Answer
  • 8.

    What of following will happen?

    public class A {
    
      public void main(String[] args) {
        A[] array = new A[10];
        System.out.println(array.length);
      }
    
    }
    1. The code will not compile

    2. There will be a run time error

    3. The code will print 0

    4. The code will print 10

    Answer
  • 9.

    Which of the following is true?

    package a;
    class A {
      public void foo() {
         System.out.println("a");
      }
    }
    
    package b;
    import a.A;
    public class B extends A {
      public void foo() {
        System.out.println("b");
      }
    
      public static void main(String[] args) {
        new B().foo();
      }
    }
    1. The code won't compile

    2. There will be a run time error

    3. The code will print "a"

    4. The code will print "b"

    Answer
  • 10.

    What will be printed?

    int a = 10;
    System.out.println(a + ~a);
    1. -1

    2. 0

    3. 1

    4. 10

    5. 20

    Answer
  • 11.

    What will be printed?

    String s1 = "a";
    String s2 = s1;
    s1 += "b";
    System.out.println(s2);
    1. a

    2. b

    3. ab

    Answer
  • 12.

    What will be printed?

    String s1 = "a";
    String s2 = "a";
    System.out.println(s1 == s2);
    1. true

    2. false

    Answer
  • 13.

    What will be printed?

    int a = 1;
    a += ++a * a;
    System.out.println(a);
    1. 1

    2. 2

    3. 4

    4. 5

    5. 6

    Answer
  • 14.

    What will be the result of running this code?

    public class Test {
    
      static boolean printAndReturn(boolean value) {
        System.out.println(value);
        return value;
      }
    
      public static void main(String[] args) {
        if (printAndReturn(false) && printAndReturn(true)) {
          System.out.println(true);
        } else {
          System.out.println(false);
        }
      }
    
    }
    1. output: false true false

    2. output: false false

    3. output: true false true

    4. compilation error

    5. run time error

    Answer
  • 15.

    What will be printed?

    String s1 = new String("a");
    String s2 = new String("a");
    
    System.out.println(s1 == s2);
    1. true

    2. false

    Answer
  • 16.

    What will be the result of executing this code?

    String s = null;
    if ((s != null) || (!s.isEmpty())) {
      System.out.println(s);
    } else {
      System.out.println("the string is null");
    }
    1. "the string is null"

    2. "null"

    3. NullPointerException

    4. compilation error

    Answer
  • 17.

    What will be printed?

    byte b = (byte) 128;
    b >>>= 1;
    System.out.println(b);
    1. 128

    2. 64

    3. -128

    4. -64

    Answer
  • 18.

    What will be printed?

    int a = -1;
    a = a >>> 1;
    System.out.println(a);
    1. -1

    2. 0

    3. 1

    4. 2147483647

    5. -2147483647

    Answer
  • 19.

    What will be printed?

    int a = -1;
    a = a >> 1;
    System.out.println(a);
    1. 0

    2. 1

    3. -1

    4. 2147483647

    5. -2147483647

    Answer
  • 20.

    What will be printed?

    boolean value = 
      Double.POSITIVE_INFINITY == Double.POSITIVE_INFINITY;
    
    System.out.println(value);
    1. true

    2. false

    Answer

© 2017 QuizBucket.org