Java java core 2 quiz

Java fundamental quiz collection that covers Java features such as OOP, design common Java core classes.

This quiz is in Java quiz collection.

Start quiz
  • 1.

    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();
      }
    }
  • 2.

    What of following will happen?

    public class A {
      public void main(String[] args) {
        A[] array = new A[10];
        System.out.println(array.length);
      }
    }
  • 3.

    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() {
      }
    }
  • 4.

    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);
      }
    }
  • 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());
      }
    }
  • 6.

    Which of the following is true?

    public class Main {
      public static void main() {
        System.out.println("1234".length());
      }
    }
  • 7.

    Which of the following is true?

    public static final class Class {
      static final int id = 1;
      Class() {
      }
      static public int id() {
        return id;
      }
    }
  • 8.

    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);
      }
    }

     

  • 9.

    which of the following will happen?

    import java.util.Arrays;
    import java.util.Comparator;
    public class X {
      private int x;
      public X(int x) {
        this.x = x;
      }
      public String toString() {
        return Integer.toString(x);
      }
      public static void main(String[] args) {
        X[] array = new X[3];
        array[0] = new X(3);
        array[1] = new X(1);
        array[2] = new X(2);
        Arrays.sort(array, new Comparator() {
          public int compare(X x1, X x2) {
            return x2.x - x1.x;
          }
        });
        System.out.println(Arrays.toString(array));
      }
    }
  • 10.

    What is correct syntax for main method of a java class?

  • 11.

    What is the size of boolean variable?

  • 12.

    What is the default value of long variable?

  • 13.

    What is polymorphism?

  • 14.

    What is Abstraction?

  • 15.

    What is JRE?

  • 16.

    When static binding occurs?

  • 17.

    What is runtime polymorphism?

  • 18.

    When finally block gets executed?

  • 19.

    What will be the output of the program?

    try 
    { 
        int x = 0; 
        int y = 5 / x; 
    } 
    catch (Exception e) 
    {
        System.out.println("Exception"); 
    } 
    catch (ArithmeticException ae) 
    {
        System.out.println(" Arithmetic Exception"); 
    } 
    System.out.println("finished");
  • 20.

    What will be printed?

    double value = 2 / 3;
    System.out.println(value);
  • 21.

    Which of the following statements about the hashcode() method are incorrect?

    1. The value returned by hashcode() is used in some collection classes to help locate objects.
    2. The hashcode() method is required to return a positive int value.
    3. The hashcode() method in the String class is the one inherited from Object.
    4. Two new empty String objects will produce identical hashcodes.
  • 22.

    Which statement is true about a static nested class?

  • 23.

    Which is true about an anonymous inner class?

  • 24.

    Which of the following statements is true?

  • 25.

    What will be the output of the program?

    class MyThread extends Thread 
    {
        MyThread() 
        {
            System.out.print(" MyThread");
        }
        public void run() 
        {
            System.out.print(" bar");
        }
        public void run(String s) 
        {
            System.out.println(" baz");
        }
    }
    public class TestThreads 
    {
        public static void main (String [] args) 
        {
            Thread t = new MyThread() 
            {
                public void run() 
                {
                    System.out.println(" foo");
                }
            };
            t.start();
        }
    }

© 2017 QuizBucket.org