Java Quiz

0 votes, 0 avg
279

Java Questions

1 / 15

1. Which of these statements are incorrect?

2 / 15

2. What will be the output of the following Java code?

class Char {
public static void main(String args[]) {
char ch1, ch2;
ch1 = 88; // code for X
ch2 = 'Y';
System.out.print("ch1 and ch2: ");
System.out.println(ch1 + " " + ch2);
}
} 

3 / 15

3. Which two classes use the Shape class correctly?

A. public class Circle implements Shape 
   {
    private int radius;
   }
B. public abstract class Circle extends Shape 
   {
    private int radius;
   }
C. public class Circle extends Shape 
   {
   private int radius;
   public void draw();
   }
D. public abstract class Circle implements Shape 
   {
    private int radius;
    public void draw();
   }
E. public class Circle extends Shape 
   {
    private int radius;
    public void draw()
    {
     /* code here */
    }
   }
F. public abstract class Circle implements Shape 
   {
     private int radius;
     public void draw() 
     { 
      /* code here */ 
     }
   }

4 / 15

4. What will be the output of the following Java program?

        class Output 

        {

            public static void main(String args[])

            {

                int arr[] = {1, 2, 3, 4, 5};

                for ( int i = 0; i < arr.length - 2; ++i)

                    System.out.println(arr[i] + " ");

            } 

        }

5 / 15

5. What will be the output of the following Java code?

    class San

    {

     public void m1 (int i,float f)

     {

      System.out.println(" int float method");

     }

     

     public void m1(float f,int i);

      {

      System.out.println("float int method");

      }

     

      public static void main(String[]args)

      {

        San s=new San();

            s.m1(20,20);

      }

    }

6 / 15

6. Which of these can be used to differentiate two or more methods having the same name?

7 / 15

7. Which of these statements is incorrect about Thread?

8 / 15

8. What will be the output of the following Java code?

        class A 

        {

            public int i;

            protected int j;

        }    

        class B extends A 

        {

            int j;

            void display() 

            {

                super.j = 3;

                System.out.println(i + " " + j);

            }

        }    

        class Output 

        {

            public static void main(String args[])

            {

                B obj = new B();

                obj.i=1;

                obj.j=2;   

                obj.display();     

            }

       }

9 / 15

9. What will be the output of the following Java code?

        class static_out 

        {

            static int x;

     	static int y;

            void add(int a, int b)

            {

                x = a + b;

                y = x + b;

            }

        }    

        public class static_use 

        {

            public static void main(String args[])

            {

                static_out obj1 = new static_out();

                static_out obj2 = new static_out();   

                int a = 2;

                obj1.add(a, a + 1);

                obj2.add(5, a);

                System.out.println(obj1.x + " " + obj2.y);     

            }

       }

10 / 15

10. What is an ineterface?

11 / 15

11. Identify the modifier which cannot be used for constructor.

12 / 15

12. Find the output of the following code.

        import java.util.*;

        class Collection_iterators 

        {

            public static void main(String args[]) 

            {

                LinkedList list = new LinkedList();

                list.add(new Integer(2));

                list.add(new Integer(8));

                list.add(new Integer(5));

                list.add(new Integer(1));

                Iterator i = list.iterator();

                Collections.reverse(list);

    	    Collections.sort(list);

                while(i.hasNext())

    	        System.out.print(i.next() + " ");

            }

        }

13 / 15

13. Find the value of A[1] after execution of the following program.

int[] A = {0,2,4,1,3};
for(int i = 0; i < a.length; i++){
    a[i] = a[(a[i] + 3) % a.length];
}

14 / 15

14. What will be the output of the following Java code?

        class output 

        {

            public static void main(String args[])

            {

                String a = "hello i love java";

                System.out.println(a.indexOf('i')+" "+a.indexOf('o') +" "+a.lastIndexOf('i')+" "+a.lastIndexOf('o'));

            }

        }

15 / 15

15. What will be the output of the following Java code?

        class output 

        {

            public static void main(String args[])

            {

                 String chars[] = {"a", "b", "c", "a", "c"};

                 for (int i = 0; i < chars.length; ++i)

                     for (int j = i + 1; j < chars.length; ++j)

                         if(chars[i].compareTo(chars[j]) == 0)

                             System.out.print(chars[j]); 

            }

       }

Your score is

The average score is 36%

0%